QNLP  v1.0
bit_group.hpp
Go to the documentation of this file.
1 
13 #ifndef QNLP_BIT_GROUP
14 #define QNLP_BIT_GROUP
15 
16 #include<algorithm>
17 
18 namespace QNLP{
24  template <class SimulatorType>
25  class BitGroup{
26  private:
36  static void q_ops(SimulatorType& qSim, std::size_t qreg_idx0, std::size_t qreg_idx1, const std::vector<std::size_t>& qaux_idx){
37 
38  //Swap if C0 is set and C1 is not
39  qSim.applyGateCCX(qreg_idx0, qaux_idx[0], qaux_idx[1]);
40  qSim.applyGateCCX(qreg_idx1, qreg_idx0, qaux_idx[1]);
41  qSim.applyGateCSwap(qaux_idx[1], qreg_idx0, qreg_idx1);
42 
43  //Reset Aux gate to |0>
44  qSim.applyGateH(qaux_idx[1]);
45  qSim.collapseToBasisZ(qaux_idx[1], 0);
46  }
47 
48 
49  /*
50  //@brief WIP:Swaps all qubits in register indices given by qreg_idx to their right-most positions.
51  // Method works from opposing ends of register and subregisters iteratively until all qubits are in the correct positions.
52  //
53  //@param qSim Quantum simulator object derived from SimulatorGeneral
54  //@param qreg_idx Indices of the qSim register to group to the LSB position.
55  //@param qaux_idx Indices of auxiliary qubits in qSim register
56  static void bit_swap_s2e(SimulatorType& qSim, const std::vector<std::size_t>& qreg_idx, const std::vector<std::size_t>& qaux_idx){
57  assert(qaux_idx.size() >= 2);
58  std::size_t num_qubits = qreg_idx.size();
59 
60  //Apply the op from opposite ends of register working towards centre, reduce size from left, then repeat until 2 qubits remain.
61 
62  qSim.applyGateX(qaux_idx[0]);
63  for(int num_offset=0; num_offset < qreg_idx.size()/2 + 1 + qreg_idx.size()%2; num_offset++){
64  for(std::size_t i=0; i < num_qubits/2 + num_qubits%2; i++){
65  if (i + num_offset == num_qubits - (i+1) + num_offset)
66  continue;
67  q_ops(qSim, qreg_idx[i + num_offset], qreg_idx[num_qubits - (i+1) + num_offset], qaux_idx);
68  }
69  num_qubits--;
70  }
71  qSim.applyGateX(qaux_idx[0]);
72  }*/
73 
83  static void bit_swap_pair(SimulatorType& qSim, const std::vector<std::size_t>& qreg_idx, const std::vector<std::size_t>& qaux_idx, bool lsb){
84  bool isOdd = qreg_idx.size() % 2;
85  qSim.applyGateX(qaux_idx[0]);
86 
87  for(std::size_t num_steps = 0; num_steps < qreg_idx.size(); num_steps++){
88  for(int i = 0; i < qreg_idx.size()-1; i+=2){
89  if(i + 1 + isOdd < qreg_idx.size()){
90  if(lsb)
91  q_ops(qSim, qreg_idx[i + isOdd], qreg_idx[i + 1 + isOdd], qaux_idx);
92  else
93  q_ops(qSim, qreg_idx[i + 1 + isOdd], qreg_idx[i + isOdd], qaux_idx);
94 
95  }
96  }
97  isOdd = !isOdd;
98  }
99  qSim.applyGateX(qaux_idx[0]);
100  }
101 
102  public:
103 
114  static void bit_group(SimulatorType& qSim, const std::vector<std::size_t>& qreg_idx, const std::vector<std::size_t>& qaux_idx, bool lsb=true){
115  bit_swap_pair(qSim, qreg_idx, qaux_idx, lsb);
116  }
117  };
118 }
119 
120 #endif
static void bit_group(SimulatorType &qSim, const std::vector< std::size_t > &qreg_idx, const std::vector< std::size_t > &qaux_idx, bool lsb=true)
Swaps all qubits in register indices given by qreg_idx to their right-most positions....
Definition: bit_group.hpp:114
Class definition for bit-wise grouping in register.
Definition: bit_group.hpp:25
static void bit_swap_pair(SimulatorType &qSim, const std::vector< std::size_t > &qreg_idx, const std::vector< std::size_t > &qaux_idx, bool lsb)
Swaps all qubits in register indices given by qreg_idx to their right-most positions....
Definition: bit_group.hpp:83
static void q_ops(SimulatorType &qSim, std::size_t qreg_idx0, std::size_t qreg_idx1, const std::vector< std::size_t > &qaux_idx)
Swap the qubits if qreg_idx0 is set and qreg_idx1 is not. Uses Auxiliary qubits qaux_idx which are se...
Definition: bit_group.hpp:36