prism-q 0.35.0

Fast Rust quantum circuit simulator. OpenQASM 3.0, multiple backends, AVX2 SIMD kernels, optional CUDA and MPI, QEC tooling, Python bindings.
Documentation
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
//! Row-level Clifford tableau operations shared by the dense stabilizer
//! backend, its SGI path, and the factored sub-tableaux.
//!
//! Rows live in a flat `xz` buffer of `2 * nw` words per row (X words then
//! Z words) with a parallel `phase` bit per row. Callers resolve a qubit to
//! a `QubitBit` once per gate; the per-row functions take the precomputed
//! coordinates so no derivation sits inside the row loop.

use super::simd::rowmul_words;
use crate::error::{PrismError, Result};
use crate::sim::unified_pauli::{PauliAxis, PauliTerm};
use std::borrow::Cow;

#[derive(Clone, Copy)]
pub(crate) struct QubitBit {
    pub word: usize,
    pub bit: usize,
    pub mask: u64,
}

impl QubitBit {
    #[inline(always)]
    pub(crate) fn of(q: usize) -> Self {
        QubitBit {
            word: q / 64,
            bit: q % 64,
            mask: 1u64 << (q % 64),
        }
    }
}

#[inline(always)]
pub(crate) fn h_row(row: &mut [u64], phase: &mut bool, nw: usize, a: QubitBit) {
    let xw = row[a.word];
    let zw = row[nw + a.word];
    let x = xw & a.mask;
    let z = zw & a.mask;
    *phase ^= (x != 0) && (z != 0);
    row[a.word] = (xw & !a.mask) | z;
    row[nw + a.word] = (zw & !a.mask) | x;
}

#[inline(always)]
pub(crate) fn s_row(row: &mut [u64], phase: &mut bool, nw: usize, a: QubitBit) {
    let x = row[a.word] & a.mask;
    let z = row[nw + a.word] & a.mask;
    *phase ^= (x != 0) && (z != 0);
    row[nw + a.word] ^= x;
}

#[inline(always)]
pub(crate) fn sdg_row(row: &mut [u64], phase: &mut bool, nw: usize, a: QubitBit) {
    let x = row[a.word] & a.mask;
    row[nw + a.word] ^= x;
    let z_new = row[nw + a.word] & a.mask;
    *phase ^= (x != 0) && (z_new != 0);
}

#[inline(always)]
pub(crate) fn x_row(row: &[u64], phase: &mut bool, nw: usize, a: QubitBit) {
    let z = row[nw + a.word] & a.mask;
    *phase ^= z != 0;
}

#[inline(always)]
pub(crate) fn y_row(row: &[u64], phase: &mut bool, nw: usize, a: QubitBit) {
    let x = row[a.word] & a.mask;
    let z = row[nw + a.word] & a.mask;
    *phase ^= (x ^ z) != 0;
}

#[inline(always)]
pub(crate) fn z_row(row: &[u64], phase: &mut bool, _nw: usize, a: QubitBit) {
    let x = row[a.word] & a.mask;
    *phase ^= x != 0;
}

#[inline(always)]
pub(crate) fn sx_row(row: &mut [u64], phase: &mut bool, nw: usize, a: QubitBit) {
    let x = row[a.word] & a.mask;
    let z = row[nw + a.word] & a.mask;
    *phase ^= (z != 0) && (x == 0);
    row[a.word] ^= z;
}

#[inline(always)]
pub(crate) fn sxdg_row(row: &mut [u64], phase: &mut bool, nw: usize, a: QubitBit) {
    let x = row[a.word] & a.mask;
    let z = row[nw + a.word] & a.mask;
    *phase ^= (x != 0) && (z != 0);
    row[a.word] ^= z;
}

#[inline(always)]
pub(crate) fn cx_row(row: &mut [u64], phase: &mut bool, nw: usize, c: QubitBit, t: QubitBit) {
    let xa = (row[c.word] >> c.bit) & 1;
    let za = (row[nw + c.word] >> c.bit) & 1;
    let xb = (row[t.word] >> t.bit) & 1;
    let zb = (row[nw + t.word] >> t.bit) & 1;

    *phase ^= (xa & zb & (xb ^ za ^ 1)) == 1;

    if xa == 1 {
        row[t.word] ^= t.mask;
    }
    if zb == 1 {
        row[nw + c.word] ^= c.mask;
    }
}

#[inline(always)]
pub(crate) fn cz_row(row: &mut [u64], phase: &mut bool, nw: usize, a: QubitBit, b: QubitBit) {
    let xa = (row[a.word] >> a.bit) & 1;
    let xb = (row[b.word] >> b.bit) & 1;
    let za = (row[nw + a.word] >> a.bit) & 1;
    let zb = (row[nw + b.word] >> b.bit) & 1;

    *phase ^= (xa & xb & (za ^ zb)) == 1;

    if xb == 1 {
        row[nw + a.word] ^= a.mask;
    }
    if xa == 1 {
        row[nw + b.word] ^= b.mask;
    }
}

#[inline(always)]
pub(crate) fn swap_row(row: &mut [u64], nw: usize, a: QubitBit, b: QubitBit) {
    let xa = (row[a.word] >> a.bit) & 1;
    let xb = (row[b.word] >> b.bit) & 1;
    if xa != xb {
        row[a.word] ^= a.mask;
        row[b.word] ^= b.mask;
    }

    let za = (row[nw + a.word] >> a.bit) & 1;
    let zb = (row[nw + b.word] >> b.bit) & 1;
    if za != zb {
        row[nw + a.word] ^= a.mask;
        row[nw + b.word] ^= b.mask;
    }
}

#[inline(always)]
pub(crate) fn sweep(
    xz: &mut [u64],
    phase: &mut [bool],
    nw: usize,
    par: bool,
    row_op: impl Fn(&mut [u64], &mut bool) + Send + Sync,
) {
    let stride = 2 * nw;
    #[cfg(feature = "parallel")]
    if par {
        use rayon::prelude::*;
        xz.par_chunks_mut(stride)
            .zip(phase.par_iter_mut())
            .for_each(|(row, phase)| row_op(row, phase));
        return;
    }
    #[cfg(not(feature = "parallel"))]
    let _ = par;
    for (row, phase) in xz.chunks_mut(stride).zip(phase.iter_mut()) {
        row_op(row, phase);
    }
}

#[inline(always)]
pub(crate) fn h_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize) {
    let a = QubitBit::of(a);
    sweep(xz, phase, nw, par, |row, phase| h_row(row, phase, nw, a));
}

#[inline(always)]
pub(crate) fn s_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize) {
    let a = QubitBit::of(a);
    sweep(xz, phase, nw, par, |row, phase| s_row(row, phase, nw, a));
}

#[inline(always)]
pub(crate) fn sdg_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize) {
    let a = QubitBit::of(a);
    sweep(xz, phase, nw, par, |row, phase| sdg_row(row, phase, nw, a));
}

#[inline(always)]
pub(crate) fn x_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize) {
    let a = QubitBit::of(a);
    sweep(xz, phase, nw, par, |row, phase| x_row(row, phase, nw, a));
}

#[inline(always)]
pub(crate) fn y_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize) {
    let a = QubitBit::of(a);
    sweep(xz, phase, nw, par, |row, phase| y_row(row, phase, nw, a));
}

#[inline(always)]
pub(crate) fn z_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize) {
    let a = QubitBit::of(a);
    sweep(xz, phase, nw, par, |row, phase| z_row(row, phase, nw, a));
}

#[inline(always)]
pub(crate) fn sx_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize) {
    let a = QubitBit::of(a);
    sweep(xz, phase, nw, par, |row, phase| sx_row(row, phase, nw, a));
}

#[inline(always)]
pub(crate) fn sxdg_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize) {
    let a = QubitBit::of(a);
    sweep(xz, phase, nw, par, |row, phase| sxdg_row(row, phase, nw, a));
}

#[inline(always)]
pub(crate) fn cx_all(
    xz: &mut [u64],
    phase: &mut [bool],
    nw: usize,
    par: bool,
    control: usize,
    target: usize,
) {
    let c = QubitBit::of(control);
    let t = QubitBit::of(target);
    sweep(xz, phase, nw, par, |row, phase| {
        cx_row(row, phase, nw, c, t)
    });
}

