-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcpp_sparse_sim_bindings.rs
More file actions
592 lines (544 loc) · 19.8 KB
/
cpp_sparse_sim_bindings.rs
File metadata and controls
592 lines (544 loc) · 19.8 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright 2025 The PECOS Developers
//
// 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.
use pecos::prelude::*;
use pyo3::IntoPyObjectExt;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList, PySet, PyTuple};
// Monte Carlo engines create independent simulator copies for each thread.
// CppSparseStab implements Send, so each thread gets exclusive access to its own instance.
#[pyclass(name = "SparseSimCpp")]
pub struct PySparseSimCpp {
inner: CppSparseStab,
}
#[pymethods]
impl PySparseSimCpp {
#[new]
#[pyo3(signature = (num_qubits, seed=None))]
fn new(num_qubits: usize, seed: Option<u64>) -> Self {
let inner = match seed {
Some(s) => CppSparseStab::with_seed(num_qubits, s),
None => CppSparseStab::new(num_qubits),
};
PySparseSimCpp { inner }
}
fn set_seed(&mut self, seed: u64) {
self.inner.set_seed(seed);
}
fn reset(mut slf: PyRefMut<'_, Self>) -> PyRefMut<'_, Self> {
slf.inner.reset();
slf
}
fn __repr__(&self) -> String {
format!("SparseSimCpp(num_qubits={})", self.inner.num_qubits())
}
#[getter]
fn num_qubits(&self) -> usize {
self.inner.num_qubits()
}
#[allow(clippy::too_many_lines)]
#[pyo3(signature = (symbol, location, params=None))]
fn run_1q_gate(
&mut self,
symbol: &str,
location: usize,
params: Option<&Bound<'_, PyDict>>,
) -> PyResult<Option<u8>> {
match symbol {
"X" => {
self.inner.x(location);
Ok(None)
}
"Y" => {
self.inner.y(location);
Ok(None)
}
"Z" => {
self.inner.z(location);
Ok(None)
}
"H" => {
self.inner.h(location);
Ok(None)
}
"H2" => {
self.inner.h2(location);
Ok(None)
}
"H3" => {
self.inner.h3(location);
Ok(None)
}
"H4" => {
self.inner.h4(location);
Ok(None)
}
"H5" => {
self.inner.h5(location);
Ok(None)
}
"H6" => {
self.inner.h6(location);
Ok(None)
}
"F" | "F1" => {
self.inner.f(location);
Ok(None)
}
"Fdg" | "F1d" => {
self.inner.fdg(location);
Ok(None)
}
"F2" => {
self.inner.f2(location);
Ok(None)
}
"F2dg" | "F2d" => {
self.inner.f2dg(location);
Ok(None)
}
"F3" => {
self.inner.f3(location);
Ok(None)
}
"F3dg" | "F3d" => {
self.inner.f3dg(location);
Ok(None)
}
"F4" => {
self.inner.f4(location);
Ok(None)
}
"F4dg" | "F4d" => {
self.inner.f4dg(location);
Ok(None)
}
"MZ" => {
let result = self.inner.mz(location);
Ok(Some(u8::from(result.outcome)))
}
"MX" | "Measure +X" => {
let result = self.inner.mx(location);
Ok(Some(u8::from(result.outcome)))
}
"MY" | "Measure +Y" => {
let result = self.inner.my(location);
Ok(Some(u8::from(result.outcome)))
}
"MZForced" => {
if let Some(params) = params {
// Extract forced_outcome as integer first, then convert to bool
let forced_int = params
.get_item("forced_outcome")?
.ok_or_else(|| {
PyErr::new::<pyo3::exceptions::PyValueError, _>(
"MZForced requires a 'forced_outcome' parameter",
)
})?
.extract::<i32>()?;
let forced_value = forced_int != 0;
let result = self.inner.force_measure(location, forced_value);
Ok(Some(u8::from(result.outcome)))
} else {
Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
"MZForced requires a 'forced_outcome' parameter",
))
}
}
// Gate aliases - alternative names for common gates
"I" => Ok(None), // Identity gate - no operation
"Q" | "SX" | "SqrtX" => {
self.inner.sx(location);
Ok(None)
}
"Qd" | "SXdg" | "SqrtXdg" => {
self.inner.sxdg(location);
Ok(None)
}
"R" | "SY" | "SqrtY" => {
self.inner.sy(location);
Ok(None)
}
"Rd" | "SYdg" | "SqrtYdg" => {
self.inner.sydg(location);
Ok(None)
}
"S" | "SZ" | "SqrtZ" => {
self.inner.sz(location);
Ok(None)
}
"Sd" | "SZdg" | "SqrtZdg" => {
self.inner.szdg(location);
Ok(None)
}
"Measure" | "Measure +Z" | "measure Z" => {
// Check if forced_outcome parameter is provided
if let Some(params) = params
&& let Ok(Some(forced_item)) = params.get_item("forced_outcome")
{
// Has forced_outcome, use forced measurement
let forced_int: i32 = forced_item.extract()?;
let forced_value = forced_int != 0;
let result = self.inner.force_measure(location, forced_value);
return Ok(Some(u8::from(result.outcome)));
}
// No forced_outcome, use regular measurement
let result = self.inner.mz(location);
Ok(Some(u8::from(result.outcome)))
}
"Init" | "init |0>" => {
// Check if forced_outcome parameter is provided
// If so, do forced measurement + correction (matches old Python behavior)
if let Some(params) = params
&& let Ok(Some(forced_item)) = params.get_item("forced_outcome")
{
let forced_int: i32 = forced_item.extract()?;
if forced_int != -1 {
// Use forced measurement approach
let forced_value = forced_int != 0;
let result = self.inner.force_measure(location, forced_value);
// If measured |1>, flip to |0>
if result.outcome {
self.inner.x(location);
}
return Ok(None);
}
}
// No forced_outcome or forced_outcome==-1, use native preparation
self.inner.pz(location);
Ok(None)
}
"init |1>" => {
// Use native preparation gate
self.inner.pnz(location);
Ok(None)
}
"init |+>" => {
// Use native preparation gate
self.inner.px(location);
Ok(None)
}
"init |->" => {
// Use native preparation gate
self.inner.pnx(location);
Ok(None)
}
"init |+i>" => {
// Use native preparation gate
self.inner.py(location);
Ok(None)
}
"init |-i>" => {
// Use native preparation gate
self.inner.pny(location);
Ok(None)
}
"PZForced" => {
// Alias for "init |0>" with forced_outcome - used in random circuit tests
// Just handle it the same way as "init |0>"
if let Some(params) = params
&& let Ok(Some(forced_item)) = params.get_item("forced_outcome")
{
let forced_int: i32 = forced_item.extract()?;
if forced_int != -1 {
// Use forced measurement approach
let forced_value = forced_int != 0;
let result = self.inner.force_measure(location, forced_value);
// If measured |1>, flip to |0>
if result.outcome {
self.inner.x(location);
}
return Ok(None);
}
}
// No forced_outcome or forced_outcome==-1, use native preparation
self.inner.pz(location);
Ok(None)
}
_ => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
"Unsupported single-qubit gate: {symbol}"
))),
}
}
fn run_2q_gate(
&mut self,
symbol: &str,
location: &Bound<'_, PyTuple>,
_params: Option<&Bound<'_, PyDict>>,
) -> PyResult<Option<u8>> {
if location.len() != 2 {
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
"Two-qubit gate requires exactly 2 qubit locations",
));
}
let q1: usize = location.get_item(0)?.extract()?;
let q2: usize = location.get_item(1)?.extract()?;
match symbol {
"CX" | "CNOT" => {
self.inner.cx(q1, q2);
Ok(None)
}
"CY" => {
self.inner.cy(q1, q2);
Ok(None)
}
"CZ" => {
self.inner.cz(q1, q2);
Ok(None)
}
"SWAP" => {
self.inner.swap(q1, q2);
Ok(None)
}
"G2" | "G" => {
self.inner.g2(q1, q2);
Ok(None)
}
"SXX" | "SqrtXX" => {
self.inner.sxx(q1, q2);
Ok(None)
}
"SXXdg" | "SqrtXXdg" => {
self.inner.sxxdg(q1, q2);
Ok(None)
}
// Gate aliases - alternative names for two-qubit gates
"II" => Ok(None), // Two-qubit identity - no operation
_ => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
"Unsupported two-qubit gate: {symbol}"
))),
}
}
/// Internal gate dispatcher (tuple-based) - for internal use
fn run_gate_internal(
&mut self,
symbol: &str,
location: &Bound<'_, PyTuple>,
params: Option<&Bound<'_, PyDict>>,
) -> PyResult<Option<u8>> {
match location.len() {
1 => {
let qubit: usize = location.get_item(0)?.extract()?;
self.run_1q_gate(symbol, qubit, params)
}
2 => self.run_2q_gate(symbol, location, params),
_ => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
"Gates must have either 1 or 2 qubit locations",
)),
}
}
/// High-level `run_gate` that accepts a set of locations (Python wrapper compatible)
#[pyo3(signature = (symbol, locations, **params))]
fn run_gate(
&mut self,
symbol: &str,
locations: &Bound<'_, PyAny>,
params: Option<&Bound<'_, PyDict>>,
py: Python<'_>,
) -> PyResult<Py<PyDict>> {
self.run_gate_highlevel(symbol, locations, params, py)
}
// Additional methods that mirror SparseSim's API
fn h(&mut self, qubit: usize) {
self.inner.h(qubit);
}
fn x(&mut self, qubit: usize) {
self.inner.x(qubit);
}
fn y(&mut self, qubit: usize) {
self.inner.y(qubit);
}
fn z(&mut self, qubit: usize) {
self.inner.z(qubit);
}
fn cx(&mut self, control: usize, target: usize) {
self.inner.cx(control, target);
}
fn mz(&mut self, qubit: usize) -> bool {
self.inner.mz(qubit).outcome
}
fn mx(&mut self, qubit: usize) -> bool {
self.inner.mx(qubit).outcome
}
fn my(&mut self, qubit: usize) -> bool {
self.inner.my(qubit).outcome
}
fn stab_tableau(&self) -> String {
self.inner.stab_tableau()
}
fn destab_tableau(&self) -> String {
self.inner.destab_tableau()
}
// Expose preparation gates for testing
fn py(&mut self, qubit: usize) {
self.inner.py(qubit);
}
fn pny(&mut self, qubit: usize) {
self.inner.pny(qubit);
}
/// High-level `run_gate` method that accepts a set of locations
#[pyo3(signature = (symbol, locations, **params))]
fn run_gate_highlevel(
&mut self,
symbol: &str,
locations: &Bound<'_, PyAny>,
params: Option<&Bound<'_, PyDict>>,
py: Python<'_>,
) -> PyResult<Py<PyDict>> {
let output = PyDict::new(py);
// Check if simulate_gate is False
if let Some(p) = params
&& let Ok(Some(sg)) = p.get_item("simulate_gate")
&& let Ok(false) = sg.extract::<bool>()
{
return Ok(output.into());
}
// Convert locations to a vector
let locations_set: Bound<PySet> = locations.clone().cast_into()?;
for location in locations_set.iter() {
// Convert location to tuple
let loc_tuple: Bound<'_, PyTuple> = if location.is_instance_of::<PyTuple>() {
location.clone().cast_into()?
} else {
// Single qubit - wrap in tuple
PyTuple::new(py, std::slice::from_ref(&location))?
};
// Call the underlying run_gate_internal
let result = self.run_gate_internal(symbol, &loc_tuple, params)?;
// Only add to output if result is Some (non-zero measurement)
if let Some(value) = result {
output.set_item(location, value)?;
}
}
Ok(output.into())
}
/// Execute a quantum circuit
#[pyo3(signature = (circuit, removed_locations=None))]
fn run_circuit(
&mut self,
circuit: &Bound<'_, PyAny>,
removed_locations: Option<&Bound<'_, PySet>>,
py: Python<'_>,
) -> PyResult<Py<PyDict>> {
let results = PyDict::new(py);
// Iterate over circuit items
for item in circuit.call_method0("items")?.try_iter()? {
let item = item?;
let tuple: Bound<PyTuple> = item.clone().cast_into()?;
let symbol: String = tuple.get_item(0)?.extract()?;
let locations_item = tuple.get_item(1)?;
let locations: Bound<PySet> = locations_item.clone().cast_into()?;
let params_item = tuple.get_item(2)?;
let params: Bound<PyDict> = params_item.clone().cast_into()?;
// Subtract removed_locations if provided
let final_locations = if let Some(removed) = removed_locations {
locations.call_method1("__sub__", (removed,))?
} else {
locations.clone().into_any()
};
// Run the gate
let gate_results =
self.run_gate_highlevel(&symbol, &final_locations, Some(¶ms), py)?;
// Update results
results.call_method1("update", (gate_results,))?;
}
Ok(results.into())
}
/// Add faults by running a circuit
#[pyo3(signature = (circuit, removed_locations=None))]
fn add_faults(
&mut self,
circuit: &Bound<'_, PyAny>,
removed_locations: Option<&Bound<'_, PySet>>,
py: Python<'_>,
) -> PyResult<()> {
self.run_circuit(circuit, removed_locations, py)?;
Ok(())
}
#[getter]
fn bindings(slf: PyRef<'_, Self>) -> PyResult<crate::simulator_utils::GateBindingsDict> {
// Create a Rust GateBindingsDict directly
let py = slf.py();
let sim_obj: Py<PyAny> = slf.into_bound_py_any(py)?.unbind();
Ok(crate::simulator_utils::GateBindingsDict::new(sim_obj))
}
#[getter]
fn stabs(slf: PyRef<'_, Self>) -> PyResult<crate::simulator_utils::TableauWrapper> {
// Create a Rust TableauWrapper directly with is_stab=true
let py = slf.py();
let sim_obj: Py<PyAny> = slf.into_bound_py_any(py)?.unbind();
Ok(crate::simulator_utils::TableauWrapper::new(sim_obj, true))
}
#[getter]
fn destabs(slf: PyRef<'_, Self>) -> PyResult<crate::simulator_utils::TableauWrapper> {
// Create a Rust TableauWrapper directly with is_stab=false
let py = slf.py();
let sim_obj: Py<PyAny> = slf.into_bound_py_any(py)?.unbind();
Ok(crate::simulator_utils::TableauWrapper::new(sim_obj, false))
}
#[pyo3(signature = (verbose=None, print_y=None, print_destabs=None))]
fn print_stabs(
&self,
verbose: Option<bool>,
print_y: Option<bool>,
print_destabs: Option<bool>,
py: Python<'_>,
) -> PyResult<Py<PyAny>> {
let verbose = verbose.unwrap_or(true);
let print_y = print_y.unwrap_or(true);
let print_destabs = print_destabs.unwrap_or(false);
// Get raw tableaus
let stabs_raw = self.inner.stab_tableau();
let adjust_fn = py.import("pecos_rslib")?.getattr("adjust_tableau_string")?;
// Process stabilizers
let stabs_lines: Vec<&str> = stabs_raw.lines().collect();
let mut stabs_formatted = Vec::new();
for line in stabs_lines {
let adjusted = adjust_fn.call1((line, true, print_y))?;
stabs_formatted.push(adjusted.extract::<String>()?);
}
if print_destabs {
// Process destabilizers
let destabs_raw = self.inner.destab_tableau();
let destabs_lines: Vec<&str> = destabs_raw.lines().collect();
let mut destabs_formatted = Vec::new();
for line in destabs_lines {
let adjusted = adjust_fn.call1((line, false, print_y))?;
destabs_formatted.push(adjusted.extract::<String>()?);
}
if verbose {
println!("Stabilizers:");
for line in &stabs_formatted {
println!("{line}");
}
println!("Destabilizers:");
for line in &destabs_formatted {
println!("{line}");
}
}
// Return tuple of (stabs, destabs) - convert to Python lists first, then tuple
let stabs_list = PyList::new(py, stabs_formatted)?;
let destabs_list = PyList::new(py, destabs_formatted)?;
let tuple = PyTuple::new(py, [stabs_list.as_any(), destabs_list.as_any()])?;
Ok(tuple.into())
} else {
if verbose {
println!("Stabilizers:");
for line in &stabs_formatted {
println!("{line}");
}
}
// Return just stabs as a list
let stabs_list = PyList::new(py, stabs_formatted)?;
Ok(stabs_list.into())
}
}
}