-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_simulator.cpp
337 lines (305 loc) · 9.95 KB
/
cpu_simulator.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <vector>
#include <complex>
#include <assert.h>
#include <bitset>
using namespace std;
#include "CycleTimer.h"
// Sparse matrix reserves the first element for dimension, assuming matrix is square.
// It is implemented using a vector.
struct sparse_elt {
long u;
long v;
complex<double> amp;
};
sparse_elt createElt(int u, int v, complex<double> amp) {
sparse_elt elt;
elt.u = u;
elt.v = v;
elt.amp = amp;
return elt;
}
bool equalsZero(complex<double> amplitude) {
return (norm(amplitude) < 1e-15);
}
bool isNormalized (vector<complex<double> > state) {
double sum = 0;
for (auto i = state.begin(); i != state.end(); ++i) {
sum += norm(*i);
}
return (abs(1.0 - sum) < 1e-15);
}
vector<complex<double> > uniform(const long N) {
vector<complex<double> > state;
state.assign(N, sqrt(1.0/N));
return state;
}
vector<complex<double> > zero(const long N) {
vector<complex<double> > state;
state.assign(N, 0);
return state;
}
vector<complex<double> > classical(const long N, const int init) {
vector<complex<double> > state = zero(N);
state.data()[init] = 1.0;
return state;
}
vector<complex<double> > applyOperator(vector<complex<double> > state, vector<sparse_elt> unitary) {
assert(unitary.begin()->u == state.size());
vector<complex<double> > newstate = zero(state.size());
for(auto i = unitary.begin() + 1; i != unitary.end(); ++i) {
newstate.data()[i->u] += i->amp * state.data()[i->v];
}
return newstate;
}
vector<sparse_elt> tensor(vector<sparse_elt> u1, vector<sparse_elt> u2) {
int dim1 = u1.begin()->u;
int dim2 = u2.begin()->u;
vector<sparse_elt> u3;
u3.push_back(createElt(dim1*dim2, dim1*dim2, 0.0));
for(auto i = u1.begin() + 1; i != u1.end(); ++i) {
for(auto j = u2.begin() + 1; j != u2.end(); ++j) {
u3.push_back(createElt(i->u * dim2 + j->u, i->v * dim2 + j->v, i->amp * j->amp));
}
}
return u3;
}
/* Print features */
void printComplex(complex<double> amplitude) {
cout << "(" << real(amplitude) << "+" << imag(amplitude) << "i)";
}
void printVec(vector<complex<double> > state, const int n, const long N, bool printAll) {
cout << "State:\n";
for (int i = 0; i < N; ++i) {
if (!printAll && equalsZero(state.at(i)))
continue;
printComplex(state.at(i));
cout << "|";
for (int j = n-1; j >= 0; --j) {
cout << (0x1 & (i>>j));
}
cout << "> + ";
cout << "\n";
}
}
void printVecProbs(vector<complex<double> > state, const int n, const long N, bool printAll) {
cout << "Probability distribution:\n";
for (int i = 0; i < N; ++i) {
if (!printAll && equalsZero(state.at(i)))
continue;
cout << "|<";
for (int j = n-1; j >= 0; --j) {
cout << (0x1 & (i>>j));
}
cout << "|Psi>|^2 = " << norm(state.at(i)) << "\n";
}
}
void printOp(vector<sparse_elt> U) {
vector<vector<complex<double> > > densified;
int dim = U.begin()->u;
densified.assign(dim, zero(dim));
for (auto x = U.begin()+1; x != U.end(); ++x) {
densified.data()[x->u].data()[x->v] += x->amp;
}
for (int x = 0; x < dim; ++x) {
for (int y = 0; y < dim; ++y) {
cout << densified.data()[x].data()[y] << " ";
}
cout << "\n";
}
}
long getMostLikely(vector<complex<double> > state, int n) {
long ans = -1;
double prob = 0.0;
long N = pow(2, n);
for (long i = 0; i < N; i++) {
if (norm(state.data()[i]) > prob) {
prob = norm(state.data()[i]);
ans = i;
}
}
return ans;
}
/* One qubit gates */
vector<sparse_elt> identity(){
vector<sparse_elt> id;
// dimensions
id.push_back(createElt(2, 2, 0.0));
// values
id.push_back(createElt(0, 0, 1.0));
id.push_back(createElt(1, 1, 1.0));
return id;
}
vector<sparse_elt> naught() {
vector<sparse_elt> naught;
// dimensions
naught.push_back(createElt(2, 2, 0.0));
// values
naught.push_back(createElt(1, 0, 1.0));
naught.push_back(createElt(0, 1, 1.0));
return naught;
}
vector<sparse_elt> hadamard() {
vector<sparse_elt> had;
// dimensions
had.push_back(createElt(2, 2, 0.0));
// values
double norm = 1.0 / sqrt(2);
had.push_back(createElt(0, 0, norm));
had.push_back(createElt(0, 1, norm));
had.push_back(createElt(1, 0, norm));
had.push_back(createElt(1, 1, -norm));
return had;
}
vector<sparse_elt> phase(double phi){
vector<sparse_elt> phase;
// dimensions
phase.push_back(createElt(2,2, 0.0));
// values
phase.push_back(createElt(0,0, 1.0));
complex<double> ex (cos(phi), sin(phi));
phase.push_back(createElt(1,1,ex));
return phase;
}
/* Expanding operations for 1 qubit gates. */
/* It is much more efficient to do many 1 qubit gates sequentially than to compute their tensor product */
vector<sparse_elt> oneQubitGateExpand(vector<sparse_elt> oneQubitGate, const int n, const int which) {
vector<sparse_elt> total = (which == 0 ? oneQubitGate : identity());
for(int i = 1; i < n; ++i) {
if(which == i)
total = tensor(total, oneQubitGate);
else
total = tensor(total, identity());
}
return total;
}
/* Two qubit gates */
vector<sparse_elt> CNOTExpanded(const int n, const int u, const int v) {
vector<sparse_elt> cnot;
long N = pow(2, n);
cnot.push_back(createElt(N, N, 0.0));
int j;
for(int i = 0; i < N; ++i) {
if (i>>(n-u-1) & 0x1) {
j = i ^ (0x1<<(n-v-1));
cnot.push_back(createElt(i, j, 1.0));
} else {
cnot.push_back(createElt(i, i, 1.0));
}
}
return cnot;
}
vector<sparse_elt> CU1Expanded(vector<sparse_elt> U1, const int n, const int u, const int v) {
vector<sparse_elt> CU1;
long N = pow(2, n);
CU1.push_back(createElt(N, N, 0.0));
vector<complex<double> > stateOfV;
for(long i = 0; i < N; ++i) {
if (i>>(n-u-1) & 0x1) {
stateOfV = classical(2, (i>>(n-v-1)) & 0x1);
stateOfV = applyOperator(stateOfV, U1);
if(!equalsZero(stateOfV.data()[0])) {
CU1.push_back(createElt(i, i & ~(0x1L<<(n-v-1)), stateOfV.data()[0]));
}
if(!equalsZero(stateOfV.data()[1])) {
CU1.push_back(createElt(i, i | (0x1L<<(n-v-1)), stateOfV.data()[1]));
}
} else {
CU1.push_back(createElt(i, i, 1.0));
}
}
return CU1;
}
vector<sparse_elt> CPhase(const int n, const int u, const int v, double phi) {
return CU1Expanded(phase(phi), n, u, v);
}
vector<sparse_elt> swapExpanded(const int n, const int u, const int v) {
assert(u != v);
vector<sparse_elt> swp;
long N = pow(2, n);
//dimensions
swp.push_back(createElt(N, N, 0.0));
// values
int j;
for(int i = 0; i < N; ++i) {
if ((i>>(n-u-1) & 0x1) ^ (i>>(n-v-1) & 0x1)) {
j = (i ^ (0x1<<(n-u-1))) ^ (0x1<<(n-v-1));
swp.push_back(createElt(i, j, 1.0));
} else {
swp.push_back(createElt(i, i, 1.0));
}
}
return swp;
}
/* Pi estimation functions for benchmarking performance. */
/* Inverse quantum fourier transform */
vector<complex<double> > qft_dagger(vector<complex<double> > state, const int n) {
const int n_prime = n+1;
for (int i = 0; i < n/2; ++i) {
state = applyOperator(state, swapExpanded(n_prime, i, n-i-1));
}
for (int j = 0; j < n; ++j) {
for (int m = 0; m < j; ++m) {
double phi = -M_PI / ((double)pow(2, j - m));
state = applyOperator(state, CPhase(n_prime, j, m, phi));
}
state = applyOperator(state, oneQubitGateExpand(hadamard(), n_prime, j));
}
return state;
}
// Setup for quantum phase estimation.
vector<complex<double> > qpe_pre(const int n){
const long N = pow(2, n+1);
const int n_prime = n+1;
vector<complex<double> > state = classical(N, 0);
for (int i = 0; i < n; ++i) {
state = applyOperator(state, oneQubitGateExpand(hadamard(), n_prime, i));
}
state = applyOperator(state, oneQubitGateExpand(naught(), n_prime, n));
for (int i = n-1; i >= 0; --i) {
for (int j = 0; j < pow(2, n-i-1); ++j) {
state = applyOperator(state, CPhase(n_prime, n, n-i-1, 1.0));
}
}
return state;
}
/* The bits we want for this task are from 1 to n inclusive, since the 0 bit is our extra for
* setting up the problem. Additionally, we want to read them in reverse. */
long getCorrectBitsForPiEstimate(long bits, int n){
long answer = 0;
for(int i = 1; i <= n; i++){
answer = answer<<1;
answer += (bits>>i) & 0x1L;
}
return answer;
}
double get_pi_estimate(const int n) {
vector<complex<double> > state = qpe_pre(n);
state = qft_dagger(state, n);
//printVec(state, n+1, pow(2, n+1), false);
long mostLikely = getMostLikely(state, n+1);
//cout << mostLikely << endl;
double theta = (double)(getCorrectBitsForPiEstimate(mostLikely, n)) / pow(2.0,n);
return 1.0 / (2 * theta);
}
int main() {
const int n = 8;
//const int N = pow(2, n);
double cpu_start_time = CycleTimer::currentSeconds();
cout << get_pi_estimate(n) << endl;
double cpu_end_time = CycleTimer::currentSeconds();
printf("Total CPU Time: %.3f ms\n", 1000.f * (cpu_end_time-cpu_start_time));
//Testing functions out
//vector<complex<double> > state = classical(N, 0);
/* Uniform superposition: */
/*for (int i = 0; i < n; ++i) {
state = applyOperator(state, oneQubitGateExpand(hadamard(), n, i));
}
//state = applyOperator(state, oneQubitGateExpand(hadamard(), n, 0));
state = applyOperator(state, oneQubitGateExpand(naught(), n, 0));
state = applyOperator(state, CPhase(n, 0,1, 0.1));
printVec(state, n, N, false);*/
//printVecProbs(state, n, N, false);
}