-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantum.cpp
More file actions
130 lines (117 loc) · 2.83 KB
/
Copy pathquantum.cpp
File metadata and controls
130 lines (117 loc) · 2.83 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <random>
#include <iostream>
#include "quantum.h"
using namespace std;
bool Qubit::perform_measure(amplitude zero_amp, amplitude one_amp) {
bool observation;
int probA = (int) (norm(zero_amp) * (1<<((sizeof(int)*4)-1)));
int probB = (int) (norm(one_amp) * (1<<((sizeof(int)*4)-1)));
int randValue = rand() % (probA+probB);
if (randValue < probA) {
observation = false;
} else {
observation = true;
}
if (DEBUGPRINT) {
cout << "qubit:" << alpha << "," << beta << " observed to be " << observation << "(";
cout << probA << "," << probB << "," << randValue << ")" << endl;
}
return observation;
}
Qubit::Qubit(state s) {
Qubit(s.first, s.second);
}
Qubit::Qubit(amplitude a, amplitude b) {
double squareSum = norm(a) + norm(b);
if (abs(squareSum-0) <= eps) {
cout << "0|0> + 0|1> is an invalid quantum state!" << endl;
throw -1;
} else if (abs(squareSum-1) > eps) {
cout << "Renormalizing input!" << endl;
a /= sqrt(squareSum);
b /= sqrt(squareSum);
}
alpha = a;
beta = b;
};
bool Qubit::observe() {
bool observation = perform_measure(alpha, beta);
if (observation) {
alpha = 0;
beta = 1;
} else {
alpha = 1;
beta = 0;
}
return observation;
}
bool Qubit::observe(basis basisChoice) {
state first_state, second_state;
first_state = basisChoice.first;
second_state = basisChoice.second;
amplitude a1,b1,a2,b2;
a1 = first_state.first;
b1 = first_state.second;
a2 = second_state.first;
b2 = second_state.second;
amplitude new_alpha, new_beta;
new_alpha = ((alpha*b2)-(beta*a2)) / ((a1*b2)-(b1*a2));
new_beta = ((alpha*b1)-(beta*a1)) / ((a2*b1)-(b2*a1));
// cout << new_alpha << "|" << new_beta << ":";
bool observation = perform_measure(new_alpha, new_beta);
if (observation) {
alpha = a2;
beta = b2;
} else {
alpha = a1;
beta = b1;
}
return observation;
}
void Qubit::changeState(amplitude a, amplitude b) {
double squareSum = norm(a) + norm(b);
if (abs(squareSum-0) < eps) {
cout << "0|0> + 0|1> is an invalid quantum state!" << endl;
throw -1;
} else if (abs(squareSum-1) > eps) {
cout << "Renormalizing input!" << endl;
a /= sqrt(squareSum);
b /= sqrt(squareSum);
}
alpha = a;
beta = b;
}
void Qubit::changeState(state s) {
changeState(s.first, s.second);
}
Pulse::Pulse() {
qubits = vector<Qubit*>();
}
Pulse::Pulse(vector<Qubit*>& _qubits) {
for (auto q : _qubits) {
qubits.push_back(q);
}
}
Pulse::Pulse(Qubit* qubit){
qubits.push_back(qubit);
}
Qubit* Pulse::extract() {
auto extractedQubit = qubits.back();
qubits.pop_back();
return extractedQubit;
}
int Pulse::size() {
return qubits.size();
}
void Pulse::insert(Qubit *qubit) {
qubits.push_back(qubit);
}
Qubit* Pulse::operator[] (int idx) {
if (idx >= size() || idx < 0){
cout << "Index " << idx << " is out of bounds (size=" << size() << ")";
cout << endl;
throw -1;
} else {
return qubits[idx];
}
}