QNLP  v1.0
arithmetic.hpp
Go to the documentation of this file.
1 
12 #ifndef QNLP_ARITHMETIC
13 #define QNLP_ARITHMETIC
14 
15 namespace QNLP{
21  template <class SimulatorType>
22  class Arithmetic{
23  public:
24  using CST = const std::size_t;
34  static void sum_reg(SimulatorType& qSim, CST r1_min, CST r1_max, CST r2_min, CST r2_max){
35  std::size_t num_qubits_r1 = r1_max - r1_min;
36  std::size_t num_qubits_r2 = r2_max - r2_min;
37 
38  assert( num_qubits_r1 == num_qubits_r2 );
39 
40  qSim.applyQFT(r2_min, r2_max);
41  double theta = 0.0;
42 
43  //Targets
44  for(int i = r2_max; i >= (int) r2_min; i--){
45  //Controls
46  for(int j = r1_max - (r2_max - i); j >= (int) r1_min; j--){
47  theta = 2.0*M_PI / (std::size_t) (1<<(1 + (i-j)));
48  qSim.applyGateCPhaseShift(theta, j, i);
49  }
50  }
51 
52  qSim.applyIQFT(r2_min, r2_max);
53  }
54 
64  static void sub_reg(SimulatorType& qReg, const unsigned int r1_min, const unsigned int r1_max, const unsigned int r2_min, const unsigned int r2_max){
65  std::size_t num_qubits_r1 = r1_max - r1_min;
66  std::size_t num_qubits_r2 = r2_max - r2_min;
67 
68  assert( num_qubits_r1 == num_qubits_r2 );
69 
70  //Flip all states in r1
71  for(int i = r1_min; i < r1_max; i++){
72  qReg.applyGateX(i);
73  }
74  sum_reg(qReg, r1_min, r1_max, r2_min, r2_max);
75  //Flip states to return the sum
76  for(int i = r2_min; i < r2_max; i++){
77  qReg.applyGateX(i);
78  }
79  //Return all qubits in r1 to original state
80  for(int i = r1_min; i < r1_max; i++){
81  qReg.applyGateX(i);
82  }
83  }
84  };
85 }
86 
87 #endif
static void sum_reg(SimulatorType &qSim, CST r1_min, CST r1_max, CST r2_min, CST r2_max)
Implements |r1>|r2> -> |r1>|r1+r2>. Required bits to represent |r1+r2> should be available prior to c...
Definition: arithmetic.hpp:34
const std::size_t CST
Definition: arithmetic.hpp:24
Class definition for bit-wise summation and subtraction of qubits.
Definition: arithmetic.hpp:22
static void sub_reg(SimulatorType &qReg, const unsigned int r1_min, const unsigned int r1_max, const unsigned int r2_min, const unsigned int r2_max)
Implements |r1>|r2> -> |r1>|r1-r2>. If r1 < r2 the state will underflow, with the subtraction continu...
Definition: arithmetic.hpp:64