QNLP  v1.0
test_arithmetic.cpp File Reference

Tests for quantum arithmetic using the (QFT) implementation. More...

#include "catch2/catch.hpp"
#include "Simulator.hpp"
#include "IntelSimulator.cpp"
#include <memory>
#include "arithmetic.hpp"
#include <bitset>
Include dependency graph for test_arithmetic.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("Arithmetic subtraction","[arithmetic]")
 Test Arithmetic: subtraction. More...
 
 TEST_CASE ("Arithmetic summation","[arithmetic]")
 Test Arithmetic: Summation. More...
 

Detailed Description

Tests for quantum arithmetic using the (QFT) implementation.

Author
Lee J. O'Riordan (lee.o.nosp@m.rior.nosp@m.dan@i.nosp@m.chec.nosp@m..ie)
Version
0.1
Date
2019-09-10

Definition in file test_arithmetic.cpp.

Function Documentation

◆ TEST_CASE() [1/2]

TEST_CASE ( "Arithmetic subtraction"  ,
""  [arithmetic] 
)

Test Arithmetic: subtraction.

Definition at line 27 of file test_arithmetic.cpp.

27  {
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 }
Class definition for IntelSimulator. The purpose of this class is to map the functionality of the und...
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

References ncu_opt_tester::num_qubits, ncu_opt_tester::sim, and QNLP::Arithmetic< SimulatorType >::sub_reg().

Here is the call graph for this function:

◆ TEST_CASE() [2/2]

TEST_CASE ( "Arithmetic summation"  ,
""  [arithmetic] 
)

Test Arithmetic: Summation.

Definition at line 72 of file test_arithmetic.cpp.

72  {
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 }
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

References ncu_opt_tester::num_qubits, ncu_opt_tester::sim, and QNLP::Arithmetic< SimulatorType >::sum_reg().

Here is the call graph for this function: