-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmarker.cpp
More file actions
409 lines (386 loc) · 10.6 KB
/
Copy pathbenchmarker.cpp
File metadata and controls
409 lines (386 loc) · 10.6 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "ps_stabilizer/chstabilizer.hpp"
#include "ps_stabilizer/dchstabilizer.hpp"
#include "chp/chp.hpp"
#include "GraphSim/graphsim.hpp"
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <map>
#include <fstream>
#include <string>
#include <vector>
enum class Gate {
X, Y, Z, H, S, Sdag, CZ, CX
};
std::map<std::string, Gate> gate_names(
{
{"X", Gate::X},
{"Y", Gate::Y},
{"Z", Gate::Z},
{"H", Gate::H},
{"S", Gate::S},
{"Sdag", Gate::Sdag},
{"CZ", Gate::CZ},
{"CX", Gate::CX}
});
template<typename T> void apply_gate(T &state, Gate g, unsigned control, unsigned target)
{
switch (g)
{
case Gate::X:
state.X(control);
break;
case Gate::Y:
state.Y(control);
break;
case Gate::Z:
state.Z(control);
break;
case Gate::H:
state.H(control);
break;
case Gate::S:
state.S(control);
break;
case Gate::Sdag:
state.Sdag(control);
break;
case Gate::CZ:
state.CZ(control, target);
break;
case Gate::CX:
state.CX(control, target);
break;
default:
throw std::logic_error("Wat");
break;
}
}
template<> void apply_gate(CHP::QState &state, Gate g, unsigned control, unsigned target)
{
switch (g)
{
case Gate::X: //This might throw the benchmarks off somewhat?
CHP::x(&state, control);
break;
case Gate::Y:
CHP::y(&state, control);
break;
case Gate::Z:
CHP::z(&state, control);
break;
case Gate::H:
CHP::hadamard(&state, control);
break;
case Gate::S:
CHP::phase(&state, control);
break;
case Gate::Sdag:
CHP::phase(&state, control);
CHP::phase(&state, control);
CHP::phase(&state, control);
break;
case Gate::CZ:
CHP::hadamard(&state, target);
CHP::cnot(&state, control, target);
CHP::hadamard(&state, target);
break;
case Gate::CX:
CHP::cnot(&state, control, target);
break;
default:
throw std::logic_error("Wat");
break;
}
}
template<> void apply_gate(GraphSim::GraphRegister &state, Gate g, unsigned control, unsigned target)
{
switch (g)
{
case Gate::X: //This might throw the benchmarks off somewhat?
state.local_op(control, GraphSim::lco_X);
break;
case Gate::Y:
state.local_op(control, GraphSim::lco_Y);
break;
case Gate::Z:
state.local_op(control, GraphSim::lco_X);
break;
case Gate::H:
state.hadamard(control);
break;
case Gate::S:
state.local_op(control, GraphSim::lco_S);
break;
case Gate::Sdag:
state.local_op(control, GraphSim::lco_Sh);
break;
case Gate::CZ:
state.cphase(control, target);
break;
case Gate::CX:
state.cnot(control, target);
break;
default:
throw std::logic_error("Wat");
break;
}
}
template<class T> void random_circuit(T &state, std::vector<Gate> &gates, unsigned n_qubits, unsigned n_gates=25)
{
for(unsigned i=0; i<n_gates; i++)
{
unsigned control = (rand()%n_qubits);
unsigned target = (rand()%n_qubits);
if(target == control)
{
target = (target +1)%n_qubits;
}
unsigned gate_index = (rand()%gates.size());
Gate g = gates[gate_index];
apply_gate(state, g, control, target);
}
}
unsigned DEFAULT_REPETITIONS = 1000;
enum class Representation
{
CH,
DCH,
CHP,
GRAPHSIM
};
std::map<std::string, Representation> rep_names(
{
{"ch", Representation::CH},
{"dch", Representation::DCH},
{"chp", Representation::CHP},
{"graphsim", Representation::GRAPHSIM}
});
std::vector<Gate> ALL_GATES = {Gate::X, Gate::Y, Gate::Z, Gate::H, Gate::S, Gate::Sdag, Gate::CZ, Gate::CX};
template<class T> std::chrono::duration<double> benchmark_gate(T &state, unsigned n_qubits, Gate g)
{
unsigned control = (rand()%n_qubits);
unsigned target = (rand()%n_qubits);
if(target == control)
{
target = (target +1)%n_qubits;
}
auto start = std::chrono::high_resolution_clock::now();
apply_gate(state, g, control, target);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
return diff;
}
enum class Basis {X, Y, Z};
std::map<std::string, Basis> basis_names(
{
{"X", Basis::X},
{"Y", Basis::Y},
{"Z", Basis::Z}
});
//Global Measurement Variables
int CHPRES;
int GSRES;
template<class T> std::chrono::duration<double> benchmark_measure(T &state, unsigned n_qubits, unsigned warmup, Basis b)
{
//placeholder
random_circuit(state, ALL_GATES, n_qubits, warmup);
unsigned qubit = rand() % n_qubits;
StabilizerSimulator::pauli_t P;
switch (b)
{
case Basis::X:
P.X ^= (1ULL << qubit);
break;
case Basis::Y:
P.X ^= (1ULL << qubit);
P.Z ^= (1ULL << qubit);
case Basis::Z:
P.Z ^= (1ULL << qubit);
break;
}
auto start = std::chrono::high_resolution_clock::now();
state.MeasurePauli(P);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
return diff;
}
template<> std::chrono::duration<double> benchmark_measure(CHP::QState &state, unsigned n_qubits, unsigned warmup, Basis b)
{
//placeholder
random_circuit(state, ALL_GATES, n_qubits, warmup);
unsigned qubit = rand() % n_qubits;
StabilizerSimulator::pauli_t P;
switch (b)
{
case Basis::X:
CHP::hadamard(&state, qubit);
break;
case Basis::Y:
CHP::hadamard(&state, qubit);
CHP::phase(&state, qubit);
break;
}
auto start = std::chrono::high_resolution_clock::now();
CHPRES = CHP::measure(&state, qubit, 1);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
return diff;
}
template<> std::chrono::duration<double> benchmark_measure(GraphSim::GraphRegister &state, unsigned n_qubits, unsigned warmup, Basis b)
{
GraphSim::LocCliffOp basis_choice = GraphSim::lco_Z;
random_circuit(state, ALL_GATES, n_qubits, warmup);
unsigned qubit = rand() % n_qubits;
switch(b)
{
case Basis::X:
basis_choice = GraphSim::lco_X;
break;
case Basis::Y:
basis_choice = GraphSim::lco_Y;
break;
}
auto start = std::chrono::high_resolution_clock::now();
GSRES = state.measure(qubit, basis_choice);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
return diff;
}
template<class T> std::chrono::duration<double> benchmark_innerprod(T &state, T &state2, unsigned n_qubits, unsigned warmup)
{
//placeholder
random_circuit(state, ALL_GATES, n_qubits, warmup);
random_circuit(state2, ALL_GATES, n_qubits, warmup);
auto start = std::chrono::high_resolution_clock::now();
// state.InnerProduct(state2);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end-start;
return diff;
}
template<class T> std::chrono::duration<double> benchmark_op(T &state, unsigned n_qubits,
std::string op_string, unsigned warmup, Basis b)
{
random_circuit(state, ALL_GATES, n_qubits, warmup);
auto is_gate = gate_names.find(op_string);
if(is_gate != gate_names.end())
{
return benchmark_gate(state, n_qubits, is_gate->second);
}
if(op_string == "measure")
{
return benchmark_measure(state, n_qubits, warmup, b);
}
throw std::runtime_error("Don't recognise op_string: " + op_string);
}
int main(int argc, char* argv[])
{
if(argc < 5)
{
throw std::runtime_error("Expected at least four arguments: simulator, operation, beta, and an output file name.");
}
std::string sim_name = argv[1];
auto sim = rep_names.find(sim_name);
if(sim == rep_names.end())
{
throw std::runtime_error("Do not recognise simulator " + sim_name);
}
std::string op_string = argv[2];
double beta = std::stod(argv[3]);
std::cout << "Running with Simulator: " << sim_name << " and beta: " << beta << std::endl;
std::ofstream output_data;
std::string out_name = argv[4];
output_data.open(out_name);
output_data << "Num Qubits \t Average Operation Time\n";
Basis b = Basis::Z;
if (argc == 6)
{
if (op_string != "measure")
{
std::cout << "Extra arguments ignored unless this is a measurement" << std::endl;
}
else
{
std::string basis_choice = argv[5];
auto basis = basis_names.find(basis_choice);
if (basis == basis_names.end())
{
throw std::runtime_error("Do not recognise measurement basis " + basis_choice);
}
b = basis->second;
}
}
for(unsigned n=5; n<62; n++)
{
std::cout << n << " qubits run." << std::endl;
unsigned circuit_warmup = std::lrint(beta*n*std::log2(n));
std::chrono::duration<double> sum_time(0);
for(unsigned i=0; i<DEFAULT_REPETITIONS; i++)
{
switch(sim->second)
{
case Representation::CH:
{
StabilizerSimulator::CHState ch(n);
if(op_string != "innerprod")
{
sum_time += benchmark_op(ch, n, op_string, circuit_warmup, b);
}
else
{
StabilizerSimulator::CHState ch2(n);
sum_time += benchmark_innerprod(ch, ch2, n, circuit_warmup);
}
break;
}
case Representation::DCH:
{
StabilizerSimulator::DCHState dch(n);
if(op_string != "innerprod")
{
sum_time += benchmark_op(dch, n, op_string, circuit_warmup, b);
}
else
{
StabilizerSimulator::DCHState dch2(n);
sum_time += benchmark_innerprod(dch, dch2, n, circuit_warmup);
}
break;
}
case Representation::CHP:
{
CHP::QState chp;
CHP::initstae_(&chp, n, NULL);
if(op_string != "innerprod")
{
sum_time += benchmark_op(chp, n, op_string, circuit_warmup, b);
}
else
{
CHP::QState chp2;
CHP::initstae_(&chp2, n, NULL);
sum_time += benchmark_innerprod(chp, chp2, n, circuit_warmup);
}
break;
}
case Representation::GRAPHSIM:
{
GraphSim::GraphRegister gsim(n);
if(op_string != "innerprod")
{
sum_time += benchmark_op(gsim, n, op_string, circuit_warmup, b);
}
else
{
throw std::runtime_error("Graphsim doesn't support the Inner Product operation.");
}
break;
}
}
}
std::cout << "Cumulative time: " << sum_time.count() << "Average: " << sum_time.count()/DEFAULT_REPETITIONS << std::endl;
output_data << n << "\t" << sum_time.count()/DEFAULT_REPETITIONS << "\n";
}
output_data.close();
return 0;
}