-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathpybind_main.h
More file actions
491 lines (467 loc) · 33.4 KB
/
Copy pathpybind_main.h
File metadata and controls
491 lines (467 loc) · 33.4 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __PYBIND_MAIN
#define __PYBIND_MAIN
#include <pybind11/complex.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
namespace py = pybind11;
#include <map>
#include <stdexcept>
#include <vector>
#include "../lib/circuit.h"
#include "../lib/expect.h"
#include "../lib/gates_cirq.h"
#include "../lib/operation.h"
#include "../lib/qtrajectory.h"
// Methods for mutating noiseless circuits.
void add_gate(const qsim::Cirq::GateKind gate_kind, const unsigned time,
const std::vector<unsigned>& qubits,
const std::map<std::string, float>& params,
qsim::Circuit<qsim::Operation<float>>* circuit);
void add_diagonal_gate(const unsigned time, const std::vector<unsigned>& qubits,
const std::vector<float>& angles,
qsim::Circuit<qsim::Operation<float>>* circuit);
void add_matrix_gate(const unsigned time, const std::vector<unsigned>& qubits,
const std::vector<float>& matrix,
qsim::Circuit<qsim::Operation<float>>* circuit);
void add_measurement(const unsigned time, const std::vector<unsigned>& qubits,
qsim::Circuit<qsim::Operation<float>>* circuit);
void control_last_gate(const std::vector<unsigned>& qubits,
const std::vector<unsigned>& values,
qsim::Circuit<qsim::Operation<float>>* circuit);
void add_channel(const unsigned time,
const std::vector<unsigned>& qubits,
const std::vector<std::tuple<float, std::vector<float>, bool>>&
prob_matrix_unitary_triples,
qsim::Circuit<qsim::Operation<float>>* ncircuit);
// Methods for populating opstrings.
void add_gate_to_opstring(
const qsim::Cirq::GateKind gate_kind,
const std::vector<unsigned>& qubits,
qsim::OpString<float>* opstring);
void add_matrix_gate_to_opstring(
const std::vector<unsigned>& qubits,
const std::vector<float>& matrix,
qsim::OpString<float>* opstring);
// Methods for simulating noiseless circuits.
std::vector<std::complex<float>> qsim_simulate(const py::dict &options);
py::array_t<float> qsim_simulate_fullstate(
const py::dict &options, uint64_t input_state);
py::array_t<float> qsim_simulate_fullstate(
const py::dict &options, const py::array_t<float> &input_vector);
std::vector<unsigned> qsim_sample(const py::dict &options);
std::vector<uint64_t> qsim_sample_final(
const py::dict &options, uint64_t num_samples);
// Methods for simulating noisy circuits.
std::vector<std::complex<float>> qtrajectory_simulate(const py::dict &options);
py::array_t<float> qtrajectory_simulate_fullstate(
const py::dict &options, uint64_t input_state);
py::array_t<float> qtrajectory_simulate_fullstate(
const py::dict &options, const py::array_t<float> &input_vector);
std::vector<unsigned> qtrajectory_sample(const py::dict &options);
std::vector<uint64_t> qtrajectory_sample_final(
const py::dict &options, uint64_t num_samples);
// As above, but returning expectation values instead.
std::vector<std::complex<double>> qsim_simulate_expectation_values(
const py::dict &options,
const std::vector<std::tuple<
std::vector<qsim::OpString<float>>,
unsigned>>& opsums_and_qubit_counts,
uint64_t input_state);
std::vector<std::complex<double>> qsim_simulate_expectation_values(
const py::dict &options,
const std::vector<std::tuple<
std::vector<qsim::OpString<float>>,
unsigned>>& opsums_and_qubit_counts,
const py::array_t<float> &input_vector);
std::vector<std::vector<std::complex<double>>>
qsim_simulate_moment_expectation_values(
const py::dict &options,
const std::vector<std::tuple<uint64_t, std::vector<
std::tuple<
std::vector<qsim::OpString<float>>,
unsigned
>>>>& opsums_and_qubit_counts,
uint64_t input_state);
std::vector<std::vector<std::complex<double>>>
qsim_simulate_moment_expectation_values(
const py::dict &options,
const std::vector<std::tuple<uint64_t, std::vector<
std::tuple<
std::vector<qsim::OpString<float>>,
unsigned
>>>>& opsums_and_qubit_counts,
const py::array_t<float> &input_vector);
std::vector<std::complex<double>> qtrajectory_simulate_expectation_values(
const py::dict &options,
const std::vector<std::tuple<
std::vector<qsim::OpString<float>>,
unsigned>>& opsums_and_qubit_counts,
uint64_t input_state);
std::vector<std::complex<double>> qtrajectory_simulate_expectation_values(
const py::dict &options,
const std::vector<std::tuple<
std::vector<qsim::OpString<float>>,
unsigned>>& opsums_and_qubit_counts,
const py::array_t<float> &input_vector);
std::vector<std::vector<std::complex<double>>>
qtrajectory_simulate_moment_expectation_values(
const py::dict &options,
const std::vector<std::tuple<uint64_t, std::vector<
std::tuple<
std::vector<qsim::OpString<float>>,
unsigned
>>>>& opsums_and_qubit_counts,
uint64_t input_state);
std::vector<std::vector<std::complex<double>>>
qtrajectory_simulate_moment_expectation_values(
const py::dict &options,
const std::vector<std::tuple<uint64_t, std::vector<
std::tuple<
std::vector<qsim::OpString<float>>,
unsigned
>>>>& opsums_and_qubit_counts,
const py::array_t<float> &input_vector);
// Hybrid simulator.
std::vector<std::complex<float>> qsimh_simulate(const py::dict &options);
#ifdef QSIM_DEVICE_STATE_BINDINGS
void bind_device_state_vector(py::module_& m);
#else
inline void bind_device_state_vector(py::module_& m) {}
#endif
template <typename T>
T ParseOptions(const py::dict& options, const char* key) {
if (!options.contains(key)) {
std::string msg = std::string("Argument ") + key + " is not provided.\n";
throw std::invalid_argument(msg);
}
const auto& value = options[key];
return value.cast<T>();
}
#define MODULE_BINDINGS \
m.doc() = "pybind11 plugin"; /* optional module docstring */ \
/* Methods for returning amplitudes */ \
m.def("qsim_simulate", &qsim_simulate, "Call the qsim simulator"); \
m.def("qtrajectory_simulate", &qtrajectory_simulate, \
"Call the qtrajectory simulator"); \
\
/* Methods for returning full state */ \
m.def("qsim_simulate_fullstate", \
static_cast<py::array_t<float>(*)(const py::dict&, uint64_t)>( \
&qsim_simulate_fullstate), \
"Call the qsim simulator for full state vector simulation"); \
m.def("qsim_simulate_fullstate", \
static_cast<py::array_t<float>(*)(const py::dict&, \
const py::array_t<float>&)>( \
&qsim_simulate_fullstate), \
"Call the qsim simulator for full state vector simulation"); \
\
m.def("qtrajectory_simulate_fullstate", \
static_cast<py::array_t<float>(*)(const py::dict&, uint64_t)>( \
&qtrajectory_simulate_fullstate), \
"Call the qtrajectory simulator for full state vector simulation"); \
m.def("qtrajectory_simulate_fullstate", \
static_cast<py::array_t<float>(*)(const py::dict&, \
const py::array_t<float>&)>( \
&qtrajectory_simulate_fullstate), \
"Call the qtrajectory simulator for full state vector simulation"); \
\
/* Methods for returning samples */ \
m.def("qsim_sample", &qsim_sample, "Call the qsim sampler"); \
m.def("qsim_sample_final", &qsim_sample_final, \
"Call the qsim final-state sampler"); \
m.def("qtrajectory_sample", &qtrajectory_sample, \
"Call the qtrajectory sampler"); \
m.def("qtrajectory_sample_final", &qtrajectory_sample_final, \
"Call the qtrajectory final-state sampler"); \
\
using OpString = qsim::OpString<float>; \
\
/* Methods for returning expectation values */ \
m.def("qsim_simulate_expectation_values", \
static_cast<std::vector<std::complex<double>>(*)( \
const py::dict&, \
const std::vector<std::tuple<std::vector<OpString>, unsigned>>&, \
uint64_t)>( \
&qsim_simulate_expectation_values), \
"Call the qsim simulator for expectation value simulation"); \
m.def("qsim_simulate_expectation_values", \
static_cast<std::vector<std::complex<double>>(*)( \
const py::dict&, \
const std::vector<std::tuple<std::vector<OpString>, unsigned>>&, \
const py::array_t<float>&)>( \
&qsim_simulate_expectation_values), \
"Call the qsim simulator for expectation value simulation"); \
\
m.def("qsim_simulate_moment_expectation_values", \
static_cast<std::vector<std::vector<std::complex<double>>>(*)( \
const py::dict&, \
const std::vector<std::tuple<uint64_t, std::vector< \
std::tuple<std::vector<OpString>, unsigned> \
>>>&, \
uint64_t)>( \
&qsim_simulate_moment_expectation_values), \
"Call the qsim simulator for step-by-step expectation value simulation"); \
m.def("qsim_simulate_moment_expectation_values", \
static_cast<std::vector<std::vector<std::complex<double>>>(*)( \
const py::dict&, \
const std::vector<std::tuple<uint64_t, std::vector< \
std::tuple<std::vector<OpString>, unsigned> \
>>>&, \
const py::array_t<float>&)>( \
&qsim_simulate_moment_expectation_values), \
"Call the qsim simulator for step-by-step expectation value simulation"); \
\
m.def("qtrajectory_simulate_expectation_values", \
static_cast<std::vector<std::complex<double>>(*)( \
const py::dict&, \
const std::vector<std::tuple<std::vector<OpString>, unsigned>>&, \
uint64_t)>( \
&qtrajectory_simulate_expectation_values), \
"Call the qtrajectory simulator for expectation value simulation"); \
m.def("qtrajectory_simulate_expectation_values", \
static_cast<std::vector<std::complex<double>>(*)( \
const py::dict&, \
const std::vector<std::tuple<std::vector<OpString>, unsigned>>&, \
const py::array_t<float>&)>( \
&qtrajectory_simulate_expectation_values), \
"Call the qtrajectory simulator for expectation value simulation"); \
\
m.def("qtrajectory_simulate_moment_expectation_values", \
static_cast<std::vector<std::vector<std::complex<double>>>(*)( \
const py::dict&, \
const std::vector<std::tuple<uint64_t, std::vector< \
std::tuple<std::vector<OpString>, unsigned> \
>>>&, \
uint64_t)>( \
&qtrajectory_simulate_moment_expectation_values), \
"Call the qtrajectory simulator for step-by-step " \
"expectation value simulation"); \
m.def("qtrajectory_simulate_moment_expectation_values", \
static_cast<std::vector<std::vector<std::complex<double>>>(*)( \
const py::dict&, \
const std::vector<std::tuple<uint64_t, std::vector< \
std::tuple<std::vector<OpString>, unsigned> \
>>>&, \
const py::array_t<float>&)>( \
&qtrajectory_simulate_moment_expectation_values), \
"Call the qtrajectory simulator for step-by-step " \
"expectation value simulation"); \
\
/* Method for hybrid simulation */ \
m.def("qsimh_simulate", &qsimh_simulate, "Call the qsimh simulator"); \
\
using GateKind = qsim::Cirq::GateKind; \
using OtherGateKind = qsim::OtherGateKind; \
using Circuit = qsim::Circuit<qsim::Operation<float>>; \
\
py::class_<Circuit>(m, "Circuit") \
.def(py::init<>()) \
.def_readwrite("num_qubits", &Circuit::num_qubits) \
.def_readwrite("ops", &Circuit::ops); \
\
py::class_<OpString>(m, "OpString") \
.def(py::init<>()) \
.def_readwrite("weight", &OpString::weight) \
.def_readwrite("ops", &OpString::ops); \
\
py::enum_<OtherGateKind>(m, "OtherGateKind") \
.value("kMeasurement", OtherGateKind::kMeasurement) \
.export_values(); \
\
py::enum_<GateKind>(m, "GateKind") \
.value("kI1", GateKind::kI1) \
.value("kI2", GateKind::kI2) \
.value("kI", GateKind::kI) \
.value("kXPowGate", GateKind::kXPowGate) \
.value("kYPowGate", GateKind::kYPowGate) \
.value("kZPowGate", GateKind::kZPowGate) \
.value("kHPowGate", GateKind::kHPowGate) \
.value("kCZPowGate", GateKind::kCZPowGate) \
.value("kCXPowGate", GateKind::kCXPowGate) \
.value("krx", GateKind::krx) \
.value("kry", GateKind::kry) \
.value("krz", GateKind::krz) \
.value("kH", GateKind::kH) \
.value("kS", GateKind::kS) \
.value("kCZ", GateKind::kCZ) \
.value("kCX", GateKind::kCX) \
.value("kT", GateKind::kT) \
.value("kX", GateKind::kX) \
.value("kY", GateKind::kY) \
.value("kZ", GateKind::kZ) \
.value("kPhasedXPowGate", GateKind::kPhasedXPowGate) \
.value("kPhasedXZGate", GateKind::kPhasedXZGate) \
.value("kXXPowGate", GateKind::kXXPowGate) \
.value("kYYPowGate", GateKind::kYYPowGate) \
.value("kZZPowGate", GateKind::kZZPowGate) \
.value("kXX", GateKind::kXX) \
.value("kYY", GateKind::kYY) \
.value("kZZ", GateKind::kZZ) \
.value("kSwapPowGate", GateKind::kSwapPowGate) \
.value("kISwapPowGate", GateKind::kISwapPowGate) \
.value("kriswap", GateKind::kriswap) \
.value("kSWAP", GateKind::kSWAP) \
.value("kISWAP", GateKind::kISWAP) \
.value("kPhasedISwapPowGate", GateKind::kPhasedISwapPowGate) \
.value("kgivens", GateKind::kgivens) \
.value("kFSimGate", GateKind::kFSimGate) \
.value("kTwoQubitDiagonalGate", GateKind::kTwoQubitDiagonalGate) \
.value("kThreeQubitDiagonalGate", GateKind::kThreeQubitDiagonalGate) \
.value("kCCZPowGate", GateKind::kCCZPowGate) \
.value("kCCXPowGate", GateKind::kCCXPowGate) \
.value("kCSwapGate", GateKind::kCSwapGate) \
.value("kCCZ", GateKind::kCCZ) \
.value("kCCX", GateKind::kCCX) \
.value("kMatrixGate", GateKind::kMatrixGate) \
.export_values(); \
\
m.def("add_gate", &add_gate, "Adds a gate to the given circuit."); \
m.def("add_diagonal_gate", &add_diagonal_gate, \
"Adds a two- or three-qubit diagonal gate to the given circuit."); \
m.def("add_matrix_gate", &add_matrix_gate, \
"Adds a matrix-defined gate to the given circuit."); \
m.def("add_measurement", &add_measurement, \
"Adds a measurement gate to the given circuit."); \
m.def("control_last_gate", &control_last_gate, \
"Applies controls to the final gate of a circuit."); \
\
m.def("add_channel", &add_channel, \
"Adds a channel to the given noisy circuit."); \
\
m.def("add_gate_to_opstring", &add_gate_to_opstring, \
"Adds a gate to the given opstring."); \
m.def("add_matrix_gate_to_opstring", &add_matrix_gate_to_opstring, \
"Adds a matrix gate to the given opstring.");
#define GPU_MODULE_BINDINGS \
m.doc() = "pybind11 plugin"; /* optional module docstring */ \
/* Methods for returning amplitudes */ \
m.def("qsim_simulate", &qsim_simulate, "Call the qsim simulator"); \
m.def("qtrajectory_simulate", &qtrajectory_simulate, \
"Call the qtrajectory simulator"); \
\
/* Methods for returning full state */ \
m.def("qsim_simulate_fullstate", \
static_cast<py::array_t<float>(*)(const py::dict&, uint64_t)>( \
&qsim_simulate_fullstate), \
"Call the qsim simulator for full state vector simulation"); \
m.def("qsim_simulate_fullstate", \
static_cast<py::array_t<float>(*)(const py::dict&, \
const py::array_t<float>&)>( \
&qsim_simulate_fullstate), \
"Call the qsim simulator for full state vector simulation"); \
\
m.def("qtrajectory_simulate_fullstate", \
static_cast<py::array_t<float>(*)(const py::dict&, uint64_t)>( \
&qtrajectory_simulate_fullstate), \
"Call the qtrajectory simulator for full state vector simulation"); \
m.def("qtrajectory_simulate_fullstate", \
static_cast<py::array_t<float>(*)(const py::dict&, \
const py::array_t<float>&)>( \
&qtrajectory_simulate_fullstate), \
"Call the qtrajectory simulator for full state vector simulation"); \
\
/* Zero-copy access to the final state in device memory (issue #836) */ \
bind_device_state_vector(m); \
\
/* Methods for returning samples */ \
m.def("qsim_sample", &qsim_sample, "Call the qsim sampler"); \
m.def("qsim_sample_final", &qsim_sample_final, \
"Call the qsim final-state sampler"); \
m.def("qtrajectory_sample", &qtrajectory_sample, \
"Call the qtrajectory sampler"); \
m.def("qtrajectory_sample_final", &qtrajectory_sample_final, \
"Call the qtrajectory final-state sampler"); \
\
using GateCirq = qsim::Cirq::GateCirq<float>; \
using OpString = qsim::OpString<float>; \
\
/* Methods for returning expectation values */ \
m.def("qsim_simulate_expectation_values", \
static_cast<std::vector<std::complex<double>>(*)( \
const py::dict&, \
const std::vector<std::tuple<std::vector<OpString>, unsigned>>&, \
uint64_t)>( \
&qsim_simulate_expectation_values), \
"Call the qsim simulator for expectation value simulation"); \
m.def("qsim_simulate_expectation_values", \
static_cast<std::vector<std::complex<double>>(*)( \
const py::dict&, \
const std::vector<std::tuple<std::vector<OpString>, unsigned>>&, \
const py::array_t<float>&)>( \
&qsim_simulate_expectation_values), \
"Call the qsim simulator for expectation value simulation"); \
\
m.def("qsim_simulate_moment_expectation_values", \
static_cast<std::vector<std::vector<std::complex<double>>>(*)( \
const py::dict&, \
const std::vector<std::tuple<uint64_t, std::vector< \
std::tuple<std::vector<OpString>, unsigned> \
>>>&, \
uint64_t)>( \
&qsim_simulate_moment_expectation_values), \
"Call the qsim simulator for step-by-step expectation value simulation"); \
m.def("qsim_simulate_moment_expectation_values", \
static_cast<std::vector<std::vector<std::complex<double>>>(*)( \
const py::dict&, \
const std::vector<std::tuple<uint64_t, std::vector< \
std::tuple<std::vector<OpString>, unsigned> \
>>>&, \
const py::array_t<float>&)>( \
&qsim_simulate_moment_expectation_values), \
"Call the qsim simulator for step-by-step expectation value simulation"); \
\
\
m.def("qtrajectory_simulate_expectation_values", \
static_cast<std::vector<std::complex<double>>(*)( \
const py::dict&, \
const std::vector<std::tuple<std::vector<OpString>, unsigned>>&, \
uint64_t)>( \
&qtrajectory_simulate_expectation_values), \
"Call the qtrajectory simulator for expectation value simulation"); \
m.def("qtrajectory_simulate_expectation_values", \
static_cast<std::vector<std::complex<double>>(*)( \
const py::dict&, \
const std::vector<std::tuple<std::vector<OpString>, unsigned>>&, \
const py::array_t<float>&)>( \
&qtrajectory_simulate_expectation_values), \
"Call the qtrajectory simulator for expectation value simulation"); \
\
m.def("qtrajectory_simulate_moment_expectation_values", \
static_cast<std::vector<std::vector<std::complex<double>>>(*)( \
const py::dict&, \
const std::vector<std::tuple<uint64_t, std::vector< \
std::tuple<std::vector<OpString>, unsigned> \
>>>&, \
uint64_t)>( \
&qtrajectory_simulate_moment_expectation_values), \
"Call the qtrajectory simulator for step-by-step " \
"expectation value simulation"); \
m.def("qtrajectory_simulate_moment_expectation_values", \
static_cast<std::vector<std::vector<std::complex<double>>>(*)( \
const py::dict&, \
const std::vector<std::tuple<uint64_t, std::vector< \
std::tuple<std::vector<OpString>, unsigned> \
>>>&, \
const py::array_t<float>&)>( \
&qtrajectory_simulate_moment_expectation_values), \
"Call the qtrajectory simulator for step-by-step " \
"expectation value simulation"); \
\
/* Method for hybrid simulation */ \
m.def("qsimh_simulate", &qsimh_simulate, "Call the qsimh simulator");
#endif