QNLP  v1.0
test_arithmetic.cpp
Go to the documentation of this file.
1 
11 //#define CATCH_CONFIG_RUNNER
12 //#define CATCH_CONFIG_MAIN
13 
14 #include "catch2/catch.hpp"
15 #include "Simulator.hpp"
16 #include "IntelSimulator.cpp"
17 #include <memory>
18 #include "arithmetic.hpp"
19 
20 using namespace QNLP;
21 #include <bitset>
22 
27 TEST_CASE("Arithmetic subtraction","[arithmetic]"){
28  std::size_t num_qubits = 6;
30  Arithmetic<decltype(sim)> arr;
31  auto& r = sim.getQubitRegister();
32 
33  SECTION("OLD_TEST"){
34  unsigned int r1_min = 0, r1_max=(num_qubits/2)-1, r2_min = r1_max+1, r2_max = num_qubits-1;
35  CAPTURE(r1_min, r1_max, r2_min, r2_max );
36 
37  for(std::size_t pattern = 0; pattern < 1<<(num_qubits/2); pattern ++){
38  unsigned int bitmask = 0x1;
39  unsigned int a = 0;
40  sim.initRegister();
41  for (unsigned int i=0; i <= r1_max -1; i++){
42  sim.applyGateX(i);
43  a += (unsigned int) pow(2,i);
44  }
45  for (unsigned int j = r2_min; j <= r2_max; j++){
46  if ( (pattern & bitmask) > 0 ){
47  sim.applyGateX(j);
48  }
49  bitmask <<=1;
50  }
51  CAPTURE(pattern, a, bitmask);
52  arr.sub_reg(sim, r1_min, r1_max, r2_min, r2_max);
53  std::string result="";
54  CAPTURE(result);
55 
56  for (unsigned int j = r2_min; j <= r2_max; j++){
57  result += std::to_string( r.GetProbability( j )).at(0);
58  }
59  CAPTURE(result);
60 
61  std::reverse(result.begin(), result.end());
62  std::cout << "Sub = " << std::bitset<4>(a) << "-" << std::bitset<4>(pattern) << "=> Expected:=" << std::bitset<4>(a-pattern) << " : Actual:=" << result << std::endl;
63  CAPTURE(r);
64  }
65  }
66 }
67 
72 TEST_CASE("Arithmetic summation","[arithmetic]"){
73  std::size_t min_idx=0, max_idx=3;
74  std::size_t num_qubits = max_idx - min_idx +1;
76  Arithmetic<decltype(sim)> arr;
77  auto& r = sim.getQubitRegister();
78 
79  SECTION("OLD_TEST","[arithmetic]"){
80  unsigned int r1_min = 0, r1_max=(num_qubits/2)-1, r2_min = r1_max+1, r2_max = num_qubits-1;
81  for(int pattern = 0; pattern < 1<<(num_qubits/2); pattern ++){
82  unsigned int bitmask = 0x1;
83  unsigned int a = 0;
84  sim.initRegister();
85  for (unsigned int i=0; i <= r1_max -1; i++){
86  sim.applyGateX(i);
87  a += (unsigned int) pow(2,i);
88  }
89  for (unsigned int j = r2_min; j <= r2_max; j++){
90  if ( (pattern & bitmask) > 0 ){
91  sim.applyGateX(j);
92  }
93  bitmask <<=1;
94  }
95  arr.sum_reg(sim, r1_min, r1_max, r2_min, r2_max);
96  std::string result="";
97 
98  for (unsigned int j = r2_min; j <= r2_max; j++){
99  result += std::to_string( r.GetProbability( j )).at(0);
100  }
101  std::reverse(result.begin(), result.end());
102  std::cout << "Sub = " << std::bitset<4>(a) << "-" << std::bitset<4>(pattern) << "=> Expected:=" << std::bitset<4>(a-pattern) << " : Actual:=" << result << std::endl;
103  CAPTURE(r);
104  }
105  }
106 }
TEST_CASE("Arithmetic subtraction","[arithmetic]")
Test Arithmetic: subtraction.
Class definition for IntelSimulator. The purpose of this class is to map the functionality of the und...
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
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