-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoq.cpp
114 lines (95 loc) · 2.1 KB
/
stoq.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include <complex>
#include <bitset>
#define DEBUG
std::string bit2Packet(bool bit, std::string gates) {
std::complex<double> alpha; // p0
std::complex<double> beta; // p1
std::complex<double> temp_a;
std::complex<double> temp_b;
char gate;
// initialize the qbit
alpha.imag(0.0);
beta.imag(0.0);
if (bit) {
alpha.real(0.0);
beta.real(1.0);
} else {
alpha.real(1.0);
beta.real(0.0);
}
// apply gates to the bit
for (int i=0; !gates.empty() && gates[i] != '\0'; i++) {
gate = gates[i];
temp_a = alpha;
temp_b = beta;
// Hadamard gate
if (gate == 'H') {
alpha = (temp_a + temp_b) * 0.70710678;
beta = (temp_a - temp_b) * 0.70710678;
}
// Pauli-X gate
else if (gate == 'X') {
alpha = beta;
beta = temp_a;
}
// Pauli-Y gate
else if (gate == 'Y') {
alpha.real(temp_b.imag());
alpha.imag(-1.0*(temp_b.real()));
beta.real(-1.0*(temp_a.imag()));
beta.imag(temp_a.real());
}
// Pauli-Z gate
else if (gate == 'Z') {
beta.real(-1.0*temp_b.real());
beta.imag(-1.0*temp_b.imag());
}
}
// return result
std::stringstream ss;
ss << "QBIT a:";
ss << alpha;
ss << " b:";
ss << beta;
ss << " END";
std::string res = ss.str();
return res;
}
int string2Packet(std::string str, std::string gates) {
std::string zero = bit2Packet(0, gates);
std::string one = bit2Packet(1, gates);
// each char
for (size_t i=0; i<str.size(); i++) {
std::bitset<8> bits(str[i]);
#ifdef DEBUG
std::cout << "\tDEBUG BITS: "<< bits << std::endl;
#endif
// each bit
for (size_t j=0; j<bits.size(); j++) {
if (bits[j] == false)
std::cout << zero << std::endl;
else if (bits[j] == true)
std::cout << one << std::endl;
}
}
return 0;
}
int main(int argc, char **argv) {
if (argc <= 1 || argc > 3 || argv[1] == "help") {
std::cout << "./stoq [string] [gates]" << std::endl;
std::cout << "\tgates: X,Y,Z,H" << std::endl;
exit(0);
}
if (argc == 3) {
string2Packet(argv[1], argv[2]);
}
else if (argc == 2) {
string2Packet(argv[1], "");
}
return 0;
}