#[inline(always)]
pub(crate) fn cz_all(xz: &mut [u64], phase: &mut [bool], nw: usize, par: bool, a: usize, b: usize) {
    let a = QubitBit::of(a);
    let b = QubitBit::of(b);
    sweep(xz, phase, nw, par, |row, phase| {
        cz_row(row, phase, nw, a, b)
    });
}

#[inline(always)]
pub(crate) fn swap_all(
    xz: &mut [u64],
    phase: &mut [bool],
    nw: usize,
    par: bool,
    a: usize,
    b: usize,
) {
    let a = QubitBit::of(a);
    let b = QubitBit::of(b);
    sweep(xz, phase, nw, par, |row, _phase| swap_row(row, nw, a, b));
}

/// Multiply row `h` by row `i` (replace `h` with the Pauli product).
///
/// Fused phase+XOR: AG g-function with wordwise popcount, row XOR in the
/// same loop to avoid a separate memory pass.
pub(crate) fn rowmul(xz: &mut [u64], phase: &mut [bool], nw: usize, h: usize, i: usize) {
    debug_assert_ne!(h, i);
    let stride = 2 * nw;
    let base_h = h * stride;
    let base_i = i * stride;

    let initial_sum = if phase[i] { 2u64 } else { 0 } + if phase[h] { 2u64 } else { 0 };

    // SAFETY: h != i, so row regions [base_h..base_h+stride] and
    // [base_i..base_i+stride] are non-overlapping.
    let (dst_x, dst_z, src_x, src_z) = unsafe {
        let ptr = xz.as_mut_ptr();
        (
            std::slice::from_raw_parts_mut(ptr.add(base_h), nw),
            std::slice::from_raw_parts_mut(ptr.add(base_h + nw), nw),
            std::slice::from_raw_parts(ptr.add(base_i) as *const u64, nw),
            std::slice::from_raw_parts(ptr.add(base_i + nw) as *const u64, nw),
        )
    };

    let sum = rowmul_words(dst_x, dst_z, src_x, src_z, initial_sum);
    phase[h] = (sum & 3) >= 2;
}

pub(crate) fn copy_row(xz: &mut [u64], phase: &mut [bool], nw: usize, dst: usize, src: usize) {
    let stride = 2 * nw;
    let src_start = src * stride;
    let dst_start = dst * stride;
    xz.copy_within(src_start..src_start + stride, dst_start);
    phase[dst] = phase[src];
}

pub(crate) fn zero_row(xz: &mut [u64], phase: &mut [bool], nw: usize, r: usize) {
    let stride = 2 * nw;
    let start = r * stride;
    xz[start..start + stride].fill(0);
    phase[r] = false;
}

/// Rebuild rows 0..n as unit destabilizers via Gaussian elimination and
/// replace the stabilizer half (rows n..2n) with the reduced basis they pair
/// with. Row operations preserve the stabilizer group and its phases, so the
/// state is unchanged; the write-back is what makes destabilizer i
/// anticommute with stabilizer i and commute with every other generator,
/// which measurement relies on. After the call, stabilizer row n+q is the
/// unique generator with a pivot at qubit q, so each generator's support is
/// confined to one entanglement component of the state.
pub(crate) fn materialize_destabilizers(xz: &mut [u64], phase: &mut [bool], n: usize, nw: usize) {
    if n == 0 {
        return;
    }
    let stride = 2 * nw;

    xz[..n * stride].fill(0);
    phase[..n].fill(false);

    let mut stab_copy: Vec<u64> = xz[n * stride..2 * n * stride].to_vec();
    let mut stab_phase: Vec<bool> = phase[n..2 * n].to_vec();
    let mut pivot_row: Vec<u64> = vec![0u64; stride];

    for col in 0..n {
        let mut pivot = None;
        for row in col..n {
            let word = col / 64;
            let bit = col % 64;
            if stab_copy[row * stride + word] & (1u64 << bit) != 0 {
                pivot = Some(row);
                break;
            }
        }

        if pivot.is_none() {
            for row in col..n {
                let word = col / 64;
                let bit = col % 64;
                if stab_copy[row * stride + nw + word] & (1u64 << bit) != 0 {
                    pivot = Some(row);
                    break;
                }
            }

            if let Some(p) = pivot {
                if p != col {
                    let col_off = col * stride;
                    let p_off = p * stride;
                    for w in 0..stride {
                        stab_copy.swap(col_off + w, p_off + w);
                    }
                    stab_phase.swap(col, p);
                }

                let word = col / 64;
                let bit = col % 64;
                let bit_mask = 1u64 << bit;
                pivot_row.copy_from_slice(&stab_copy[col * stride..(col + 1) * stride]);
                let sp = stab_phase[col];

                for row in 0..n {
                    if row == col {
                        continue;
                    }
                    if stab_copy[row * stride + nw + word] & bit_mask != 0 {
                        let dst = &mut stab_copy[row * stride..(row + 1) * stride];
                        let initial =
                            if sp { 2u64 } else { 0 } + if stab_phase[row] { 2u64 } else { 0 };
                        let (dx, dz) = dst.split_at_mut(nw);
                        let sum = rowmul_words(
                            dx,
                            &mut dz[..nw],
                            &pivot_row[..nw],
                            &pivot_row[nw..2 * nw],
                            initial,
                        );
                        stab_phase[row] = (sum & 3) >= 2;
                    }
                }

                xz[col * stride + word] |= bit_mask;
                phase[col] = false;
            } else {
                xz[col * stride + col / 64] |= 1u64 << (col % 64);
                phase[col] = false;
            }
            continue;
        }

        let p = pivot.unwrap();
        if p != col {
            let col_off = col * stride;
            let p_off = p * stride;
            for w in 0..stride {
                stab_copy.swap(col_off + w, p_off + w);
            }
            stab_phase.swap(col, p);
        }

        let word = col / 64;
        let bit = col % 64;
        let bit_mask = 1u64 << bit;
        pivot_row.copy_from_slice(&stab_copy[col * stride..(col + 1) * stride]);
        let sp = stab_phase[col];

        for row in 0..n {
            if row == col {
                continue;
            }
            if stab_copy[row * stride + word] & bit_mask != 0 {
                let dst = &mut stab_copy[row * stride..(row + 1) * stride];
                let initial = if sp { 2u64 } else { 0 } + if stab_phase[row] { 2u64 } else { 0 };
                let (dx, dz) = dst.split_at_mut(nw);
                let sum = rowmul_words(
                    dx,
                    &mut dz[..nw],
                    &pivot_row[..nw],
                    &pivot_row[nw..2 * nw],
                    initial,
                );
                stab_phase[row] = (sum & 3) >= 2;
            }
        }

        xz[col * stride + nw + word] |= bit_mask;
        phase[col] = false;
    }

    xz[n * stride..2 * n * stride].copy_from_slice(&stab_copy);
    for (i, p) in stab_phase.into_iter().enumerate() {
        phase[n + i] = p;
    }
}

/// True when the Pauli string `(term_x, term_z)` anticommutes with the tableau
/// row `row` (`2 * nw` words, X then Z).
#[inline(always)]
fn anticommutes(row: &[u64], nw: usize, term_x: &[u64], term_z: &[u64]) -> bool {
    let (row_x, row_z) = row.split_at(nw);
    let mut parity = 0u32;
    for w in 0..nw {
        parity ^= (term_x[w] & row_z[w]).count_ones() ^ (term_z[w] & row_x[w]).count_ones();
    }
    parity & 1 == 1
}

/// Check that `xz` and `phase` hold `2 * n + 1` rows in the export layout and
/// that destabilizer `i` anticommutes with stabilizer `i` for every `i`. The
/// rest of the commutation structure is not checked.
pub(crate) fn check_imported_rows(n: usize, xz: &[u64], phase: &[bool]) -> Result<()> {
    let nw = n.div_ceil(64);
    let stride = 2 * nw;
    let rows = 2 * n + 1;
    if xz.len() != rows * stride {
        return Err(PrismError::InvalidParameter {
            message: format!(
                "tableau import for {n} qubits takes {} words ({rows} rows of {stride}), got {}",
                rows * stride,
                xz.len()
            ),
        });
    }
    if phase.len() != rows {
        return Err(PrismError::InvalidParameter {
            message: format!(
                "tableau import for {n} qubits takes {rows} phases, got {}",
                phase.len()
            ),
        });
    }
    for i in 0..n {
        let destab = &xz[i * stride..(i + 1) * stride];
        let stab = &xz[(n + i) * stride..(n + i + 1) * stride];
        if !anticommutes(stab, nw, &destab[..nw], &destab[nw..]) {
            return Err(PrismError::InvalidParameter {
                message: format!(
                    "tableau import: destabilizer row {i} commutes with stabilizer row {i}"
                ),
            });
        }
    }
    Ok(())
}

/// Tableau rows with current destabilizers for a read-only query, borrowed
/// when the backend's own rows qualify and owned when they had to be rebuilt.
pub(crate) type TableauRows<'a> = (Cow<'a, [u64]>, Cow<'a, [bool]>);

/// Expectation of the Pauli string `term` (one row: `nw` X words then `nw` Z
/// words) on the state the tableau stabilizes: 0 when the string anticommutes
/// with a generator, otherwise the sign (+1 or -1) that puts it in the
/// stabilizer group.
///
/// Destabilizer rows must be current: destabilizer `j` anticommutes with the
/// string exactly when generator `j` is a factor of it, so the sign comes from
/// multiplying those generators into the scratch row `acc` with the `rowmul`
/// phase rule. Cost is `O(n * nw)` words per string.
pub(crate) fn pauli_expectation(
    xz: &[u64],
    phase: &[bool],
    n: usize,
    nw: usize,
    term: &[u64],
    acc: &mut [u64],
) -> f64 {
    let stride = 2 * nw;
    let (term_x, term_z) = term.split_at(nw);
    let (acc_x, acc_z) = acc.split_at_mut(nw);
    for r in n..2 * n {
        if anticommutes(&xz[r * stride..(r + 1) * stride], nw, term_x, term_z) {
            return 0.0;
        }
    }

    acc_x.fill(0);
    acc_z.fill(0);
    let mut acc_phase = false;
    for j in 0..n {
        if anticommutes(&xz[j * stride..(j + 1) * stride], nw, term_x, term_z) {
            let g = (n + j) * stride;
            let initial = if phase[n + j] { 2u64 } else { 0 } + if acc_phase { 2u64 } else { 0 };
            let sum = rowmul_words(
                acc_x,
                acc_z,
                &xz[g..g + nw],
                &xz[g + nw..g + stride],
                initial,
            );
            acc_phase = (sum & 3) >= 2;
        }
    }
    debug_assert_eq!(acc_x, term_x);
    debug_assert_eq!(acc_z, term_z);
    if acc_phase { -1.0 } else { 1.0 }
}

/// Pack a joint Pauli observable into one tableau row over `n` qubits (`nw` X
/// words then `nw` Z words). Rejects a factor past `n` and a duplicate factor,
/// which the set bits detect without a separate table.
pub(crate) fn pack_pauli_string(
    observable: &[PauliTerm],
    n: usize,
    nw: usize,
    term: &mut [u64],
) -> Result<()> {
    term.fill(0);
    let (term_x, term_z) = term.split_at_mut(nw);
    for factor in observable {
        if factor.qubit >= n {
            return Err(PrismError::InvalidQubit {
                index: factor.qubit,
                register_size: n,
            });
        }
        let q = QubitBit::of(factor.qubit);
        if (term_x[q.word] | term_z[q.word]) & q.mask != 0 {
            return Err(PrismError::InvalidParameter {
                message: format!(
                    "joint Pauli observable has duplicate factor on qubit {}",
                    factor.qubit
                ),
            });
        }
        match factor.axis {
            PauliAxis::X => term_x[q.word] |= q.mask,
            PauliAxis::Z => term_z[q.word] |= q.mask,
            PauliAxis::Y => {
                term_x[q.word] |= q.mask;
                term_z[q.word] |= q.mask;
            }
        }
    }
    Ok(())
}