QNLP  v1.0
test_hamming.cpp File Reference
#include "hamming.hpp"
#include "Simulator.hpp"
#include "IntelSimulator.cpp"
#include "catch2/catch.hpp"
#include <bitset>
Include dependency graph for test_hamming.cpp:

Go to the source code of this file.

Macros

#define IS_SET(byte, bit)
 

Typedefs

typedef ComplexDP Type
 

Functions

std::size_t calc_hammingDist (std::size_t pattern1, std::size_t pattern2, std::size_t len_bin_pattern)
 Calulates the Hamming distance between two binary strings stored as integers. More...
 
std::map< std::size_t, double > expected_amplitude (std::vector< std::size_t > &target_vec, std::size_t test, std::size_t len_bin_pattern)
 Classically calculates the Hamming distance between each binary string in a vector and a test binary string. More...
 
 TEST_CASE ("Test Hamming distance with Roatation about y axis routine","[hammingroty]")
 Test the Hamming distance routine which uses Y rotations to encode the Hamming distance into each states' ampitudes. Each state's amplitude is calculated classically and compared to the quantum simulator computed counterpart. More...
 

Macro Definition Documentation

◆ IS_SET

#define IS_SET (   byte,
  bit 
)

Definition at line 10 of file test_hamming.cpp.

Typedef Documentation

◆ Type

typedef ComplexDP Type

Definition at line 15 of file test_hamming.cpp.

Function Documentation

◆ calc_hammingDist()

std::size_t calc_hammingDist ( std::size_t  pattern1,
std::size_t  pattern2,
std::size_t  len_bin_pattern 
)

Calulates the Hamming distance between two binary strings stored as integers.

Parameters
pattern1First binary pattern
pattern2Second binary pattern
len_bin_patternLength if binary strings
Returns
std::size_t Returns Hamming distance between binary representation of two integers.

Definition at line 27 of file test_hamming.cpp.

27  {
28  std::size_t num_diffs = 0;
29 
30  for(int i = 0; i < len_bin_pattern; i++){
31  num_diffs += (IS_SET(pattern1,i) == IS_SET(pattern2,i));
32  }
33  return num_diffs;
34 }
#define IS_SET(byte, bit)

References IS_SET.

Referenced by expected_amplitude().

Here is the caller graph for this function:

◆ expected_amplitude()

std::map<std::size_t, double> expected_amplitude ( std::vector< std::size_t > &  target_vec,
std::size_t  test,
std::size_t  len_bin_pattern 
)

Classically calculates the Hamming distance between each binary string in a vector and a test binary string.

Parameters
target_vecVector consisting of binary strings represented in std::size_t format
testTest vector for calculating Hamming distance between it and all binary strings in target_vec
len_bin_patternLength if binary strings
Returns
std::map<std::size_t, double> Returns Hash map of encoded integers and the real component of their expected amplitudes which is calculated classically.

Definition at line 44 of file test_hamming.cpp.

44  {
45  const std::size_t num_bin_pattern = target_vec.size();
46  double norm_factor = 0.0;
47 
48  // Calculate Hamming Distance for each training pattern to test pattern
49  std::map<std::size_t, double> exp_amp;
50  for(std::size_t i = 0; i < num_bin_pattern; i++){
51  exp_amp.insert(pair<std::size_t, double>(target_vec[i],(double) calc_hammingDist(target_vec[i],test, len_bin_pattern)));
52  }
53 
54  for(auto& [k,v] : exp_amp){
55  v = sin(v * 0.5* M_PI / (double) len_bin_pattern);
56  norm_factor += v*v;
57  }
58 
59  norm_factor = sqrt(norm_factor);
60 
61  for(auto& [k,v] : exp_amp){
62  v /= norm_factor;
63  }
64 
65  return exp_amp;
66 }
std::size_t calc_hammingDist(std::size_t pattern1, std::size_t pattern2, std::size_t len_bin_pattern)
Calulates the Hamming distance between two binary strings stored as integers.

References calc_hammingDist(), and QNLP_Python_MPI::num_bin_pattern.

Referenced by TEST_CASE().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TEST_CASE()

TEST_CASE ( "Test Hamming distance with Roatation about y axis routine"  ,
""  [hammingroty] 
)

Test the Hamming distance routine which uses Y rotations to encode the Hamming distance into each states' ampitudes. Each state's amplitude is calculated classically and compared to the quantum simulator computed counterpart.

Definition at line 72 of file test_hamming.cpp.

72  {
73  const std::size_t max_qubits = 6;
74  double mach_eps = 7./3. - 4./3. -1.;
75 
76  std::size_t num_qubits;
77  std::size_t len_reg_auxiliary;
78  std::size_t num_bin_pattern;
79  std::size_t val;
80 
81  std::size_t test_pattern = 2;
82 
83  for(std::size_t len_reg_memory = 2; len_reg_memory < max_qubits; len_reg_memory++){
84  DYNAMIC_SECTION("Testing " << len_reg_memory << " memory qubits"){
85 
86  for(std::size_t test_pattern = 2; test_pattern < pow(2,len_reg_memory); test_pattern++){
87  DYNAMIC_SECTION("Testing test pattern (integer format) = " << test_pattern ){
88 
89  // Experiment set-up
90  num_qubits = 2*len_reg_memory + 2;
93 
95  sim.initRegister();
96  auto &r = sim.getQubitRegister();
97 
98  // Set up registers to store indices
99  std::vector<std::size_t> reg_memory(len_reg_memory);
100  for(std::size_t i = 0; i < len_reg_memory; i++){
101  reg_memory[i] = i;
102  }
103  std::vector<std::size_t> reg_auxiliary(len_reg_auxiliary);
104  for(std::size_t i = 0; i < len_reg_auxiliary; i++){
105  reg_auxiliary[i] = i + len_reg_memory;
106  }
107 
108  // Init data to encode
109  std::vector<std::size_t> vec_to_encode(num_bin_pattern);
110  for(std::size_t i = 0; i < num_bin_pattern; i++){
111  vec_to_encode[i] = i;
112  }
113 
114  // Encode
115  sim.encodeBinToSuperpos_unique(reg_memory, reg_auxiliary, vec_to_encode, len_reg_memory);
116 
117  // Compute Hamming distance
118  sim.applyHammingDistanceRotY(test_pattern, reg_memory, reg_auxiliary, len_reg_memory);
119 
120  // Post-selection
121  sim.collapseToBasisZ(reg_auxiliary[len_reg_auxiliary-2], 1);
122 
123  // Generate expected amplitudes from classical computation
124  std::map<std::size_t, double> exp_amp = expected_amplitude(vec_to_encode, test_pattern, len_reg_memory);
125 
126  for(int i = 0; i < num_bin_pattern; i++){
127  REQUIRE(r[i].real() + 10*mach_eps == Approx(0.).margin(1e-12));
128  REQUIRE(r[i].imag() + 10*mach_eps == Approx(0.).margin(1e-12) );
129 
130  }
131  for(int i = num_bin_pattern; i < pow(2,2*len_reg_memory); i++){
132  REQUIRE(r[i].real() + 10*mach_eps == Approx(0.).margin(1e-12));
133  REQUIRE(r[i].imag() + 10*mach_eps == Approx(0.).margin(1e-12) );
134  }
135  // Check relevant state's amplitudes are set correctly.
136  for(int i = pow(2,2*len_reg_memory); i < pow(2,2*len_reg_memory) + num_bin_pattern; i++){
137 
138  CAPTURE( i, r[i].real() + 10*mach_eps == Approx(exp_amp[vec_to_encode[i - pow(2,2*len_reg_memory)]]).margin(1e-12));
139  REQUIRE(r[i].real() + 10*mach_eps == Approx(exp_amp[vec_to_encode[i - pow(2,2*len_reg_memory)]]).margin(1e-12));
140  REQUIRE(r[i].imag() + 10*mach_eps == Approx(0.).margin(1e-12) );
141 
142  }
143  for(int i = pow(2,2*len_reg_memory) + num_bin_pattern; i < pow(2,num_qubits); i++){
144  REQUIRE(r[i].real() + 10*mach_eps == Approx(0.).margin(1e-12));
145  REQUIRE(r[i].imag() + 10*mach_eps == Approx(0.).margin(1e-12) );
146 
147  }
148 
149  val = sim.applyMeasurementToRegister(reg_memory);
150 
151  CHECK_THAT(vec_to_encode, VectorContains(val));
152 
153  }
154  }
155  }
156  }
157 }
Class definition for IntelSimulator. The purpose of this class is to map the functionality of the und...
std::map< std::size_t, double > expected_amplitude(std::vector< std::size_t > &target_vec, std::size_t test, std::size_t len_bin_pattern)
Classically calculates the Hamming distance between each binary string in a vector and a test binary ...

References expected_amplitude(), QNLP_Python_MPI::len_reg_auxiliary, QNLP_EndToEnd_MPI::len_reg_memory, QNLP_Python_MPI::num_bin_pattern, ncu_opt_tester::num_qubits, QNLP_Python_MPI::reg_auxiliary, QNLP_EndToEnd_MPI::reg_memory, ncu_opt_tester::sim, QNLP_EndToEnd_MPI::test_pattern, QNLP.tagging.tag_file::val, and QNLP_EndToEnd_MPI::vec_to_encode.

Here is the call graph for this function: