From 25877c18ad4c4788bac781542168373717b373cf Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 11:23:48 +0100 Subject: [PATCH 01/60] feat(ppvm-tableau-2): add the inverse tableau with physical gates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit InverseTableau stores C⁻¹ as packed rows with one sign bit each, with u64 words behind a private alias. Physical gates go through ApplyGateRule (B = bool): the rule evaluated on the gate's generators names the rows to multiply with row_product. Checked against Heisenberg propagation of Phased and the commutation invariant. Widen the ApplyGateRule docs to types that apply a rule other than by updating each held Pauli. Co-Authored-By: Claude Opus 5.5 (1M context) --- Cargo.lock | 10 + Cargo.toml | 2 +- crates/ppvm-tableau-2/Cargo.toml | 14 + crates/ppvm-tableau-2/src/inverse_tableau.rs | 357 ++++++++++++++++++ crates/ppvm-tableau-2/src/lib.rs | 9 + .../src/gates/apply_gate_rule.rs | 31 +- 6 files changed, 410 insertions(+), 13 deletions(-) create mode 100644 crates/ppvm-tableau-2/Cargo.toml create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau.rs create mode 100644 crates/ppvm-tableau-2/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 80c5f4d06..d3dc6990b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2353,6 +2353,16 @@ dependencies = [ "smallvec", ] +[[package]] +name = "ppvm-tableau-2" +version = "0.3.0" +dependencies = [ + "ppvm-pauli-kernels", + "ppvm-pauli-word-2", + "ppvm-phased-pauli-word-2", + "ppvm-traits-2", +] + [[package]] name = "ppvm-tableau-sum" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index ff106b1a7..dc401b399 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ members = [ # Built by `cargo build --workspace --all-targets` in CI so the skill # can't silently drift away from the public API. "crates/ppvm-tui", - "skills/ppvm-usage/examples/rust", "crates/ppvm-lossy-pauli-word-2", "crates/ppvm-phased-pauli-word-2", "crates/ppvm-pauli-kernels", + "skills/ppvm-usage/examples/rust", "crates/ppvm-lossy-pauli-word-2", "crates/ppvm-phased-pauli-word-2", "crates/ppvm-pauli-kernels", "crates/ppvm-tableau-2", ] # Single version for every crate and the `bloqade-ppvm` Python package diff --git a/crates/ppvm-tableau-2/Cargo.toml b/crates/ppvm-tableau-2/Cargo.toml new file mode 100644 index 000000000..fd3082d4e --- /dev/null +++ b/crates/ppvm-tableau-2/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ppvm-tableau-2" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true + +[dependencies] +ppvm-pauli-kernels = { path = "../ppvm-pauli-kernels" } +ppvm-traits-2 = { path = "../ppvm-traits-2" } + +[dev-dependencies] +ppvm-pauli-word-2 = { path = "../ppvm-pauli-word-2" } +ppvm-phased-pauli-word-2 = { path = "../ppvm-phased-pauli-word-2" } diff --git a/crates/ppvm-tableau-2/src/inverse_tableau.rs b/crates/ppvm-tableau-2/src/inverse_tableau.rs new file mode 100644 index 000000000..468b6a4d4 --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau.rs @@ -0,0 +1,357 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! The inverse stabilizer tableau `C⁻¹` of a Clifford `C`. +//! +//! Row `ix_q` is `C⁻¹ X_q C` and row `iz_q` is `C⁻¹ Z_q C`, each stored as +//! packed X and Z bits plus one sign bit (rows are Hermitian, so their phase +//! is ±1). Rows are indexed `ix_q = q`, `iz_q = n + q`. +//! +//! Words are touched only through [`BitWord`], [`BITWORD_BITS`], [`ZERO`], +//! [`get_bit`], [`flip_bit`] and `Bits`, so a wider storage word can be tried +//! by changing the alias. Where rows sit in the arena is decided only by +//! `x_at` / `z_at`. + +use ppvm_pauli_kernels::row_product; +use ppvm_traits_2::ApplyGateRule; + +/// Storage word. Swap to benchmark other widths. +type BitWord = u64; +const BITWORD_BITS: usize = BitWord::BITS as usize; +const ZERO: BitWord = 0; + +#[inline(always)] +fn get_bit(w: BitWord, i: usize) -> bool { + (w >> i) & 1 == 1 +} + +#[inline(always)] +fn flip_bit(w: &mut BitWord, i: usize) { + *w ^= 1 << i; +} + +/// The inverse tableau of an `n`-qubit Clifford. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct InverseTableau { + n: usize, + /// Words per row and plane: `n.div_ceil(BITWORD_BITS)`. + stride: usize, + /// X and Z words of all `2n` rows, arranged by `x_at` / `z_at`. + words: Vec, + /// Bit `r` is the sign of row `r` (set = −). + signs: Vec, +} + +// NOTE: we could impl Word for RowRef to make it a true PauliWord if we ever need it +/// A borrowed row: its X words, Z words and sign. +#[derive(Clone, Copy, Debug)] +pub struct RowRef<'a> { + pub x: &'a [BitWord], + pub z: &'a [BitWord], + pub negative: bool, +} + +impl RowRef<'_> { + /// X bit of the row at qubit `q`. + pub fn x_bit(&self, q: usize) -> bool { + get_bit(self.x[q / BITWORD_BITS], q % BITWORD_BITS) + } + + /// Z bit of the row at qubit `q`. + pub fn z_bit(&self, q: usize) -> bool { + get_bit(self.z[q / BITWORD_BITS], q % BITWORD_BITS) + } +} + +impl InverseTableau { + /// The identity Clifford, i.e. the state `|0…0⟩`: `ix_q = X_q`, `iz_q = Z_q`. + pub fn new(n: usize) -> Self { + let stride = n.div_ceil(BITWORD_BITS); + let mut t = Self { + n, + stride, + words: vec![ZERO; 4 * n * stride], + signs: vec![ZERO; (2 * n).div_ceil(BITWORD_BITS)], + }; + for q in 0..n { + let (w, b) = (q / BITWORD_BITS, q % BITWORD_BITS); + let ix = t.x_at(q) + w; + let iz = t.z_at(n + q) + w; + flip_bit(&mut t.words[ix], b); + flip_bit(&mut t.words[iz], b); + } + t + } + + pub fn num_qubits(&self) -> usize { + self.n + } + + /// Row `ix_q = C⁻¹ X_q C`. + pub fn ix(&self, q: usize) -> RowRef<'_> { + debug_assert!(q < self.n); + self.row(q) + } + + /// Row `iz_q = C⁻¹ Z_q C`. + pub fn iz(&self, q: usize) -> RowRef<'_> { + debug_assert!(q < self.n); + self.row(self.n + q) + } + + fn row(&self, r: usize) -> RowRef<'_> { + let (x, z) = (self.x_at(r), self.z_at(r)); + RowRef { + x: &self.words[x..x + self.stride], + z: &self.words[z..z + self.stride], + negative: get_bit(self.signs[r / BITWORD_BITS], r % BITWORD_BITS), + } + } + + fn flip_sign(&mut self, r: usize) { + flip_bit(&mut self.signs[r / BITWORD_BITS], r % BITWORD_BITS); + } + + /// Replace rows `rows[g]` by the images of their generators. + /// + /// `images[g] = (bits, flip)` says that generator `g` maps to + /// `(−1)^flip · P(bits)`, with `bits` over the generators in `rows` order + /// (X then Z per qubit). The new row is that product of the old rows. + fn recombine(&mut self, rows: [usize; M], images: [([bool; M], bool); M]) { + let s = self.stride; + // Old rows, then new rows, each stored as [x | z]. + let mut buf = vec![ZERO; 2 * M * 2 * s]; + let (old, new) = buf.split_at_mut(M * 2 * s); + let mut old_negative = [false; M]; + for (j, &r) in rows.iter().enumerate() { + let row = self.row(r); + old[2 * j * s..(2 * j + 1) * s].copy_from_slice(row.x); + old[(2 * j + 1) * s..(2 * j + 2) * s].copy_from_slice(row.z); + old_negative[j] = row.negative; + } + let mut new_negative = [false; M]; + for (g, (bits, flip)) in images.iter().enumerate() { + let (nx, nz) = new[2 * g * s..(2 * g + 2) * s].split_at_mut(s); + // i per Y (P(bits) = i^#Y · X…Z… in generator order), −1 per flip. + let ys = bits.chunks(2).filter(|xz| xz[0] && xz[1]).count() as u32; + let mut e = ys + 2 * *flip as u32; + for j in (0..M).filter(|&j| bits[j]) { + let (ox, oz) = old[2 * j * s..(2 * j + 2) * s].split_at(s); + e += row_product(nx, nz, ox, oz) as u32 + 2 * old_negative[j] as u32; + } + debug_assert!(e.is_multiple_of(2), "recombined row is not Hermitian"); + new_negative[g] = e % 4 == 2; + } + for (g, &r) in rows.iter().enumerate() { + let (x, z) = (self.x_at(r), self.z_at(r)); + self.words[x..x + s].copy_from_slice(&new[2 * g * s..(2 * g + 1) * s]); + self.words[z..z + s].copy_from_slice(&new[(2 * g + 1) * s..(2 * g + 2) * s]); + if self.row(r).negative != new_negative[g] { + self.flip_sign(r); + } + } + } + + // Arrangement: blocks by row kind, ix-X, ix-Z, iz-X, iz-Z (#204's order). + #[inline(always)] + fn x_at(&self, r: usize) -> usize { + let (kind, i) = (r / self.n, r % self.n); + ((2 * kind) * self.n + i) * self.stride + } + + #[inline(always)] + fn z_at(&self, r: usize) -> usize { + let (kind, i) = (r / self.n, r % self.n); + ((2 * kind + 1) * self.n + i) * self.stride + } +} + +/// Physical gates, `C ← G·C`: the new row for generator `P` is `C⁻¹ (G† P G) C`, +/// so the rule evaluated on `P` names the old rows to multiply. +impl ApplyGateRule for InverseTableau { + type B = bool; + + fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + let images = [rule(true, false), rule(false, true)]; + self.recombine([q, self.n + q], images); + } + + fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { + let unit = |j: usize| std::array::from_fn(|i| i == j); + let images = std::array::from_fn(|j| rule(unit(j))); + self.recombine([a, self.n + a, b, self.n + b], images); + } + + fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + let ((x_bits, x_flip), (z_bits, z_flip)) = (rule(true, false), rule(false, true)); + debug_assert!(x_bits == [true, false] && z_bits == [false, true]); + if x_flip { + self.flip_sign(q); + } + if z_flip { + self.flip_sign(self.n + q); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use ppvm_pauli_kernels::Bits; + use ppvm_pauli_word_2::PauliWord; + use ppvm_phased_pauli_word_2::Phased; + use ppvm_traits_2::{Clifford, PauliBits, Phase}; + + const SIZES: [usize; 7] = [1, 2, 7, 63, 64, 65, 130]; + + /// Whether two rows anticommute: odd symplectic product. + fn anticommute(a: RowRef, b: RowRef) -> bool { + let mut parity = 0; + for i in 0..a.x.len() { + parity ^= ((a.x[i] & b.z[i]) ^ (a.z[i] & b.x[i])).popcount() & 1; + } + parity == 1 + } + + #[test] + fn identity_rows_are_single_paulis() { + for n in SIZES { + let t = InverseTableau::new(n); + for q in 0..n { + for j in 0..n { + assert_eq!(t.ix(q).x_bit(j), j == q); + assert!(!t.ix(q).z_bit(j)); + assert!(!t.iz(q).x_bit(j)); + assert_eq!(t.iz(q).z_bit(j), j == q); + } + assert!(!t.ix(q).negative && !t.iz(q).negative); + } + } + } + + /// Conjugation preserves commutation: `ix_a`, `iz_b` anticommute iff `a == b`. + fn assert_canonical_commutation(t: &InverseTableau) { + let n = t.num_qubits(); + for a in 0..n { + for b in 0..n { + assert_eq!(anticommute(t.ix(a), t.iz(b)), a == b); + assert!(!anticommute(t.ix(a), t.ix(b))); + assert!(!anticommute(t.iz(a), t.iz(b))); + } + } + } + + #[test] + fn identity_has_canonical_commutation() { + for n in SIZES { + assert_canonical_commutation(&InverseTableau::new(n)); + } + } + + #[derive(Clone, Copy, Debug)] + enum Gate { + One(usize, usize), + Two(usize, usize, usize), + } + + fn apply(t: &mut T, gate: Gate) { + match gate { + Gate::One(g, q) => match g { + 0 => t.x(q), + 1 => t.y(q), + 2 => t.z(q), + 3 => t.h(q), + 4 => t.s(q), + 5 => t.s_dag(q), + 6 => t.sqrt_x(q), + 7 => t.sqrt_x_dag(q), + 8 => t.sqrt_y(q), + _ => t.sqrt_y_dag(q), + }, + Gate::Two(g, a, b) => match g { + 0 => t.cnot(a, b), + 1 => t.cz(a, b), + _ => t.cy(a, b), + }, + } + } + + /// A random circuit from a xorshift seed. + fn random_circuit(n: usize, len: usize, mut seed: u64) -> Vec { + let mut next = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed as usize + }; + (0..len) + .map(|_| { + if n < 2 || next() % 3 == 0 { + Gate::One(next() % 10, next() % n) + } else { + let a = next() % n; + let b = (a + 1 + next() % (n - 1)) % n; + Gate::Two(next() % 3, a, b) + } + }) + .collect() + } + + type Reference = Phased>; + + /// `C⁻¹ P C` for `C = G_m ⋯ G_1`: propagate `P` through the gates in reverse. + fn heisenberg(n: usize, q: usize, z: bool, circuit: &[Gate]) -> Reference { + let mut word = PauliWord::new(n); + if z { + word.set_z_bit(q, true); + } else { + word.set_x_bit(q, true); + } + let mut p = Phased::new(word); + for &gate in circuit.iter().rev() { + apply(&mut p, gate); + } + p + } + + fn assert_row_eq(row: RowRef, expected: &Reference, n: usize) { + for j in 0..n { + assert_eq!(row.x_bit(j), expected.word().x_bit(j), "x bit {j}"); + assert_eq!(row.z_bit(j), expected.word().z_bit(j), "z bit {j}"); + } + let negative = match expected.phase() { + Phase::Pos1 => false, + Phase::Neg1 => true, + phase => panic!("non-Hermitian reference phase {phase:?}"), + }; + assert_eq!(row.negative, negative, "sign"); + } + + /// Rows must equal `C⁻¹ X_q C` / `C⁻¹ Z_q C` computed by Heisenberg + /// propagation of single Pauli words. + #[test] + fn physical_gates_match_heisenberg_propagation() { + for (i, n) in SIZES.into_iter().enumerate() { + let circuit = random_circuit(n, 200, 0x9e37_79b9 + i as u64); + let mut t = InverseTableau::new(n); + for &gate in &circuit { + apply(&mut t, gate); + } + for q in 0..n { + assert_row_eq(t.ix(q), &heisenberg(n, q, false, &circuit), n); + assert_row_eq(t.iz(q), &heisenberg(n, q, true, &circuit), n); + } + } + } + + #[test] + fn physical_gates_preserve_commutation() { + for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { + let mut t = InverseTableau::new(n); + for gate in random_circuit(n, 100, 0x5eed + i as u64) { + apply(&mut t, gate); + assert_canonical_commutation(&t); + } + } + } +} diff --git a/crates/ppvm-tableau-2/src/lib.rs b/crates/ppvm-tableau-2/src/lib.rs new file mode 100644 index 000000000..43315bd6f --- /dev/null +++ b/crates/ppvm-tableau-2/src/lib.rs @@ -0,0 +1,9 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Hybrid stabilizer simulation: an inverse tableau plus a dense vector over +//! the active qubits. Design: `docs/design/hybrid-tableau.md`. + +mod inverse_tableau; + +pub use inverse_tableau::{InverseTableau, RowRef}; diff --git a/crates/ppvm-traits-2/src/gates/apply_gate_rule.rs b/crates/ppvm-traits-2/src/gates/apply_gate_rule.rs index 674bce625..3b5bbf888 100644 --- a/crates/ppvm-traits-2/src/gates/apply_gate_rule.rs +++ b/crates/ppvm-traits-2/src/gates/apply_gate_rule.rs @@ -1,10 +1,10 @@ // SPDX-FileCopyrightText: 2026 The PPVM Authors // SPDX-License-Identifier: Apache-2.0 -//! [`ApplyGateRule`]: the one trait a Pauli container implements to get [`Clifford`]. +//! [`ApplyGateRule`]: the one trait a type implements to get [`Clifford`]. //! -//! The gate formulas live once, in `ppvm_pauli_kernels::heisenberg`. A container -//! only says how to visit its bits: it receives a whole rule and runs it in its +//! The gate formulas live once, in `ppvm_pauli_kernels::heisenberg`. A type +//! only says how to apply a rule: it receives a whole rule and runs it in its //! own loop, so every gate stays a single fused pass. The blanket impl below //! maps each gate to its rule. @@ -12,13 +12,20 @@ use ppvm_pauli_kernels::{Bits, heisenberg}; use super::Clifford; -/// A container of Pauli operators that can run a Heisenberg gate rule on them. +/// A type that applies a Clifford gate given by its Heisenberg rule. /// -/// For each Pauli it holds, `apply1(q, rule)` reads that Pauli's bits at qubit -/// `q` as `(x, z)`, calls `rule`, writes the returned bits back, and multiplies -/// the Pauli's sign by `(−1)^flip`. With `B = bool` the container visits its -/// Paulis one at a time; with an integer `B` each bit position is a separate -/// Pauli. A container without signs ignores the flip. +/// `apply1(q, rule)` applies the gate whose rule is `rule` (it maps `P` to +/// `G† P G`) at qubit `q`; `apply2` does the same for two qubits. How depends on +/// the type: +/// +/// - A container of Pauli operators reads each Pauli's bits at `q` as `(x, z)`, +/// calls `rule`, writes the returned bits back, and multiplies the Pauli's sign +/// by `(−1)^flip`. With `B = bool` it visits its Paulis one at a time; with an +/// integer `B` each bit position is a separate Pauli. A container without +/// signs ignores the flip. +/// - Other types may evaluate `rule` on fixed inputs instead, e.g. an inverse +/// tableau evaluates it on the gate's generators to find which rows to +/// multiply. /// /// With `B = bool`, `apply1` and `apply2` must call `rule` exactly once if they /// apply the gate, and not at all if they skip it (e.g. on a lost site). Check @@ -31,11 +38,11 @@ pub trait ApplyGateRule { /// The value one rule call works on: `bool` or a bit-sliced integer. type B: Bits; - /// Run a one-qubit rule on qubit `q`. + /// Apply a one-qubit rule at qubit `q`. fn apply1(&mut self, q: usize, rule: impl Fn(Self::B, Self::B) -> ([Self::B; 2], Self::B)); - /// Run a two-qubit rule on the bits `[x_a, z_a, x_b, z_b]`. For controlled - /// gates `a` is the control. + /// Apply a two-qubit rule at qubits `a`, `b`; the rule's bits are + /// `[x_a, z_a, x_b, z_b]`. For controlled gates `a` is the control. fn apply2( &mut self, a: usize, From e60089f758bad9f4a77c5cffc0b6479619f94c1e Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 13:07:35 +0100 Subject: [PATCH 02/60] perf(ppvm-tableau-2): fuse each physical gate into one pass Recombine the rows a gate touches in one loop over word indices, in place: new words at an index depend only on old words at that index. Removes the per-gate scratch buffer and copies, and skips rows a gate leaves unchanged. Name the rule's output an Image, document the derivation, and check every gate on all 11520 two-qubit Cliffords against dense gate matrices. Co-Authored-By: Claude Opus 5.5 (1M context) --- Cargo.lock | 1 + crates/ppvm-tableau-2/Cargo.toml | 1 + crates/ppvm-tableau-2/src/inverse_tableau.rs | 153 +++++++--- crates/ppvm-tableau-2/tests/dense_matrix.rs | 296 +++++++++++++++++++ 4 files changed, 411 insertions(+), 40 deletions(-) create mode 100644 crates/ppvm-tableau-2/tests/dense_matrix.rs diff --git a/Cargo.lock b/Cargo.lock index d3dc6990b..c62045ecf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2357,6 +2357,7 @@ dependencies = [ name = "ppvm-tableau-2" version = "0.3.0" dependencies = [ + "num-complex", "ppvm-pauli-kernels", "ppvm-pauli-word-2", "ppvm-phased-pauli-word-2", diff --git a/crates/ppvm-tableau-2/Cargo.toml b/crates/ppvm-tableau-2/Cargo.toml index fd3082d4e..37cdc2418 100644 --- a/crates/ppvm-tableau-2/Cargo.toml +++ b/crates/ppvm-tableau-2/Cargo.toml @@ -12,3 +12,4 @@ ppvm-traits-2 = { path = "../ppvm-traits-2" } [dev-dependencies] ppvm-pauli-word-2 = { path = "../ppvm-pauli-word-2" } ppvm-phased-pauli-word-2 = { path = "../ppvm-phased-pauli-word-2" } +num-complex = "0.4.6" diff --git a/crates/ppvm-tableau-2/src/inverse_tableau.rs b/crates/ppvm-tableau-2/src/inverse_tableau.rs index 468b6a4d4..545189c64 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau.rs @@ -12,7 +12,9 @@ //! by changing the alias. Where rows sit in the arena is decided only by //! `x_at` / `z_at`. -use ppvm_pauli_kernels::row_product; +use std::ops::Range; + +use ppvm_pauli_kernels::word_product; use ppvm_traits_2::ApplyGateRule; /// Storage word. Swap to benchmark other widths. @@ -112,46 +114,30 @@ impl InverseTableau { flip_bit(&mut self.signs[r / BITWORD_BITS], r % BITWORD_BITS); } - /// Replace rows `rows[g]` by the images of their generators. - /// - /// `images[g] = (bits, flip)` says that generator `g` maps to - /// `(−1)^flip · P(bits)`, with `bits` over the generators in `rows` order - /// (X then Z per qubit). The new row is that product of the old rows. - fn recombine(&mut self, rows: [usize; M], images: [([bool; M], bool); M]) { - let s = self.stride; - // Old rows, then new rows, each stored as [x | z]. - let mut buf = vec![ZERO; 2 * M * 2 * s]; - let (old, new) = buf.split_at_mut(M * 2 * s); - let mut old_negative = [false; M]; - for (j, &r) in rows.iter().enumerate() { - let row = self.row(r); - old[2 * j * s..(2 * j + 1) * s].copy_from_slice(row.x); - old[(2 * j + 1) * s..(2 * j + 2) * s].copy_from_slice(row.z); - old_negative[j] = row.negative; - } - let mut new_negative = [false; M]; - for (g, (bits, flip)) in images.iter().enumerate() { - let (nx, nz) = new[2 * g * s..(2 * g + 2) * s].split_at_mut(s); - // i per Y (P(bits) = i^#Y · X…Z… in generator order), −1 per flip. - let ys = bits.chunks(2).filter(|xz| xz[0] && xz[1]).count() as u32; - let mut e = ys + 2 * *flip as u32; - for j in (0..M).filter(|&j| bits[j]) { - let (ox, oz) = old[2 * j * s..(2 * j + 2) * s].split_at(s); - e += row_product(nx, nz, ox, oz) as u32 + 2 * old_negative[j] as u32; - } - debug_assert!(e.is_multiple_of(2), "recombined row is not Hermitian"); - new_negative[g] = e % 4 == 2; - } + /// Starting phase of each new row, as a power of `i`: one `i` per Y in the + /// image, `−1` for its flip and `−1` per negative old row it multiplies. + fn initial_phases(&self, rows: [usize; M], images: &[Image; M]) -> [u32; M] { + images.map(|image| { + let negatives = (0..M).filter(|&j| image.factors[j] && self.row(rows[j]).negative); + image.y_count() + 2 * image.flip as u32 + 2 * negatives.count() as u32 + }) + } + + /// Set each row's sign from its final phase (`i⁰ = +`, `i² = −`). + fn set_signs(&mut self, rows: [usize; M], phases: [u32; M]) { for (g, &r) in rows.iter().enumerate() { - let (x, z) = (self.x_at(r), self.z_at(r)); - self.words[x..x + s].copy_from_slice(&new[2 * g * s..(2 * g + 1) * s]); - self.words[z..z + s].copy_from_slice(&new[(2 * g + 1) * s..(2 * g + 2) * s]); - if self.row(r).negative != new_negative[g] { + debug_assert!(phases[g].is_multiple_of(2), "row is not Hermitian"); + if self.row(r).negative != (phases[g] % 4 == 2) { self.flip_sign(r); } } } + fn xz(&self, r: usize) -> [Range; 2] { + let (x, z) = (self.x_at(r), self.z_at(r)); + [x..x + self.stride, z..z + self.stride] + } + // Arrangement: blocks by row kind, ix-X, ix-Z, iz-X, iz-Z (#204's order). #[inline(always)] fn x_at(&self, r: usize) -> usize { @@ -166,20 +152,107 @@ impl InverseTableau { } } +/// Where a gate sends one of its generators: `G† P G = (−1)^flip · P(factors)`. +/// +/// The generators are X then Z for each gate qubit; `factors[j]` says whether +/// the Hermitian Pauli `P(factors)` contains generator `j`. +#[derive(Clone, Copy)] +struct Image { + factors: [bool; M], + flip: bool, +} + +impl Image { + fn new((factors, flip): ([bool; M], bool)) -> Self { + Self { factors, flip } + } + + /// Whether generator `g` maps to itself up to sign, so its row keeps its bits. + fn keeps_bits(&self, g: usize) -> bool { + (0..M).all(|j| self.factors[j] == (j == g)) + } + + /// Number of Y factors: an X and a Z on the same qubit. + fn y_count(&self) -> u32 { + self.factors.chunks(2).filter(|xz| xz[0] && xz[1]).count() as u32 + } +} + +/// Rewrite row `g` (X words `x[g]`, Z words `z[g]`) as `C⁻¹ (G† P_g G) C`. +/// +/// Write the image as `(−1)^flip · i^#Y` times its generators in order (a Y is +/// `i·X·Z`). Conjugating by `C` turns each generator into its old row, so the +/// new row is the product of those old rows times `(−1)^flip · i^#Y`. For +/// example `S` sends `X ↦ −Y = −i·X·Z`, so `ix ← −i · ix · iz`, and keeps `Z`, +/// so `iz` stays. +/// +/// The product is taken site by site, so the new words at index `w` depend +/// only on the old words at `w`: each index is loaded, multiplied in registers +/// and stored once. `phases[g]` (a power of `i`) comes in with the image's own +/// factors and signs and collects each word's product phase. +#[inline(always)] +fn recombine( + x: [&mut [BitWord]; M], + z: [&mut [BitWord]; M], + images: &[Image; M], + phases: &mut [u32; M], +) { + let len = x[0].len(); + assert!(x.iter().chain(&z).all(|s| s.len() == len)); + for w in 0..len { + let old_words: [[BitWord; 2]; M] = std::array::from_fn(|j| [x[j][w], z[j][w]]); + for (g, image) in images.iter().enumerate() { + if image.keeps_bits(g) { + continue; + } + let mut new_word = [ZERO; 2]; + for j in (0..M).filter(|&j| image.factors[j]) { + let [ox, oz] = old_words[j]; + let (product, k) = word_product(new_word[0], new_word[1], ox, oz); + new_word = product; + phases[g] = phases[g].wrapping_add(k); + } + x[g][w] = new_word[0]; + z[g][w] = new_word[1]; + } + } +} + /// Physical gates, `C ← G·C`: the new row for generator `P` is `C⁻¹ (G† P G) C`, /// so the rule evaluated on `P` names the old rows to multiply. impl ApplyGateRule for InverseTableau { type B = bool; fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - let images = [rule(true, false), rule(false, true)]; - self.recombine([q, self.n + q], images); + let images = [rule(true, false), rule(false, true)].map(Image::new); + let rows = [q, self.n + q]; + let mut phases = self.initial_phases(rows, &images); + let [[ix_x, ix_z], [iz_x, iz_z]] = rows.map(|r| self.xz(r)); + let [ix_x, ix_z, iz_x, iz_z] = self + .words + .get_disjoint_mut([ix_x, ix_z, iz_x, iz_z]) + .unwrap(); + recombine([ix_x, iz_x], [ix_z, iz_z], &images, &mut phases); + self.set_signs(rows, phases); } fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { - let unit = |j: usize| std::array::from_fn(|i| i == j); - let images = std::array::from_fn(|j| rule(unit(j))); - self.recombine([a, self.n + a, b, self.n + b], images); + let generator = |j: usize| std::array::from_fn(|i| i == j); + let images = std::array::from_fn(|j| Image::new(rule(generator(j)))); + let rows = [a, self.n + a, b, self.n + b]; + let mut phases = self.initial_phases(rows, &images); + let [[r0x, r0z], [r1x, r1z], [r2x, r2z], [r3x, r3z]] = rows.map(|r| self.xz(r)); + let [r0x, r0z, r1x, r1z, r2x, r2z, r3x, r3z] = self + .words + .get_disjoint_mut([r0x, r0z, r1x, r1z, r2x, r2z, r3x, r3z]) + .unwrap(); + recombine( + [r0x, r1x, r2x, r3x], + [r0z, r1z, r2z, r3z], + &images, + &mut phases, + ); + self.set_signs(rows, phases); } fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs new file mode 100644 index 000000000..fd76fc4e9 --- /dev/null +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -0,0 +1,296 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Physical gates against dense matrices: row `ix_q` must equal `C† X_q C` and +//! `iz_q` must equal `C† Z_q C`, with `C` the product of textbook gate matrices. +//! No Pauli code is shared with the tableau. + +use std::collections::{HashSet, VecDeque}; + +use num_complex::Complex64 as C; +use ppvm_tableau_2::InverseTableau; +use ppvm_traits_2::Clifford; + +/// A dense `2^n × 2^n` matrix; basis index bit `q` is qubit `q`. +#[derive(Clone)] +struct Mat { + d: usize, + a: Vec, +} + +impl Mat { + fn identity(n: usize) -> Self { + let d = 1 << n; + Self { + d, + a: (0..d * d) + .map(|k| C::from(f64::from(u8::from(k % (d + 1) == 0)))) + .collect(), + } + } + + fn mul(&self, o: &Mat) -> Mat { + let d = self.d; + let mut a = vec![C::default(); d * d]; + for i in 0..d { + for k in 0..d { + let s = self.a[i * d + k]; + for j in 0..d { + a[i * d + j] += s * o.a[k * d + j]; + } + } + } + Mat { d, a } + } + + fn adjoint(&self) -> Mat { + let d = self.d; + Mat { + d, + a: (0..d * d) + .map(|k| self.a[(k % d) * d + k / d].conj()) + .collect(), + } + } +} + +type U2 = [[C; 2]; 2]; + +fn c(re: f64, im: f64) -> C { + C::new(re, im) +} + +/// One-qubit gate `u` on qubit `q`. +fn single(u: U2, q: usize, n: usize) -> Mat { + let d = 1 << n; + let mut m = Mat { + d, + a: vec![C::default(); d * d], + }; + for i in 0..d { + for j in 0..d { + if (i ^ j) & !(1 << q) == 0 { + m.a[i * d + j] = u[(i >> q) & 1][(j >> q) & 1]; + } + } + } + m +} + +/// `u` on `target`, controlled on `control` being `|1⟩`. +fn controlled(u: U2, control: usize, target: usize, n: usize) -> Mat { + let d = 1 << n; + let mut m = Mat { + d, + a: vec![C::default(); d * d], + }; + for i in 0..d { + for j in 0..d { + if (i ^ j) & !(1 << target) == 0 { + m.a[i * d + j] = if (i >> control) & 1 == 1 { + u[(i >> target) & 1][(j >> target) & 1] + } else { + C::from(f64::from(u8::from(i == j))) + }; + } + } + } + m +} + +fn gate_matrices() -> [U2; 10] { + let (o, l, i) = (c(0.0, 0.0), c(1.0, 0.0), c(0.0, 1.0)); + let r = std::f64::consts::FRAC_1_SQRT_2; + let h = |a: C, b: C, cc: C, dd: C| [[a * 0.5, b * 0.5], [cc * 0.5, dd * 0.5]]; + [ + [[o, l], [l, o]], // X + [[o, -i], [i, o]], // Y + [[l, o], [o, -l]], // Z + [[c(r, 0.0), c(r, 0.0)], [c(r, 0.0), c(-r, 0.0)]], // H + [[l, o], [o, i]], // S + [[l, o], [o, -i]], // S† + h(c(1.0, 1.0), c(1.0, -1.0), c(1.0, -1.0), c(1.0, 1.0)), // √X + h(c(1.0, -1.0), c(1.0, 1.0), c(1.0, 1.0), c(1.0, -1.0)), // √X† + h(c(1.0, 1.0), c(-1.0, -1.0), c(1.0, 1.0), c(1.0, 1.0)), // √Y + h(c(1.0, -1.0), c(1.0, -1.0), c(-1.0, 1.0), c(1.0, -1.0)), // √Y† + ] +} + +#[derive(Clone, Copy, Debug)] +enum Gate { + One(usize, usize), + Two(usize, usize, usize), +} + +fn apply(t: &mut InverseTableau, gate: Gate) { + match gate { + Gate::One(g, q) => match g { + 0 => t.x(q), + 1 => t.y(q), + 2 => t.z(q), + 3 => t.h(q), + 4 => t.s(q), + 5 => t.s_dag(q), + 6 => t.sqrt_x(q), + 7 => t.sqrt_x_dag(q), + 8 => t.sqrt_y(q), + _ => t.sqrt_y_dag(q), + }, + Gate::Two(g, a, b) => match g { + 0 => t.cnot(a, b), + 1 => t.cz(a, b), + _ => t.cy(a, b), + }, + } +} + +fn matrix(gate: Gate, n: usize) -> Mat { + let u = gate_matrices(); + match gate { + Gate::One(g, q) => single(u[g], q, n), + Gate::Two(g, a, b) => controlled([u[0], u[2], u[1]][g], a, b, n), + } +} + +fn all_gates(n: usize) -> Vec { + let mut gates: Vec = (0..10) + .flat_map(|g| (0..n).map(move |q| Gate::One(g, q))) + .collect(); + for g in 0..3 { + for a in 0..n { + for b in (0..n).filter(|&b| b != a) { + gates.push(Gate::Two(g, a, b)); + } + } + } + gates +} + +/// All Hermitian Pauli strings on `n` qubits, indexed by `x | z << n`. +struct Paulis { + n: usize, + table: Vec, +} + +impl Paulis { + fn new(n: usize) -> Self { + let u = gate_matrices(); + let table = (0..1usize << (2 * n)) + .map(|code| { + (0..n).fold(Mat::identity(n), |m, q| { + match ((code >> q) & 1, (code >> (n + q)) & 1) { + (0, 0) => m, + (1, 0) => m.mul(&single(u[0], q, n)), + (0, _) => m.mul(&single(u[2], q, n)), + _ => m.mul(&single(u[1], q, n)), + } + }) + }) + .collect(); + Self { n, table } + } + + fn x(&self, q: usize) -> &Mat { + &self.table[1 << q] + } + + fn z(&self, q: usize) -> &Mat { + &self.table[1 << (self.n + q)] + } + + /// The signed Pauli string equal to `a`, as `(x | z << n, negative)`. + fn decompose(&self, a: &Mat) -> (usize, bool) { + // A Pauli string maps |0⟩ to a multiple of |x⟩, so column 0 gives x. + let x = (0..a.d) + .find(|&r| a.a[r * a.d].norm() > 0.5) + .expect("zero column"); + for z in 0..1usize << self.n { + let code = x | z << self.n; + let p = &self.table[code]; + let overlap = (0..a.d * a.d).map(|k| p.a[k].conj() * a.a[k]).sum::() / a.d as f64; + if (overlap - 1.0).norm() < 1e-9 { + return (code, false); + } + if (overlap + 1.0).norm() < 1e-9 { + return (code, true); + } + } + panic!("not a signed Pauli string"); + } +} + +/// Row `ix_q` (or `iz_q`) as `(x | z << n, negative)`. +fn row_code(t: &InverseTableau, q: usize, z: bool) -> (usize, bool) { + let row = if z { t.iz(q) } else { t.ix(q) }; + let n = t.num_qubits(); + let code = (0..n) + .map(|j| usize::from(row.x_bit(j)) << j | usize::from(row.z_bit(j)) << (n + j)) + .sum(); + (code, row.negative) +} + +fn assert_rows_match(t: &InverseTableau, cliff: &Mat, paulis: &Paulis, context: &str) { + let dag = cliff.adjoint(); + for q in 0..t.num_qubits() { + let x = paulis.decompose(&dag.mul(paulis.x(q)).mul(cliff)); + let z = paulis.decompose(&dag.mul(paulis.z(q)).mul(cliff)); + assert_eq!(row_code(t, q, false), x, "ix_{q} after {context}"); + assert_eq!(row_code(t, q, true), z, "iz_{q} after {context}"); + } +} + +/// The whole tableau packed into one integer (small `n` only). +fn key(t: &InverseTableau) -> u64 { + let n = t.num_qubits(); + (0..n) + .flat_map(|q| [row_code(t, q, false), row_code(t, q, true)]) + .fold(0, |k, (code, negative)| { + (k << (2 * n + 1)) | (code as u64) << 1 | u64::from(negative) + }) +} + +/// Every gate on every two-qubit Clifford. Visiting the whole group also +/// checks that the tableaux reached are exactly its 11520 elements. +#[test] +fn every_gate_on_every_two_qubit_clifford() { + let n = 2; + let gates = all_gates(n); + let paulis = Paulis::new(n); + let start = InverseTableau::new(n); + let mut seen = HashSet::from([key(&start)]); + let mut queue = VecDeque::from([(start, Mat::identity(n))]); + while let Some((t, cliff)) = queue.pop_front() { + for &gate in &gates { + let mut next = t.clone(); + apply(&mut next, gate); + let next_cliff = matrix(gate, n).mul(&cliff); + assert_rows_match(&next, &next_cliff, &paulis, &format!("{gate:?}")); + if seen.insert(key(&next)) { + queue.push_back((next, next_cliff)); + } + } + } + assert_eq!(seen.len(), 11520); +} + +/// Random three-qubit circuits, covering two-qubit gates on non-adjacent qubits. +#[test] +fn random_three_qubit_circuits() { + let n = 3; + let gates = all_gates(n); + let paulis = Paulis::new(n); + let mut seed = 0x2545_f491_4f6c_dd1du64; + for round in 0..50 { + let mut t = InverseTableau::new(n); + let mut cliff = Mat::identity(n); + for _ in 0..60 { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + let gate = gates[seed as usize % gates.len()]; + apply(&mut t, gate); + cliff = matrix(gate, n).mul(&cliff); + } + assert_rows_match(&t, &cliff, &paulis, &format!("round {round}")); + } +} From ae2cf03610b601e1eea4a71a419ee2a3c3745c53 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 13:19:43 +0100 Subject: [PATCH 03/60] refactor(ppvm-tableau-2): share the bit split and name the word product get_bit / flip_bit take a slice and a bit index, so the word/bit split lives in one place. The per-word product of an image's factor rows moves into Image::multiply, flattening recombine. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/src/inverse_tableau.rs | 61 +++++++++++--------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau.rs b/crates/ppvm-tableau-2/src/inverse_tableau.rs index 545189c64..08df7a0a7 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau.rs @@ -22,14 +22,15 @@ type BitWord = u64; const BITWORD_BITS: usize = BitWord::BITS as usize; const ZERO: BitWord = 0; +/// Bit `i` of a packed bit string. #[inline(always)] -fn get_bit(w: BitWord, i: usize) -> bool { - (w >> i) & 1 == 1 +fn get_bit(words: &[BitWord], i: usize) -> bool { + (words[i / BITWORD_BITS] >> (i % BITWORD_BITS)) & 1 == 1 } #[inline(always)] -fn flip_bit(w: &mut BitWord, i: usize) { - *w ^= 1 << i; +fn flip_bit(words: &mut [BitWord], i: usize) { + words[i / BITWORD_BITS] ^= 1 << (i % BITWORD_BITS); } /// The inverse tableau of an `n`-qubit Clifford. @@ -56,12 +57,12 @@ pub struct RowRef<'a> { impl RowRef<'_> { /// X bit of the row at qubit `q`. pub fn x_bit(&self, q: usize) -> bool { - get_bit(self.x[q / BITWORD_BITS], q % BITWORD_BITS) + get_bit(self.x, q) } /// Z bit of the row at qubit `q`. pub fn z_bit(&self, q: usize) -> bool { - get_bit(self.z[q / BITWORD_BITS], q % BITWORD_BITS) + get_bit(self.z, q) } } @@ -76,11 +77,9 @@ impl InverseTableau { signs: vec![ZERO; (2 * n).div_ceil(BITWORD_BITS)], }; for q in 0..n { - let (w, b) = (q / BITWORD_BITS, q % BITWORD_BITS); - let ix = t.x_at(q) + w; - let iz = t.z_at(n + q) + w; - flip_bit(&mut t.words[ix], b); - flip_bit(&mut t.words[iz], b); + let (ix, iz) = (t.x_at(q), t.z_at(n + q)); + flip_bit(&mut t.words[ix..], q); + flip_bit(&mut t.words[iz..], q); } t } @@ -106,12 +105,12 @@ impl InverseTableau { RowRef { x: &self.words[x..x + self.stride], z: &self.words[z..z + self.stride], - negative: get_bit(self.signs[r / BITWORD_BITS], r % BITWORD_BITS), + negative: get_bit(&self.signs, r), } } fn flip_sign(&mut self, r: usize) { - flip_bit(&mut self.signs[r / BITWORD_BITS], r % BITWORD_BITS); + flip_bit(&mut self.signs, r); } /// Starting phase of each new row, as a power of `i`: one `i` per Y in the @@ -133,6 +132,7 @@ impl InverseTableau { } } + /// Positions of row `r`'s X words and Z words in the arena. fn xz(&self, r: usize) -> [Range; 2] { let (x, z) = (self.x_at(r), self.z_at(r)); [x..x + self.stride, z..z + self.stride] @@ -176,6 +176,19 @@ impl Image { fn y_count(&self) -> u32 { self.factors.chunks(2).filter(|xz| xz[0] && xz[1]).count() as u32 } + + /// One word of the product of the old rows in `factors`, and its phase. + #[inline(always)] + fn multiply(&self, old_words: &[[BitWord; 2]; M]) -> ([BitWord; 2], u32) { + let mut word = [ZERO; 2]; + let mut phase = 0u32; + for j in (0..M).filter(|&j| self.factors[j]) { + let (product, k) = word_product(word[0], word[1], old_words[j][0], old_words[j][1]); + word = product; + phase = phase.wrapping_add(k); + } + (word, phase) + } } /// Rewrite row `g` (X words `x[g]`, Z words `z[g]`) as `C⁻¹ (G† P_g G) C`. @@ -201,19 +214,15 @@ fn recombine( assert!(x.iter().chain(&z).all(|s| s.len() == len)); for w in 0..len { let old_words: [[BitWord; 2]; M] = std::array::from_fn(|j| [x[j][w], z[j][w]]); - for (g, image) in images.iter().enumerate() { - if image.keeps_bits(g) { - continue; - } - let mut new_word = [ZERO; 2]; - for j in (0..M).filter(|&j| image.factors[j]) { - let [ox, oz] = old_words[j]; - let (product, k) = word_product(new_word[0], new_word[1], ox, oz); - new_word = product; - phases[g] = phases[g].wrapping_add(k); - } - x[g][w] = new_word[0]; - z[g][w] = new_word[1]; + for (g, image) in images + .iter() + .enumerate() + .filter(|(g, image)| !image.keeps_bits(*g)) + { + let ([new_x, new_z], phase) = image.multiply(&old_words); + x[g][w] = new_x; + z[g][w] = new_z; + phases[g] = phases[g].wrapping_add(phase); } } } From 6317eccc1d9ae37a5236b52bfb1b0f26f3eeac70 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 13:40:56 +0100 Subject: [PATCH 04/60] feat(ppvm-tableau-2): add changes of basis on virtual qubits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit change_basis1 / change_basis2 apply C ← C·V: every row R becomes V† R V, by running the kernel rule on each row's bits in the gate's columns (the strided layout). Used later by localization, measurement collapse and reset. Checked against dense matrices on all two-qubit Cliffords, commutation with physical gates, and the commutation invariant. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/src/inverse_tableau.rs | 139 ++++++++++++++++++- crates/ppvm-tableau-2/tests/dense_matrix.rs | 51 ++++++- 2 files changed, 181 insertions(+), 9 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau.rs b/crates/ppvm-tableau-2/src/inverse_tableau.rs index 08df7a0a7..1907a9821 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau.rs @@ -33,6 +33,18 @@ fn flip_bit(words: &mut [BitWord], i: usize) { words[i / BITWORD_BITS] ^= 1 << (i % BITWORD_BITS); } +/// Word index and mask of bit `i`. +#[inline(always)] +fn bit_position(i: usize) -> (usize, BitWord) { + (i / BITWORD_BITS, 1 << (i % BITWORD_BITS)) +} + +/// All ones if `b`, else zero (for branch-free masked updates). +#[inline(always)] +fn mask_if(b: bool) -> BitWord { + ZERO.wrapping_sub(BitWord::from(b)) +} + /// The inverse tableau of an `n`-qubit Clifford. #[derive(Clone, Debug, PartialEq, Eq)] pub struct InverseTableau { @@ -138,17 +150,84 @@ impl InverseTableau { [x..x + self.stride, z..z + self.stride] } + /// Change the basis of virtual qubit `v` by the one-qubit Clifford `V` + /// whose Heisenberg rule is `rule`: `C ← C·V`. + /// + /// Updates the tableau only. The state `C (|φ⟩ ⊗ |0…0⟩)` stays the same if + /// the caller also applies `V†` to `φ`, or if `V` acts trivially on it (e.g. + /// `v` dormant and `V` fixes `|0⟩`). + /// + /// Every row `R` becomes `V† R V`: the rule runs on each row's bits in + /// column `v`. Strided: one word per row. + /// + /// Used by: dormant-pivot collapse (`H` on the pivot, `X` to fix the + /// outcome), active collapse (the final rotation onto `Z_h`), reset. + pub fn change_basis1(&mut self, v: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + debug_assert!(v < self.n); + let (w, m) = bit_position(v); + for (r, (x, z)) in self.row_offsets().enumerate() { + let (x, z) = (x + w, z + w); + let old = [self.words[x] & m != ZERO, self.words[z] & m != ZERO]; + let (new, flip) = rule(old[0], old[1]); + self.words[x] ^= m & mask_if(new[0] != old[0]); + self.words[z] ^= m & mask_if(new[1] != old[1]); + let (sw, sm) = bit_position(r); + self.signs[sw] ^= sm & mask_if(flip); + } + } + + /// Change the basis of virtual qubits `a`, `b` by the two-qubit Clifford + /// `V` whose Heisenberg rule is `rule` (`a` is the control for controlled + /// gates): `C ← C·V`. Same contract as [`change_basis1`](Self::change_basis1). + /// + /// Used by: T-gate localization (`CX(p, v)` collects the X bits of `O` onto + /// the pivot `p`), dormant-pivot collapse (`CX(p, v)`, `CZ(p, c)`), active + /// collapse (the `CX` / `CZ` that reduce `P` to one qubit). + pub fn change_basis2( + &mut self, + a: usize, + b: usize, + rule: impl Fn([bool; 4]) -> ([bool; 4], bool), + ) { + debug_assert!(a < self.n && b < self.n && a != b); + let [(wa, ma), (wb, mb)] = [bit_position(a), bit_position(b)]; + for (r, (x, z)) in self.row_offsets().enumerate() { + let words = [x + wa, z + wa, x + wb, z + wb]; + let masks = [ma, ma, mb, mb]; + let old: [bool; 4] = std::array::from_fn(|j| self.words[words[j]] & masks[j] != ZERO); + let (new, flip) = rule(old); + for j in 0..4 { + self.words[words[j]] ^= masks[j] & mask_if(new[j] != old[j]); + } + let (sw, sm) = bit_position(r); + self.signs[sw] ^= sm & mask_if(flip); + } + } + // Arrangement: blocks by row kind, ix-X, ix-Z, iz-X, iz-Z (#204's order). + // Row r is row i = r % n of kind r / n (0 for ix, 1 for iz). + #[inline(always)] + fn offsets(n: usize, stride: usize, kind: usize, i: usize) -> (usize, usize) { + ( + ((2 * kind) * n + i) * stride, + ((2 * kind + 1) * n + i) * stride, + ) + } + #[inline(always)] fn x_at(&self, r: usize) -> usize { - let (kind, i) = (r / self.n, r % self.n); - ((2 * kind) * self.n + i) * self.stride + Self::offsets(self.n, self.stride, r / self.n, r % self.n).0 } #[inline(always)] fn z_at(&self, r: usize) -> usize { - let (kind, i) = (r / self.n, r % self.n); - ((2 * kind + 1) * self.n + i) * self.stride + Self::offsets(self.n, self.stride, r / self.n, r % self.n).1 + } + + /// `(x_at(r), z_at(r))` for every row in order, without a division per row. + fn row_offsets(&self) -> impl Iterator + use<> { + let (n, stride) = (self.n, self.stride); + (0..2).flat_map(move |kind| (0..n).map(move |i| Self::offsets(n, stride, kind, i))) } } @@ -436,4 +515,56 @@ mod tests { } } } + + type Rule1 = fn(bool, bool) -> ([bool; 2], bool); + type Rule2 = fn([bool; 4]) -> ([bool; 4], bool); + + /// `gate` as a change of basis on virtual qubits, `C ← C·V`. + fn change_basis(t: &mut InverseTableau, gate: Gate) { + use ppvm_pauli_kernels::heisenberg as rules; + let one: [Rule1; 10] = [ + rules::x, + rules::y, + rules::z, + rules::h, + rules::s, + rules::s_dag, + rules::sqrt_x, + rules::sqrt_x_dag, + rules::sqrt_y, + rules::sqrt_y_dag, + ]; + let two: [Rule2; 3] = [rules::cnot, rules::cz, rules::cy]; + match gate { + Gate::One(g, v) => t.change_basis1(v, one[g]), + Gate::Two(g, a, b) => t.change_basis2(a, b, two[g]), + } + } + + #[test] + fn change_basis_preserves_commutation() { + for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { + let mut t = InverseTableau::new(n); + for gate in random_circuit(n, 100, 0xba5e + i as u64) { + change_basis(&mut t, gate); + assert_canonical_commutation(&t); + } + } + } + + /// `(G·C)·V = G·(C·V)`: the order of physical gates relative to changes of + /// basis does not matter. + #[test] + fn physical_gates_commute_with_changes_of_basis() { + for (i, n) in SIZES.into_iter().filter(|&n| n > 1).enumerate() { + let physical = random_circuit(n, 100, 0xc0de + i as u64); + let basis = random_circuit(n, 100, 0xfeed + i as u64); + let (mut first, mut second) = (InverseTableau::new(n), InverseTableau::new(n)); + physical.iter().for_each(|&g| apply(&mut first, g)); + basis.iter().for_each(|&g| change_basis(&mut first, g)); + basis.iter().for_each(|&g| change_basis(&mut second, g)); + physical.iter().for_each(|&g| apply(&mut second, g)); + assert_eq!(first, second, "n = {n}"); + } + } } diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs index fd76fc4e9..d1bce5e0f 100644 --- a/crates/ppvm-tableau-2/tests/dense_matrix.rs +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2026 The PPVM Authors // SPDX-License-Identifier: Apache-2.0 -//! Physical gates against dense matrices: row `ix_q` must equal `C† X_q C` and +//! Gates against dense matrices: row `ix_q` must equal `C† X_q C` and //! `iz_q` must equal `C† Z_q C`, with `C` the product of textbook gate matrices. //! No Pauli code is shared with the tableau. @@ -144,6 +144,31 @@ fn apply(t: &mut InverseTableau, gate: Gate) { } } +type Rule1 = fn(bool, bool) -> ([bool; 2], bool); +type Rule2 = fn([bool; 4]) -> ([bool; 4], bool); + +/// `gate` as a change of basis on virtual qubits, `C ← C·V`. +fn change_basis(t: &mut InverseTableau, gate: Gate) { + use ppvm_pauli_kernels::heisenberg as rules; + let one: [Rule1; 10] = [ + rules::x, + rules::y, + rules::z, + rules::h, + rules::s, + rules::s_dag, + rules::sqrt_x, + rules::sqrt_x_dag, + rules::sqrt_y, + rules::sqrt_y_dag, + ]; + let two: [Rule2; 3] = [rules::cnot, rules::cz, rules::cy]; + match gate { + Gate::One(g, v) => t.change_basis1(v, one[g]), + Gate::Two(g, a, b) => t.change_basis2(a, b, two[g]), + } +} + fn matrix(gate: Gate, n: usize) -> Mat { let u = gate_matrices(); match gate { @@ -249,7 +274,8 @@ fn key(t: &InverseTableau) -> u64 { }) } -/// Every gate on every two-qubit Clifford. Visiting the whole group also +/// Every gate, physical and as a change of basis, on every two-qubit +/// Clifford. Visiting the whole group also /// checks that the tableaux reached are exactly its 11520 elements. #[test] fn every_gate_on_every_two_qubit_clifford() { @@ -265,6 +291,15 @@ fn every_gate_on_every_two_qubit_clifford() { apply(&mut next, gate); let next_cliff = matrix(gate, n).mul(&cliff); assert_rows_match(&next, &next_cliff, &paulis, &format!("{gate:?}")); + let mut rebased = t.clone(); + change_basis(&mut rebased, gate); + let rebased_cliff = cliff.mul(&matrix(gate, n)); + assert_rows_match( + &rebased, + &rebased_cliff, + &paulis, + &format!("basis {gate:?}"), + ); if seen.insert(key(&next)) { queue.push_back((next, next_cliff)); } @@ -273,7 +308,8 @@ fn every_gate_on_every_two_qubit_clifford() { assert_eq!(seen.len(), 11520); } -/// Random three-qubit circuits, covering two-qubit gates on non-adjacent qubits. +/// Random three-qubit circuits mixing physical gates and changes of basis, +/// covering two-qubit gates on non-adjacent qubits. #[test] fn random_three_qubit_circuits() { let n = 3; @@ -288,8 +324,13 @@ fn random_three_qubit_circuits() { seed ^= seed >> 7; seed ^= seed << 17; let gate = gates[seed as usize % gates.len()]; - apply(&mut t, gate); - cliff = matrix(gate, n).mul(&cliff); + if seed >> 63 == 0 { + apply(&mut t, gate); + cliff = matrix(gate, n).mul(&cliff); + } else { + change_basis(&mut t, gate); + cliff = cliff.mul(&matrix(gate, n)); + } } assert_rows_match(&t, &cliff, &paulis, &format!("round {round}")); } From 10b0a52cd3a285e3ec935b997bf411ddc9a7fd73 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 14:00:30 +0100 Subject: [PATCH 05/60] refactor(ppvm-tableau-2): split the inverse tableau into a module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit data (storage, row reads, placement of rows), bitword (the storage word), physical (C ← G·C) and virtual_side (C ← C·V), with shared test helpers in a cfg(test) module. mod.rs and lib.rs only declare and re-export. No behaviour change. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/src/inverse_tableau.rs | 570 ------------------ .../src/inverse_tableau/bitword.rs | 34 ++ .../src/inverse_tableau/data.rs | 142 +++++ .../ppvm-tableau-2/src/inverse_tableau/mod.rs | 25 + .../src/inverse_tableau/physical.rs | 232 +++++++ .../src/inverse_tableau/test_support.rs | 107 ++++ .../src/inverse_tableau/virtual_side.rs | 96 +++ 7 files changed, 636 insertions(+), 570 deletions(-) delete mode 100644 crates/ppvm-tableau-2/src/inverse_tableau.rs create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/data.rs create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/mod.rs create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/physical.rs create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/test_support.rs create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/virtual_side.rs diff --git a/crates/ppvm-tableau-2/src/inverse_tableau.rs b/crates/ppvm-tableau-2/src/inverse_tableau.rs deleted file mode 100644 index 1907a9821..000000000 --- a/crates/ppvm-tableau-2/src/inverse_tableau.rs +++ /dev/null @@ -1,570 +0,0 @@ -// SPDX-FileCopyrightText: 2026 The PPVM Authors -// SPDX-License-Identifier: Apache-2.0 - -//! The inverse stabilizer tableau `C⁻¹` of a Clifford `C`. -//! -//! Row `ix_q` is `C⁻¹ X_q C` and row `iz_q` is `C⁻¹ Z_q C`, each stored as -//! packed X and Z bits plus one sign bit (rows are Hermitian, so their phase -//! is ±1). Rows are indexed `ix_q = q`, `iz_q = n + q`. -//! -//! Words are touched only through [`BitWord`], [`BITWORD_BITS`], [`ZERO`], -//! [`get_bit`], [`flip_bit`] and `Bits`, so a wider storage word can be tried -//! by changing the alias. Where rows sit in the arena is decided only by -//! `x_at` / `z_at`. - -use std::ops::Range; - -use ppvm_pauli_kernels::word_product; -use ppvm_traits_2::ApplyGateRule; - -/// Storage word. Swap to benchmark other widths. -type BitWord = u64; -const BITWORD_BITS: usize = BitWord::BITS as usize; -const ZERO: BitWord = 0; - -/// Bit `i` of a packed bit string. -#[inline(always)] -fn get_bit(words: &[BitWord], i: usize) -> bool { - (words[i / BITWORD_BITS] >> (i % BITWORD_BITS)) & 1 == 1 -} - -#[inline(always)] -fn flip_bit(words: &mut [BitWord], i: usize) { - words[i / BITWORD_BITS] ^= 1 << (i % BITWORD_BITS); -} - -/// Word index and mask of bit `i`. -#[inline(always)] -fn bit_position(i: usize) -> (usize, BitWord) { - (i / BITWORD_BITS, 1 << (i % BITWORD_BITS)) -} - -/// All ones if `b`, else zero (for branch-free masked updates). -#[inline(always)] -fn mask_if(b: bool) -> BitWord { - ZERO.wrapping_sub(BitWord::from(b)) -} - -/// The inverse tableau of an `n`-qubit Clifford. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct InverseTableau { - n: usize, - /// Words per row and plane: `n.div_ceil(BITWORD_BITS)`. - stride: usize, - /// X and Z words of all `2n` rows, arranged by `x_at` / `z_at`. - words: Vec, - /// Bit `r` is the sign of row `r` (set = −). - signs: Vec, -} - -// NOTE: we could impl Word for RowRef to make it a true PauliWord if we ever need it -/// A borrowed row: its X words, Z words and sign. -#[derive(Clone, Copy, Debug)] -pub struct RowRef<'a> { - pub x: &'a [BitWord], - pub z: &'a [BitWord], - pub negative: bool, -} - -impl RowRef<'_> { - /// X bit of the row at qubit `q`. - pub fn x_bit(&self, q: usize) -> bool { - get_bit(self.x, q) - } - - /// Z bit of the row at qubit `q`. - pub fn z_bit(&self, q: usize) -> bool { - get_bit(self.z, q) - } -} - -impl InverseTableau { - /// The identity Clifford, i.e. the state `|0…0⟩`: `ix_q = X_q`, `iz_q = Z_q`. - pub fn new(n: usize) -> Self { - let stride = n.div_ceil(BITWORD_BITS); - let mut t = Self { - n, - stride, - words: vec![ZERO; 4 * n * stride], - signs: vec![ZERO; (2 * n).div_ceil(BITWORD_BITS)], - }; - for q in 0..n { - let (ix, iz) = (t.x_at(q), t.z_at(n + q)); - flip_bit(&mut t.words[ix..], q); - flip_bit(&mut t.words[iz..], q); - } - t - } - - pub fn num_qubits(&self) -> usize { - self.n - } - - /// Row `ix_q = C⁻¹ X_q C`. - pub fn ix(&self, q: usize) -> RowRef<'_> { - debug_assert!(q < self.n); - self.row(q) - } - - /// Row `iz_q = C⁻¹ Z_q C`. - pub fn iz(&self, q: usize) -> RowRef<'_> { - debug_assert!(q < self.n); - self.row(self.n + q) - } - - fn row(&self, r: usize) -> RowRef<'_> { - let (x, z) = (self.x_at(r), self.z_at(r)); - RowRef { - x: &self.words[x..x + self.stride], - z: &self.words[z..z + self.stride], - negative: get_bit(&self.signs, r), - } - } - - fn flip_sign(&mut self, r: usize) { - flip_bit(&mut self.signs, r); - } - - /// Starting phase of each new row, as a power of `i`: one `i` per Y in the - /// image, `−1` for its flip and `−1` per negative old row it multiplies. - fn initial_phases(&self, rows: [usize; M], images: &[Image; M]) -> [u32; M] { - images.map(|image| { - let negatives = (0..M).filter(|&j| image.factors[j] && self.row(rows[j]).negative); - image.y_count() + 2 * image.flip as u32 + 2 * negatives.count() as u32 - }) - } - - /// Set each row's sign from its final phase (`i⁰ = +`, `i² = −`). - fn set_signs(&mut self, rows: [usize; M], phases: [u32; M]) { - for (g, &r) in rows.iter().enumerate() { - debug_assert!(phases[g].is_multiple_of(2), "row is not Hermitian"); - if self.row(r).negative != (phases[g] % 4 == 2) { - self.flip_sign(r); - } - } - } - - /// Positions of row `r`'s X words and Z words in the arena. - fn xz(&self, r: usize) -> [Range; 2] { - let (x, z) = (self.x_at(r), self.z_at(r)); - [x..x + self.stride, z..z + self.stride] - } - - /// Change the basis of virtual qubit `v` by the one-qubit Clifford `V` - /// whose Heisenberg rule is `rule`: `C ← C·V`. - /// - /// Updates the tableau only. The state `C (|φ⟩ ⊗ |0…0⟩)` stays the same if - /// the caller also applies `V†` to `φ`, or if `V` acts trivially on it (e.g. - /// `v` dormant and `V` fixes `|0⟩`). - /// - /// Every row `R` becomes `V† R V`: the rule runs on each row's bits in - /// column `v`. Strided: one word per row. - /// - /// Used by: dormant-pivot collapse (`H` on the pivot, `X` to fix the - /// outcome), active collapse (the final rotation onto `Z_h`), reset. - pub fn change_basis1(&mut self, v: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - debug_assert!(v < self.n); - let (w, m) = bit_position(v); - for (r, (x, z)) in self.row_offsets().enumerate() { - let (x, z) = (x + w, z + w); - let old = [self.words[x] & m != ZERO, self.words[z] & m != ZERO]; - let (new, flip) = rule(old[0], old[1]); - self.words[x] ^= m & mask_if(new[0] != old[0]); - self.words[z] ^= m & mask_if(new[1] != old[1]); - let (sw, sm) = bit_position(r); - self.signs[sw] ^= sm & mask_if(flip); - } - } - - /// Change the basis of virtual qubits `a`, `b` by the two-qubit Clifford - /// `V` whose Heisenberg rule is `rule` (`a` is the control for controlled - /// gates): `C ← C·V`. Same contract as [`change_basis1`](Self::change_basis1). - /// - /// Used by: T-gate localization (`CX(p, v)` collects the X bits of `O` onto - /// the pivot `p`), dormant-pivot collapse (`CX(p, v)`, `CZ(p, c)`), active - /// collapse (the `CX` / `CZ` that reduce `P` to one qubit). - pub fn change_basis2( - &mut self, - a: usize, - b: usize, - rule: impl Fn([bool; 4]) -> ([bool; 4], bool), - ) { - debug_assert!(a < self.n && b < self.n && a != b); - let [(wa, ma), (wb, mb)] = [bit_position(a), bit_position(b)]; - for (r, (x, z)) in self.row_offsets().enumerate() { - let words = [x + wa, z + wa, x + wb, z + wb]; - let masks = [ma, ma, mb, mb]; - let old: [bool; 4] = std::array::from_fn(|j| self.words[words[j]] & masks[j] != ZERO); - let (new, flip) = rule(old); - for j in 0..4 { - self.words[words[j]] ^= masks[j] & mask_if(new[j] != old[j]); - } - let (sw, sm) = bit_position(r); - self.signs[sw] ^= sm & mask_if(flip); - } - } - - // Arrangement: blocks by row kind, ix-X, ix-Z, iz-X, iz-Z (#204's order). - // Row r is row i = r % n of kind r / n (0 for ix, 1 for iz). - #[inline(always)] - fn offsets(n: usize, stride: usize, kind: usize, i: usize) -> (usize, usize) { - ( - ((2 * kind) * n + i) * stride, - ((2 * kind + 1) * n + i) * stride, - ) - } - - #[inline(always)] - fn x_at(&self, r: usize) -> usize { - Self::offsets(self.n, self.stride, r / self.n, r % self.n).0 - } - - #[inline(always)] - fn z_at(&self, r: usize) -> usize { - Self::offsets(self.n, self.stride, r / self.n, r % self.n).1 - } - - /// `(x_at(r), z_at(r))` for every row in order, without a division per row. - fn row_offsets(&self) -> impl Iterator + use<> { - let (n, stride) = (self.n, self.stride); - (0..2).flat_map(move |kind| (0..n).map(move |i| Self::offsets(n, stride, kind, i))) - } -} - -/// Where a gate sends one of its generators: `G† P G = (−1)^flip · P(factors)`. -/// -/// The generators are X then Z for each gate qubit; `factors[j]` says whether -/// the Hermitian Pauli `P(factors)` contains generator `j`. -#[derive(Clone, Copy)] -struct Image { - factors: [bool; M], - flip: bool, -} - -impl Image { - fn new((factors, flip): ([bool; M], bool)) -> Self { - Self { factors, flip } - } - - /// Whether generator `g` maps to itself up to sign, so its row keeps its bits. - fn keeps_bits(&self, g: usize) -> bool { - (0..M).all(|j| self.factors[j] == (j == g)) - } - - /// Number of Y factors: an X and a Z on the same qubit. - fn y_count(&self) -> u32 { - self.factors.chunks(2).filter(|xz| xz[0] && xz[1]).count() as u32 - } - - /// One word of the product of the old rows in `factors`, and its phase. - #[inline(always)] - fn multiply(&self, old_words: &[[BitWord; 2]; M]) -> ([BitWord; 2], u32) { - let mut word = [ZERO; 2]; - let mut phase = 0u32; - for j in (0..M).filter(|&j| self.factors[j]) { - let (product, k) = word_product(word[0], word[1], old_words[j][0], old_words[j][1]); - word = product; - phase = phase.wrapping_add(k); - } - (word, phase) - } -} - -/// Rewrite row `g` (X words `x[g]`, Z words `z[g]`) as `C⁻¹ (G† P_g G) C`. -/// -/// Write the image as `(−1)^flip · i^#Y` times its generators in order (a Y is -/// `i·X·Z`). Conjugating by `C` turns each generator into its old row, so the -/// new row is the product of those old rows times `(−1)^flip · i^#Y`. For -/// example `S` sends `X ↦ −Y = −i·X·Z`, so `ix ← −i · ix · iz`, and keeps `Z`, -/// so `iz` stays. -/// -/// The product is taken site by site, so the new words at index `w` depend -/// only on the old words at `w`: each index is loaded, multiplied in registers -/// and stored once. `phases[g]` (a power of `i`) comes in with the image's own -/// factors and signs and collects each word's product phase. -#[inline(always)] -fn recombine( - x: [&mut [BitWord]; M], - z: [&mut [BitWord]; M], - images: &[Image; M], - phases: &mut [u32; M], -) { - let len = x[0].len(); - assert!(x.iter().chain(&z).all(|s| s.len() == len)); - for w in 0..len { - let old_words: [[BitWord; 2]; M] = std::array::from_fn(|j| [x[j][w], z[j][w]]); - for (g, image) in images - .iter() - .enumerate() - .filter(|(g, image)| !image.keeps_bits(*g)) - { - let ([new_x, new_z], phase) = image.multiply(&old_words); - x[g][w] = new_x; - z[g][w] = new_z; - phases[g] = phases[g].wrapping_add(phase); - } - } -} - -/// Physical gates, `C ← G·C`: the new row for generator `P` is `C⁻¹ (G† P G) C`, -/// so the rule evaluated on `P` names the old rows to multiply. -impl ApplyGateRule for InverseTableau { - type B = bool; - - fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - let images = [rule(true, false), rule(false, true)].map(Image::new); - let rows = [q, self.n + q]; - let mut phases = self.initial_phases(rows, &images); - let [[ix_x, ix_z], [iz_x, iz_z]] = rows.map(|r| self.xz(r)); - let [ix_x, ix_z, iz_x, iz_z] = self - .words - .get_disjoint_mut([ix_x, ix_z, iz_x, iz_z]) - .unwrap(); - recombine([ix_x, iz_x], [ix_z, iz_z], &images, &mut phases); - self.set_signs(rows, phases); - } - - fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { - let generator = |j: usize| std::array::from_fn(|i| i == j); - let images = std::array::from_fn(|j| Image::new(rule(generator(j)))); - let rows = [a, self.n + a, b, self.n + b]; - let mut phases = self.initial_phases(rows, &images); - let [[r0x, r0z], [r1x, r1z], [r2x, r2z], [r3x, r3z]] = rows.map(|r| self.xz(r)); - let [r0x, r0z, r1x, r1z, r2x, r2z, r3x, r3z] = self - .words - .get_disjoint_mut([r0x, r0z, r1x, r1z, r2x, r2z, r3x, r3z]) - .unwrap(); - recombine( - [r0x, r1x, r2x, r3x], - [r0z, r1z, r2z, r3z], - &images, - &mut phases, - ); - self.set_signs(rows, phases); - } - - fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - let ((x_bits, x_flip), (z_bits, z_flip)) = (rule(true, false), rule(false, true)); - debug_assert!(x_bits == [true, false] && z_bits == [false, true]); - if x_flip { - self.flip_sign(q); - } - if z_flip { - self.flip_sign(self.n + q); - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use ppvm_pauli_kernels::Bits; - use ppvm_pauli_word_2::PauliWord; - use ppvm_phased_pauli_word_2::Phased; - use ppvm_traits_2::{Clifford, PauliBits, Phase}; - - const SIZES: [usize; 7] = [1, 2, 7, 63, 64, 65, 130]; - - /// Whether two rows anticommute: odd symplectic product. - fn anticommute(a: RowRef, b: RowRef) -> bool { - let mut parity = 0; - for i in 0..a.x.len() { - parity ^= ((a.x[i] & b.z[i]) ^ (a.z[i] & b.x[i])).popcount() & 1; - } - parity == 1 - } - - #[test] - fn identity_rows_are_single_paulis() { - for n in SIZES { - let t = InverseTableau::new(n); - for q in 0..n { - for j in 0..n { - assert_eq!(t.ix(q).x_bit(j), j == q); - assert!(!t.ix(q).z_bit(j)); - assert!(!t.iz(q).x_bit(j)); - assert_eq!(t.iz(q).z_bit(j), j == q); - } - assert!(!t.ix(q).negative && !t.iz(q).negative); - } - } - } - - /// Conjugation preserves commutation: `ix_a`, `iz_b` anticommute iff `a == b`. - fn assert_canonical_commutation(t: &InverseTableau) { - let n = t.num_qubits(); - for a in 0..n { - for b in 0..n { - assert_eq!(anticommute(t.ix(a), t.iz(b)), a == b); - assert!(!anticommute(t.ix(a), t.ix(b))); - assert!(!anticommute(t.iz(a), t.iz(b))); - } - } - } - - #[test] - fn identity_has_canonical_commutation() { - for n in SIZES { - assert_canonical_commutation(&InverseTableau::new(n)); - } - } - - #[derive(Clone, Copy, Debug)] - enum Gate { - One(usize, usize), - Two(usize, usize, usize), - } - - fn apply(t: &mut T, gate: Gate) { - match gate { - Gate::One(g, q) => match g { - 0 => t.x(q), - 1 => t.y(q), - 2 => t.z(q), - 3 => t.h(q), - 4 => t.s(q), - 5 => t.s_dag(q), - 6 => t.sqrt_x(q), - 7 => t.sqrt_x_dag(q), - 8 => t.sqrt_y(q), - _ => t.sqrt_y_dag(q), - }, - Gate::Two(g, a, b) => match g { - 0 => t.cnot(a, b), - 1 => t.cz(a, b), - _ => t.cy(a, b), - }, - } - } - - /// A random circuit from a xorshift seed. - fn random_circuit(n: usize, len: usize, mut seed: u64) -> Vec { - let mut next = move || { - seed ^= seed << 13; - seed ^= seed >> 7; - seed ^= seed << 17; - seed as usize - }; - (0..len) - .map(|_| { - if n < 2 || next() % 3 == 0 { - Gate::One(next() % 10, next() % n) - } else { - let a = next() % n; - let b = (a + 1 + next() % (n - 1)) % n; - Gate::Two(next() % 3, a, b) - } - }) - .collect() - } - - type Reference = Phased>; - - /// `C⁻¹ P C` for `C = G_m ⋯ G_1`: propagate `P` through the gates in reverse. - fn heisenberg(n: usize, q: usize, z: bool, circuit: &[Gate]) -> Reference { - let mut word = PauliWord::new(n); - if z { - word.set_z_bit(q, true); - } else { - word.set_x_bit(q, true); - } - let mut p = Phased::new(word); - for &gate in circuit.iter().rev() { - apply(&mut p, gate); - } - p - } - - fn assert_row_eq(row: RowRef, expected: &Reference, n: usize) { - for j in 0..n { - assert_eq!(row.x_bit(j), expected.word().x_bit(j), "x bit {j}"); - assert_eq!(row.z_bit(j), expected.word().z_bit(j), "z bit {j}"); - } - let negative = match expected.phase() { - Phase::Pos1 => false, - Phase::Neg1 => true, - phase => panic!("non-Hermitian reference phase {phase:?}"), - }; - assert_eq!(row.negative, negative, "sign"); - } - - /// Rows must equal `C⁻¹ X_q C` / `C⁻¹ Z_q C` computed by Heisenberg - /// propagation of single Pauli words. - #[test] - fn physical_gates_match_heisenberg_propagation() { - for (i, n) in SIZES.into_iter().enumerate() { - let circuit = random_circuit(n, 200, 0x9e37_79b9 + i as u64); - let mut t = InverseTableau::new(n); - for &gate in &circuit { - apply(&mut t, gate); - } - for q in 0..n { - assert_row_eq(t.ix(q), &heisenberg(n, q, false, &circuit), n); - assert_row_eq(t.iz(q), &heisenberg(n, q, true, &circuit), n); - } - } - } - - #[test] - fn physical_gates_preserve_commutation() { - for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { - let mut t = InverseTableau::new(n); - for gate in random_circuit(n, 100, 0x5eed + i as u64) { - apply(&mut t, gate); - assert_canonical_commutation(&t); - } - } - } - - type Rule1 = fn(bool, bool) -> ([bool; 2], bool); - type Rule2 = fn([bool; 4]) -> ([bool; 4], bool); - - /// `gate` as a change of basis on virtual qubits, `C ← C·V`. - fn change_basis(t: &mut InverseTableau, gate: Gate) { - use ppvm_pauli_kernels::heisenberg as rules; - let one: [Rule1; 10] = [ - rules::x, - rules::y, - rules::z, - rules::h, - rules::s, - rules::s_dag, - rules::sqrt_x, - rules::sqrt_x_dag, - rules::sqrt_y, - rules::sqrt_y_dag, - ]; - let two: [Rule2; 3] = [rules::cnot, rules::cz, rules::cy]; - match gate { - Gate::One(g, v) => t.change_basis1(v, one[g]), - Gate::Two(g, a, b) => t.change_basis2(a, b, two[g]), - } - } - - #[test] - fn change_basis_preserves_commutation() { - for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { - let mut t = InverseTableau::new(n); - for gate in random_circuit(n, 100, 0xba5e + i as u64) { - change_basis(&mut t, gate); - assert_canonical_commutation(&t); - } - } - } - - /// `(G·C)·V = G·(C·V)`: the order of physical gates relative to changes of - /// basis does not matter. - #[test] - fn physical_gates_commute_with_changes_of_basis() { - for (i, n) in SIZES.into_iter().filter(|&n| n > 1).enumerate() { - let physical = random_circuit(n, 100, 0xc0de + i as u64); - let basis = random_circuit(n, 100, 0xfeed + i as u64); - let (mut first, mut second) = (InverseTableau::new(n), InverseTableau::new(n)); - physical.iter().for_each(|&g| apply(&mut first, g)); - basis.iter().for_each(|&g| change_basis(&mut first, g)); - basis.iter().for_each(|&g| change_basis(&mut second, g)); - physical.iter().for_each(|&g| apply(&mut second, g)); - assert_eq!(first, second, "n = {n}"); - } - } -} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs new file mode 100644 index 000000000..3b01aa337 --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! The storage word and the only code that knows its width. Everything else +//! touches words through these items and `Bits`, so a wider word can be tried +//! by changing the alias. + +/// Storage word. Swap to benchmark other widths. +pub(super) type BitWord = u64; +pub(super) const BITWORD_BITS: usize = BitWord::BITS as usize; +pub(super) const ZERO: BitWord = 0; + +/// Bit `i` of a packed bit string. +#[inline(always)] +pub(super) fn get_bit(words: &[BitWord], i: usize) -> bool { + (words[i / BITWORD_BITS] >> (i % BITWORD_BITS)) & 1 == 1 +} + +#[inline(always)] +pub(super) fn flip_bit(words: &mut [BitWord], i: usize) { + words[i / BITWORD_BITS] ^= 1 << (i % BITWORD_BITS); +} + +/// Word index and mask of bit `i`. +#[inline(always)] +pub(super) fn bit_position(i: usize) -> (usize, BitWord) { + (i / BITWORD_BITS, 1 << (i % BITWORD_BITS)) +} + +/// All ones if `b`, else zero (for branch-free masked updates). +#[inline(always)] +pub(super) fn mask_if(b: bool) -> BitWord { + ZERO.wrapping_sub(BitWord::from(b)) +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs new file mode 100644 index 000000000..234635e9b --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs @@ -0,0 +1,142 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! The tableau's storage: rows, signs, and where rows sit in the arena. + +use super::bitword::{BITWORD_BITS, BitWord, ZERO, flip_bit, get_bit}; + +/// The inverse tableau of an `n`-qubit Clifford. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct InverseTableau { + pub(super) n: usize, + /// Words per row and plane: `n.div_ceil(BITWORD_BITS)`. + pub(super) stride: usize, + /// X and Z words of all `2n` rows, arranged by `offsets`. + pub(super) words: Vec, + /// Bit `r` is the sign of row `r` (set = −). + pub(super) signs: Vec, +} + +// NOTE: we could impl Word for RowRef to make it a true PauliWord if we ever need it +/// A borrowed row: its X words, Z words and sign. +#[derive(Clone, Copy, Debug)] +pub struct RowRef<'a> { + pub x: &'a [BitWord], + pub z: &'a [BitWord], + pub negative: bool, +} + +impl RowRef<'_> { + /// X bit of the row at qubit `q`. + pub fn x_bit(&self, q: usize) -> bool { + get_bit(self.x, q) + } + + /// Z bit of the row at qubit `q`. + pub fn z_bit(&self, q: usize) -> bool { + get_bit(self.z, q) + } +} + +impl InverseTableau { + /// The identity Clifford, i.e. the state `|0…0⟩`: `ix_q = X_q`, `iz_q = Z_q`. + pub fn new(n: usize) -> Self { + let stride = n.div_ceil(BITWORD_BITS); + let mut t = Self { + n, + stride, + words: vec![ZERO; 4 * n * stride], + signs: vec![ZERO; (2 * n).div_ceil(BITWORD_BITS)], + }; + for q in 0..n { + let (ix, iz) = (t.x_at(q), t.z_at(n + q)); + flip_bit(&mut t.words[ix..], q); + flip_bit(&mut t.words[iz..], q); + } + t + } + + pub fn num_qubits(&self) -> usize { + self.n + } + + /// Row `ix_q = C⁻¹ X_q C`. + pub fn ix(&self, q: usize) -> RowRef<'_> { + debug_assert!(q < self.n); + self.row(q) + } + + /// Row `iz_q = C⁻¹ Z_q C`. + pub fn iz(&self, q: usize) -> RowRef<'_> { + debug_assert!(q < self.n); + self.row(self.n + q) + } + + pub(super) fn row(&self, r: usize) -> RowRef<'_> { + let (x, z) = (self.x_at(r), self.z_at(r)); + RowRef { + x: &self.words[x..x + self.stride], + z: &self.words[z..z + self.stride], + negative: get_bit(&self.signs, r), + } + } + + pub(super) fn flip_sign(&mut self, r: usize) { + flip_bit(&mut self.signs, r); + } + + // Arrangement: blocks by row kind, ix-X, ix-Z, iz-X, iz-Z (#204's order). + // Row r is row i = r % n of kind r / n (0 for ix, 1 for iz). + #[inline(always)] + fn offsets(n: usize, stride: usize, kind: usize, i: usize) -> (usize, usize) { + ( + ((2 * kind) * n + i) * stride, + ((2 * kind + 1) * n + i) * stride, + ) + } + + #[inline(always)] + pub(super) fn x_at(&self, r: usize) -> usize { + Self::offsets(self.n, self.stride, r / self.n, r % self.n).0 + } + + #[inline(always)] + pub(super) fn z_at(&self, r: usize) -> usize { + Self::offsets(self.n, self.stride, r / self.n, r % self.n).1 + } + + /// `(x_at(r), z_at(r))` for every row in order, without a division per row. + pub(super) fn row_offsets(&self) -> impl Iterator + use<> { + let (n, stride) = (self.n, self.stride); + (0..2).flat_map(move |kind| (0..n).map(move |i| Self::offsets(n, stride, kind, i))) + } +} + +#[cfg(test)] +mod tests { + use super::super::test_support::*; + use super::*; + + #[test] + fn identity_rows_are_single_paulis() { + for n in SIZES { + let t = InverseTableau::new(n); + for q in 0..n { + for j in 0..n { + assert_eq!(t.ix(q).x_bit(j), j == q); + assert!(!t.ix(q).z_bit(j)); + assert!(!t.iz(q).x_bit(j)); + assert_eq!(t.iz(q).z_bit(j), j == q); + } + assert!(!t.ix(q).negative && !t.iz(q).negative); + } + } + } + + #[test] + fn identity_has_canonical_commutation() { + for n in SIZES { + assert_canonical_commutation(&InverseTableau::new(n)); + } + } +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs new file mode 100644 index 000000000..5f4620259 --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! The inverse stabilizer tableau `C⁻¹` of a Clifford `C`. +//! +//! Row `ix_q` is `C⁻¹ X_q C` and row `iz_q` is `C⁻¹ Z_q C`, each stored as +//! packed X and Z bits plus one sign bit (rows are Hermitian, so their phase +//! is ±1). Rows are indexed `ix_q = q`, `iz_q = n + q`. +//! +//! - `data`: the storage, row reads, and where rows sit in the arena. +//! - `bitword`: the storage word; the only code that knows its width. +//! - `physical`: physical gates `C ← G·C`, which mix rows. +//! - `virtual_side`: changes of basis `C ← C·V`, which rewrite columns. +//! +//! Where rows sit in the arena is decided only by `offsets` (used by `x_at`, +//! `z_at` and `row_offsets`). + +mod bitword; +mod data; +mod physical; +#[cfg(test)] +mod test_support; +mod virtual_side; + +pub use data::{InverseTableau, RowRef}; diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/physical.rs b/crates/ppvm-tableau-2/src/inverse_tableau/physical.rs new file mode 100644 index 000000000..890438726 --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/physical.rs @@ -0,0 +1,232 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Physical gates, `C ← G·C`: they mix whole rows. + +use std::ops::Range; + +use ppvm_pauli_kernels::word_product; +use ppvm_traits_2::ApplyGateRule; + +use super::InverseTableau; +use super::bitword::{BitWord, ZERO}; + +/// Where a gate sends one of its generators: `G† P G = (−1)^flip · P(factors)`. +/// +/// The generators are X then Z for each gate qubit; `factors[j]` says whether +/// the Hermitian Pauli `P(factors)` contains generator `j`. +#[derive(Clone, Copy)] +struct Image { + factors: [bool; M], + flip: bool, +} + +impl Image { + fn new((factors, flip): ([bool; M], bool)) -> Self { + Self { factors, flip } + } + + /// Whether generator `g` maps to itself up to sign, so its row keeps its bits. + fn keeps_bits(&self, g: usize) -> bool { + (0..M).all(|j| self.factors[j] == (j == g)) + } + + /// Number of Y factors: an X and a Z on the same qubit. + fn y_count(&self) -> u32 { + self.factors.chunks(2).filter(|xz| xz[0] && xz[1]).count() as u32 + } + + /// One word of the product of the old rows in `factors`, and its phase. + #[inline(always)] + fn multiply(&self, old_words: &[[BitWord; 2]; M]) -> ([BitWord; 2], u32) { + let mut word = [ZERO; 2]; + let mut phase = 0u32; + for j in (0..M).filter(|&j| self.factors[j]) { + let (product, k) = word_product(word[0], word[1], old_words[j][0], old_words[j][1]); + word = product; + phase = phase.wrapping_add(k); + } + (word, phase) + } +} + +/// Rewrite row `g` (X words `x[g]`, Z words `z[g]`) as `C⁻¹ (G† P_g G) C`. +/// +/// Write the image as `(−1)^flip · i^#Y` times its generators in order (a Y is +/// `i·X·Z`). Conjugating by `C` turns each generator into its old row, so the +/// new row is the product of those old rows times `(−1)^flip · i^#Y`. For +/// example `S` sends `X ↦ −Y = −i·X·Z`, so `ix ← −i · ix · iz`, and keeps `Z`, +/// so `iz` stays. +/// +/// The product is taken site by site, so the new words at index `w` depend +/// only on the old words at `w`: each index is loaded, multiplied in registers +/// and stored once. `phases[g]` (a power of `i`) comes in with the image's own +/// factors and signs and collects each word's product phase. +#[inline(always)] +fn recombine( + x: [&mut [BitWord]; M], + z: [&mut [BitWord]; M], + images: &[Image; M], + phases: &mut [u32; M], +) { + let len = x[0].len(); + assert!(x.iter().chain(&z).all(|s| s.len() == len)); + for w in 0..len { + let old_words: [[BitWord; 2]; M] = std::array::from_fn(|j| [x[j][w], z[j][w]]); + for (g, image) in images + .iter() + .enumerate() + .filter(|(g, image)| !image.keeps_bits(*g)) + { + let ([new_x, new_z], phase) = image.multiply(&old_words); + x[g][w] = new_x; + z[g][w] = new_z; + phases[g] = phases[g].wrapping_add(phase); + } + } +} + +impl InverseTableau { + /// Starting phase of each new row, as a power of `i`: one `i` per Y in the + /// image, `−1` for its flip and `−1` per negative old row it multiplies. + fn initial_phases(&self, rows: [usize; M], images: &[Image; M]) -> [u32; M] { + images.map(|image| { + let negatives = (0..M).filter(|&j| image.factors[j] && self.row(rows[j]).negative); + image.y_count() + 2 * image.flip as u32 + 2 * negatives.count() as u32 + }) + } + + /// Set each row's sign from its final phase (`i⁰ = +`, `i² = −`). + fn set_signs(&mut self, rows: [usize; M], phases: [u32; M]) { + for (g, &r) in rows.iter().enumerate() { + debug_assert!(phases[g].is_multiple_of(2), "row is not Hermitian"); + if self.row(r).negative != (phases[g] % 4 == 2) { + self.flip_sign(r); + } + } + } + + /// Positions of row `r`'s X words and Z words in the arena. + fn xz(&self, r: usize) -> [Range; 2] { + let (x, z) = (self.x_at(r), self.z_at(r)); + [x..x + self.stride, z..z + self.stride] + } +} + +/// Physical gates, `C ← G·C`: the new row for generator `P` is `C⁻¹ (G† P G) C`, +/// so the rule evaluated on `P` names the old rows to multiply. +impl ApplyGateRule for InverseTableau { + type B = bool; + + fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + let images = [rule(true, false), rule(false, true)].map(Image::new); + let rows = [q, self.n + q]; + let mut phases = self.initial_phases(rows, &images); + let [[ix_x, ix_z], [iz_x, iz_z]] = rows.map(|r| self.xz(r)); + let [ix_x, ix_z, iz_x, iz_z] = self + .words + .get_disjoint_mut([ix_x, ix_z, iz_x, iz_z]) + .unwrap(); + recombine([ix_x, iz_x], [ix_z, iz_z], &images, &mut phases); + self.set_signs(rows, phases); + } + + fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { + let generator = |j: usize| std::array::from_fn(|i| i == j); + let images = std::array::from_fn(|j| Image::new(rule(generator(j)))); + let rows = [a, self.n + a, b, self.n + b]; + let mut phases = self.initial_phases(rows, &images); + let [[r0x, r0z], [r1x, r1z], [r2x, r2z], [r3x, r3z]] = rows.map(|r| self.xz(r)); + let [r0x, r0z, r1x, r1z, r2x, r2z, r3x, r3z] = self + .words + .get_disjoint_mut([r0x, r0z, r1x, r1z, r2x, r2z, r3x, r3z]) + .unwrap(); + recombine( + [r0x, r1x, r2x, r3x], + [r0z, r1z, r2z, r3z], + &images, + &mut phases, + ); + self.set_signs(rows, phases); + } + + fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + let ((x_bits, x_flip), (z_bits, z_flip)) = (rule(true, false), rule(false, true)); + debug_assert!(x_bits == [true, false] && z_bits == [false, true]); + if x_flip { + self.flip_sign(q); + } + if z_flip { + self.flip_sign(self.n + q); + } + } +} + +#[cfg(test)] +mod tests { + use ppvm_pauli_word_2::PauliWord; + use ppvm_phased_pauli_word_2::Phased; + use ppvm_traits_2::{PauliBits, Phase}; + + use super::super::RowRef; + use super::super::test_support::*; + use super::*; + + type Reference = Phased>; + + /// `C⁻¹ P C` for `C = G_m ⋯ G_1`: propagate `P` through the gates in reverse. + fn heisenberg(n: usize, q: usize, z: bool, circuit: &[Gate]) -> Reference { + let mut word = PauliWord::new(n); + if z { + word.set_z_bit(q, true); + } else { + word.set_x_bit(q, true); + } + let mut p = Phased::new(word); + for &gate in circuit.iter().rev() { + apply(&mut p, gate); + } + p + } + + fn assert_row_eq(row: RowRef, expected: &Reference, n: usize) { + for j in 0..n { + assert_eq!(row.x_bit(j), expected.word().x_bit(j), "x bit {j}"); + assert_eq!(row.z_bit(j), expected.word().z_bit(j), "z bit {j}"); + } + let negative = match expected.phase() { + Phase::Pos1 => false, + Phase::Neg1 => true, + phase => panic!("non-Hermitian reference phase {phase:?}"), + }; + assert_eq!(row.negative, negative, "sign"); + } + + /// Rows must equal `C⁻¹ X_q C` / `C⁻¹ Z_q C` computed by Heisenberg + /// propagation of single Pauli words. + #[test] + fn physical_gates_match_heisenberg_propagation() { + for (i, n) in SIZES.into_iter().enumerate() { + let circuit = random_circuit(n, 200, 0x9e37_79b9 + i as u64); + let mut t = InverseTableau::new(n); + for &gate in &circuit { + apply(&mut t, gate); + } + for q in 0..n { + assert_row_eq(t.ix(q), &heisenberg(n, q, false, &circuit), n); + assert_row_eq(t.iz(q), &heisenberg(n, q, true, &circuit), n); + } + } + } + + #[test] + fn physical_gates_preserve_commutation() { + for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { + let mut t = InverseTableau::new(n); + for gate in random_circuit(n, 100, 0x5eed + i as u64) { + apply(&mut t, gate); + assert_canonical_commutation(&t); + } + } + } +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/test_support.rs b/crates/ppvm-tableau-2/src/inverse_tableau/test_support.rs new file mode 100644 index 000000000..af5be0163 --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/test_support.rs @@ -0,0 +1,107 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Helpers shared by the tableau's unit tests. + +use ppvm_pauli_kernels::Bits; +use ppvm_traits_2::Clifford; + +use super::{InverseTableau, RowRef}; + +pub(super) const SIZES: [usize; 7] = [1, 2, 7, 63, 64, 65, 130]; + +/// Whether two rows anticommute: odd symplectic product. +fn anticommute(a: RowRef, b: RowRef) -> bool { + let mut parity = 0; + for i in 0..a.x.len() { + parity ^= ((a.x[i] & b.z[i]) ^ (a.z[i] & b.x[i])).popcount() & 1; + } + parity == 1 +} + +/// Conjugation preserves commutation: `ix_a`, `iz_b` anticommute iff `a == b`. +pub(super) fn assert_canonical_commutation(t: &InverseTableau) { + let n = t.num_qubits(); + for a in 0..n { + for b in 0..n { + assert_eq!(anticommute(t.ix(a), t.iz(b)), a == b); + assert!(!anticommute(t.ix(a), t.ix(b))); + assert!(!anticommute(t.iz(a), t.iz(b))); + } + } +} + +#[derive(Clone, Copy, Debug)] +pub(super) enum Gate { + One(usize, usize), + Two(usize, usize, usize), +} + +/// `gate` as a physical gate (or on any `Clifford`). +pub(super) fn apply(t: &mut T, gate: Gate) { + match gate { + Gate::One(g, q) => match g { + 0 => t.x(q), + 1 => t.y(q), + 2 => t.z(q), + 3 => t.h(q), + 4 => t.s(q), + 5 => t.s_dag(q), + 6 => t.sqrt_x(q), + 7 => t.sqrt_x_dag(q), + 8 => t.sqrt_y(q), + _ => t.sqrt_y_dag(q), + }, + Gate::Two(g, a, b) => match g { + 0 => t.cnot(a, b), + 1 => t.cz(a, b), + _ => t.cy(a, b), + }, + } +} + +type Rule1 = fn(bool, bool) -> ([bool; 2], bool); +type Rule2 = fn([bool; 4]) -> ([bool; 4], bool); + +/// `gate` as a change of basis on virtual qubits, `C ← C·V`. +pub(super) fn change_basis(t: &mut InverseTableau, gate: Gate) { + use ppvm_pauli_kernels::heisenberg as rules; + let one: [Rule1; 10] = [ + rules::x, + rules::y, + rules::z, + rules::h, + rules::s, + rules::s_dag, + rules::sqrt_x, + rules::sqrt_x_dag, + rules::sqrt_y, + rules::sqrt_y_dag, + ]; + let two: [Rule2; 3] = [rules::cnot, rules::cz, rules::cy]; + match gate { + Gate::One(g, v) => t.change_basis1(v, one[g]), + Gate::Two(g, a, b) => t.change_basis2(a, b, two[g]), + } +} + +/// A random circuit from a xorshift seed. +pub(super) fn random_circuit(n: usize, len: usize, mut seed: u64) -> Vec { + let mut next = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed as usize + }; + (0..len) + .map(|_| { + if n < 2 || next() % 3 == 0 { + Gate::One(next() % 10, next() % n) + } else { + let a = next() % n; + let b = (a + 1 + next() % (n - 1)) % n; + Gate::Two(next() % 3, a, b) + } + }) + .collect() +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/virtual_side.rs b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_side.rs new file mode 100644 index 000000000..e87a13b53 --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_side.rs @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Virtual gates, i.e. changes of basis `C ← C·V`: they rewrite columns. + +use super::InverseTableau; +use super::bitword::{ZERO, bit_position, mask_if}; + +impl InverseTableau { + /// Change the basis of virtual qubit `v` by the one-qubit Clifford `V` + /// whose Heisenberg rule is `rule`: `C ← C·V`. + /// + /// Updates the tableau only. The state `C (|φ⟩ ⊗ |0…0⟩)` stays the same if + /// the caller also applies `V†` to `φ`, or if `V` acts trivially on it (e.g. + /// `v` dormant and `V` fixes `|0⟩`). + /// + /// Every row `R` becomes `V† R V`: the rule runs on each row's bits in + /// column `v`. Strided: one word per row. + /// + /// Used by: dormant-pivot collapse (`H` on the pivot, `X` to fix the + /// outcome), active collapse (the final rotation onto `Z_h`), reset. + pub fn change_basis1(&mut self, v: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + debug_assert!(v < self.n); + let (w, m) = bit_position(v); + for (r, (x, z)) in self.row_offsets().enumerate() { + let (x, z) = (x + w, z + w); + let old = [self.words[x] & m != ZERO, self.words[z] & m != ZERO]; + let (new, flip) = rule(old[0], old[1]); + self.words[x] ^= m & mask_if(new[0] != old[0]); + self.words[z] ^= m & mask_if(new[1] != old[1]); + let (sw, sm) = bit_position(r); + self.signs[sw] ^= sm & mask_if(flip); + } + } + + /// Change the basis of virtual qubits `a`, `b` by the two-qubit Clifford + /// `V` whose Heisenberg rule is `rule` (`a` is the control for controlled + /// gates): `C ← C·V`. Same contract as [`change_basis1`](Self::change_basis1). + /// + /// Used by: T-gate localization (`CX(p, v)` collects the X bits of `O` onto + /// the pivot `p`), dormant-pivot collapse (`CX(p, v)`, `CZ(p, c)`), active + /// collapse (the `CX` / `CZ` that reduce `P` to one qubit). + pub fn change_basis2( + &mut self, + a: usize, + b: usize, + rule: impl Fn([bool; 4]) -> ([bool; 4], bool), + ) { + debug_assert!(a < self.n && b < self.n && a != b); + let [(wa, ma), (wb, mb)] = [bit_position(a), bit_position(b)]; + for (r, (x, z)) in self.row_offsets().enumerate() { + let words = [x + wa, z + wa, x + wb, z + wb]; + let masks = [ma, ma, mb, mb]; + let old: [bool; 4] = std::array::from_fn(|j| self.words[words[j]] & masks[j] != ZERO); + let (new, flip) = rule(old); + for j in 0..4 { + self.words[words[j]] ^= masks[j] & mask_if(new[j] != old[j]); + } + let (sw, sm) = bit_position(r); + self.signs[sw] ^= sm & mask_if(flip); + } + } +} + +#[cfg(test)] +mod tests { + use super::super::test_support::*; + use super::*; + + #[test] + fn change_basis_preserves_commutation() { + for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { + let mut t = InverseTableau::new(n); + for gate in random_circuit(n, 100, 0xba5e + i as u64) { + change_basis(&mut t, gate); + assert_canonical_commutation(&t); + } + } + } + + /// `(G·C)·V = G·(C·V)`: the order of physical gates relative to changes of + /// basis does not matter. + #[test] + fn physical_gates_commute_with_changes_of_basis() { + for (i, n) in SIZES.into_iter().filter(|&n| n > 1).enumerate() { + let physical = random_circuit(n, 100, 0xc0de + i as u64); + let basis = random_circuit(n, 100, 0xfeed + i as u64); + let (mut first, mut second) = (InverseTableau::new(n), InverseTableau::new(n)); + physical.iter().for_each(|&g| apply(&mut first, g)); + basis.iter().for_each(|&g| change_basis(&mut first, g)); + basis.iter().for_each(|&g| change_basis(&mut second, g)); + physical.iter().for_each(|&g| apply(&mut second, g)); + assert_eq!(first, second, "n = {n}"); + } + } +} From facff5ff1c2c5247785b045c40931aa80148a991 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 14:40:50 +0100 Subject: [PATCH 06/60] feat(ppvm-tableau-2): measure and reset the stabilizer state measure(q, coin) reads O = iz_q: deterministic from its sign without X bits, otherwise Stim's collapse on the lowest X bit via change_basis. The caller supplies the coin. Checked against a dense state vector after every gate, measurement and reset. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/inverse_tableau/bitword.rs | 14 ++ .../src/inverse_tableau/measure.rs | 104 +++++++++++++ .../ppvm-tableau-2/src/inverse_tableau/mod.rs | 2 + crates/ppvm-tableau-2/tests/dense_matrix.rs | 144 +++++++++++++++++- 4 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/measure.rs diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs index 3b01aa337..ddc999263 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs @@ -32,3 +32,17 @@ pub(super) fn bit_position(i: usize) -> (usize, BitWord) { pub(super) fn mask_if(b: bool) -> BitWord { ZERO.wrapping_sub(BitWord::from(b)) } + +/// Indices of the set bits of a packed bit string, ascending. +pub(super) fn set_bits(words: &[BitWord]) -> impl Iterator + '_ { + words.iter().enumerate().flat_map(|(w, &word)| { + let mut rest = word; + std::iter::from_fn(move || { + (rest != ZERO).then(|| { + let b = rest.trailing_zeros() as usize; + rest &= rest - 1; + w * BITWORD_BITS + b + }) + }) + }) +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs new file mode 100644 index 000000000..a864070eb --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs @@ -0,0 +1,104 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Z measurement and reset of the stabilizer state `C |0…0⟩` (no active +//! qubits). + +use ppvm_pauli_kernels::heisenberg; +use ppvm_traits_2::Clifford; + +use super::InverseTableau; +use super::bitword::set_bits; + +impl InverseTableau { + /// Measure `Z_q` on `C |0…0⟩` and collapse; returns the outcome (`true` + /// for `|1⟩`). `coin` is called only if the outcome is random. + /// + /// Measuring `Z_q` is measuring `O = C⁻¹ Z_q C` (row `iz_q`) on `|0…0⟩`: + /// + /// - **No X in `O`:** `O` is a signed Z string, so the outcome is its sign. + /// - **X on some virtual qubits:** 50/50. Pick the lowest, `p`, as pivot + /// and change basis so that `O` becomes `±Z_p`: `CX(p, v)` clears every + /// other X (trivial on the state, since `p` is in `|0⟩`), leaving + /// `O = s · X_p · Z…` or `s · Y_p · Z…`. The projected state on `p` is + /// `|0⟩ ± |1⟩` (or `|0⟩ ± i|1⟩`), which `C ← C·H` (or `C·S·H`) and an + /// `X` when the sign calls for it map back to `|0⟩`. + pub fn measure(&mut self, q: usize, coin: impl FnOnce() -> bool) -> bool { + let mut x_support = set_bits(self.iz(q).x); + let Some(pivot) = x_support.next() else { + return self.iz(q).negative; + }; + let targets: Vec = x_support.collect(); + for v in targets { + self.change_basis2(pivot, v, heisenberg::cnot); + } + let (has_y, negative) = (self.iz(q).z_bit(pivot), self.iz(q).negative); + if has_y { + self.change_basis1(pivot, heisenberg::s); + } + self.change_basis1(pivot, heisenberg::h); + let outcome = coin(); + if outcome != negative { + self.change_basis1(pivot, heisenberg::x); + } + outcome + } + + /// Reset qubit `q` to `|0⟩`: measure it, then flip it if it was `|1⟩`. + pub fn reset(&mut self, q: usize, coin: impl FnOnce() -> bool) { + if self.measure(q, coin) { + self.x(q); + } + } +} + +#[cfg(test)] +mod tests { + use super::super::test_support::*; + use super::*; + + /// After a measurement, measuring again is deterministic and repeats it. + #[test] + fn measuring_again_repeats_the_outcome() { + for (i, n) in SIZES.into_iter().enumerate() { + let mut t = InverseTableau::new(n); + random_circuit(n, 200, 0x3e5 + i as u64) + .into_iter() + .for_each(|g| apply(&mut t, g)); + for q in 0..n { + let first = t.measure(q, || q % 2 == 0); + let again = t.measure(q, || panic!("second measurement is random")); + assert_eq!(first, again, "n = {n}, q = {q}"); + } + } + } + + #[test] + fn measurement_preserves_commutation() { + for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { + let mut t = InverseTableau::new(n); + for (k, gate) in random_circuit(n, 100, 0x5ca1 + i as u64) + .into_iter() + .enumerate() + { + apply(&mut t, gate); + t.measure(k % n, || k % 3 == 0); + assert_canonical_commutation(&t); + } + } + } + + #[test] + fn reset_leaves_zero() { + for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { + let mut t = InverseTableau::new(n); + random_circuit(n, 100, 0x7e5e + i as u64) + .into_iter() + .for_each(|g| apply(&mut t, g)); + for q in 0..n { + t.reset(q, || true); + assert!(!t.measure(q, || panic!("reset qubit is random"))); + } + } + } +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs index 5f4620259..03c89e162 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -11,12 +11,14 @@ //! - `bitword`: the storage word; the only code that knows its width. //! - `physical`: physical gates `C ← G·C`, which mix rows. //! - `virtual_side`: changes of basis `C ← C·V`, which rewrite columns. +//! - `measure`: Z measurement and reset of the stabilizer state. //! //! Where rows sit in the arena is decided only by `offsets` (used by `x_at`, //! `z_at` and `row_offsets`). mod bitword; mod data; +mod measure; mod physical; #[cfg(test)] mod test_support; diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs index d1bce5e0f..cee9d9ab8 100644 --- a/crates/ppvm-tableau-2/tests/dense_matrix.rs +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -3,7 +3,8 @@ //! Gates against dense matrices: row `ix_q` must equal `C† X_q C` and //! `iz_q` must equal `C† Z_q C`, with `C` the product of textbook gate matrices. -//! No Pauli code is shared with the tableau. +//! Measurements are checked against a state vector. No Pauli code is shared +//! with the tableau. use std::collections::{HashSet, VecDeque}; @@ -335,3 +336,144 @@ fn random_three_qubit_circuits() { assert_rows_match(&t, &cliff, &paulis, &format!("round {round}")); } } + +/// Row `ix_q` (or `iz_q`) as a signed Pauli matrix. +fn row_matrix(t: &InverseTableau, q: usize, z: bool, paulis: &Paulis) -> Mat { + let (code, negative) = row_code(t, q, z); + let mut m = paulis.table[code].clone(); + if negative { + m.a.iter_mut().for_each(|e| *e = -*e); + } + m +} + +/// `⟨0…0| C⁻¹ P C |0…0⟩` for the Pauli string `code`, from the rows alone: +/// `P = i^#Y · ∏ X_q^x Z_q^z`, so `C⁻¹ P C = i^#Y · ∏ ix_q^x iz_q^z`. +fn tableau_expectation(t: &InverseTableau, code: usize, paulis: &Paulis) -> C { + let n = t.num_qubits(); + let mut m = Mat::identity(n); + let mut phase = c(1.0, 0.0); + for q in 0..n { + let (x, z) = ((code >> q) & 1 == 1, (code >> (n + q)) & 1 == 1); + if x { + m = m.mul(&row_matrix(t, q, false, paulis)); + } + if z { + m = m.mul(&row_matrix(t, q, true, paulis)); + } + if x && z { + phase *= c(0.0, 1.0); + } + } + phase * m.a[0] +} + +fn apply_to_state(m: &Mat, v: &[C]) -> Vec { + (0..m.d) + .map(|i| (0..m.d).map(|j| m.a[i * m.d + j] * v[j]).sum()) + .collect() +} + +fn state_expectation(v: &[C], p: &Mat) -> C { + let pv = apply_to_state(p, v); + v.iter().zip(&pv).map(|(a, b)| a.conj() * b).sum() +} + +/// Probability that qubit `q` is `|1⟩`. +fn probability_one(v: &[C], q: usize) -> f64 { + v.iter() + .enumerate() + .filter(|(i, _)| (i >> q) & 1 == 1) + .map(|(_, a)| a.norm_sqr()) + .sum() +} + +/// Project onto outcome `m` of qubit `q` and renormalize. +fn project(v: &mut [C], q: usize, m: bool) { + v.iter_mut() + .enumerate() + .filter(|(i, _)| ((i >> q) & 1 == 1) != m) + .for_each(|(_, a)| *a = C::default()); + let norm = v.iter().map(|a| a.norm_sqr()).sum::().sqrt(); + v.iter_mut().for_each(|a| *a /= norm); +} + +/// Measure `q` on both the tableau and the state vector, forcing `forced` when +/// the outcome is random. The tableau must call the coin exactly when the +/// state vector says the outcome is random. +fn measure_both(t: &mut InverseTableau, v: &mut [C], q: usize, forced: bool) -> bool { + let p1 = probability_one(v, q); + let random = p1 > 1e-9 && p1 < 1.0 - 1e-9; + if random { + assert!( + (p1 - 0.5).abs() < 1e-9, + "random outcome must be 50/50, got {p1}" + ); + } + let mut called = false; + let m = t.measure(q, || { + called = true; + forced + }); + assert_eq!(called, random, "coin called iff the outcome is random"); + if !random { + assert_eq!(m, p1 > 0.5, "deterministic outcome"); + } + project(v, q, m); + m +} + +/// Random three-qubit circuits with measurements and resets: after every step, +/// all 64 Pauli expectations from the tableau match the state vector. +#[test] +fn measurements_match_the_state_vector() { + let n = 3; + let gates = all_gates(n); + let paulis = Paulis::new(n); + let x = gate_matrices()[0]; + let mut seed = 0x9e37_79b9_7f4a_7c15u64; + let mut next = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed as usize + }; + for round in 0..30 { + let mut t = InverseTableau::new(n); + let mut v = vec![C::default(); 1 << n]; + v[0] = c(1.0, 0.0); + for step in 0..60 { + let (q, forced) = (next() % n, next() % 2 == 1); + match next() % 10 { + 0..7 => { + let gate = gates[next() % gates.len()]; + apply(&mut t, gate); + v = apply_to_state(&matrix(gate, n), &v); + } + 7..9 => { + measure_both(&mut t, &mut v, q, forced); + } + _ => { + let m = measure_both(&mut t, &mut v, q, forced); + let mut t_reset = t.clone(); + t_reset.reset(q, || unreachable!("qubit was just measured")); + if m { + t.x(q); + v = apply_to_state(&single(x, q, n), &v); + } + assert_eq!(t, t_reset, "reset = measure + conditional X"); + } + } + for code in 0..1usize << (2 * n) { + let (from_rows, from_state) = ( + tableau_expectation(&t, code, &paulis), + state_expectation(&v, &paulis.table[code]), + ); + assert!( + (from_rows - from_state).norm() < 1e-9, + "round {round} step {step}: Pauli {code:#b}: tableau {from_rows}, state {from_state}" + ); + } + } + } +} From c95c3deecd024e682cf053abe2e8faf2f34ad74c Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 14:58:15 +0100 Subject: [PATCH 07/60] perf(ppvm-tableau-2): cut physical-gate bookkeeping; add a Stim harness MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Read row signs directly, find a row's kind by comparison instead of dividing by n, and hand out disjoint row slices through one checked helper (rows_mut). The clifft-bench d=7 surface code goes from 135 to 108 µs/shot. Add the run_stim example (any Stim circuit's gates, measurements and resets, one shot at a time), scripts to fetch clifft-bench circuits and summarize samply profiles, and the batched variants to the design. Co-Authored-By: Claude Opus 5.5 (1M context) --- Cargo.lock | 1 + crates/ppvm-tableau-2/Cargo.toml | 1 + crates/ppvm-tableau-2/examples/run_stim.rs | 167 ++++++++++++++++++ .../src/inverse_tableau/data.rs | 46 ++++- .../src/inverse_tableau/physical.rs | 33 +--- scripts/fetch_clifft_bench.sh | 10 ++ scripts/samply_top.py | 0 7 files changed, 228 insertions(+), 30 deletions(-) create mode 100644 crates/ppvm-tableau-2/examples/run_stim.rs create mode 100755 scripts/fetch_clifft_bench.sh mode change 100644 => 100755 scripts/samply_top.py diff --git a/Cargo.lock b/Cargo.lock index c62045ecf..5e988753b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2362,6 +2362,7 @@ dependencies = [ "ppvm-pauli-word-2", "ppvm-phased-pauli-word-2", "ppvm-traits-2", + "stim-parser", ] [[package]] diff --git a/crates/ppvm-tableau-2/Cargo.toml b/crates/ppvm-tableau-2/Cargo.toml index 37cdc2418..ee04b94fe 100644 --- a/crates/ppvm-tableau-2/Cargo.toml +++ b/crates/ppvm-tableau-2/Cargo.toml @@ -13,3 +13,4 @@ ppvm-traits-2 = { path = "../ppvm-traits-2" } ppvm-pauli-word-2 = { path = "../ppvm-pauli-word-2" } ppvm-phased-pauli-word-2 = { path = "../ppvm-phased-pauli-word-2" } num-complex = "0.4.6" +stim-parser = { path = "../stim-parser" } diff --git a/crates/ppvm-tableau-2/examples/run_stim.rs b/crates/ppvm-tableau-2/examples/run_stim.rs new file mode 100644 index 000000000..5b36b32ea --- /dev/null +++ b/crates/ppvm-tableau-2/examples/run_stim.rs @@ -0,0 +1,167 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Benchmark and profiling harness: runs a Stim circuit's Clifford gates, +//! measurements and resets on `InverseTableau`, one shot at a time with a fresh +//! tableau per shot. Noise and annotations are skipped (noise sampling is the +//! circuit runner's job, not the tableau's). +//! +//! ```text +//! cargo run --release -p ppvm-tableau-2 --example run_stim -- [shots] +//! ``` +//! +//! The clifft-bench surface code (d = 7, 7 rounds) is fetched by +//! `scripts/fetch_clifft_bench.sh` into `target/clifft-bench/`. + +use std::time::Instant; + +use ppvm_tableau_2::InverseTableau; +use ppvm_traits_2::Clifford; +use stim_parser::instructions::{GateName, MeasureName}; +use stim_parser::prelude::{Instruction, Target}; + +#[derive(Clone, Copy)] +enum Op { + One(GateName, usize), + Two(GateName, usize, usize), + Measure(usize), + Reset(usize), + MeasureReset(usize), +} + +fn qubits(targets: &[Target]) -> Vec { + targets + .iter() + .map(|t| t.as_qubit().expect("only qubit targets are supported")) + .collect() +} + +/// Flatten the program into ops, unrolling `REPEAT` blocks. +fn lower(instructions: &[Instruction], ops: &mut Vec) { + for instruction in instructions { + match instruction { + Instruction::Gate(g) => { + let qs = qubits(&g.targets); + match g.name { + GateName::Reset | GateName::ResetZ => { + ops.extend(qs.iter().map(|&q| Op::Reset(q))) + } + GateName::CX | GateName::CZ | GateName::CY => { + ops.extend(qs.chunks(2).map(|p| Op::Two(g.name, p[0], p[1]))) + } + name => ops.extend(qs.iter().map(|&q| Op::One(name, q))), + } + } + Instruction::Measure(m) => match m.name { + MeasureName::M | MeasureName::MZ => { + ops.extend(m.targets.iter().map(|&q| Op::Measure(q))) + } + MeasureName::MR => ops.extend(m.targets.iter().map(|&q| Op::MeasureReset(q))), + other => panic!("unsupported measurement {other:?}"), + }, + Instruction::Repeat { count, body, .. } => { + let mut block = Vec::new(); + lower(body, &mut block); + for _ in 0..*count { + ops.extend_from_slice(&block); + } + } + Instruction::Noise(_) | Instruction::Annotation(_) => {} + other => panic!("unsupported instruction {other:?}"), + } + } +} + +fn apply_one(t: &mut InverseTableau, name: GateName, q: usize) { + match name { + GateName::X => t.x(q), + GateName::Y => t.y(q), + GateName::Z => t.z(q), + GateName::H | GateName::HXZ => t.h(q), + GateName::S | GateName::SqrtZ => t.s(q), + GateName::SDag | GateName::SqrtZDag => t.s_dag(q), + GateName::SqrtX => t.sqrt_x(q), + GateName::SqrtXDag => t.sqrt_x_dag(q), + GateName::SqrtY => t.sqrt_y(q), + GateName::SqrtYDag => t.sqrt_y_dag(q), + other => panic!("unsupported gate {other:?}"), + } +} + +fn apply_two(t: &mut InverseTableau, name: GateName, a: usize, b: usize) { + match name { + GateName::CX => t.cnot(a, b), + GateName::CZ => t.cz(a, b), + _ => t.cy(a, b), + } +} + +fn main() { + let mut args = std::env::args().skip(1); + let path = args.next().expect("usage: run_stim [shots]"); + let shots: usize = args.next().map_or(1000, |s| s.parse().expect("shots")); + let program = stim_parser::parse(&std::fs::read_to_string(&path).expect("read circuit")) + .unwrap_or_else(|e| panic!("parse error: {e:?}")); + let mut ops = Vec::new(); + lower(&program.instructions, &mut ops); + let n = 1 + ops + .iter() + .map(|op| match *op { + Op::Two(_, a, b) => a.max(b), + Op::One(_, q) | Op::Measure(q) | Op::Reset(q) | Op::MeasureReset(q) => q, + }) + .max() + .expect("empty circuit"); + + let mut seed = 0x9e37_79b9_7f4a_7c15u64; + let mut coin = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed & 1 == 1 + }; + let (mut parity, mut random) = (false, 0usize); + let start = Instant::now(); + for _ in 0..shots { + let mut t = InverseTableau::new(n); + for &op in &ops { + let mut flip = || { + random += 1; + coin() + }; + match op { + Op::One(name, q) => apply_one(&mut t, name, q), + Op::Two(name, a, b) => apply_two(&mut t, name, a, b), + Op::Measure(q) => parity ^= t.measure(q, &mut flip), + Op::Reset(q) => t.reset(q, &mut flip), + Op::MeasureReset(q) => { + let m = t.measure(q, &mut flip); + parity ^= m; + if m { + t.x(q); + } + } + } + } + } + let elapsed = start.elapsed().as_secs_f64(); + let measurements = ops + .iter() + .filter(|op| matches!(op, Op::Measure(_) | Op::MeasureReset(_))) + .count(); + println!( + "{path}: {n} qubits, {} ops ({measurements} measurements) per shot, {shots} shots", + ops.len() + ); + println!( + "{:.1} µs/shot, {:.0} shots/s, {:.1}% of measurements and resets random (parity {parity})", + elapsed / shots as f64 * 1e6, + shots as f64 / elapsed, + 100.0 * random as f64 + / (shots + * ops + .iter() + .filter(|op| !matches!(op, Op::One(..) | Op::Two(..))) + .count()) as f64, + ); +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs index 234635e9b..cbe0b66ee 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs @@ -77,10 +77,16 @@ impl InverseTableau { RowRef { x: &self.words[x..x + self.stride], z: &self.words[z..z + self.stride], - negative: get_bit(&self.signs, r), + negative: self.negative(r), } } + /// Whether row `r` has sign `−`. + #[inline(always)] + pub(super) fn negative(&self, r: usize) -> bool { + get_bit(&self.signs, r) + } + pub(super) fn flip_sign(&mut self, r: usize) { flip_bit(&mut self.signs, r); } @@ -95,14 +101,48 @@ impl InverseTableau { ) } + /// The X words and the Z words of distinct rows, mutably. + /// + /// Panics unless every row is in range and no row repeats: that is what + /// makes the ranges disjoint, so this is the one place that checks it. + #[inline(always)] + pub(super) fn rows_mut( + &mut self, + rows: [usize; R], + ) -> ([&mut [BitWord]; R], [&mut [BitWord]; R]) { + for (i, &r) in rows.iter().enumerate() { + assert!(r < 2 * self.n, "row {r} out of range"); + assert!(!rows[..i].contains(&r), "row {r} repeated"); + } + let (xs, zs) = (rows.map(|r| self.x_at(r)), rows.map(|r| self.z_at(r))); + let (stride, words) = (self.stride, self.words.as_mut_ptr()); + // SAFETY: distinct rows in range own disjoint `stride`-word ranges of + // `words`, and the slices borrow `self` mutably for their lifetime. + unsafe { + ( + xs.map(|x| std::slice::from_raw_parts_mut(words.add(x), stride)), + zs.map(|z| std::slice::from_raw_parts_mut(words.add(z), stride)), + ) + } + } + + /// Kind and index of row `r < 2n`, without dividing. + #[inline(always)] + fn kind_and_index(&self, r: usize) -> (usize, usize) { + let kind = usize::from(r >= self.n); + (kind, r - kind * self.n) + } + #[inline(always)] pub(super) fn x_at(&self, r: usize) -> usize { - Self::offsets(self.n, self.stride, r / self.n, r % self.n).0 + let (kind, i) = self.kind_and_index(r); + Self::offsets(self.n, self.stride, kind, i).0 } #[inline(always)] pub(super) fn z_at(&self, r: usize) -> usize { - Self::offsets(self.n, self.stride, r / self.n, r % self.n).1 + let (kind, i) = self.kind_and_index(r); + Self::offsets(self.n, self.stride, kind, i).1 } /// `(x_at(r), z_at(r))` for every row in order, without a division per row. diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/physical.rs b/crates/ppvm-tableau-2/src/inverse_tableau/physical.rs index 890438726..93c3bcd1f 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/physical.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/physical.rs @@ -3,8 +3,6 @@ //! Physical gates, `C ← G·C`: they mix whole rows. -use std::ops::Range; - use ppvm_pauli_kernels::word_product; use ppvm_traits_2::ApplyGateRule; @@ -91,7 +89,7 @@ impl InverseTableau { /// image, `−1` for its flip and `−1` per negative old row it multiplies. fn initial_phases(&self, rows: [usize; M], images: &[Image; M]) -> [u32; M] { images.map(|image| { - let negatives = (0..M).filter(|&j| image.factors[j] && self.row(rows[j]).negative); + let negatives = (0..M).filter(|&j| image.factors[j] && self.negative(rows[j])); image.y_count() + 2 * image.flip as u32 + 2 * negatives.count() as u32 }) } @@ -100,17 +98,11 @@ impl InverseTableau { fn set_signs(&mut self, rows: [usize; M], phases: [u32; M]) { for (g, &r) in rows.iter().enumerate() { debug_assert!(phases[g].is_multiple_of(2), "row is not Hermitian"); - if self.row(r).negative != (phases[g] % 4 == 2) { + if self.negative(r) != (phases[g] % 4 == 2) { self.flip_sign(r); } } } - - /// Positions of row `r`'s X words and Z words in the arena. - fn xz(&self, r: usize) -> [Range; 2] { - let (x, z) = (self.x_at(r), self.z_at(r)); - [x..x + self.stride, z..z + self.stride] - } } /// Physical gates, `C ← G·C`: the new row for generator `P` is `C⁻¹ (G† P G) C`, @@ -122,12 +114,8 @@ impl ApplyGateRule for InverseTableau { let images = [rule(true, false), rule(false, true)].map(Image::new); let rows = [q, self.n + q]; let mut phases = self.initial_phases(rows, &images); - let [[ix_x, ix_z], [iz_x, iz_z]] = rows.map(|r| self.xz(r)); - let [ix_x, ix_z, iz_x, iz_z] = self - .words - .get_disjoint_mut([ix_x, ix_z, iz_x, iz_z]) - .unwrap(); - recombine([ix_x, iz_x], [ix_z, iz_z], &images, &mut phases); + let (x, z) = self.rows_mut(rows); + recombine(x, z, &images, &mut phases); self.set_signs(rows, phases); } @@ -136,17 +124,8 @@ impl ApplyGateRule for InverseTableau { let images = std::array::from_fn(|j| Image::new(rule(generator(j)))); let rows = [a, self.n + a, b, self.n + b]; let mut phases = self.initial_phases(rows, &images); - let [[r0x, r0z], [r1x, r1z], [r2x, r2z], [r3x, r3z]] = rows.map(|r| self.xz(r)); - let [r0x, r0z, r1x, r1z, r2x, r2z, r3x, r3z] = self - .words - .get_disjoint_mut([r0x, r0z, r1x, r1z, r2x, r2z, r3x, r3z]) - .unwrap(); - recombine( - [r0x, r1x, r2x, r3x], - [r0z, r1z, r2z, r3z], - &images, - &mut phases, - ); + let (x, z) = self.rows_mut(rows); + recombine(x, z, &images, &mut phases); self.set_signs(rows, phases); } diff --git a/scripts/fetch_clifft_bench.sh b/scripts/fetch_clifft_bench.sh new file mode 100755 index 000000000..32937b74a --- /dev/null +++ b/scripts/fetch_clifft_bench.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# Fetch clifft-bench workload circuits into target/clifft-bench/ (not committed). +set -euo pipefail +dest="$(git rev-parse --show-toplevel)/target/clifft-bench" +mkdir -p "$dest" +base=https://raw.githubusercontent.com/unitaryfoundation/clifft-bench/main/workloads/circuits +for name in "${@:-pure_surface_d7_r7_p1e-3}"; do + curl -fsSL "$base/$name.stim" -o "$dest/$name.stim" + echo "$dest/$name.stim" +done diff --git a/scripts/samply_top.py b/scripts/samply_top.py old mode 100644 new mode 100755 From 1b5afe0345445e5ce347641d5def6895a1c40145 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 14:59:03 +0100 Subject: [PATCH 08/60] refactor(ppvm-tableau-2): rename virtual_side to virtual Matches physical.rs. `virtual` is a reserved keyword, so the module is declared as r#virtual. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/src/inverse_tableau/mod.rs | 4 ++-- .../src/inverse_tableau/{virtual_side.rs => virtual.rs} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename crates/ppvm-tableau-2/src/inverse_tableau/{virtual_side.rs => virtual.rs} (100%) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs index 03c89e162..4cc7c3b3c 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -10,7 +10,7 @@ //! - `data`: the storage, row reads, and where rows sit in the arena. //! - `bitword`: the storage word; the only code that knows its width. //! - `physical`: physical gates `C ← G·C`, which mix rows. -//! - `virtual_side`: changes of basis `C ← C·V`, which rewrite columns. +//! - `virtual`: changes of basis `C ← C·V`, which rewrite columns. //! - `measure`: Z measurement and reset of the stabilizer state. //! //! Where rows sit in the arena is decided only by `offsets` (used by `x_at`, @@ -22,6 +22,6 @@ mod measure; mod physical; #[cfg(test)] mod test_support; -mod virtual_side; +mod r#virtual; pub use data::{InverseTableau, RowRef}; diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/virtual_side.rs b/crates/ppvm-tableau-2/src/inverse_tableau/virtual.rs similarity index 100% rename from crates/ppvm-tableau-2/src/inverse_tableau/virtual_side.rs rename to crates/ppvm-tableau-2/src/inverse_tableau/virtual.rs From 9276d043020e206d99ded11806d35746c8d6c7fc Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 15:00:59 +0100 Subject: [PATCH 09/60] refactor(ppvm-tableau-2): name the gate modules physical_gates and virtual_gates Avoids the reserved keyword `virtual` and keeps the two names a pair. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/src/inverse_tableau/mod.rs | 8 ++++---- .../inverse_tableau/{physical.rs => physical_gates.rs} | 0 .../src/inverse_tableau/{virtual.rs => virtual_gates.rs} | 0 3 files changed, 4 insertions(+), 4 deletions(-) rename crates/ppvm-tableau-2/src/inverse_tableau/{physical.rs => physical_gates.rs} (100%) rename crates/ppvm-tableau-2/src/inverse_tableau/{virtual.rs => virtual_gates.rs} (100%) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs index 4cc7c3b3c..eaccb7685 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -9,8 +9,8 @@ //! //! - `data`: the storage, row reads, and where rows sit in the arena. //! - `bitword`: the storage word; the only code that knows its width. -//! - `physical`: physical gates `C ← G·C`, which mix rows. -//! - `virtual`: changes of basis `C ← C·V`, which rewrite columns. +//! - `physical_gates`: physical gates `C ← G·C`, which mix rows. +//! - `virtual_gates`: changes of basis `C ← C·V`, which rewrite columns. //! - `measure`: Z measurement and reset of the stabilizer state. //! //! Where rows sit in the arena is decided only by `offsets` (used by `x_at`, @@ -19,9 +19,9 @@ mod bitword; mod data; mod measure; -mod physical; +mod physical_gates; #[cfg(test)] mod test_support; -mod r#virtual; +mod virtual_gates; pub use data::{InverseTableau, RowRef}; diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/physical.rs b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs similarity index 100% rename from crates/ppvm-tableau-2/src/inverse_tableau/physical.rs rename to crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/virtual.rs b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs similarity index 100% rename from crates/ppvm-tableau-2/src/inverse_tableau/virtual.rs rename to crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs From 790c8af8bd65bc0392cc0c97a44f0dd5a6bf68b0 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 15:05:44 +0100 Subject: [PATCH 10/60] perf(ppvm-tableau-2): fuse the pivot fan-out into one pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fan_out(pivot, cx_targets, cz_targets, last) applies C ← C·V for a CX/CZ fan-out plus a final one-qubit gate in one pass over the rows, equal to the separate changes of basis. measure computes the reduced observable from row iz_q alone and collapses with a single fan-out. The d=7 surface code goes from 108 to 86 µs/shot. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/inverse_tableau/measure.rs | 36 +++-- .../src/inverse_tableau/virtual_gates.rs | 145 +++++++++++++++++- 2 files changed, 169 insertions(+), 12 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs index a864070eb..1155d4090 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs @@ -29,18 +29,32 @@ impl InverseTableau { return self.iz(q).negative; }; let targets: Vec = x_support.collect(); - for v in targets { - self.change_basis2(pivot, v, heisenberg::cnot); - } - let (has_y, negative) = (self.iz(q).z_bit(pivot), self.iz(q).negative); - if has_y { - self.change_basis1(pivot, heisenberg::s); - } - self.change_basis1(pivot, heisenberg::h); - let outcome = coin(); - if outcome != negative { - self.change_basis1(pivot, heisenberg::x); + // The CXs reduce O to s · X_p or s · Y_p (times Z's); find which, and s, + // from row iz_q alone before changing the tableau. + let row = self.iz(q); + let (mut pivot_bits, mut negative) = ([true, row.z_bit(pivot)], row.negative); + for &t in &targets { + let ([px, pz, _, _], flip) = + heisenberg::cnot([pivot_bits[0], pivot_bits[1], row.x_bit(t), row.z_bit(t)]); + pivot_bits = [px, pz]; + negative ^= flip; } + let (has_y, outcome) = (pivot_bits[1], coin()); + let fix_sign = outcome != negative; + self.fan_out(pivot, &targets, &[], |x, z| { + let ([x, z], f1) = if has_y { + heisenberg::s(x, z) + } else { + ([x, z], false) + }; + let ([x, z], f2) = heisenberg::h(x, z); + let ([x, z], f3) = if fix_sign { + heisenberg::x(x, z) + } else { + ([x, z], false) + }; + ([x, z], f1 ^ f2 ^ f3) + }); outcome } diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs index e87a13b53..ca2e9d66c 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs @@ -3,8 +3,10 @@ //! Virtual gates, i.e. changes of basis `C ← C·V`: they rewrite columns. +use ppvm_pauli_kernels::heisenberg; + use super::InverseTableau; -use super::bitword::{ZERO, bit_position, mask_if}; +use super::bitword::{BitWord, ZERO, bit_position, mask_if}; impl InverseTableau { /// Change the basis of virtual qubit `v` by the one-qubit Clifford `V` @@ -60,6 +62,93 @@ impl InverseTableau { self.signs[sw] ^= sm & mask_if(flip); } } + + /// Change basis by a pivot fan-out in one pass over the rows: + /// `C ← C·V` with `V = CX(p, t₁)⋯CX(p, t_k) · CZ(p, c₁)⋯CZ(p, c_m) · L(p)`, + /// where `L` is the one-qubit Clifford whose Heisenberg rule is `last`. + /// + /// Same result and contract as the corresponding [`change_basis2`] / + /// [`change_basis1`] calls in that order, but each row loads and stores the + /// pivot's bits and its sign once instead of once per gate. Targets must be + /// distinct and differ from the pivot. + /// + /// Used by: T-gate localization (CX only, `last` the identity), both + /// random measurement collapses (CX, CZ, then `H`/`S·H` and an `X`). + /// + /// [`change_basis1`]: Self::change_basis1 + /// [`change_basis2`]: Self::change_basis2 + pub fn fan_out( + &mut self, + pivot: usize, + cx_targets: &[usize], + cz_targets: &[usize], + last: impl Fn(bool, bool) -> ([bool; 2], bool), + ) { + debug_assert!( + cx_targets + .iter() + .chain(cz_targets) + .all(|&t| t < self.n && t != pivot) + ); + let (pw, pm) = bit_position(pivot); + let cx = cx_targets + .iter() + .map(|&t| bit_position(t)) + .collect::>(); + let cz = cz_targets + .iter() + .map(|&t| bit_position(t)) + .collect::>(); + for (r, (x, z)) in self.row_offsets().enumerate() { + let old_pivot = [ + self.words[x + pw] & pm != ZERO, + self.words[z + pw] & pm != ZERO, + ]; + let (mut pivot_bits, mut flip) = (old_pivot, false); + for &(tw, tm) in &cx { + flip ^= target_step( + &mut self.words, + [x + tw, z + tw], + tm, + &mut pivot_bits, + heisenberg::cnot, + ); + } + for &(tw, tm) in &cz { + flip ^= target_step( + &mut self.words, + [x + tw, z + tw], + tm, + &mut pivot_bits, + heisenberg::cz, + ); + } + let (new_pivot, f) = last(pivot_bits[0], pivot_bits[1]); + self.words[x + pw] ^= pm & mask_if(new_pivot[0] != old_pivot[0]); + self.words[z + pw] ^= pm & mask_if(new_pivot[1] != old_pivot[1]); + let (sw, sm) = bit_position(r); + self.signs[sw] ^= sm & mask_if(flip ^ f); + } + } +} + +/// Run a two-qubit `rule` on the pivot's bits (held in `pivot_bits`) and one +/// target's bits (at `words[at]`, bit `mask`); store the target's changed bits. +/// Returns the sign flip. +#[inline(always)] +fn target_step( + words: &mut [BitWord], + at: [usize; 2], + mask: BitWord, + pivot_bits: &mut [bool; 2], + rule: impl Fn([bool; 4]) -> ([bool; 4], bool), +) -> bool { + let old = at.map(|i| words[i] & mask != ZERO); + let ([px, pz, tx, tz], flip) = rule([pivot_bits[0], pivot_bits[1], old[0], old[1]]); + words[at[0]] ^= mask & mask_if(tx != old[0]); + words[at[1]] ^= mask & mask_if(tz != old[1]); + *pivot_bits = [px, pz]; + flip } #[cfg(test)] @@ -93,4 +182,58 @@ mod tests { assert_eq!(first, second, "n = {n}"); } } + + type Rule1 = fn(bool, bool) -> ([bool; 2], bool); + + /// The fused fan-out equals the separate changes of basis it replaces. + #[test] + fn fan_out_matches_separate_changes_of_basis() { + use ppvm_pauli_kernels::heisenberg as rules; + let lasts: [Rule1; 6] = [ + |x, z| ([x, z], false), + rules::h, + rules::s, + rules::x, + rules::sqrt_x, + rules::sqrt_y_dag, + ]; + let mut seed = 0xfa40_u64; + let mut next = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed as usize + }; + for n in SIZES.into_iter().filter(|&n| n > 1) { + for round in 0..20 { + let mut start = InverseTableau::new(n); + random_circuit(n, 60, next() as u64) + .into_iter() + .for_each(|g| apply(&mut start, g)); + random_circuit(n, 20, next() as u64) + .into_iter() + .for_each(|g| change_basis(&mut start, g)); + let pivot = next() % n; + // Each other qubit is a CX target, a CZ target, or neither. + let (mut cx, mut cz) = (Vec::new(), Vec::new()); + for t in (0..n).filter(|&t| t != pivot) { + match next() % 4 { + 0 => cx.push(t), + 1 => cz.push(t), + _ => {} + } + } + let last = lasts[round % lasts.len()]; + let mut fused = start.clone(); + fused.fan_out(pivot, &cx, &cz, last); + let mut separate = start; + cx.iter() + .for_each(|&t| separate.change_basis2(pivot, t, rules::cnot)); + cz.iter() + .for_each(|&t| separate.change_basis2(pivot, t, rules::cz)); + separate.change_basis1(pivot, last); + assert_eq!(fused, separate, "n = {n}, round {round}"); + } + } + } } From ffcf16be14038b6b4e709ae2135a3ccdb0b39e69 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 15:31:30 +0100 Subject: [PATCH 11/60] bench(ppvm-tableau-2): add microbenchmark circuits Per-operation costs at the d=7 surface code's size: per-shot overhead, physical CX, and random measure-reset with and without per-instruction batching. Seeded generator included. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../ppvm-tableau-2/benches/circuits/base.stim | 1 + .../ppvm-tableau-2/benches/circuits/cx.stim | 257 ++++ crates/ppvm-tableau-2/benches/circuits/gen.py | 32 + .../benches/circuits/mr_batched.stim | 100 ++ .../benches/circuits/mr_split.stim | 1270 +++++++++++++++++ 5 files changed, 1660 insertions(+) create mode 100644 crates/ppvm-tableau-2/benches/circuits/base.stim create mode 100644 crates/ppvm-tableau-2/benches/circuits/cx.stim create mode 100644 crates/ppvm-tableau-2/benches/circuits/gen.py create mode 100644 crates/ppvm-tableau-2/benches/circuits/mr_batched.stim create mode 100644 crates/ppvm-tableau-2/benches/circuits/mr_split.stim diff --git a/crates/ppvm-tableau-2/benches/circuits/base.stim b/crates/ppvm-tableau-2/benches/circuits/base.stim new file mode 100644 index 000000000..76a4ec815 --- /dev/null +++ b/crates/ppvm-tableau-2/benches/circuits/base.stim @@ -0,0 +1 @@ +H 0 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 diff --git a/crates/ppvm-tableau-2/benches/circuits/cx.stim b/crates/ppvm-tableau-2/benches/circuits/cx.stim new file mode 100644 index 000000000..009a35aca --- /dev/null +++ b/crates/ppvm-tableau-2/benches/circuits/cx.stim @@ -0,0 +1,257 @@ +H 0 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 +CX 17 72 108 102 97 8 32 15 63 113 57 60 83 48 100 26 12 62 3 49 55 77 0 89 107 34 29 75 13 40 99 2 +CX 3 83 69 1 112 48 87 27 54 92 117 67 28 97 56 63 70 29 44 100 86 105 58 37 2 53 71 82 12 23 80 94 +CX 15 95 42 114 92 91 64 54 111 106 85 24 38 36 75 63 109 50 103 4 61 31 116 51 53 107 22 46 70 86 47 11 +CX 56 84 65 13 99 20 66 107 50 47 62 93 3 60 5 39 90 78 75 74 109 82 21 95 64 29 1 25 69 70 92 51 +CX 65 44 108 73 45 58 34 84 70 77 93 0 49 100 94 117 16 66 99 71 26 54 7 61 46 72 109 25 64 52 62 113 +CX 53 44 0 68 69 79 100 78 42 58 76 3 102 29 81 22 70 74 23 11 101 32 4 86 9 10 2 57 1 35 31 34 +CX 14 102 79 23 44 37 8 21 20 32 67 110 84 34 82 91 112 58 89 41 63 60 117 3 39 49 43 53 24 33 13 108 +CX 115 93 65 26 77 55 104 2 28 110 50 18 4 92 20 57 90 64 86 54 69 109 80 88 66 102 96 67 83 3 107 99 +CX 73 102 41 84 80 54 7 94 38 16 27 6 39 9 104 105 109 95 20 53 72 32 108 1 71 4 75 107 97 58 21 79 +CX 65 4 48 25 44 12 26 73 86 55 75 24 63 13 85 49 37 64 105 2 41 78 51 36 98 20 114 97 72 17 43 54 +CX 27 34 86 12 107 48 70 44 113 87 68 62 98 109 30 8 92 5 10 17 21 97 104 117 116 42 76 64 32 47 43 108 +CX 14 37 30 111 77 99 91 62 17 74 70 98 13 41 5 52 9 48 18 16 43 117 78 75 100 101 73 107 28 72 10 34 +CX 46 114 37 72 68 14 58 35 13 100 5 105 115 1 78 85 104 11 52 112 107 24 30 75 53 20 98 57 21 87 95 92 +CX 95 108 13 55 48 103 69 104 37 70 32 91 61 40 12 26 83 110 5 3 1 109 92 76 100 57 50 93 51 8 88 90 +CX 76 58 14 32 27 100 79 99 69 88 60 84 45 33 23 109 26 39 25 31 46 10 35 11 57 94 83 73 82 43 29 49 +CX 39 5 41 23 40 101 108 74 38 31 42 12 69 78 110 76 11 111 28 2 100 51 9 34 70 95 92 98 81 1 37 45 +CX 63 60 110 109 19 12 64 99 101 41 9 65 85 22 104 115 113 18 40 39 13 90 106 77 37 16 26 100 69 4 102 79 +CX 102 86 70 107 95 88 26 22 38 55 68 20 6 91 85 31 32 99 8 87 57 108 115 101 69 56 114 58 1 50 43 21 +CX 33 62 3 101 82 53 73 2 7 88 45 74 17 75 16 105 117 35 50 72 51 22 78 11 29 116 0 96 67 40 64 83 +CX 117 56 87 81 93 28 30 40 63 115 61 112 91 52 43 71 78 113 83 35 82 106 6 9 65 97 47 20 100 26 39 38 +CX 88 38 108 70 47 21 89 111 94 59 76 10 15 77 65 73 48 22 19 32 54 27 72 92 6 63 87 50 81 44 49 103 +CX 108 21 69 93 5 67 11 103 32 80 12 34 94 10 17 99 78 84 87 89 104 56 30 48 55 50 116 41 96 16 79 62 +CX 27 15 55 76 68 52 116 84 37 35 31 48 95 71 0 24 67 56 74 2 3 80 77 107 33 26 22 36 18 69 25 34 +CX 39 74 96 32 106 87 57 101 103 21 69 45 62 53 15 98 26 73 49 110 36 13 3 109 72 1 107 37 86 83 17 9 +CX 64 47 73 103 39 55 117 86 45 97 67 41 0 15 56 91 57 44 113 69 51 43 93 87 115 63 14 82 48 89 26 71 +CX 0 35 81 76 92 112 94 106 93 65 25 59 114 66 52 95 91 39 89 21 57 79 85 67 107 46 111 117 86 49 74 54 +CX 51 43 110 79 74 93 89 95 8 63 115 31 81 83 37 80 2 52 92 102 19 105 50 34 22 9 77 1 44 33 100 69 +CX 38 19 59 106 33 62 21 115 65 5 34 109 12 95 75 54 8 45 101 84 56 2 111 64 90 20 88 11 51 81 35 77 +CX 38 26 67 116 30 42 34 8 9 89 106 66 84 47 59 65 71 94 6 21 117 83 100 91 101 111 45 78 29 50 93 51 +CX 22 61 101 33 110 78 42 91 28 114 112 90 31 84 3 79 51 40 55 97 105 34 24 9 80 21 74 56 113 18 77 108 +CX 58 67 20 17 99 114 91 56 46 39 96 51 30 14 111 26 103 87 108 8 13 29 50 41 63 12 23 5 7 76 2 27 +CX 87 4 63 90 67 104 92 78 56 43 84 35 15 110 88 22 12 28 51 29 115 57 48 21 98 30 36 59 70 74 49 27 +CX 57 91 33 42 63 75 14 27 10 5 1 102 0 61 40 49 74 36 25 51 20 82 19 3 107 106 18 85 69 7 72 48 +CX 32 16 10 59 83 107 38 1 4 68 7 67 116 5 35 99 15 55 11 24 3 63 81 105 103 87 98 84 57 49 42 80 +CX 34 33 82 81 31 113 7 75 100 110 22 44 54 77 89 71 114 66 111 45 70 52 68 25 91 95 105 84 8 117 78 9 +CX 32 22 12 19 7 26 109 54 111 5 6 81 11 104 65 60 64 47 115 40 108 16 68 4 56 85 96 50 57 3 67 34 +CX 11 32 102 41 10 38 4 110 49 7 93 33 40 94 16 106 101 48 14 86 112 12 54 31 64 71 26 42 43 65 50 74 +CX 61 13 16 83 104 57 67 71 92 108 106 74 89 66 68 3 37 95 20 25 47 49 113 41 12 52 44 115 73 8 5 38 +CX 104 102 83 68 40 53 38 113 45 34 41 95 106 66 64 1 67 15 19 110 93 107 96 73 8 57 35 61 58 46 48 10 +CX 74 102 7 17 6 67 62 73 109 32 100 31 89 110 95 43 46 82 47 51 39 59 76 116 68 64 21 3 18 108 87 28 +CX 72 17 14 23 98 52 93 79 6 103 12 69 87 34 91 13 26 33 8 80 73 67 82 10 9 27 95 22 65 55 2 75 +CX 47 115 108 62 90 103 36 28 25 76 63 30 54 57 86 46 69 24 61 92 9 32 52 109 1 68 48 65 114 97 51 78 +CX 112 65 101 74 114 54 5 45 108 58 0 24 38 89 88 82 107 69 15 105 116 95 40 100 102 73 70 36 67 52 94 66 +CX 52 77 80 74 39 57 38 16 64 56 75 17 70 98 20 32 81 1 54 94 84 72 4 47 53 51 36 97 85 2 11 87 +CX 108 0 49 34 59 114 101 100 47 81 95 61 98 43 115 58 14 106 45 18 53 105 2 22 33 109 16 75 36 52 93 65 +CX 36 94 53 88 35 55 42 99 62 27 91 106 109 51 107 54 11 8 16 26 19 29 93 3 13 32 97 61 12 104 83 23 +CX 106 0 11 54 78 6 70 27 68 114 44 112 83 13 94 111 86 53 85 103 15 33 87 35 22 61 90 117 110 101 82 115 +CX 110 49 15 85 57 37 87 65 63 50 14 77 61 13 19 116 78 89 25 21 66 32 53 68 36 109 81 69 27 79 43 62 +CX 13 1 96 93 84 44 90 34 7 69 80 56 38 97 12 29 65 35 110 111 31 52 18 16 32 24 115 71 107 76 109 68 +CX 106 77 65 19 52 34 35 61 89 39 112 62 27 63 47 76 60 30 43 22 116 23 94 74 88 57 68 114 7 64 41 67 +CX 88 17 82 97 103 27 40 79 63 61 42 15 16 116 89 32 28 11 81 68 113 6 72 22 87 14 101 95 25 64 90 84 +CX 113 39 54 41 0 99 2 105 116 110 78 28 10 95 106 35 87 80 43 34 76 92 66 48 111 15 42 44 17 14 32 18 +CX 87 73 5 44 9 11 92 13 38 40 31 34 67 6 46 3 10 17 51 47 111 81 88 30 12 86 42 35 1 65 41 14 +CX 45 102 101 82 92 107 16 77 34 51 11 86 73 79 113 67 60 72 53 68 50 38 28 80 96 70 17 6 76 65 14 22 +CX 30 27 114 55 35 69 2 32 68 34 67 33 60 16 51 90 13 95 47 8 83 112 46 96 71 92 64 87 74 3 79 39 +CX 57 87 16 19 9 74 18 86 105 27 61 102 98 42 46 37 20 114 48 56 51 15 76 111 34 106 85 116 81 77 1 68 +CX 1 104 82 16 48 95 71 12 58 3 99 55 76 86 54 35 47 52 51 77 59 6 110 60 4 115 90 89 0 5 14 75 +CX 17 67 65 97 45 70 34 100 72 83 113 102 60 104 89 31 79 30 13 71 107 20 14 5 90 40 54 44 32 84 80 7 +CX 78 55 53 48 45 37 96 104 43 56 102 89 30 81 117 66 18 7 109 86 14 65 22 69 82 80 62 99 15 74 2 61 +CX 114 26 49 80 106 22 50 91 29 12 31 42 113 84 107 100 86 59 95 60 47 63 83 104 92 24 55 56 51 69 15 73 +CX 62 34 107 16 19 1 48 53 13 102 3 83 9 23 58 98 111 85 64 36 113 97 67 109 32 2 59 50 81 29 68 90 +CX 0 69 102 31 54 20 84 22 43 111 30 9 99 68 71 112 110 48 74 2 65 27 113 107 5 66 24 89 64 88 78 83 +CX 68 9 31 50 99 59 15 72 82 6 49 11 71 12 109 61 5 66 30 1 2 39 112 35 92 53 21 76 17 105 40 117 +CX 81 57 64 102 53 70 21 89 50 110 49 103 25 63 35 46 19 33 72 106 22 92 79 10 93 114 43 18 100 32 88 44 +CX 49 35 72 59 1 19 16 32 28 25 9 102 74 68 79 108 69 54 91 30 73 17 70 58 50 99 106 10 80 107 112 85 +CX 7 3 95 51 48 53 87 17 75 76 16 86 68 69 9 30 113 110 36 25 84 92 50 45 22 28 38 90 18 44 62 105 +CX 37 11 65 105 38 26 90 59 2 117 102 103 79 75 13 78 47 96 56 32 114 7 6 40 20 16 80 106 14 55 81 104 +CX 31 95 26 64 114 50 15 90 27 104 49 84 66 17 91 74 32 92 0 103 111 25 72 48 106 61 69 78 29 34 4 81 +CX 21 85 116 70 64 29 109 52 35 98 84 53 51 34 63 12 115 16 23 71 2 58 5 62 27 50 68 43 31 102 9 86 +CX 95 5 108 54 106 56 24 22 76 64 111 65 49 66 46 25 29 103 84 75 96 8 43 6 58 116 78 110 18 36 60 92 +CX 74 64 8 110 106 72 50 11 51 101 65 73 82 38 111 34 45 60 6 70 61 2 54 104 75 40 19 76 93 71 35 115 +CX 110 77 100 101 99 46 53 50 66 114 3 73 74 14 4 106 67 1 12 42 43 47 70 103 81 96 105 9 62 93 10 69 +CX 57 42 64 103 69 0 20 41 46 27 18 74 107 75 13 51 40 65 53 109 43 33 77 47 4 91 8 80 31 96 50 70 +CX 36 73 100 79 10 9 90 21 34 52 113 16 117 70 92 82 33 30 26 12 35 103 61 6 65 38 99 69 112 104 40 43 +CX 37 110 66 17 4 56 104 46 102 95 113 3 40 53 108 20 71 5 90 75 89 84 80 67 54 23 25 29 14 98 16 88 +CX 64 15 92 34 58 25 100 7 46 113 42 78 115 45 28 81 1 101 62 4 21 32 70 5 111 29 10 67 22 98 90 112 +CX 26 56 36 31 62 64 47 41 50 83 9 24 76 23 106 87 79 38 74 54 78 60 46 2 113 94 13 84 80 73 85 101 +CX 108 55 105 90 74 43 112 9 82 53 24 89 65 102 63 77 72 84 70 64 61 76 87 94 73 57 104 60 21 34 86 67 +CX 38 72 97 103 50 77 69 33 32 39 1 112 96 5 100 58 102 45 29 65 56 26 89 60 42 95 80 18 49 55 6 82 +CX 14 45 111 100 1 32 96 69 94 6 39 48 113 41 43 107 75 114 108 26 91 10 42 15 85 82 8 16 88 37 52 77 +CX 43 29 3 82 89 88 23 96 98 110 64 95 73 114 46 38 37 48 53 67 59 9 25 52 116 77 5 79 30 80 28 31 +CX 91 50 48 26 79 19 92 38 95 111 46 0 117 90 87 39 56 63 21 86 18 3 47 55 70 43 65 62 40 77 14 74 +CX 82 37 103 70 84 35 54 1 107 39 96 11 81 62 14 64 28 77 95 117 99 33 55 47 29 6 13 76 65 89 88 20 +CX 16 37 6 113 8 27 0 86 7 54 93 91 2 114 109 1 4 68 43 42 105 78 102 71 112 60 25 34 116 74 70 66 +CX 32 113 29 23 26 50 7 30 71 89 57 4 42 41 52 15 2 72 114 64 81 11 99 27 28 22 38 12 111 40 18 8 +CX 106 56 19 29 5 95 36 44 7 75 11 116 25 101 114 85 23 15 109 105 6 112 93 14 107 28 111 32 67 54 31 4 +CX 92 32 97 24 41 44 45 58 115 84 78 48 86 49 11 54 31 62 43 22 77 83 14 30 9 55 35 68 38 42 47 52 +CX 58 46 45 40 50 60 65 2 47 16 38 21 107 72 108 70 91 93 19 106 117 82 80 99 17 20 10 78 32 30 115 96 +CX 40 21 35 109 60 39 9 54 19 70 45 57 13 114 87 117 8 103 23 61 68 4 5 92 24 83 107 46 64 91 89 80 +CX 103 85 47 43 83 15 23 48 4 34 78 91 102 26 7 31 38 41 72 51 105 46 6 29 37 89 99 0 25 12 17 28 +CX 47 64 113 34 18 20 29 9 39 73 65 107 69 76 105 101 55 56 74 106 60 23 98 45 25 102 110 35 26 111 115 17 +CX 98 26 2 20 62 46 23 6 100 112 10 78 30 86 89 27 11 56 82 83 25 77 43 21 73 88 85 90 115 102 40 61 +CX 70 4 115 6 109 46 63 71 44 17 62 8 65 40 85 94 72 103 39 77 104 73 11 61 43 53 9 33 106 84 82 41 +CX 2 23 41 28 40 33 106 104 32 39 62 53 1 37 20 81 110 6 14 55 98 78 27 35 45 83 72 63 73 36 77 109 +CX 86 22 41 18 45 12 50 113 66 95 72 89 24 111 57 19 61 106 31 4 93 81 99 10 9 98 109 64 60 107 101 115 +CX 117 66 101 21 72 90 63 50 1 49 70 92 71 94 57 114 75 115 47 6 106 99 45 56 30 88 82 84 69 38 11 104 +CX 97 111 45 24 20 17 56 106 5 46 72 43 103 22 107 62 61 1 73 29 78 7 116 83 113 65 26 51 59 15 40 33 +CX 17 21 42 16 23 103 94 79 67 39 29 70 90 54 59 58 65 106 108 116 66 78 64 99 75 26 36 86 19 87 0 43 +CX 15 54 48 91 83 65 94 22 79 56 57 104 68 108 46 26 6 10 92 13 12 105 49 17 106 50 23 60 107 66 75 4 +CX 75 24 117 57 62 49 37 44 99 96 22 76 34 23 109 3 71 7 85 8 70 29 114 40 56 42 13 112 6 59 35 52 +CX 59 42 64 12 21 51 69 110 54 78 94 61 115 19 40 18 44 17 108 24 28 27 58 83 104 13 89 92 109 6 95 93 +CX 47 71 41 35 50 1 49 62 91 57 38 94 109 107 82 74 111 40 96 37 22 12 110 23 108 19 58 13 68 15 89 100 +CX 40 105 63 86 71 81 43 92 74 117 113 75 102 59 41 62 88 50 68 27 21 30 99 25 76 31 6 103 79 7 42 53 +CX 3 44 46 115 76 113 84 52 26 100 36 28 40 50 89 49 85 99 22 1 102 83 116 77 78 106 29 8 93 105 97 109 +CX 90 111 37 12 55 0 103 44 11 116 52 19 14 68 101 93 22 96 43 18 48 113 41 69 81 89 66 35 26 24 20 21 +CX 68 20 18 15 56 74 66 16 55 17 42 77 101 93 89 87 40 76 108 2 45 22 28 30 88 63 75 62 4 83 11 99 +CX 68 60 72 18 26 46 90 17 35 94 44 8 49 116 3 67 59 25 92 30 113 88 0 99 89 38 5 34 66 24 9 13 +CX 104 102 98 109 14 110 51 42 13 57 91 73 66 90 83 61 85 35 18 55 47 82 44 49 52 115 97 70 26 25 8 99 +CX 30 117 2 116 85 50 58 100 78 56 72 12 6 22 67 0 5 55 35 53 16 114 88 84 47 98 43 74 105 64 111 97 +CX 88 66 46 74 7 44 15 107 31 81 108 111 55 19 103 2 115 16 104 36 3 60 110 97 61 8 75 105 11 96 69 77 +CX 64 12 16 68 108 86 90 50 82 77 69 52 30 66 48 61 93 40 56 14 8 26 75 78 89 47 13 116 45 91 25 98 +CX 88 83 75 11 0 65 55 30 114 39 62 78 7 73 54 71 38 50 80 5 85 76 3 35 79 61 56 28 34 41 92 91 +CX 68 7 34 65 22 95 90 56 58 37 74 75 23 41 114 84 50 97 85 88 52 87 71 76 101 61 80 28 39 2 8 18 +CX 63 105 114 14 116 46 33 108 107 39 101 69 38 17 13 64 104 58 4 57 60 93 72 41 106 47 16 1 68 25 34 79 +CX 103 8 100 59 36 1 82 34 92 64 88 2 72 51 14 12 87 41 77 79 81 89 107 73 57 11 78 63 67 43 75 86 +CX 5 24 21 7 78 14 103 117 15 71 67 39 96 100 25 20 68 19 29 27 11 64 45 88 72 55 34 113 17 36 73 31 +CX 8 109 76 33 7 2 55 78 36 107 60 54 111 103 117 23 27 98 86 4 81 96 106 53 45 93 65 18 22 28 29 113 +CX 46 8 111 57 41 104 27 28 32 19 88 90 66 48 13 61 87 93 78 0 60 39 33 89 37 26 16 107 81 112 84 4 +CX 100 98 48 58 68 110 3 16 29 63 82 12 37 89 79 55 25 66 42 106 31 97 62 73 14 22 108 45 107 76 81 103 +CX 55 51 70 53 107 96 3 80 116 18 54 16 7 37 49 78 117 81 12 25 76 34 61 97 113 33 65 13 41 19 71 68 +CX 100 33 86 115 3 71 84 93 12 98 47 58 116 96 109 36 17 10 52 90 48 113 61 74 92 16 112 50 62 29 65 104 +CX 48 102 7 52 76 10 31 86 5 58 112 37 78 109 44 98 104 8 9 101 74 39 45 96 11 68 60 79 95 41 21 80 +CX 45 112 67 31 41 77 29 114 81 98 89 27 39 105 68 113 91 38 75 0 84 61 32 97 111 18 30 20 10 33 51 25 +CX 17 21 104 70 101 109 107 79 9 40 49 90 27 20 4 57 105 51 14 89 39 28 92 81 37 65 83 102 43 10 8 112 +CX 105 29 15 67 59 91 117 71 58 1 76 21 109 55 69 14 24 108 31 39 27 66 77 37 98 33 44 34 36 6 3 100 +CX 80 99 109 56 5 26 9 40 57 84 38 14 31 86 106 24 3 102 81 17 79 76 87 85 101 114 93 71 29 60 22 68 +CX 117 1 28 17 8 2 111 114 41 73 10 66 69 33 24 50 116 105 35 45 104 100 49 51 67 93 68 59 99 11 23 61 +CX 72 50 17 79 26 67 3 66 6 108 40 18 28 107 116 5 52 93 75 60 64 8 88 4 16 71 101 69 49 90 34 99 +CX 5 27 24 38 90 48 114 66 2 72 34 115 68 110 94 105 20 29 11 26 61 21 6 84 51 36 1 19 12 117 75 55 +CX 60 22 104 27 72 59 103 84 13 110 51 28 8 16 43 64 117 63 65 108 47 55 74 31 56 33 107 45 49 113 29 48 +CX 115 79 110 13 23 87 111 76 80 103 44 9 3 53 75 63 7 97 58 14 109 82 29 99 107 65 11 42 86 4 35 108 +CX 67 98 78 108 42 16 73 21 55 87 39 91 56 93 31 62 95 49 3 64 32 15 36 33 99 72 10 41 80 65 84 23 +CX 28 37 94 10 21 58 47 51 81 104 99 57 103 86 60 85 84 13 72 62 107 114 102 4 7 2 35 115 34 39 22 68 +CX 61 79 91 87 43 2 58 113 30 108 28 44 94 115 7 112 56 65 25 50 19 22 29 10 98 5 96 40 0 111 68 116 +CX 67 20 4 107 54 28 104 32 86 66 56 24 5 77 91 48 52 51 65 113 34 114 43 72 3 10 60 53 116 55 89 69 +CX 65 99 64 93 115 79 22 34 52 95 61 36 44 90 58 50 70 48 106 30 45 69 96 91 104 67 28 33 2 84 9 114 +CX 90 112 49 20 33 98 102 75 32 62 2 114 105 61 14 28 19 103 115 7 22 8 12 59 70 83 94 3 116 34 6 67 +CX 60 113 94 82 26 45 76 56 14 43 40 48 83 49 37 10 29 92 110 71 44 54 55 90 93 95 75 34 23 19 6 42 +CX 45 48 8 80 75 101 40 73 22 18 97 92 114 14 68 26 61 90 29 117 78 67 105 91 20 102 38 21 17 82 51 54 +CX 62 44 90 99 4 68 9 3 47 31 19 27 50 56 65 75 34 54 76 43 61 98 10 102 78 7 17 71 60 22 11 1 +CX 8 3 23 35 24 92 58 51 91 69 65 34 89 87 33 71 49 13 90 50 59 30 9 93 94 40 17 104 77 116 80 48 +CX 81 7 37 44 98 84 110 2 88 79 56 40 74 1 97 68 106 93 50 109 103 100 6 105 87 57 96 113 83 12 54 51 +CX 94 15 109 72 112 2 1 99 71 76 52 97 44 22 51 117 5 18 36 66 89 78 107 82 21 114 60 37 74 108 32 86 +CX 4 99 112 50 69 75 52 18 41 21 58 114 73 71 84 16 64 82 10 77 115 78 106 33 95 62 117 80 37 20 81 34 +CX 49 35 15 32 1 115 106 85 101 107 13 59 19 111 30 100 103 5 28 10 108 12 93 4 74 84 14 102 114 53 18 44 +CX 14 6 107 102 49 101 78 79 105 28 20 68 73 62 21 45 77 50 65 72 85 103 41 67 9 80 116 1 109 38 12 57 +CX 11 0 111 84 6 94 35 70 39 75 79 97 100 32 58 49 15 82 28 109 105 85 16 65 64 3 46 90 57 12 55 86 +CX 19 35 14 47 32 97 105 27 42 79 18 71 28 78 0 29 90 61 45 82 16 52 85 43 54 108 56 115 113 6 67 37 +CX 91 65 40 25 26 29 107 94 30 48 44 32 0 62 98 64 17 54 97 61 11 66 35 12 28 13 100 51 18 14 85 56 +CX 105 66 85 102 27 20 113 34 46 107 90 41 44 95 32 72 96 19 3 28 103 61 76 68 2 43 93 22 89 25 33 82 +CX 116 29 9 54 88 47 113 112 96 104 98 24 13 0 50 43 73 42 87 52 102 75 111 32 51 78 35 45 92 115 55 28 +CX 78 60 112 44 98 36 91 3 13 76 67 6 21 79 96 28 113 68 56 37 54 51 104 0 8 50 19 75 26 116 85 92 +CX 63 12 52 100 82 21 91 84 117 27 83 39 71 99 4 38 37 114 17 32 113 65 102 60 104 55 42 66 41 26 35 5 +CX 39 64 110 73 37 63 38 33 20 113 115 102 42 19 107 49 86 56 84 112 92 21 106 5 11 74 26 40 6 66 117 94 +CX 53 101 13 79 80 89 41 16 1 44 30 114 45 66 55 91 107 104 10 4 42 2 83 56 3 21 35 85 78 26 54 37 +CX 80 21 5 4 63 49 111 69 87 84 14 48 36 55 6 29 42 53 75 73 62 76 26 74 65 86 11 43 81 51 83 22 +CX 99 30 66 106 62 110 9 102 81 53 86 50 27 32 117 0 36 4 33 10 23 79 104 57 88 55 39 12 38 6 61 22 +CX 104 32 70 26 16 5 86 51 115 1 72 64 38 0 93 48 43 12 116 20 76 89 25 9 22 96 87 73 82 102 67 107 +CX 92 2 28 51 87 116 85 0 67 53 97 78 102 98 22 6 94 99 50 83 108 24 20 115 11 106 57 69 90 42 111 113 +CX 32 24 64 76 117 105 49 31 85 37 79 113 91 18 112 80 34 46 74 92 35 115 82 28 116 68 84 2 13 27 97 21 +CX 94 41 28 20 83 82 4 77 115 49 33 32 27 81 107 48 5 98 111 19 92 63 55 38 46 51 45 79 25 36 35 103 +CX 61 79 19 100 74 45 18 49 7 9 33 108 99 63 26 58 39 5 34 43 0 88 86 78 104 55 92 54 46 77 62 24 +CX 94 55 110 50 37 12 10 93 20 90 43 46 72 54 108 48 15 102 6 116 77 25 14 29 88 61 49 22 87 17 28 79 +CX 12 88 44 41 64 56 103 98 21 48 80 61 72 23 4 113 25 87 31 97 16 15 35 71 1 0 47 36 27 6 39 84 +CX 86 18 16 8 91 20 75 53 34 97 103 115 9 24 108 112 77 110 27 50 69 62 22 101 114 63 31 26 105 87 19 113 +CX 115 24 78 117 89 18 70 33 8 98 96 73 47 10 45 67 35 22 84 72 60 55 71 106 90 95 28 16 111 75 15 107 +CX 94 54 47 28 56 86 71 50 42 85 72 23 6 98 105 115 79 59 20 84 92 100 73 102 45 19 58 25 69 61 68 36 +CX 76 26 16 77 28 36 13 106 85 11 83 29 54 65 27 109 38 63 91 7 48 116 81 89 6 39 101 79 96 105 1 58 +CX 41 53 31 82 13 21 64 6 94 48 20 106 2 65 63 62 47 70 54 3 114 76 85 55 49 30 66 1 71 80 5 83 +CX 113 25 82 107 40 4 108 17 56 114 89 22 57 18 104 32 64 48 88 11 65 15 9 111 49 61 46 26 112 67 52 28 +CX 61 25 21 30 116 67 85 43 82 89 38 117 105 102 98 78 73 64 114 97 27 36 15 72 1 4 79 40 11 112 83 22 +CX 58 69 10 52 92 17 104 116 5 101 91 112 40 85 95 45 57 103 24 51 59 115 50 46 1 36 27 74 102 47 80 0 +CX 12 57 101 107 97 51 39 89 22 38 94 83 28 95 42 103 27 5 6 2 90 23 76 59 41 91 4 34 75 67 113 8 +CX 91 81 31 112 110 1 16 96 52 44 34 103 63 4 49 14 39 88 53 113 115 29 66 74 55 72 33 114 0 19 60 100 +CX 113 117 44 9 76 108 31 71 82 70 19 16 51 17 78 88 93 42 24 106 104 77 102 14 98 4 116 35 34 45 0 18 +CX 89 106 1 8 102 58 91 55 111 50 39 87 17 108 54 96 46 100 57 44 37 60 23 34 115 107 29 88 6 63 7 0 +CX 10 76 59 0 100 81 31 16 91 50 103 94 78 49 28 75 79 35 23 92 27 20 11 42 44 95 15 71 29 26 106 90 +CX 68 57 101 99 90 10 55 73 45 21 23 79 13 109 107 87 63 78 112 58 111 27 8 95 85 81 32 42 50 47 74 41 +CX 55 66 79 76 83 9 24 52 44 67 94 63 109 88 15 57 42 1 28 38 110 85 86 17 111 35 90 65 78 5 20 74 +CX 39 7 84 14 34 81 75 114 23 90 112 71 57 72 31 59 55 6 18 63 46 64 37 48 11 104 101 16 87 65 29 54 +CX 62 8 75 92 108 46 68 66 23 7 90 26 24 2 44 30 102 107 67 99 83 52 71 53 21 101 0 28 65 111 113 97 +CX 19 110 70 11 102 3 18 69 34 29 46 42 93 17 12 32 52 45 76 5 115 7 86 58 4 81 40 39 38 95 49 90 +CX 101 49 61 37 86 15 74 87 83 1 91 13 54 9 26 112 80 108 31 60 104 27 43 96 114 36 59 107 89 71 73 65 +CX 26 89 59 48 10 93 3 9 38 92 79 57 117 108 37 53 23 82 77 85 50 114 112 115 28 31 63 1 103 34 60 91 +CX 114 109 45 96 14 75 92 88 85 117 15 27 110 57 49 106 53 7 93 22 97 89 87 103 54 47 67 18 8 65 20 5 +CX 74 99 27 97 64 69 92 59 83 39 38 60 17 2 90 57 55 85 86 117 44 101 91 46 115 34 26 100 110 73 106 111 +CX 87 79 53 36 35 111 56 8 14 41 57 117 86 114 66 29 65 40 31 19 20 32 90 99 52 3 105 54 50 28 17 9 +CX 10 21 59 77 93 48 101 111 28 37 114 50 34 1 36 19 15 94 55 103 100 85 38 88 53 70 7 84 87 102 12 116 +CX 66 62 104 55 14 7 45 82 40 39 4 108 102 57 107 44 36 71 80 27 33 95 35 21 101 65 43 100 11 103 16 87 +CX 49 41 43 106 102 60 21 37 3 96 33 2 69 81 78 72 114 53 105 59 109 79 65 117 13 15 73 113 101 94 11 63 +CX 110 27 45 74 4 105 52 60 69 40 24 1 16 117 104 32 55 80 65 12 53 57 70 66 37 11 6 54 19 44 25 10 +CX 58 46 14 75 77 42 102 84 12 26 113 40 20 105 112 98 10 108 95 37 71 74 8 62 72 59 64 56 49 116 66 81 +CX 67 60 103 22 107 86 106 18 0 114 39 108 83 81 19 26 99 17 88 31 58 100 10 62 64 70 49 50 78 104 54 69 +CX 82 64 87 115 55 60 35 61 16 85 84 25 48 4 111 78 100 17 56 27 19 49 99 80 86 7 44 90 28 108 97 36 +CX 75 99 87 72 116 62 42 19 102 76 81 92 8 84 79 50 85 9 100 0 3 101 113 11 16 70 32 7 26 55 43 35 +CX 84 115 45 25 105 87 20 52 10 116 14 54 57 42 64 13 1 117 7 18 110 76 91 27 114 8 85 111 58 65 3 41 +CX 89 102 79 37 84 39 100 19 57 6 5 36 20 97 3 115 40 103 18 32 13 29 98 83 73 63 92 24 9 16 106 111 +CX 82 107 30 20 98 86 22 31 75 78 58 13 0 104 25 72 46 83 81 21 34 12 10 38 29 48 94 69 17 91 89 88 +CX 105 68 15 38 66 12 98 97 26 56 51 81 96 112 3 50 61 82 0 37 88 60 117 47 23 109 101 70 25 113 116 100 +CX 28 17 55 89 95 25 91 46 62 29 6 31 14 110 8 114 5 27 115 42 94 52 57 56 58 84 76 78 83 82 113 47 +CX 5 26 87 33 16 105 66 97 94 13 100 110 53 25 41 108 0 109 82 28 104 91 49 24 38 39 84 47 29 3 86 31 +CX 76 35 39 23 102 85 89 14 1 44 93 19 88 72 96 49 62 58 99 87 110 28 117 108 7 10 30 21 25 54 17 48 +CX 51 75 45 10 4 109 70 58 102 108 105 73 115 36 89 47 95 100 42 112 1 14 49 38 34 7 85 65 61 35 6 62 +CX 40 106 90 54 94 58 74 71 107 65 28 21 108 5 51 32 24 117 25 113 14 78 116 53 98 72 102 77 16 11 33 30 +CX 89 35 73 111 23 58 105 54 2 18 39 65 17 108 52 4 59 64 95 63 66 92 5 49 13 88 37 103 101 93 83 68 +CX 54 103 110 52 3 115 33 76 7 37 111 41 64 113 17 69 109 27 40 94 12 20 38 114 66 18 10 67 63 93 8 117 +CX 46 80 106 19 100 58 51 78 41 87 36 63 71 9 67 17 2 116 11 26 77 76 89 117 61 65 18 22 95 16 54 5 +CX 9 45 81 94 89 102 39 87 32 92 46 47 111 52 51 60 56 106 41 79 113 104 19 16 97 61 109 115 88 91 54 73 +CX 60 77 6 37 67 103 63 44 111 16 57 18 82 89 117 19 29 42 99 8 87 70 72 75 46 21 53 80 52 38 34 28 +CX 78 1 109 83 61 45 10 106 33 62 92 50 58 104 5 54 32 108 67 19 41 18 26 94 48 15 14 97 96 60 80 66 +CX 102 57 110 67 19 63 107 113 108 7 24 90 51 42 82 94 33 62 37 4 55 10 27 3 73 40 105 99 31 38 97 44 +CX 110 56 114 41 105 87 95 35 15 86 47 4 113 36 26 92 75 13 77 93 33 2 97 42 14 51 58 37 69 117 107 18 +CX 63 7 13 48 113 56 40 0 107 59 80 30 17 111 19 26 62 21 36 44 28 82 89 85 102 78 116 77 58 35 94 86 +CX 59 117 60 46 54 113 8 28 44 7 41 13 85 49 69 91 88 63 42 18 25 111 34 100 62 116 40 92 103 10 94 76 +CX 35 93 69 107 82 14 90 73 103 104 16 53 11 95 18 48 12 54 116 71 27 109 0 98 86 79 112 101 10 113 42 15 +CX 86 74 71 82 47 53 87 50 9 52 105 114 57 38 95 99 55 56 110 79 69 77 17 58 96 61 49 13 42 35 80 18 +CX 33 84 5 12 99 21 93 116 4 0 41 43 115 20 53 56 79 2 55 70 72 51 15 86 50 7 3 17 30 64 62 83 +CX 49 40 16 36 78 17 72 111 109 112 70 18 97 106 75 30 92 25 3 27 62 71 58 46 60 81 54 67 0 53 102 48 +CX 106 109 34 79 3 98 67 116 41 13 103 65 105 99 31 54 32 16 66 59 72 70 47 101 15 11 91 44 51 56 46 53 +CX 117 86 64 56 63 48 68 3 107 6 105 62 41 97 29 13 2 44 9 91 22 88 60 15 103 18 58 99 21 80 35 95 +CX 115 88 46 23 29 5 37 83 56 69 70 58 76 36 31 1 38 60 19 25 98 114 39 9 32 21 50 80 75 101 96 106 +CX 113 50 87 11 104 100 45 77 111 29 0 101 69 79 21 58 117 16 36 49 19 43 96 32 107 18 24 94 27 80 93 7 +CX 82 4 109 58 36 28 67 96 117 14 95 9 77 19 25 22 3 55 20 21 56 41 6 57 79 64 45 90 113 74 16 69 +CX 112 115 78 66 6 31 14 57 62 29 65 40 111 85 39 19 34 81 53 33 105 2 0 47 64 23 7 41 20 5 4 3 +CX 33 31 3 92 37 61 114 111 76 82 63 43 75 11 25 22 44 91 95 104 47 18 46 24 59 48 57 101 78 105 10 116 +CX 29 12 30 11 93 39 66 47 10 15 75 106 76 46 31 71 36 99 80 112 108 21 91 52 27 59 17 25 109 55 9 48 +CX 19 73 31 38 32 77 65 92 116 61 49 15 13 84 46 58 108 104 50 27 75 42 117 80 34 41 81 25 76 5 100 51 +CX 34 41 103 13 80 16 26 47 60 113 116 72 5 7 90 58 17 102 63 65 96 27 35 32 78 19 36 15 40 28 43 89 +CX 25 97 15 109 53 27 113 99 17 65 104 68 11 49 3 107 114 50 44 79 116 66 100 77 18 84 105 87 96 29 117 62 +CX 60 48 98 91 40 32 71 1 67 96 109 89 94 93 33 3 116 105 66 50 18 21 78 103 12 52 14 90 61 51 6 62 +CX 10 72 117 28 43 49 78 86 84 106 113 108 45 91 85 96 32 30 52 17 102 114 87 51 79 5 31 24 116 18 89 107 +CX 23 13 115 98 26 53 6 65 42 68 96 91 46 44 84 90 55 29 104 48 9 28 59 16 45 80 47 11 95 110 112 114 +CX 99 33 9 58 59 85 19 116 90 88 1 100 67 46 54 30 44 109 113 42 56 66 107 18 91 102 108 12 28 34 104 89 +CX 108 72 23 56 45 47 106 89 4 110 85 115 49 68 113 44 84 70 73 52 38 65 33 58 10 6 13 71 34 39 27 54 +CX 71 74 101 111 25 31 35 11 53 50 87 64 36 37 77 62 110 52 28 89 113 61 15 65 45 72 69 10 4 90 41 112 +CX 110 56 11 24 113 4 116 16 10 87 60 8 101 6 18 63 117 88 102 55 61 37 1 77 32 69 81 15 76 115 80 68 +CX 71 106 79 46 4 29 69 85 117 19 2 96 48 5 16 42 110 49 109 9 77 57 39 41 26 54 61 8 105 32 0 83 +CX 84 80 111 13 38 93 104 28 72 73 48 1 50 9 83 75 11 31 8 53 67 57 70 39 64 4 18 63 62 79 47 19 +CX 70 14 26 34 69 100 1 63 80 84 75 57 18 105 40 51 56 114 77 58 107 47 90 50 86 79 73 10 2 27 117 28 +CX 31 78 86 44 55 71 97 12 10 3 99 29 82 42 30 92 25 67 20 70 109 65 107 7 38 81 32 75 87 104 114 54 +CX 47 98 26 0 78 74 63 16 22 48 114 23 2 33 46 116 27 52 84 24 31 53 99 75 25 96 73 108 5 21 112 107 +CX 104 80 26 12 7 37 18 70 50 78 46 23 75 97 85 21 20 65 111 63 82 11 57 106 36 34 44 56 31 73 5 29 +CX 83 27 109 110 103 100 34 30 113 95 90 105 55 94 91 35 115 114 46 62 52 25 58 65 86 67 40 5 29 28 69 19 +CX 54 15 69 9 97 53 117 65 48 76 58 12 101 25 7 50 26 115 89 27 55 67 95 110 91 78 94 77 60 47 81 13 +CX 48 81 86 97 37 65 101 47 40 22 53 84 111 69 31 24 9 29 10 32 87 107 94 28 63 82 98 4 50 44 51 59 +CX 53 26 6 36 29 19 18 43 84 109 95 69 86 37 78 74 57 54 105 38 47 94 40 33 113 58 50 42 4 102 116 12 +CX 84 80 17 31 45 112 18 91 64 57 96 51 32 1 53 40 111 102 8 26 49 98 106 78 65 70 11 86 81 59 27 14 +CX 17 103 48 14 49 98 66 75 92 106 102 97 104 31 59 112 61 69 10 20 96 0 45 7 72 58 117 63 113 25 24 77 +CX 7 43 25 61 35 95 22 70 63 64 112 67 53 6 19 24 1 39 105 88 9 23 82 3 48 37 65 18 36 17 21 38 +CX 65 29 101 103 85 39 110 114 57 79 27 117 24 89 6 26 77 48 41 45 84 49 5 86 50 11 20 63 19 98 112 23 +CX 5 88 61 59 45 44 112 79 65 95 69 27 99 107 17 30 53 85 8 102 32 38 68 114 42 80 56 2 62 89 16 3 +CX 54 49 23 36 71 88 48 24 75 86 51 102 94 89 3 31 82 61 43 46 56 39 10 7 66 40 20 17 6 98 60 30 +CX 109 113 78 82 90 5 105 4 1 61 58 104 59 98 54 31 51 22 13 102 36 38 29 3 99 26 103 85 97 63 35 39 +CX 14 80 59 17 35 40 100 39 106 75 69 43 4 38 25 32 115 64 92 67 28 96 0 112 58 117 8 54 45 102 5 114 +CX 88 89 83 64 39 47 100 43 51 22 99 6 59 41 20 76 29 61 103 79 54 69 50 14 113 11 78 58 107 75 55 62 +CX 81 17 94 8 3 71 89 68 61 73 76 110 16 30 5 106 101 38 52 87 108 59 32 72 44 70 42 66 46 115 4 23 diff --git a/crates/ppvm-tableau-2/benches/circuits/gen.py b/crates/ppvm-tableau-2/benches/circuits/gen.py new file mode 100644 index 000000000..2fbe55cce --- /dev/null +++ b/crates/ppvm-tableau-2/benches/circuits/gen.py @@ -0,0 +1,32 @@ +"""Microbenchmark circuits for the inverse tableau, at the d=7 surface code's +size (118 qubits). Run with the `run_stim` example; per-operation cost is +(time per shot - base) / count. + +- base: H on all qubits (per-shot overhead). +- cx: base plus 4096 random CX, in instructions of 16 disjoint pairs. +- mr_batched: 10 rounds of H on all, 128 random CX, one MR over all qubits + (random collapses with fan-outs). +- mr_split: the same with one MR per instruction (no per-instruction batching). + +Regenerate with `python3 gen.py` in this directory (seeded, deterministic). +""" +import random +n = 118 +rng = random.Random(1) +h_all = "H " + " ".join(map(str, range(n))) +def cx_layer(pairs): + lines = [] + for _ in range(pairs // 16): + qs = rng.sample(range(n), 32) + lines.append("CX " + " ".join(map(str, qs))) + return lines +def write(name, lines): + open(f"{name}.stim", "w").write("\n".join(lines) + "\n") +write("base", [h_all]) +write("cx", [h_all] + cx_layer(4096)) +rounds = [] +for _ in range(10): + rounds += [h_all] + cx_layer(128) + rounds.append("MR " + " ".join(map(str, range(n)))) +write("mr_batched", rounds) +write("mr_split", [l if not l.startswith("MR") else "\n".join(f"MR {q}" for q in range(n)) for l in rounds]) diff --git a/crates/ppvm-tableau-2/benches/circuits/mr_batched.stim b/crates/ppvm-tableau-2/benches/circuits/mr_batched.stim new file mode 100644 index 000000000..952f78539 --- /dev/null +++ b/crates/ppvm-tableau-2/benches/circuits/mr_batched.stim @@ -0,0 +1,100 @@ +H 0 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 +CX 54 28 63 104 80 47 84 32 92 95 96 31 8 78 33 37 65 98 4 102 61 97 90 20 11 86 75 67 70 25 6 73 +CX 74 66 43 36 8 71 77 28 75 83 21 18 87 92 55 80 78 40 81 90 25 111 59 85 23 0 97 6 2 9 49 46 +CX 115 55 24 107 20 0 104 54 4 31 7 50 14 47 111 70 76 17 48 66 81 12 95 40 62 80 112 59 30 51 71 10 +CX 55 108 76 75 48 2 44 65 37 29 50 63 51 47 6 7 52 77 54 114 20 88 18 42 24 95 62 101 40 0 46 104 +CX 46 93 10 100 96 49 16 91 82 4 18 6 116 39 33 42 5 115 111 17 30 40 26 20 56 25 52 47 99 104 75 23 +CX 58 3 73 22 93 80 15 4 95 18 53 84 37 65 96 27 7 9 77 67 104 69 90 54 113 34 88 60 71 56 59 82 +CX 74 79 115 10 72 43 13 38 29 99 5 30 14 26 22 83 96 50 101 51 81 16 113 109 71 82 104 62 88 75 92 19 +CX 87 94 80 96 14 49 56 45 85 2 77 103 61 81 62 98 7 31 78 36 22 64 107 60 8 40 11 33 89 66 38 86 +MR 0 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 +H 0 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 +CX 16 69 81 7 40 10 18 64 89 117 22 85 99 3 78 68 0 114 8 96 116 46 61 6 87 67 82 86 102 9 58 93 +CX 72 86 2 35 45 57 10 44 8 22 108 96 73 0 105 50 24 117 85 28 19 62 15 110 67 116 106 76 13 79 69 107 +CX 64 116 39 12 38 11 79 14 73 41 68 56 109 94 74 36 106 102 96 117 101 107 59 75 19 113 103 34 9 85 100 4 +CX 72 111 50 91 16 62 74 83 105 98 93 114 41 85 33 15 73 37 55 28 31 49 69 116 9 51 86 113 32 5 87 89 +CX 98 30 43 10 33 48 13 16 116 62 41 59 52 99 114 65 74 106 4 55 25 50 72 73 87 113 78 79 110 17 112 7 +CX 2 82 107 6 56 0 44 65 104 57 41 102 23 89 85 26 92 113 79 27 84 25 49 42 90 100 35 91 20 38 103 11 +CX 98 79 77 70 9 67 116 61 73 75 69 31 12 63 15 57 64 107 21 14 89 82 41 66 91 48 74 28 94 105 102 29 +CX 14 107 97 11 60 32 73 65 38 105 2 42 77 103 87 101 49 31 55 19 64 72 57 8 113 112 52 69 111 71 94 15 +MR 0 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 +H 0 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 +CX 116 49 15 107 34 48 109 4 11 69 44 9 50 86 60 61 95 85 82 99 20 51 5 33 105 104 93 117 57 1 92 31 +CX 68 16 9 63 51 26 13 107 98 88 91 59 25 113 81 104 55 3 35 2 5 62 94 53 116 76 22 24 64 48 36 10 +CX 70 5 52 90 18 59 20 38 116 83 110 74 37 4 78 67 41 105 104 62 11 111 94 81 86 76 49 89 80 63 33 57 +CX 100 2 53 4 37 98 6 44 25 43 23 31 17 11 5 12 19 39 40 63 75 73 76 60 116 49 96 69 48 30 67 18 +CX 70 98 13 53 117 49 28 71 1 76 105 73 110 40 52 101 112 107 80 85 27 16 55 22 14 72 93 38 94 45 60 63 +CX 98 111 67 20 113 75 110 34 81 89 30 33 9 91 65 41 31 83 78 71 28 106 114 109 16 7 59 45 93 79 101 38 +CX 94 5 54 57 63 111 95 6 22 71 48 103 85 87 113 83 92 40 39 35 20 45 105 67 93 47 11 115 31 46 12 96 +CX 31 91 8 46 86 68 84 111 87 39 5 72 45 66 1 49 110 12 0 99 73 22 59 13 27 47 115 65 17 51 23 56 +MR 0 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 +H 0 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 +CX 108 77 12 65 34 90 61 103 23 47 84 19 15 82 17 9 81 50 20 109 28 36 13 6 116 27 0 76 89 92 1 101 +CX 56 88 24 78 48 38 19 108 43 47 79 113 13 103 114 104 115 109 1 54 57 92 101 21 2 64 33 59 29 10 8 27 +CX 76 92 17 31 101 30 91 109 69 46 66 107 2 73 65 25 57 4 12 22 52 16 86 62 64 29 112 7 47 21 70 85 +CX 20 55 104 68 31 34 44 96 97 81 100 76 114 25 37 75 30 13 69 5 42 39 71 22 16 52 40 99 36 106 2 82 +CX 68 31 3 40 61 6 26 51 12 105 56 90 80 102 110 63 117 70 25 96 57 13 86 81 24 83 22 94 32 77 33 0 +CX 8 103 109 85 114 49 2 116 24 64 42 35 81 72 26 98 61 71 18 57 5 48 65 50 51 7 105 92 37 45 29 25 +CX 58 71 60 115 4 98 70 24 15 41 104 91 38 54 93 31 89 94 20 35 75 34 87 111 74 47 13 84 88 46 107 64 +CX 54 57 104 61 28 12 22 66 76 116 79 59 7 13 10 51 102 96 34 24 83 87 62 73 85 86 45 109 48 69 64 14 +MR 0 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 +H 0 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 +CX 75 1 30 62 49 33 78 107 34 10 20 50 12 100 6 27 44 23 22 37 72 14 29 82 116 53 36 110 18 74 17 77 +CX 41 70 24 15 42 84 63 25 39 79 89 101 5 91 77 115 22 113 112 64 102 97 83 54 14 117 74 72 34 106 0 81 +CX 88 91 106 112 64 38 48 69 49 51 8 40 43 17 33 34 77 56 20 59 24 109 60 11 100 90 15 71 23 32 39 52 +CX 104 37 95 100 38 25 45 83 89 16 85 76 96 27 69 82 13 55 117 33 105 68 97 107 80 30 108 94 72 84 8 43 +CX 6 94 97 57 32 110 81 90 7 84 109 77 31 4 95 45 11 21 85 23 24 115 82 78 5 64 116 91 80 66 1 86 +CX 52 63 99 80 7 69 30 66 111 84 13 104 41 54 22 4 65 79 49 39 45 3 58 43 115 56 116 60 97 83 20 55 +CX 7 86 89 67 42 33 72 6 3 49 19 60 25 108 69 78 93 59 22 28 65 117 102 21 52 58 74 82 0 105 5 111 +CX 79 50 9 90 67 51 0 18 101 28 88 105 112 43 26 13 83 49 40 4 76 115 25 44 45 89 5 82 54 75 39 55 +MR 0 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 +H 0 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 +CX 37 83 64 20 95 21 101 65 72 17 84 105 88 1 109 0 55 27 28 56 12 102 111 114 70 36 66 59 85 23 79 73 +CX 50 24 102 115 90 110 37 40 14 82 15 71 107 61 6 100 106 49 45 101 7 13 59 34 97 74 41 56 16 39 58 103 +CX 16 36 68 108 14 100 75 15 82 10 107 54 55 65 114 106 45 71 111 21 89 73 63 95 91 83 90 77 64 53 37 59 +CX 59 30 26 75 100 68 24 16 7 35 94 1 97 56 57 85 22 115 37 69 38 18 10 23 67 11 84 21 90 52 89 27 +CX 97 53 103 32 43 99 84 19 107 71 29 116 42 95 117 1 112 96 104 92 109 21 80 37 7 14 28 79 86 3 54 75 +CX 0 66 4 22 87 101 76 2 102 68 56 92 94 54 80 18 46 51 28 96 86 72 63 91 71 81 35 15 64 36 3 37 +CX 81 67 97 7 36 114 11 47 45 14 85 64 57 35 90 4 112 70 34 43 44 31 104 62 33 8 29 52 48 60 20 78 +CX 52 58 55 28 116 99 65 80 10 94 64 50 48 24 25 79 17 4 110 18 113 44 45 87 12 32 30 40 39 2 86 92 +MR 0 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 +H 0 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 +CX 55 58 33 83 43 100 91 90 10 110 95 1 34 54 89 19 29 76 47 79 49 0 30 69 108 92 22 28 85 15 24 12 +CX 24 93 80 29 5 52 116 43 18 92 86 1 59 81 76 27 7 79 4 91 97 12 99 117 45 8 48 73 103 102 87 40 +CX 50 37 21 76 116 39 35 74 46 29 28 15 94 48 18 109 36 45 114 67 75 100 6 98 7 11 55 43 105 96 69 30 +CX 39 66 110 36 49 116 69 41 21 19 68 12 84 78 108 105 28 89 38 55 54 33 64 80 79 85 82 46 117 56 57 7 +CX 58 71 52 0 73 15 38 78 64 23 82 9 14 30 46 57 103 84 37 19 35 85 13 93 16 87 62 42 70 29 45 4 +CX 7 15 23 63 58 87 65 11 51 117 73 93 19 9 38 1 80 76 113 31 78 94 29 27 91 16 70 74 109 92 90 105 +CX 20 45 86 108 107 114 7 47 3 94 87 85 63 57 11 4 88 90 104 17 48 82 34 111 102 67 93 23 28 78 36 92 +CX 15 97 73 39 23 4 75 13 61 19 116 32 14 71 100 10 86 42 16 29 98 43 111 56 44 72 91 85 1 92 8 34 +MR 0 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 +H 0 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 +CX 73 70 107 23 1 95 69 83 96 47 76 37 71 21 80 113 62 116 104 46 117 64 98 88 100 54 63 60 44 52 12 86 +CX 9 12 101 105 29 103 41 26 4 89 115 77 75 90 7 114 45 28 98 96 78 53 48 6 85 94 55 102 35 84 74 47 +CX 89 9 13 42 108 22 44 49 59 20 93 112 53 90 60 24 3 64 95 113 25 85 82 23 96 10 76 78 79 101 11 102 +CX 57 109 32 7 100 51 34 105 53 95 30 117 15 8 27 26 103 3 22 52 80 62 56 42 99 86 24 116 11 48 46 87 +CX 11 66 87 58 67 86 71 88 0 25 35 45 106 3 24 31 54 41 47 112 77 56 59 91 92 14 70 4 81 26 43 9 +CX 55 32 90 85 99 31 7 39 12 110 72 18 103 117 46 66 10 100 108 9 56 61 20 30 73 0 68 3 25 26 41 76 +CX 11 74 49 52 65 102 83 48 85 72 43 110 56 36 113 45 24 17 68 80 38 69 12 93 62 44 50 23 84 4 10 63 +CX 87 7 50 19 74 94 17 23 65 8 112 115 71 24 66 106 98 45 89 75 53 22 52 101 35 29 107 47 78 103 1 37 +MR 0 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 +H 0 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 +CX 36 64 97 104 34 106 18 26 115 59 4 101 98 113 14 92 86 11 6 81 83 8 63 17 62 108 78 49 28 87 99 53 +CX 21 6 37 58 5 19 64 83 110 24 26 80 89 20 27 16 2 48 106 101 96 51 78 47 46 42 112 41 90 68 43 99 +CX 7 41 58 98 110 97 20 11 1 92 3 62 87 104 13 24 49 79 67 52 77 14 33 66 55 5 115 27 108 70 0 37 +CX 53 8 42 44 112 74 51 7 77 84 34 16 26 1 20 57 6 78 72 52 88 13 70 4 67 21 90 83 108 82 58 116 +CX 1 109 70 117 107 18 28 0 68 4 65 82 75 87 30 76 90 55 91 13 62 47 42 20 92 84 27 31 10 83 99 21 +CX 88 38 11 10 117 26 16 95 9 57 96 99 33 46 31 18 22 74 98 30 54 109 47 67 68 101 7 69 86 5 35 0 +CX 71 95 74 61 53 24 75 100 14 45 90 42 64 105 111 116 77 96 84 97 33 21 94 115 36 37 62 65 41 104 82 78 +CX 54 14 6 8 111 114 39 78 53 82 49 47 42 73 11 90 18 71 72 95 56 64 109 28 70 32 98 43 83 113 22 5 +MR 0 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 +H 0 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 +CX 14 92 12 56 15 116 60 97 89 44 35 113 40 48 106 21 66 94 68 57 93 64 11 63 43 72 103 41 115 109 70 31 +CX 63 31 62 43 5 21 107 110 7 1 54 87 42 39 70 79 94 116 45 18 106 95 9 80 24 4 22 90 99 55 97 33 +CX 26 20 117 51 96 109 67 23 22 30 105 59 18 78 79 97 104 98 36 72 41 116 86 40 69 73 103 99 108 24 21 90 +CX 4 18 16 3 55 6 44 108 91 57 116 54 27 20 69 45 94 22 101 8 109 33 47 42 112 66 39 87 19 76 61 82 +CX 6 103 59 80 99 53 78 104 11 2 1 98 5 108 3 106 27 47 26 57 105 90 117 28 38 25 71 22 24 73 92 100 +CX 68 27 93 17 72 90 110 12 40 20 69 102 80 59 84 63 2 107 61 8 1 58 109 73 45 44 52 18 15 13 104 103 +CX 103 112 41 105 108 21 82 8 49 2 64 56 0 85 96 77 17 37 35 38 7 60 117 97 72 19 114 63 76 52 4 31 +CX 14 93 49 86 68 67 3 74 39 0 105 2 30 112 91 81 115 35 80 46 92 108 27 78 97 54 95 57 64 6 16 69 +MR 0 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 diff --git a/crates/ppvm-tableau-2/benches/circuits/mr_split.stim b/crates/ppvm-tableau-2/benches/circuits/mr_split.stim new file mode 100644 index 000000000..0c2ea5bb0 --- /dev/null +++ b/crates/ppvm-tableau-2/benches/circuits/mr_split.stim @@ -0,0 +1,1270 @@ +H 0 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 +CX 54 28 63 104 80 47 84 32 92 95 96 31 8 78 33 37 65 98 4 102 61 97 90 20 11 86 75 67 70 25 6 73 +CX 74 66 43 36 8 71 77 28 75 83 21 18 87 92 55 80 78 40 81 90 25 111 59 85 23 0 97 6 2 9 49 46 +CX 115 55 24 107 20 0 104 54 4 31 7 50 14 47 111 70 76 17 48 66 81 12 95 40 62 80 112 59 30 51 71 10 +CX 55 108 76 75 48 2 44 65 37 29 50 63 51 47 6 7 52 77 54 114 20 88 18 42 24 95 62 101 40 0 46 104 +CX 46 93 10 100 96 49 16 91 82 4 18 6 116 39 33 42 5 115 111 17 30 40 26 20 56 25 52 47 99 104 75 23 +CX 58 3 73 22 93 80 15 4 95 18 53 84 37 65 96 27 7 9 77 67 104 69 90 54 113 34 88 60 71 56 59 82 +CX 74 79 115 10 72 43 13 38 29 99 5 30 14 26 22 83 96 50 101 51 81 16 113 109 71 82 104 62 88 75 92 19 +CX 87 94 80 96 14 49 56 45 85 2 77 103 61 81 62 98 7 31 78 36 22 64 107 60 8 40 11 33 89 66 38 86 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 16 69 81 7 40 10 18 64 89 117 22 85 99 3 78 68 0 114 8 96 116 46 61 6 87 67 82 86 102 9 58 93 +CX 72 86 2 35 45 57 10 44 8 22 108 96 73 0 105 50 24 117 85 28 19 62 15 110 67 116 106 76 13 79 69 107 +CX 64 116 39 12 38 11 79 14 73 41 68 56 109 94 74 36 106 102 96 117 101 107 59 75 19 113 103 34 9 85 100 4 +CX 72 111 50 91 16 62 74 83 105 98 93 114 41 85 33 15 73 37 55 28 31 49 69 116 9 51 86 113 32 5 87 89 +CX 98 30 43 10 33 48 13 16 116 62 41 59 52 99 114 65 74 106 4 55 25 50 72 73 87 113 78 79 110 17 112 7 +CX 2 82 107 6 56 0 44 65 104 57 41 102 23 89 85 26 92 113 79 27 84 25 49 42 90 100 35 91 20 38 103 11 +CX 98 79 77 70 9 67 116 61 73 75 69 31 12 63 15 57 64 107 21 14 89 82 41 66 91 48 74 28 94 105 102 29 +CX 14 107 97 11 60 32 73 65 38 105 2 42 77 103 87 101 49 31 55 19 64 72 57 8 113 112 52 69 111 71 94 15 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 116 49 15 107 34 48 109 4 11 69 44 9 50 86 60 61 95 85 82 99 20 51 5 33 105 104 93 117 57 1 92 31 +CX 68 16 9 63 51 26 13 107 98 88 91 59 25 113 81 104 55 3 35 2 5 62 94 53 116 76 22 24 64 48 36 10 +CX 70 5 52 90 18 59 20 38 116 83 110 74 37 4 78 67 41 105 104 62 11 111 94 81 86 76 49 89 80 63 33 57 +CX 100 2 53 4 37 98 6 44 25 43 23 31 17 11 5 12 19 39 40 63 75 73 76 60 116 49 96 69 48 30 67 18 +CX 70 98 13 53 117 49 28 71 1 76 105 73 110 40 52 101 112 107 80 85 27 16 55 22 14 72 93 38 94 45 60 63 +CX 98 111 67 20 113 75 110 34 81 89 30 33 9 91 65 41 31 83 78 71 28 106 114 109 16 7 59 45 93 79 101 38 +CX 94 5 54 57 63 111 95 6 22 71 48 103 85 87 113 83 92 40 39 35 20 45 105 67 93 47 11 115 31 46 12 96 +CX 31 91 8 46 86 68 84 111 87 39 5 72 45 66 1 49 110 12 0 99 73 22 59 13 27 47 115 65 17 51 23 56 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 108 77 12 65 34 90 61 103 23 47 84 19 15 82 17 9 81 50 20 109 28 36 13 6 116 27 0 76 89 92 1 101 +CX 56 88 24 78 48 38 19 108 43 47 79 113 13 103 114 104 115 109 1 54 57 92 101 21 2 64 33 59 29 10 8 27 +CX 76 92 17 31 101 30 91 109 69 46 66 107 2 73 65 25 57 4 12 22 52 16 86 62 64 29 112 7 47 21 70 85 +CX 20 55 104 68 31 34 44 96 97 81 100 76 114 25 37 75 30 13 69 5 42 39 71 22 16 52 40 99 36 106 2 82 +CX 68 31 3 40 61 6 26 51 12 105 56 90 80 102 110 63 117 70 25 96 57 13 86 81 24 83 22 94 32 77 33 0 +CX 8 103 109 85 114 49 2 116 24 64 42 35 81 72 26 98 61 71 18 57 5 48 65 50 51 7 105 92 37 45 29 25 +CX 58 71 60 115 4 98 70 24 15 41 104 91 38 54 93 31 89 94 20 35 75 34 87 111 74 47 13 84 88 46 107 64 +CX 54 57 104 61 28 12 22 66 76 116 79 59 7 13 10 51 102 96 34 24 83 87 62 73 85 86 45 109 48 69 64 14 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 75 1 30 62 49 33 78 107 34 10 20 50 12 100 6 27 44 23 22 37 72 14 29 82 116 53 36 110 18 74 17 77 +CX 41 70 24 15 42 84 63 25 39 79 89 101 5 91 77 115 22 113 112 64 102 97 83 54 14 117 74 72 34 106 0 81 +CX 88 91 106 112 64 38 48 69 49 51 8 40 43 17 33 34 77 56 20 59 24 109 60 11 100 90 15 71 23 32 39 52 +CX 104 37 95 100 38 25 45 83 89 16 85 76 96 27 69 82 13 55 117 33 105 68 97 107 80 30 108 94 72 84 8 43 +CX 6 94 97 57 32 110 81 90 7 84 109 77 31 4 95 45 11 21 85 23 24 115 82 78 5 64 116 91 80 66 1 86 +CX 52 63 99 80 7 69 30 66 111 84 13 104 41 54 22 4 65 79 49 39 45 3 58 43 115 56 116 60 97 83 20 55 +CX 7 86 89 67 42 33 72 6 3 49 19 60 25 108 69 78 93 59 22 28 65 117 102 21 52 58 74 82 0 105 5 111 +CX 79 50 9 90 67 51 0 18 101 28 88 105 112 43 26 13 83 49 40 4 76 115 25 44 45 89 5 82 54 75 39 55 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 37 83 64 20 95 21 101 65 72 17 84 105 88 1 109 0 55 27 28 56 12 102 111 114 70 36 66 59 85 23 79 73 +CX 50 24 102 115 90 110 37 40 14 82 15 71 107 61 6 100 106 49 45 101 7 13 59 34 97 74 41 56 16 39 58 103 +CX 16 36 68 108 14 100 75 15 82 10 107 54 55 65 114 106 45 71 111 21 89 73 63 95 91 83 90 77 64 53 37 59 +CX 59 30 26 75 100 68 24 16 7 35 94 1 97 56 57 85 22 115 37 69 38 18 10 23 67 11 84 21 90 52 89 27 +CX 97 53 103 32 43 99 84 19 107 71 29 116 42 95 117 1 112 96 104 92 109 21 80 37 7 14 28 79 86 3 54 75 +CX 0 66 4 22 87 101 76 2 102 68 56 92 94 54 80 18 46 51 28 96 86 72 63 91 71 81 35 15 64 36 3 37 +CX 81 67 97 7 36 114 11 47 45 14 85 64 57 35 90 4 112 70 34 43 44 31 104 62 33 8 29 52 48 60 20 78 +CX 52 58 55 28 116 99 65 80 10 94 64 50 48 24 25 79 17 4 110 18 113 44 45 87 12 32 30 40 39 2 86 92 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 55 58 33 83 43 100 91 90 10 110 95 1 34 54 89 19 29 76 47 79 49 0 30 69 108 92 22 28 85 15 24 12 +CX 24 93 80 29 5 52 116 43 18 92 86 1 59 81 76 27 7 79 4 91 97 12 99 117 45 8 48 73 103 102 87 40 +CX 50 37 21 76 116 39 35 74 46 29 28 15 94 48 18 109 36 45 114 67 75 100 6 98 7 11 55 43 105 96 69 30 +CX 39 66 110 36 49 116 69 41 21 19 68 12 84 78 108 105 28 89 38 55 54 33 64 80 79 85 82 46 117 56 57 7 +CX 58 71 52 0 73 15 38 78 64 23 82 9 14 30 46 57 103 84 37 19 35 85 13 93 16 87 62 42 70 29 45 4 +CX 7 15 23 63 58 87 65 11 51 117 73 93 19 9 38 1 80 76 113 31 78 94 29 27 91 16 70 74 109 92 90 105 +CX 20 45 86 108 107 114 7 47 3 94 87 85 63 57 11 4 88 90 104 17 48 82 34 111 102 67 93 23 28 78 36 92 +CX 15 97 73 39 23 4 75 13 61 19 116 32 14 71 100 10 86 42 16 29 98 43 111 56 44 72 91 85 1 92 8 34 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 73 70 107 23 1 95 69 83 96 47 76 37 71 21 80 113 62 116 104 46 117 64 98 88 100 54 63 60 44 52 12 86 +CX 9 12 101 105 29 103 41 26 4 89 115 77 75 90 7 114 45 28 98 96 78 53 48 6 85 94 55 102 35 84 74 47 +CX 89 9 13 42 108 22 44 49 59 20 93 112 53 90 60 24 3 64 95 113 25 85 82 23 96 10 76 78 79 101 11 102 +CX 57 109 32 7 100 51 34 105 53 95 30 117 15 8 27 26 103 3 22 52 80 62 56 42 99 86 24 116 11 48 46 87 +CX 11 66 87 58 67 86 71 88 0 25 35 45 106 3 24 31 54 41 47 112 77 56 59 91 92 14 70 4 81 26 43 9 +CX 55 32 90 85 99 31 7 39 12 110 72 18 103 117 46 66 10 100 108 9 56 61 20 30 73 0 68 3 25 26 41 76 +CX 11 74 49 52 65 102 83 48 85 72 43 110 56 36 113 45 24 17 68 80 38 69 12 93 62 44 50 23 84 4 10 63 +CX 87 7 50 19 74 94 17 23 65 8 112 115 71 24 66 106 98 45 89 75 53 22 52 101 35 29 107 47 78 103 1 37 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 36 64 97 104 34 106 18 26 115 59 4 101 98 113 14 92 86 11 6 81 83 8 63 17 62 108 78 49 28 87 99 53 +CX 21 6 37 58 5 19 64 83 110 24 26 80 89 20 27 16 2 48 106 101 96 51 78 47 46 42 112 41 90 68 43 99 +CX 7 41 58 98 110 97 20 11 1 92 3 62 87 104 13 24 49 79 67 52 77 14 33 66 55 5 115 27 108 70 0 37 +CX 53 8 42 44 112 74 51 7 77 84 34 16 26 1 20 57 6 78 72 52 88 13 70 4 67 21 90 83 108 82 58 116 +CX 1 109 70 117 107 18 28 0 68 4 65 82 75 87 30 76 90 55 91 13 62 47 42 20 92 84 27 31 10 83 99 21 +CX 88 38 11 10 117 26 16 95 9 57 96 99 33 46 31 18 22 74 98 30 54 109 47 67 68 101 7 69 86 5 35 0 +CX 71 95 74 61 53 24 75 100 14 45 90 42 64 105 111 116 77 96 84 97 33 21 94 115 36 37 62 65 41 104 82 78 +CX 54 14 6 8 111 114 39 78 53 82 49 47 42 73 11 90 18 71 72 95 56 64 109 28 70 32 98 43 83 113 22 5 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 +H 0 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 +CX 14 92 12 56 15 116 60 97 89 44 35 113 40 48 106 21 66 94 68 57 93 64 11 63 43 72 103 41 115 109 70 31 +CX 63 31 62 43 5 21 107 110 7 1 54 87 42 39 70 79 94 116 45 18 106 95 9 80 24 4 22 90 99 55 97 33 +CX 26 20 117 51 96 109 67 23 22 30 105 59 18 78 79 97 104 98 36 72 41 116 86 40 69 73 103 99 108 24 21 90 +CX 4 18 16 3 55 6 44 108 91 57 116 54 27 20 69 45 94 22 101 8 109 33 47 42 112 66 39 87 19 76 61 82 +CX 6 103 59 80 99 53 78 104 11 2 1 98 5 108 3 106 27 47 26 57 105 90 117 28 38 25 71 22 24 73 92 100 +CX 68 27 93 17 72 90 110 12 40 20 69 102 80 59 84 63 2 107 61 8 1 58 109 73 45 44 52 18 15 13 104 103 +CX 103 112 41 105 108 21 82 8 49 2 64 56 0 85 96 77 17 37 35 38 7 60 117 97 72 19 114 63 76 52 4 31 +CX 14 93 49 86 68 67 3 74 39 0 105 2 30 112 91 81 115 35 80 46 92 108 27 78 97 54 95 57 64 6 16 69 +MR 0 +MR 1 +MR 2 +MR 3 +MR 4 +MR 5 +MR 6 +MR 7 +MR 8 +MR 9 +MR 10 +MR 11 +MR 12 +MR 13 +MR 14 +MR 15 +MR 16 +MR 17 +MR 18 +MR 19 +MR 20 +MR 21 +MR 22 +MR 23 +MR 24 +MR 25 +MR 26 +MR 27 +MR 28 +MR 29 +MR 30 +MR 31 +MR 32 +MR 33 +MR 34 +MR 35 +MR 36 +MR 37 +MR 38 +MR 39 +MR 40 +MR 41 +MR 42 +MR 43 +MR 44 +MR 45 +MR 46 +MR 47 +MR 48 +MR 49 +MR 50 +MR 51 +MR 52 +MR 53 +MR 54 +MR 55 +MR 56 +MR 57 +MR 58 +MR 59 +MR 60 +MR 61 +MR 62 +MR 63 +MR 64 +MR 65 +MR 66 +MR 67 +MR 68 +MR 69 +MR 70 +MR 71 +MR 72 +MR 73 +MR 74 +MR 75 +MR 76 +MR 77 +MR 78 +MR 79 +MR 80 +MR 81 +MR 82 +MR 83 +MR 84 +MR 85 +MR 86 +MR 87 +MR 88 +MR 89 +MR 90 +MR 91 +MR 92 +MR 93 +MR 94 +MR 95 +MR 96 +MR 97 +MR 98 +MR 99 +MR 100 +MR 101 +MR 102 +MR 103 +MR 104 +MR 105 +MR 106 +MR 107 +MR 108 +MR 109 +MR 110 +MR 111 +MR 112 +MR 113 +MR 114 +MR 115 +MR 116 +MR 117 From 5d3eef1eaea9dbce490c341c92e255bb6bcb046d Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 15:45:30 +0100 Subject: [PATCH 12/60] perf(ppvm-tableau-2): multiply rows in place when the pattern allows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When every changed row is itself times rows that keep their bits (CX, CZ, S, √X and inverses), multiply with row_product in place instead of the fused per-word pass. CX at 118 qubits goes from 31 to 24 ns. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/inverse_tableau/physical_gates.rs | 73 ++++++++++++++++--- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs index 93c3bcd1f..075aee2d8 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs @@ -3,7 +3,7 @@ //! Physical gates, `C ← G·C`: they mix whole rows. -use ppvm_pauli_kernels::word_product; +use ppvm_pauli_kernels::{row_product, word_product}; use ppvm_traits_2::ApplyGateRule; use super::InverseTableau; @@ -29,6 +29,12 @@ impl Image { (0..M).all(|j| self.factors[j] == (j == g)) } + /// Whether row `g` becomes itself times rows that keep their bits, so it can + /// be multiplied in place (CX, CZ, S, √X and their inverses). + fn multiplies_in_place(&self, g: usize, images: &[Image; M]) -> bool { + self.factors[g] && (0..M).all(|j| j == g || !self.factors[j] || images[j].keeps_bits(j)) + } + /// Number of Y factors: an X and a Z on the same qubit. fn y_count(&self) -> u32 { self.factors.chunks(2).filter(|xz| xz[0] && xz[1]).count() as u32 @@ -84,6 +90,44 @@ fn recombine( } } +/// `(&mut v[g], &v[j])` for `g ≠ j`. +fn pair(v: &mut [T], g: usize, j: usize) -> (&mut T, &T) { + if g < j { + let (lo, hi) = v.split_at_mut(j); + (&mut lo[g], &hi[0]) + } else { + let (lo, hi) = v.split_at_mut(g); + (&mut hi[0], &lo[j]) + } +} + +/// The in-place path: each changed row `g` is multiplied by its other factors +/// with `row_product`, like Stim's `zs[t] *= zs[c]`. +/// +/// That computes `R_g · R_j …` with `R_g` first, while the image is a product +/// in generator order; moving `R_g` past an earlier factor costs `−1` when the +/// two anticommute, i.e. when they are the X and Z rows of one qubit. +#[inline(always)] +fn multiply_in_place( + mut x: [&mut [BitWord]; M], + mut z: [&mut [BitWord]; M], + images: &[Image; M], + phases: &mut [u32; M], +) { + for (g, image) in images.iter().enumerate() { + if image.keeps_bits(g) { + continue; + } + for j in (0..M).filter(|&j| j != g && image.factors[j]) { + let (dst_x, src_x) = pair(&mut x, g, j); + let (dst_z, src_z) = pair(&mut z, g, j); + let k = row_product(dst_x, dst_z, src_x, src_z); + let reorder = if j < g && j / 2 == g / 2 { 2 } else { 0 }; + phases[g] = phases[g].wrapping_add(u32::from(k) + reorder); + } + } +} + impl InverseTableau { /// Starting phase of each new row, as a power of `i`: one `i` per Y in the /// image, `−1` for its flip and `−1` per negative old row it multiplies. @@ -94,6 +138,23 @@ impl InverseTableau { }) } + /// Replace each row in `rows` by its image's product of old rows: in place + /// when the pattern allows it, else with the fused pass. The choice depends + /// only on the images, which are constants for a given gate. + #[inline(always)] + fn rewrite_rows(&mut self, rows: [usize; M], images: &[Image; M]) { + let mut phases = self.initial_phases(rows, images); + let in_place = + (0..M).all(|g| images[g].keeps_bits(g) || images[g].multiplies_in_place(g, images)); + let (x, z) = self.rows_mut(rows); + if in_place { + multiply_in_place(x, z, images, &mut phases); + } else { + recombine(x, z, images, &mut phases); + } + self.set_signs(rows, phases); + } + /// Set each row's sign from its final phase (`i⁰ = +`, `i² = −`). fn set_signs(&mut self, rows: [usize; M], phases: [u32; M]) { for (g, &r) in rows.iter().enumerate() { @@ -113,20 +174,14 @@ impl ApplyGateRule for InverseTableau { fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { let images = [rule(true, false), rule(false, true)].map(Image::new); let rows = [q, self.n + q]; - let mut phases = self.initial_phases(rows, &images); - let (x, z) = self.rows_mut(rows); - recombine(x, z, &images, &mut phases); - self.set_signs(rows, phases); + self.rewrite_rows(rows, &images); } fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { let generator = |j: usize| std::array::from_fn(|i| i == j); let images = std::array::from_fn(|j| Image::new(rule(generator(j)))); let rows = [a, self.n + a, b, self.n + b]; - let mut phases = self.initial_phases(rows, &images); - let (x, z) = self.rows_mut(rows); - recombine(x, z, &images, &mut phases); - self.set_signs(rows, phases); + self.rewrite_rows(rows, &images); } fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { From ea825b763e3da0f7a50bab3d3f003be9f73e4287 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 15:46:31 +0100 Subject: [PATCH 13/60] bench(ppvm-tableau-2): add an all-gates microbenchmark 4096 gates drawn uniformly from all 13 Clifford gates (own seed, so the other circuits are unchanged), to check that gate optimizations are not tuned to CX alone. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../benches/circuits/gates.stim | 4097 +++++++++++++++++ crates/ppvm-tableau-2/benches/circuits/gen.py | 14 + 2 files changed, 4111 insertions(+) create mode 100644 crates/ppvm-tableau-2/benches/circuits/gates.stim diff --git a/crates/ppvm-tableau-2/benches/circuits/gates.stim b/crates/ppvm-tableau-2/benches/circuits/gates.stim new file mode 100644 index 000000000..d5040f25c --- /dev/null +++ b/crates/ppvm-tableau-2/benches/circuits/gates.stim @@ -0,0 +1,4097 @@ +H 0 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 +X 11 +Y 46 +Z 94 +CY 85 109 +S 32 +SQRT_Y_DAG 27 +SQRT_Y_DAG 4 +SQRT_Y_DAG 87 +Z 55 +CX 50 102 +CZ 110 65 +S_DAG 69 +SQRT_X_DAG 64 +S 115 +X 111 +X 46 +SQRT_X_DAG 40 +SQRT_X 54 +SQRT_Y 21 +SQRT_Y 22 +H 29 +X 22 +S_DAG 22 +Z 65 +SQRT_Y 46 +SQRT_Y 86 +SQRT_Y 23 +SQRT_X_DAG 101 +SQRT_X 94 +SQRT_Y 116 +CY 46 101 +SQRT_Y_DAG 45 +S_DAG 109 +SQRT_X_DAG 20 +CY 51 91 +CZ 59 83 +SQRT_Y 31 +SQRT_X_DAG 35 +SQRT_X_DAG 64 +SQRT_Y 106 +CY 45 84 +SQRT_X_DAG 115 +SQRT_X_DAG 44 +SQRT_Y_DAG 92 +SQRT_Y 92 +SQRT_X_DAG 62 +CX 28 41 +CZ 106 21 +SQRT_Y_DAG 34 +CY 116 61 +S 38 +CY 90 106 +SQRT_Y 71 +SQRT_Y 64 +CX 78 75 +SQRT_X 39 +CZ 26 62 +SQRT_Y 46 +CX 79 112 +Y 100 +S_DAG 92 +X 116 +H 95 +Y 7 +SQRT_Y_DAG 83 +X 34 +SQRT_Y_DAG 29 +CX 112 117 +Y 96 +SQRT_Y 17 +S 31 +H 112 +X 54 +CZ 97 4 +X 46 +S_DAG 22 +H 86 +X 10 +Y 8 +X 5 +CZ 117 2 +S_DAG 32 +Z 104 +Z 94 +Z 66 +CZ 0 49 +SQRT_Y_DAG 5 +CY 31 19 +X 0 +S_DAG 78 +CX 95 14 +S 43 +SQRT_X_DAG 3 +S 57 +SQRT_Y 98 +SQRT_Y_DAG 94 +X 115 +S 96 +SQRT_X 110 +SQRT_Y_DAG 90 +Z 60 +H 11 +CX 87 40 +Y 3 +SQRT_X_DAG 100 +Z 66 +SQRT_Y_DAG 99 +SQRT_X 62 +SQRT_Y 41 +Z 111 +S_DAG 33 +S 77 +SQRT_X 83 +X 89 +SQRT_Y 17 +CX 7 32 +X 16 +Z 21 +Y 58 +CX 29 65 +CZ 4 31 +H 91 +SQRT_X_DAG 9 +S 10 +SQRT_Y_DAG 29 +SQRT_Y_DAG 101 +CY 79 90 +S_DAG 32 +CX 54 35 +SQRT_Y 96 +X 19 +X 49 +SQRT_X 20 +Y 65 +CZ 11 30 +Y 12 +X 23 +CY 29 13 +H 3 +SQRT_Y 85 +SQRT_X_DAG 58 +S 68 +CX 48 27 +CX 116 97 +H 93 +CY 55 54 +SQRT_Y 2 +SQRT_Y_DAG 75 +X 112 +SQRT_X 67 +SQRT_Y_DAG 23 +Y 84 +CY 61 46 +X 66 +Y 78 +S_DAG 37 +CZ 47 39 +X 111 +CX 52 12 +Y 39 +H 107 +CY 86 105 +X 103 +SQRT_X_DAG 7 +SQRT_X 81 +SQRT_X_DAG 59 +H 113 +SQRT_Y_DAG 78 +Y 0 +S 3 +S_DAG 39 +CZ 9 28 +CY 62 24 +Y 73 +S_DAG 50 +CZ 59 17 +CY 44 50 +Y 32 +Y 15 +Y 78 +S_DAG 82 +SQRT_X 27 +CZ 13 3 +SQRT_Y_DAG 84 +SQRT_X_DAG 99 +X 92 +CZ 63 37 +S_DAG 58 +Z 102 +S_DAG 34 +SQRT_X_DAG 67 +SQRT_X_DAG 92 +CZ 102 53 +SQRT_X_DAG 107 +CX 37 50 +H 20 +SQRT_X_DAG 76 +S 70 +SQRT_X 89 +CX 89 117 +Y 74 +CZ 104 73 +Y 9 +S_DAG 22 +SQRT_Y 18 +CY 53 114 +Y 102 +Y 116 +CX 103 82 +X 16 +S 49 +H 90 +CX 113 87 +S_DAG 56 +Z 67 +S 14 +Z 69 +CY 54 12 +S_DAG 66 +H 91 +SQRT_Y 32 +Z 114 +Z 59 +CZ 30 51 +S_DAG 100 +CY 73 93 +Z 59 +SQRT_X_DAG 92 +X 103 +SQRT_Y_DAG 49 +CZ 23 50 +SQRT_Y 6 +SQRT_X_DAG 35 +SQRT_X 32 +CZ 93 52 +CZ 82 60 +S_DAG 70 +S_DAG 91 +CZ 116 84 +Y 97 +CZ 28 68 +SQRT_Y_DAG 24 +SQRT_X 104 +CX 48 112 +CX 117 1 +S_DAG 59 +SQRT_Y 91 +SQRT_X_DAG 83 +Z 104 +Y 2 +SQRT_X 27 +CZ 72 77 +SQRT_X 115 +H 113 +Y 49 +SQRT_Y 98 +CY 25 35 +CZ 117 75 +SQRT_Y_DAG 24 +SQRT_X_DAG 102 +SQRT_Y_DAG 17 +X 78 +CX 55 61 +S 65 +SQRT_Y_DAG 22 +SQRT_X_DAG 91 +H 97 +Y 44 +X 115 +SQRT_X_DAG 68 +CX 84 8 +CY 75 62 +CX 42 58 +S 112 +SQRT_Y 58 +X 10 +SQRT_Y_DAG 96 +S_DAG 22 +CY 96 100 +SQRT_X 32 +CX 80 100 +CZ 17 6 +Z 63 +SQRT_X 59 +CX 37 19 +X 36 +SQRT_Y 59 +X 46 +X 68 +SQRT_X 72 +SQRT_X_DAG 26 +CX 39 63 +CX 17 61 +CZ 68 91 +S 9 +S 105 +S_DAG 38 +S_DAG 82 +CY 39 83 +CX 50 66 +Y 65 +CX 26 50 +SQRT_Y_DAG 67 +Z 102 +SQRT_Y 80 +Y 39 +X 29 +SQRT_X_DAG 71 +H 66 +S 7 +Y 14 +CX 104 100 +SQRT_X 109 +S_DAG 27 +S_DAG 45 +Y 42 +SQRT_X_DAG 46 +Z 63 +SQRT_X_DAG 111 +S 59 +Z 117 +CZ 56 81 +H 34 +S_DAG 20 +Y 113 +H 60 +H 96 +CX 108 47 +Z 45 +Z 101 +Z 29 +S 103 +SQRT_Y 81 +SQRT_X 51 +CY 105 95 +S_DAG 35 +CZ 76 64 +SQRT_Y_DAG 88 +CZ 41 95 +SQRT_X 96 +CZ 111 90 +CX 96 91 +S 68 +SQRT_Y_DAG 81 +CX 9 47 +S 50 +SQRT_X_DAG 22 +S 115 +S_DAG 56 +SQRT_X_DAG 11 +Z 40 +SQRT_X 16 +X 13 +S_DAG 21 +S_DAG 9 +CZ 97 83 +SQRT_X 1 +SQRT_Y 41 +H 105 +SQRT_Y_DAG 49 +SQRT_Y 36 +SQRT_X_DAG 81 +Z 46 +S_DAG 25 +SQRT_X_DAG 12 +Z 100 +H 42 +S 18 +SQRT_X 46 +S 11 +S_DAG 24 +H 90 +H 93 +SQRT_Y_DAG 5 +S_DAG 47 +CX 98 78 +X 110 +Z 22 +Y 55 +SQRT_X_DAG 99 +S 16 +S_DAG 66 +SQRT_Y_DAG 108 +Y 43 +CX 98 90 +SQRT_Y_DAG 50 +H 6 +SQRT_X 98 +SQRT_X_DAG 62 +SQRT_Y_DAG 111 +S_DAG 69 +SQRT_Y_DAG 76 +Y 75 +SQRT_Y 68 +CX 63 51 +CZ 58 21 +SQRT_X 49 +SQRT_Y 57 +X 112 +Y 57 +SQRT_Y_DAG 16 +Y 117 +CX 64 117 +Z 9 +SQRT_X 39 +SQRT_X_DAG 102 +CZ 1 32 +Y 85 +S_DAG 28 +Z 3 +Z 54 +CX 11 43 +CX 59 6 +SQRT_X_DAG 30 +Y 61 +Z 71 +X 17 +CZ 64 69 +X 6 +H 69 +X 105 +SQRT_Y 43 +CX 67 112 +H 17 +S_DAG 62 +X 16 +SQRT_Y 14 +H 13 +SQRT_X_DAG 27 +CY 6 78 +H 80 +SQRT_X 43 +SQRT_Y_DAG 82 +SQRT_X 115 +CZ 67 64 +CY 117 86 +Z 65 +Y 106 +Z 80 +H 22 +SQRT_X 25 +S 43 +SQRT_X 18 +SQRT_X 16 +SQRT_X 40 +CY 38 103 +Y 71 +Y 60 +S 36 +SQRT_Y 97 +SQRT_X_DAG 35 +H 53 +CZ 17 89 +SQRT_Y 84 +Y 3 +SQRT_Y_DAG 70 +CY 25 27 +H 50 +SQRT_Y_DAG 5 +CX 17 80 +X 95 +S 89 +CZ 60 69 +X 94 +CY 110 105 +H 106 +Z 76 +S_DAG 4 +CZ 25 13 +Z 81 +CZ 69 117 +Z 97 +Y 87 +SQRT_X_DAG 80 +S 26 +Z 105 +S_DAG 89 +S 108 +SQRT_Y 72 +Y 52 +SQRT_X 85 +CZ 4 58 +S 84 +Y 81 +CZ 93 34 +X 27 +SQRT_X 42 +S 68 +CZ 50 75 +SQRT_Y 95 +H 55 +CY 16 88 +Z 100 +SQRT_X_DAG 111 +SQRT_X_DAG 44 +SQRT_X 60 +SQRT_Y_DAG 32 +SQRT_Y_DAG 24 +SQRT_Y_DAG 60 +SQRT_X_DAG 24 +CY 60 108 +SQRT_Y_DAG 43 +S 9 +Z 47 +SQRT_Y_DAG 80 +SQRT_X_DAG 28 +CY 78 83 +CX 73 113 +Z 87 +S 111 +H 68 +S 12 +X 100 +X 25 +S_DAG 7 +S_DAG 68 +S 101 +CZ 85 43 +SQRT_X_DAG 9 +SQRT_X 60 +CZ 2 36 +SQRT_Y_DAG 73 +Z 27 +Z 20 +SQRT_Y_DAG 98 +SQRT_X 92 +Y 81 +SQRT_Y_DAG 57 +S 82 +Y 63 +SQRT_X_DAG 102 +CY 117 30 +Z 72 +S 108 +H 25 +SQRT_Y_DAG 90 +S_DAG 75 +SQRT_Y_DAG 89 +SQRT_X 67 +SQRT_X 30 +CX 27 71 +X 33 +CX 31 17 +SQRT_Y_DAG 93 +SQRT_X 105 +SQRT_X 15 +SQRT_X_DAG 50 +SQRT_X 60 +SQRT_X 36 +H 30 +H 7 +SQRT_Y 67 +Y 77 +SQRT_Y 86 +X 6 +SQRT_X 90 +SQRT_X 51 +H 65 +S 12 +S_DAG 65 +S_DAG 66 +CY 63 74 +Y 90 +SQRT_X_DAG 94 +CZ 28 35 +X 3 +SQRT_X_DAG 5 +Z 83 +Z 26 +S_DAG 30 +SQRT_Y 6 +SQRT_Y_DAG 18 +CX 37 97 +Y 82 +SQRT_Y 69 +Y 86 +CX 17 55 +CZ 17 4 +S 65 +CX 34 60 +X 71 +S_DAG 97 +S_DAG 87 +Y 77 +S_DAG 13 +SQRT_Y_DAG 100 +S_DAG 46 +CY 81 35 +CY 61 114 +S 65 +SQRT_Y_DAG 19 +X 5 +S_DAG 55 +CX 1 44 +CX 68 91 +X 110 +CX 9 88 +SQRT_Y 64 +SQRT_Y_DAG 98 +SQRT_X 54 +SQRT_X 30 +CY 23 20 +SQRT_Y_DAG 5 +X 75 +CY 92 105 +S_DAG 86 +Z 37 +X 115 +X 31 +SQRT_Y_DAG 100 +H 51 +Y 116 +S_DAG 14 +SQRT_Y_DAG 116 +Y 31 +H 70 +H 13 +X 88 +SQRT_X 10 +SQRT_Y 109 +S 74 +CX 28 6 +SQRT_Y 66 +SQRT_Y 117 +SQRT_X 54 +CY 16 19 +SQRT_X 16 +SQRT_X_DAG 94 +S_DAG 6 +SQRT_Y_DAG 23 +SQRT_Y 104 +SQRT_X_DAG 55 +SQRT_Y_DAG 105 +CY 111 82 +SQRT_X_DAG 20 +SQRT_X_DAG 76 +Z 109 +S_DAG 18 +X 32 +CZ 23 19 +CX 52 72 +CX 32 56 +SQRT_X_DAG 59 +H 54 +SQRT_X 34 +CY 28 45 +CY 80 4 +SQRT_X 79 +X 54 +S 107 +X 116 +SQRT_Y 61 +SQRT_Y_DAG 33 +CX 34 31 +SQRT_X_DAG 91 +SQRT_X_DAG 46 +SQRT_Y 108 +SQRT_Y_DAG 59 +CX 31 71 +SQRT_Y 20 +SQRT_X_DAG 36 +CY 46 53 +Y 64 +CX 114 31 +CZ 83 84 +SQRT_X 14 +SQRT_X 76 +SQRT_X_DAG 79 +SQRT_Y 58 +Y 94 +SQRT_X 57 +SQRT_Y_DAG 98 +CZ 89 94 +S_DAG 116 +CY 71 44 +Z 18 +H 85 +CX 22 52 +SQRT_X_DAG 63 +CZ 92 103 +Z 52 +S 116 +S_DAG 73 +SQRT_X 38 +CX 91 100 +S 85 +S_DAG 85 +X 51 +SQRT_Y_DAG 5 +H 58 +Y 14 +X 107 +S_DAG 41 +SQRT_Y_DAG 40 +CZ 51 22 +CZ 106 41 +CY 10 67 +SQRT_Y_DAG 61 +SQRT_X 79 +H 56 +Y 77 +X 44 +X 38 +SQRT_X_DAG 17 +CZ 90 6 +X 42 +SQRT_X 61 +CX 114 78 +X 112 +SQRT_X_DAG 81 +SQRT_Y_DAG 26 +H 94 +SQRT_Y_DAG 41 +Z 42 +S 99 +SQRT_X 72 +SQRT_Y_DAG 92 +SQRT_X_DAG 59 +CY 35 10 +SQRT_Y 27 +SQRT_Y_DAG 46 +H 46 +S_DAG 102 +Z 114 +H 69 +CY 83 92 +SQRT_Y_DAG 28 +H 75 +SQRT_X_DAG 30 +SQRT_X 110 +S 75 +H 65 +Z 0 +SQRT_X 98 +SQRT_X_DAG 94 +S_DAG 83 +Z 86 +H 93 +CZ 77 23 +SQRT_X_DAG 78 +X 101 +CZ 17 117 +H 90 +H 0 +SQRT_Y_DAG 10 +SQRT_X_DAG 99 +CX 25 94 +Z 34 +SQRT_X 78 +X 1 +S_DAG 112 +Y 39 +X 73 +S_DAG 60 +SQRT_X 15 +Y 59 +Z 18 +CY 112 98 +SQRT_X_DAG 116 +CX 8 34 +Z 111 +SQRT_X_DAG 68 +Y 116 +CY 114 67 +CZ 102 37 +S 117 +X 70 +SQRT_Y 26 +Y 53 +Z 23 +SQRT_Y_DAG 39 +SQRT_X_DAG 25 +X 89 +S_DAG 58 +CY 7 19 +H 109 +SQRT_Y_DAG 44 +CX 39 102 +CZ 82 13 +H 19 +H 3 +S_DAG 15 +S 13 +S_DAG 99 +Y 64 +CY 117 109 +CZ 78 17 +S_DAG 16 +SQRT_X 93 +CZ 25 39 +SQRT_Y 97 +X 96 +X 43 +CX 19 92 +Y 110 +SQRT_Y 96 +X 17 +CZ 63 53 +CX 45 61 +CX 37 38 +Y 113 +CX 19 94 +Y 0 +Y 114 +Z 45 +CZ 19 38 +CY 72 86 +SQRT_Y 16 +Y 29 +SQRT_Y_DAG 92 +CY 95 29 +CY 42 11 +Z 85 +SQRT_X 26 +SQRT_X 83 +CX 4 106 +S_DAG 54 +SQRT_X_DAG 83 +SQRT_Y 44 +Z 67 +Y 84 +X 16 +CX 48 18 +SQRT_X 98 +CY 94 28 +CX 34 78 +SQRT_Y 89 +Y 11 +SQRT_Y 106 +SQRT_Y_DAG 92 +SQRT_Y_DAG 22 +H 84 +SQRT_Y_DAG 86 +CZ 9 68 +S_DAG 16 +SQRT_Y 30 +H 86 +SQRT_X_DAG 86 +SQRT_X 58 +Y 40 +H 6 +S_DAG 13 +SQRT_X 2 +SQRT_Y_DAG 14 +CZ 103 33 +S_DAG 70 +H 43 +SQRT_X 29 +X 17 +Z 16 +CZ 106 21 +SQRT_X 19 +Y 82 +S 97 +SQRT_X 59 +X 14 +SQRT_X 0 +CZ 12 106 +X 24 +SQRT_X 0 +H 101 +SQRT_X 104 +S_DAG 108 +Z 22 +S 109 +S 103 +S 72 +H 66 +S 39 +SQRT_Y_DAG 53 +SQRT_X_DAG 61 +SQRT_X 98 +S 50 +Z 83 +CY 53 37 +CY 73 58 +SQRT_Y 54 +SQRT_Y_DAG 88 +S_DAG 69 +Y 72 +X 32 +SQRT_X 113 +SQRT_Y_DAG 58 +CZ 11 114 +SQRT_Y 69 +SQRT_X_DAG 67 +CX 35 20 +H 47 +Y 94 +H 27 +SQRT_X 16 +SQRT_Y 113 +H 64 +SQRT_X 114 +Z 110 +Y 39 +SQRT_X_DAG 18 +CY 65 11 +CZ 47 86 +Z 28 +CY 70 5 +SQRT_X 71 +X 114 +SQRT_X 69 +CY 38 29 +Z 93 +SQRT_X 101 +H 114 +H 73 +H 64 +S 70 +S 77 +S 20 +X 82 +SQRT_Y_DAG 51 +SQRT_Y 116 +CZ 6 7 +CY 62 24 +SQRT_Y 41 +S 93 +CX 5 59 +S_DAG 23 +SQRT_X_DAG 6 +H 47 +S 95 +SQRT_X_DAG 33 +CX 64 112 +S 6 +CZ 41 1 +SQRT_X 83 +H 23 +SQRT_X 88 +CX 61 17 +SQRT_X 88 +S 9 +CX 22 97 +CY 6 53 +Y 68 +SQRT_Y_DAG 19 +X 3 +S_DAG 27 +CX 83 72 +CY 68 60 +SQRT_X 85 +CY 78 88 +SQRT_Y_DAG 42 +Y 107 +X 9 +H 2 +CZ 67 108 +CY 106 113 +SQRT_Y 3 +Z 3 +S 32 +Y 2 +CX 75 2 +S_DAG 115 +S 13 +SQRT_X 85 +CX 81 84 +Z 52 +X 30 +H 113 +Z 115 +SQRT_Y_DAG 91 +SQRT_X 69 +X 73 +S 13 +H 74 +H 6 +H 104 +CX 30 87 +Z 79 +X 21 +Y 74 +H 82 +H 36 +S_DAG 88 +Y 112 +CX 26 81 +Z 72 +CY 31 13 +S_DAG 62 +CZ 23 26 +H 49 +S 0 +Y 29 +SQRT_Y_DAG 39 +CX 47 37 +SQRT_Y 99 +S 84 +X 97 +S 11 +SQRT_X_DAG 64 +CX 17 77 +SQRT_Y_DAG 45 +S 116 +SQRT_Y_DAG 54 +SQRT_Y_DAG 54 +CX 104 12 +SQRT_Y 23 +CX 77 29 +SQRT_Y_DAG 81 +Z 8 +SQRT_X_DAG 10 +SQRT_Y_DAG 18 +SQRT_X_DAG 3 +Z 48 +H 61 +S_DAG 66 +CX 17 55 +H 13 +S_DAG 43 +S 55 +Z 31 +SQRT_Y_DAG 33 +SQRT_X_DAG 103 +S_DAG 0 +CZ 116 106 +X 0 +Y 26 +S 7 +CY 56 64 +CY 28 26 +H 107 +Y 4 +X 14 +SQRT_X 14 +SQRT_X 72 +SQRT_Y_DAG 54 +CX 65 3 +CY 112 95 +X 22 +Y 116 +H 106 +H 10 +CZ 35 66 +SQRT_Y 116 +S 14 +CY 103 23 +CY 76 38 +H 94 +SQRT_Y_DAG 11 +Z 87 +X 116 +X 85 +Y 115 +CX 102 58 +SQRT_X_DAG 2 +CX 114 75 +X 107 +H 104 +CZ 42 86 +Y 30 +CZ 35 100 +CY 110 14 +Z 24 +Y 54 +S 109 +SQRT_X_DAG 14 +S 76 +SQRT_Y_DAG 18 +S_DAG 48 +H 75 +H 25 +SQRT_X_DAG 56 +Y 62 +SQRT_X 83 +S_DAG 91 +X 58 +X 54 +SQRT_X_DAG 92 +CX 90 88 +H 103 +SQRT_Y 52 +SQRT_Y 82 +CY 110 113 +X 100 +CX 103 67 +SQRT_X 34 +Y 57 +S 38 +CZ 113 53 +H 96 +CX 112 104 +CY 21 112 +CZ 31 111 +Y 96 +X 35 +SQRT_Y 89 +Z 49 +SQRT_X 74 +CZ 4 86 +CX 17 90 +CX 5 7 +H 4 +CZ 32 68 +CY 106 23 +Z 63 +S_DAG 86 +Y 90 +SQRT_X 70 +SQRT_Y_DAG 108 +Z 13 +SQRT_X 36 +H 0 +CX 26 98 +S 7 +H 104 +H 87 +SQRT_X_DAG 112 +SQRT_Y 95 +SQRT_X_DAG 57 +CY 2 4 +S 52 +SQRT_Y 79 +CX 67 27 +SQRT_X 12 +SQRT_X_DAG 59 +SQRT_Y 106 +CX 21 71 +Z 38 +SQRT_Y_DAG 18 +CZ 69 108 +Y 66 +Z 3 +S_DAG 92 +CY 6 78 +CZ 77 34 +CX 103 50 +S_DAG 76 +SQRT_Y_DAG 73 +CX 16 8 +SQRT_Y_DAG 31 +Z 74 +S_DAG 90 +S_DAG 6 +CY 108 27 +SQRT_Y_DAG 6 +H 46 +X 1 +CY 87 62 +SQRT_X 77 +Z 90 +X 76 +CX 53 8 +S_DAG 20 +SQRT_Y 66 +H 13 +S 107 +CY 32 43 +SQRT_X_DAG 92 +CX 35 59 +CZ 100 27 +S_DAG 70 +X 78 +X 95 +SQRT_Y_DAG 94 +SQRT_Y_DAG 117 +Y 52 +SQRT_X 53 +SQRT_Y_DAG 58 +SQRT_Y_DAG 35 +SQRT_X 97 +X 32 +X 2 +Z 77 +CX 44 27 +H 105 +Y 113 +SQRT_Y 45 +S 117 +S_DAG 6 +Y 43 +CX 92 38 +Z 84 +Z 32 +S 62 +SQRT_Y 70 +CY 68 33 +S 72 +S_DAG 66 +CY 75 116 +SQRT_Y 59 +SQRT_X 103 +SQRT_X 10 +SQRT_X 10 +SQRT_Y 19 +Y 41 +SQRT_X 61 +CY 111 87 +SQRT_Y_DAG 10 +CZ 52 51 +SQRT_X_DAG 79 +S 117 +SQRT_X_DAG 2 +SQRT_Y 63 +SQRT_Y 46 +CX 53 113 +CZ 25 77 +Y 1 +Z 106 +CX 33 91 +X 37 +X 84 +SQRT_X 99 +S 50 +CZ 31 105 +CX 55 103 +SQRT_Y 34 +SQRT_Y_DAG 48 +SQRT_X 34 +X 33 +Z 110 +S 0 +H 2 +H 23 +S_DAG 100 +S 72 +H 96 +CX 89 43 +SQRT_X 75 +Z 2 +X 13 +H 52 +CX 15 48 +S 17 +S 25 +SQRT_X 4 +CX 110 53 +SQRT_X 43 +Y 26 +SQRT_X_DAG 18 +CZ 68 81 +SQRT_X_DAG 51 +SQRT_X_DAG 90 +S 104 +SQRT_Y_DAG 117 +H 54 +SQRT_X_DAG 30 +CX 113 105 +SQRT_X_DAG 81 +S 26 +CX 60 110 +SQRT_X 3 +S 67 +S 116 +S 40 +H 73 +CX 53 6 +Y 12 +Z 21 +S_DAG 36 +CY 68 28 +CX 25 97 +CX 9 13 +SQRT_Y 32 +SQRT_Y 57 +S_DAG 64 +CY 18 109 +H 29 +Y 38 +SQRT_Y 62 +CY 61 58 +CX 88 99 +SQRT_Y 43 +S 19 +SQRT_X_DAG 56 +X 113 +SQRT_X 100 +SQRT_Y 51 +Z 117 +H 76 +S_DAG 66 +SQRT_X 36 +SQRT_X_DAG 17 +S 46 +CX 53 87 +CX 85 4 +Z 99 +S_DAG 88 +CX 106 66 +SQRT_Y_DAG 27 +SQRT_Y_DAG 74 +SQRT_X 43 +SQRT_Y 27 +CY 34 86 +Z 99 +CX 39 74 +SQRT_Y_DAG 30 +CX 73 9 +X 87 +SQRT_Y 88 +CZ 104 116 +CY 62 0 +SQRT_Y 25 +X 8 +SQRT_Y 82 +S_DAG 21 +SQRT_X 23 +SQRT_Y_DAG 75 +CY 38 110 +SQRT_X 41 +Z 74 +CX 1 87 +SQRT_Y 74 +CY 59 117 +CY 115 2 +SQRT_X 100 +CZ 21 115 +Z 31 +CX 15 58 +H 43 +H 10 +S 6 +Y 34 +SQRT_X 12 +CZ 12 25 +CZ 48 24 +SQRT_X 72 +CZ 31 104 +SQRT_X_DAG 80 +CX 27 9 +Z 108 +SQRT_Y 10 +S_DAG 113 +CZ 24 27 +S_DAG 88 +SQRT_X_DAG 45 +CY 35 116 +Z 57 +CY 81 64 +S 69 +SQRT_Y_DAG 106 +Y 3 +SQRT_Y 69 +Z 42 +SQRT_Y 31 +H 106 +H 31 +CY 52 90 +SQRT_X 36 +Z 87 +S 84 +X 55 +SQRT_Y 84 +CX 58 17 +S 42 +SQRT_Y 74 +Y 19 +SQRT_Y 89 +CX 51 76 +S_DAG 4 +CZ 52 63 +SQRT_X 97 +CZ 11 63 +SQRT_X_DAG 60 +CZ 5 100 +CZ 84 22 +H 94 +S_DAG 50 +S_DAG 106 +CX 78 41 +SQRT_Y_DAG 32 +SQRT_Y_DAG 84 +CY 92 90 +SQRT_Y 58 +SQRT_Y_DAG 100 +SQRT_X 53 +CZ 94 16 +CY 67 79 +Z 24 +CZ 27 81 +H 13 +X 6 +SQRT_X 9 +SQRT_X 69 +SQRT_X_DAG 22 +S_DAG 48 +Z 98 +X 3 +SQRT_X 52 +SQRT_Y_DAG 19 +Z 98 +SQRT_Y 38 +CY 18 13 +Y 23 +H 27 +CZ 25 14 +S_DAG 5 +CY 30 43 +X 64 +CZ 115 97 +CX 68 19 +S_DAG 4 +SQRT_Y 91 +S 31 +Z 70 +H 94 +H 109 +CZ 56 6 +SQRT_Y 64 +SQRT_X 38 +CZ 62 66 +X 23 +SQRT_Y_DAG 30 +Y 44 +X 61 +CY 76 2 +CY 55 15 +H 49 +S_DAG 79 +CZ 60 13 +H 59 +CY 9 77 +CX 65 68 +H 19 +X 61 +SQRT_X_DAG 100 +S_DAG 78 +SQRT_X_DAG 61 +SQRT_X_DAG 98 +S 16 +SQRT_Y_DAG 78 +S 74 +SQRT_X_DAG 39 +Y 12 +X 108 +SQRT_X 95 +Y 40 +X 39 +CY 2 26 +SQRT_Y_DAG 117 +Z 45 +CX 80 107 +S_DAG 83 +CY 18 11 +S 78 +Y 105 +Y 19 +SQRT_Y_DAG 101 +H 0 +S 75 +Z 91 +X 49 +S_DAG 104 +Y 39 +SQRT_X_DAG 83 +SQRT_X_DAG 101 +Z 97 +SQRT_Y_DAG 7 +CY 106 30 +SQRT_X 92 +S 88 +CY 99 43 +Z 72 +SQRT_Y_DAG 58 +S_DAG 10 +S 39 +H 9 +Y 43 +SQRT_Y 61 +SQRT_Y 1 +CX 16 82 +S_DAG 46 +S 97 +SQRT_X_DAG 65 +X 18 +CX 74 45 +CZ 69 75 +Z 34 +S 116 +S 18 +CX 99 3 +SQRT_X 16 +SQRT_X 49 +CZ 15 65 +CX 103 44 +SQRT_X 40 +S 40 +SQRT_X_DAG 4 +S 50 +S 8 +Y 103 +CY 115 83 +CZ 61 17 +Z 105 +CX 15 52 +SQRT_Y 111 +S 66 +SQRT_X 104 +X 80 +SQRT_X_DAG 94 +CY 4 114 +Z 25 +X 16 +SQRT_X_DAG 92 +SQRT_X_DAG 108 +X 31 +CZ 70 93 +CZ 92 108 +CY 82 95 +H 81 +SQRT_Y 80 +CY 8 100 +CZ 38 75 +CY 106 19 +H 68 +SQRT_X_DAG 27 +S 61 +CZ 9 19 +SQRT_X_DAG 6 +H 32 +S 76 +X 114 +X 72 +S_DAG 66 +S 48 +SQRT_X_DAG 64 +CY 91 80 +X 44 +SQRT_Y_DAG 86 +S_DAG 28 +S 1 +X 44 +SQRT_X 2 +SQRT_Y_DAG 65 +H 57 +Y 98 +CX 58 2 +CZ 112 71 +SQRT_X_DAG 42 +Z 56 +S_DAG 98 +SQRT_Y_DAG 37 +CZ 101 76 +S_DAG 34 +Z 91 +SQRT_Y 107 +SQRT_Y 51 +S 6 +S 82 +S_DAG 64 +CZ 71 53 +SQRT_Y 102 +S 84 +Z 10 +CX 17 38 +SQRT_Y_DAG 51 +S_DAG 71 +SQRT_X_DAG 8 +CY 78 33 +CX 111 59 +S 15 +CX 68 102 +SQRT_Y_DAG 99 +CZ 112 107 +S 38 +Y 111 +SQRT_Y_DAG 68 +CZ 77 13 +CY 77 52 +CZ 81 80 +X 96 +X 75 +SQRT_X 80 +H 41 +H 58 +SQRT_Y 36 +CZ 109 59 +S_DAG 47 +SQRT_X 42 +X 71 +SQRT_Y 80 +CX 60 30 +SQRT_X_DAG 94 +H 83 +SQRT_Y 57 +CX 106 78 +SQRT_Y_DAG 92 +SQRT_Y_DAG 26 +Y 112 +SQRT_X 68 +Z 50 +SQRT_Y_DAG 48 +Z 75 +CX 110 50 +S_DAG 98 +X 49 +SQRT_X 80 +S 7 +S 13 +H 71 +CZ 33 37 +CY 1 93 +X 116 +S_DAG 3 +S_DAG 107 +S 88 +CZ 112 96 +SQRT_X 89 +CY 54 28 +Z 97 +SQRT_Y_DAG 4 +CX 67 63 +SQRT_Y_DAG 57 +CY 102 22 +H 55 +SQRT_X_DAG 69 +SQRT_Y 7 +CY 12 112 +Y 55 +S 54 +S_DAG 34 +SQRT_Y 24 +CX 68 94 +SQRT_X 10 +CX 15 84 +CY 24 1 +CY 88 57 +Y 52 +SQRT_Y_DAG 23 +CX 78 74 +CX 9 100 +CZ 44 53 +SQRT_Y 61 +SQRT_Y_DAG 72 +SQRT_X 35 +X 93 +H 115 +X 60 +SQRT_Y 6 +Z 74 +SQRT_Y 50 +SQRT_X_DAG 81 +SQRT_X 46 +Z 92 +H 102 +S 50 +SQRT_X 7 +SQRT_X 104 +SQRT_X 25 +SQRT_X 65 +S_DAG 71 +Z 62 +SQRT_Y 66 +CY 24 89 +S_DAG 102 +SQRT_Y_DAG 10 +S_DAG 75 +X 113 +Y 16 +CX 74 20 +SQRT_X_DAG 34 +SQRT_X_DAG 99 +Y 111 +Y 75 +S_DAG 0 +SQRT_X_DAG 72 +S 94 +S 67 +H 24 +S 57 +Z 108 +H 95 +SQRT_X 92 +CY 32 101 +SQRT_Y 90 +SQRT_Y_DAG 89 +SQRT_Y_DAG 7 +S_DAG 3 +S_DAG 39 +SQRT_X 39 +CX 59 106 +CX 44 99 +CZ 108 34 +H 116 +CX 75 48 +Z 37 +SQRT_X_DAG 80 +S_DAG 15 +SQRT_X_DAG 51 +Z 92 +S_DAG 93 +CX 112 69 +SQRT_X_DAG 3 +H 104 +CZ 36 72 +X 114 +CX 0 85 +Y 77 +Z 11 +X 2 +H 92 +Z 65 +X 45 +SQRT_Y 45 +Y 10 +SQRT_X_DAG 87 +CZ 86 28 +SQRT_X 92 +S 30 +SQRT_Y_DAG 52 +X 20 +CZ 44 30 +S 55 +SQRT_X 55 +CY 24 23 +CY 102 50 +CY 63 35 +CY 22 50 +CZ 49 28 +H 103 +CY 110 13 +SQRT_Y 52 +SQRT_X_DAG 14 +X 12 +H 62 +Z 83 +H 18 +S_DAG 81 +X 44 +X 69 +S 31 +H 106 +S 110 +Z 114 +X 32 +SQRT_X_DAG 76 +SQRT_Y 113 +SQRT_Y_DAG 61 +CX 22 80 +SQRT_X 95 +Z 11 +X 27 +SQRT_X 56 +CY 10 43 +Z 107 +CY 8 76 +CX 39 51 +CY 34 19 +SQRT_Y 66 +Y 106 +SQRT_Y 49 +CY 22 110 +S_DAG 19 +SQRT_Y 42 +SQRT_X 75 +Y 117 +Z 15 +CZ 35 56 +CY 74 31 +H 50 +S 23 +H 105 +CY 99 48 +CX 66 113 +CX 41 9 +S 91 +SQRT_X_DAG 81 +CY 33 97 +X 54 +S 36 +S 87 +X 85 +CZ 32 34 +CY 112 72 +SQRT_X_DAG 6 +SQRT_Y_DAG 85 +CX 111 50 +H 91 +Y 76 +SQRT_X 103 +S_DAG 15 +S 66 +S_DAG 66 +Y 41 +S 31 +Z 11 +SQRT_Y_DAG 92 +SQRT_Y 11 +SQRT_X 19 +Z 97 +Z 52 +SQRT_X_DAG 73 +S_DAG 114 +SQRT_Y 9 +CX 100 55 +S_DAG 8 +SQRT_Y_DAG 9 +SQRT_X_DAG 20 +SQRT_X 30 +CZ 70 43 +SQRT_X 106 +CX 67 63 +CX 101 99 +CX 31 116 +S_DAG 102 +CX 62 96 +SQRT_Y_DAG 1 +S 67 +H 6 +S_DAG 89 +SQRT_Y_DAG 38 +SQRT_Y 31 +S_DAG 6 +SQRT_X_DAG 36 +Y 46 +SQRT_X 83 +CZ 76 22 +Z 13 +SQRT_Y 27 +CY 30 65 +CZ 75 28 +SQRT_X_DAG 38 +CX 69 94 +SQRT_Y 44 +H 101 +SQRT_Y_DAG 108 +X 22 +CZ 41 71 +S_DAG 42 +CX 52 45 +SQRT_Y_DAG 62 +Z 75 +CZ 102 99 +H 38 +SQRT_X 108 +S_DAG 24 +CZ 116 74 +S 116 +SQRT_Y_DAG 27 +CY 110 67 +SQRT_X_DAG 71 +SQRT_X_DAG 86 +H 96 +SQRT_X_DAG 81 +SQRT_Y_DAG 42 +SQRT_Y_DAG 98 +S 115 +S_DAG 68 +SQRT_Y_DAG 110 +S_DAG 0 +SQRT_X_DAG 101 +CX 54 40 +CY 100 3 +CY 33 26 +SQRT_X 102 +X 66 +H 4 +SQRT_X_DAG 103 +CZ 23 47 +SQRT_X_DAG 49 +SQRT_Y_DAG 97 +SQRT_X_DAG 95 +SQRT_Y 2 +H 31 +S_DAG 14 +H 117 +Z 29 +CZ 31 99 +S 91 +Y 68 +Z 113 +SQRT_Y_DAG 78 +X 19 +CY 0 95 +SQRT_X_DAG 74 +X 80 +SQRT_Y_DAG 30 +S_DAG 110 +S_DAG 78 +SQRT_Y_DAG 112 +X 86 +S 78 +H 103 +CX 72 107 +SQRT_Y 61 +H 23 +S_DAG 50 +SQRT_Y 102 +H 94 +CX 116 109 +CZ 18 85 +Z 81 +S 28 +SQRT_X_DAG 99 +S 104 +S 29 +SQRT_X 47 +H 55 +CZ 9 15 +Z 45 +CY 50 52 +SQRT_X_DAG 21 +X 58 +Z 63 +SQRT_X 10 +SQRT_X 108 +SQRT_Y 22 +CY 37 29 +CZ 90 13 +CY 11 66 +CY 30 73 +SQRT_X_DAG 34 +H 106 +SQRT_X_DAG 73 +S_DAG 41 +SQRT_X 55 +H 115 +Z 106 +CY 13 58 +X 43 +S 51 +Y 63 +SQRT_X_DAG 18 +Z 101 +S 24 +SQRT_X_DAG 25 +SQRT_X 41 +H 31 +CZ 24 106 +CY 67 33 +SQRT_Y_DAG 55 +SQRT_X_DAG 67 +CZ 103 63 +CX 29 101 +Z 6 +H 80 +S 5 +SQRT_Y_DAG 49 +SQRT_X_DAG 46 +CZ 41 29 +S_DAG 93 +SQRT_X_DAG 100 +SQRT_Y_DAG 73 +CX 70 64 +S_DAG 43 +Z 57 +CZ 103 83 +CY 104 50 +H 11 +SQRT_X_DAG 38 +S_DAG 23 +SQRT_X_DAG 112 +Y 66 +SQRT_Y_DAG 0 +Y 47 +S_DAG 52 +S_DAG 32 +Z 69 +Y 22 +S_DAG 76 +CY 31 38 +CZ 115 36 +SQRT_Y 96 +SQRT_X 92 +SQRT_Y_DAG 73 +S_DAG 59 +CY 28 80 +Y 67 +SQRT_Y 107 +S_DAG 40 +SQRT_Y_DAG 14 +CZ 11 27 +S_DAG 115 +SQRT_Y 35 +CZ 10 80 +SQRT_X_DAG 32 +S 10 +SQRT_Y_DAG 85 +CX 14 23 +CY 2 79 +Y 115 +CX 56 31 +SQRT_X_DAG 95 +CX 86 13 +S_DAG 108 +SQRT_X_DAG 76 +SQRT_Y 88 +S 8 +S 111 +Z 85 +S_DAG 52 +SQRT_X 110 +H 60 +S 72 +SQRT_X 12 +SQRT_X 51 +Y 3 +CX 111 62 +S 26 +X 96 +X 38 +Z 69 +SQRT_Y 42 +Z 61 +S_DAG 27 +SQRT_Y 32 +X 88 +SQRT_Y_DAG 101 +CX 71 64 +S_DAG 42 +CX 54 50 +SQRT_X 115 +S 55 +X 103 +H 9 +S 85 +SQRT_Y_DAG 0 +S 82 +SQRT_X_DAG 48 +SQRT_X_DAG 82 +SQRT_X_DAG 14 +S_DAG 108 +S_DAG 73 +CZ 65 28 +CX 45 14 +Z 82 +SQRT_X 79 +S_DAG 81 +SQRT_X_DAG 5 +SQRT_Y 36 +Y 20 +S 64 +CZ 23 80 +Z 6 +Z 84 +CX 11 26 +X 32 +SQRT_Y 83 +S_DAG 5 +S 9 +Y 33 +SQRT_X 102 +CY 43 27 +SQRT_X 14 +CY 20 108 +Y 5 +SQRT_X_DAG 108 +Z 60 +SQRT_Y_DAG 37 +SQRT_X 62 +CZ 13 28 +SQRT_X 44 +X 92 +CZ 44 36 +CX 59 91 +X 3 +SQRT_X_DAG 12 +CZ 22 62 +SQRT_X_DAG 103 +SQRT_Y 55 +X 31 +CZ 7 10 +CX 90 63 +CY 81 94 +X 114 +S 79 +SQRT_X_DAG 34 +H 55 +Y 31 +CX 95 31 +CZ 68 79 +SQRT_Y_DAG 98 +CY 112 14 +H 8 +X 67 +SQRT_Y_DAG 79 +CX 111 36 +Y 69 +S 92 +SQRT_Y_DAG 71 +SQRT_Y_DAG 100 +X 116 +X 98 +S 76 +Y 64 +H 83 +SQRT_X 83 +S 27 +Z 62 +SQRT_X 98 +X 86 +CZ 52 79 +Y 30 +S 111 +Y 36 +CZ 37 18 +Z 20 +X 15 +Z 58 +SQRT_Y_DAG 96 +SQRT_X_DAG 16 +SQRT_Y_DAG 94 +CZ 1 24 +SQRT_Y_DAG 76 +SQRT_Y_DAG 26 +CZ 53 69 +H 19 +H 40 +CZ 41 103 +S_DAG 38 +Y 105 +SQRT_X 94 +Y 87 +Z 32 +Y 41 +SQRT_X 117 +SQRT_X_DAG 57 +S 9 +S_DAG 45 +Y 108 +SQRT_X_DAG 97 +Y 46 +SQRT_Y_DAG 101 +SQRT_Y 63 +CX 104 50 +SQRT_Y_DAG 105 +CY 80 50 +Y 103 +SQRT_Y_DAG 79 +SQRT_X 111 +H 85 +Y 115 +S 1 +H 93 +SQRT_X_DAG 104 +SQRT_Y 75 +H 16 +S_DAG 46 +SQRT_Y 70 +S_DAG 74 +X 114 +S_DAG 103 +Z 114 +Z 11 +S_DAG 92 +H 97 +CZ 1 93 +CZ 53 79 +CZ 114 8 +SQRT_Y 101 +S 73 +SQRT_X 52 +S 91 +SQRT_Y_DAG 45 +SQRT_Y 43 +S 111 +SQRT_X_DAG 53 +SQRT_X 58 +Y 15 +S_DAG 9 +CX 64 7 +SQRT_Y 82 +X 57 +Z 16 +S_DAG 24 +Y 99 +Y 2 +Y 26 +Z 62 +CY 105 100 +X 65 +Z 65 +SQRT_Y 103 +SQRT_Y 69 +SQRT_X 100 +SQRT_X_DAG 65 +Y 49 +SQRT_Y 27 +SQRT_X_DAG 73 +SQRT_Y_DAG 86 +SQRT_X_DAG 8 +S_DAG 69 +X 30 +SQRT_X_DAG 53 +Y 2 +SQRT_Y 29 +SQRT_X_DAG 74 +Y 46 +SQRT_Y 56 +CX 34 93 +S 38 +CX 9 17 +X 22 +Y 100 +SQRT_X 24 +X 32 +SQRT_X_DAG 81 +CY 33 3 +CZ 80 68 +Z 22 +Y 55 +Y 2 +X 56 +S 13 +S_DAG 14 +S 58 +S_DAG 66 +Z 33 +Z 87 +S 1 +H 88 +S 55 +X 88 +SQRT_X_DAG 13 +Y 30 +X 55 +SQRT_X 57 +S 31 +Y 14 +S 84 +SQRT_Y 60 +S 92 +SQRT_X 109 +CZ 56 63 +H 10 +CZ 11 98 +Z 50 +S_DAG 88 +X 27 +SQRT_Y 53 +CZ 74 78 +SQRT_X_DAG 102 +SQRT_X_DAG 2 +H 10 +SQRT_Y 88 +SQRT_Y_DAG 109 +Z 40 +SQRT_Y_DAG 45 +SQRT_X_DAG 23 +SQRT_X 117 +SQRT_X_DAG 60 +S_DAG 96 +S_DAG 75 +CX 41 21 +SQRT_X_DAG 114 +X 89 +S 103 +CX 64 1 +CX 83 117 +CY 88 111 +S 26 +X 80 +X 100 +CX 38 115 +SQRT_X 92 +Y 73 +CY 28 29 +CY 36 6 +Y 48 +Z 19 +CY 70 13 +S 98 +SQRT_Y 34 +CX 108 93 +X 116 +SQRT_Y_DAG 85 +H 59 +CY 3 95 +SQRT_X_DAG 103 +CZ 66 59 +Z 87 +SQRT_Y 16 +X 48 +Z 39 +S_DAG 17 +SQRT_X_DAG 117 +S_DAG 81 +S 68 +S 12 +SQRT_Y_DAG 20 +CZ 95 109 +S_DAG 111 +SQRT_Y 44 +CX 82 48 +Y 95 +CX 102 47 +S 0 +H 96 +Y 104 +H 1 +CX 24 13 +S 50 +S 11 +CY 24 12 +Z 19 +H 28 +S 0 +SQRT_Y_DAG 74 +SQRT_Y 64 +CY 91 84 +SQRT_Y_DAG 52 +S_DAG 106 +SQRT_Y 64 +Z 39 +S 117 +SQRT_X_DAG 48 +SQRT_X 58 +SQRT_Y_DAG 51 +CY 15 59 +S_DAG 54 +CZ 8 108 +SQRT_X_DAG 71 +X 23 +SQRT_Y_DAG 29 +CX 64 79 +SQRT_Y 33 +SQRT_Y 7 +Z 67 +CY 32 6 +CX 109 103 +Y 110 +CY 57 38 +SQRT_X 58 +S 110 +SQRT_Y 11 +S 16 +SQRT_X 49 +SQRT_X_DAG 102 +CZ 87 30 +SQRT_Y 20 +CZ 78 73 +CZ 101 2 +H 52 +SQRT_X 91 +S 38 +X 36 +CZ 64 77 +SQRT_X_DAG 30 +Z 104 +Z 93 +CY 24 74 +S 89 +CX 32 83 +SQRT_Y 42 +S_DAG 65 +S_DAG 110 +CY 105 8 +SQRT_X_DAG 43 +SQRT_X 75 +S 104 +SQRT_X_DAG 9 +Y 78 +Z 31 +CX 9 60 +Z 22 +S_DAG 27 +S_DAG 92 +SQRT_Y_DAG 6 +S_DAG 14 +H 17 +SQRT_X_DAG 77 +SQRT_Y 51 +SQRT_Y 6 +SQRT_X 59 +SQRT_Y 38 +CZ 45 29 +CZ 39 55 +X 91 +X 50 +Y 102 +CZ 103 90 +CZ 95 64 +X 15 +CX 82 88 +X 20 +CY 93 105 +CX 117 44 +SQRT_X 17 +Z 76 +H 21 +X 19 +SQRT_Y 66 +S_DAG 56 +SQRT_Y 47 +Z 68 +CY 66 45 +S 17 +S_DAG 89 +X 73 +CZ 12 17 +Y 90 +H 73 +S 79 +CY 41 50 +SQRT_X_DAG 7 +Y 78 +SQRT_X_DAG 45 +SQRT_X_DAG 9 +SQRT_Y_DAG 61 +SQRT_X_DAG 35 +SQRT_Y_DAG 55 +SQRT_Y 86 +Z 20 +Z 82 +CZ 81 100 +CY 22 81 +S 103 +CZ 11 80 +S 41 +S_DAG 104 +H 56 +S 33 +SQRT_X_DAG 27 +CZ 95 50 +S 92 +X 73 +Z 12 +CY 60 111 +SQRT_Y_DAG 106 +CZ 92 98 +Y 50 +S_DAG 94 +SQRT_X_DAG 11 +SQRT_Y_DAG 116 +H 39 +SQRT_Y 63 +SQRT_Y 92 +Y 113 +X 32 +CY 24 79 +SQRT_Y_DAG 106 +Y 5 +X 97 +X 87 +CY 60 107 +SQRT_X 81 +SQRT_X_DAG 73 +Y 39 +SQRT_X 43 +X 49 +CX 33 56 +CZ 71 28 +Y 82 +Y 6 +CZ 80 61 +CZ 109 28 +CZ 105 74 +SQRT_Y_DAG 32 +SQRT_X 78 +S_DAG 59 +CZ 36 32 +SQRT_X 79 +Y 59 +SQRT_X 116 +H 80 +SQRT_X_DAG 117 +CY 71 83 +S_DAG 1 +SQRT_X 28 +CZ 104 68 +Y 8 +SQRT_X_DAG 73 +CY 46 116 +X 14 +CZ 26 14 +X 81 +CX 69 62 +X 60 +Z 107 +SQRT_X 105 +CZ 14 115 +S 79 +S 70 +CZ 25 58 +S 76 +S_DAG 108 +H 96 +CZ 55 41 +S_DAG 87 +CZ 12 76 +H 85 +X 62 +H 93 +H 34 +CY 100 51 +X 53 +SQRT_X_DAG 6 +SQRT_X 106 +H 72 +SQRT_X_DAG 81 +S_DAG 76 +X 114 +SQRT_Y 85 +SQRT_Y 115 +H 115 +Y 4 +X 109 +SQRT_X_DAG 116 +H 13 +S_DAG 80 +CY 45 20 +SQRT_X 38 +H 7 +H 104 +SQRT_X_DAG 1 +SQRT_Y_DAG 80 +SQRT_X_DAG 5 +SQRT_X_DAG 89 +CZ 83 68 +SQRT_X 46 +SQRT_X_DAG 33 +CZ 11 72 +S 8 +H 45 +H 41 +Y 2 +H 67 +SQRT_X 28 +Y 86 +CZ 62 37 +X 99 +CZ 71 91 +CY 94 72 +S_DAG 42 +CX 23 4 +Z 108 +SQRT_Y 92 +CX 44 20 +Y 93 +X 23 +CY 112 60 +S_DAG 36 +CZ 9 110 +S 14 +CY 38 9 +X 93 +CZ 50 27 +SQRT_Y_DAG 58 +Z 18 +H 102 +SQRT_X_DAG 92 +SQRT_Y 14 +S_DAG 10 +CZ 13 109 +H 73 +S 58 +SQRT_Y 39 +CZ 10 88 +SQRT_X_DAG 76 +CX 65 101 +CX 61 96 +CX 54 19 +CY 36 6 +X 21 +SQRT_Y 107 +SQRT_Y_DAG 3 +H 50 +CY 59 45 +Z 104 +SQRT_X 68 +S 96 +Y 116 +Z 40 +S_DAG 4 +X 20 +S 99 +S 116 +CZ 31 84 +H 54 +X 39 +CY 48 91 +Y 108 +Z 117 +H 112 +S 116 +CZ 35 54 +CZ 18 2 +CX 46 14 +X 73 +S 25 +CZ 51 87 +CX 77 106 +CX 63 26 +CX 92 35 +CZ 97 30 +SQRT_X 101 +CY 40 7 +SQRT_Y_DAG 56 +SQRT_X_DAG 88 +CY 68 103 +SQRT_X_DAG 40 +CX 60 90 +CZ 51 37 +Y 49 +X 112 +Y 4 +X 10 +CZ 46 1 +SQRT_Y_DAG 117 +SQRT_X_DAG 3 +Y 38 +SQRT_X_DAG 12 +SQRT_X 96 +CY 62 110 +X 89 +X 8 +S_DAG 3 +H 84 +Z 46 +Y 21 +CY 14 41 +SQRT_Y 43 +Y 114 +SQRT_X 85 +Z 39 +Z 113 +S_DAG 89 +X 71 +CX 95 47 +SQRT_Y_DAG 86 +SQRT_Y 56 +X 94 +X 95 +Z 74 +S_DAG 17 +CX 82 18 +SQRT_Y 98 +CZ 104 7 +S_DAG 85 +SQRT_Y 95 +CY 52 84 +S 24 +CX 65 87 +SQRT_X_DAG 31 +S 97 +Y 85 +SQRT_X_DAG 57 +CZ 109 33 +CX 51 17 +SQRT_Y 57 +Z 62 +S 46 +CX 27 113 +CZ 43 44 +CY 63 106 +CZ 93 54 +CX 101 22 +H 83 +X 37 +H 14 +CX 52 58 +SQRT_X_DAG 3 +Z 93 +Y 11 +X 61 +S 22 +S_DAG 26 +SQRT_Y 38 +S 34 +S 41 +CZ 14 0 +SQRT_X_DAG 71 +S_DAG 113 +SQRT_X_DAG 49 +CX 15 37 +SQRT_X_DAG 24 +CX 108 15 +X 55 +H 36 +SQRT_X_DAG 85 +Y 52 +X 117 +Y 102 +CY 67 91 +Y 105 +CY 94 43 +S_DAG 20 +CZ 76 44 +S 44 +S 24 +Z 0 +H 101 +X 99 +Y 107 +SQRT_Y 53 +SQRT_X_DAG 89 +S_DAG 76 +S 28 +SQRT_X_DAG 73 +Y 98 +Y 90 +CX 5 37 +CZ 23 90 +SQRT_X_DAG 7 +SQRT_X 89 +SQRT_X_DAG 15 +S_DAG 41 +X 53 +CY 56 93 +SQRT_X 27 +SQRT_X 8 +SQRT_X 116 +CY 16 41 +H 76 +SQRT_X 87 +CY 23 92 +SQRT_Y_DAG 63 +Z 115 +CY 61 75 +SQRT_X 61 +SQRT_X 102 +SQRT_X_DAG 56 +CZ 100 81 +S 63 +X 37 +CX 16 19 +Z 19 +CX 43 39 +SQRT_X_DAG 100 +H 1 +Y 39 +S 61 +X 100 +SQRT_X 91 +X 36 +SQRT_Y_DAG 90 +CY 100 51 +CX 87 69 +H 35 +SQRT_Y 79 +Z 51 +S 76 +SQRT_X 12 +CZ 86 99 +SQRT_Y 108 +CZ 93 66 +SQRT_X 90 +SQRT_Y_DAG 13 +S_DAG 110 +Y 67 +CZ 64 97 +CY 64 14 +SQRT_Y 92 +SQRT_X 32 +CY 11 8 +CZ 54 115 +Y 42 +SQRT_X 41 +SQRT_X_DAG 86 +SQRT_X_DAG 1 +SQRT_Y_DAG 0 +Y 102 +SQRT_X_DAG 30 +SQRT_X_DAG 55 +H 71 +CX 101 72 +SQRT_Y 53 +SQRT_Y_DAG 96 +Y 48 +S_DAG 44 +CX 43 79 +SQRT_X_DAG 74 +CX 31 100 +CZ 108 105 +CZ 35 7 +Z 71 +SQRT_X_DAG 61 +CY 68 46 +H 92 +Y 105 +S 0 +Y 57 +H 59 +Z 30 +SQRT_X 3 +X 107 +S 104 +S_DAG 37 +X 45 +SQRT_X 109 +Y 83 +SQRT_X 66 +CY 24 35 +CZ 65 1 +SQRT_Y 44 +H 97 +Y 113 +Z 105 +Y 16 +H 99 +S_DAG 51 +CZ 26 19 +CZ 0 65 +S 32 +S_DAG 66 +SQRT_X 51 +SQRT_X_DAG 74 +Z 97 +Z 98 +Y 36 +S_DAG 24 +H 87 +S 29 +SQRT_X_DAG 47 +Y 66 +Y 79 +CX 67 83 +S_DAG 27 +S 86 +S 89 +Y 112 +CZ 27 111 +CZ 94 19 +CZ 32 12 +H 102 +SQRT_Y_DAG 58 +SQRT_Y 87 +SQRT_Y 70 +SQRT_Y_DAG 110 +X 18 +Y 42 +Y 61 +S_DAG 9 +Z 47 +CY 16 117 +CX 26 85 +CY 102 62 +CZ 69 98 +CY 24 57 +CZ 79 55 +CX 105 36 +SQRT_Y 99 +S_DAG 18 +SQRT_X 7 +H 82 +Y 97 +X 35 +SQRT_Y_DAG 92 +S 6 +CX 37 6 +SQRT_X_DAG 63 +SQRT_X_DAG 82 +S 37 +S 33 +CZ 80 73 +SQRT_X 49 +SQRT_Y_DAG 58 +CZ 79 25 +SQRT_Y_DAG 44 +CX 108 5 +CY 17 6 +SQRT_X_DAG 55 +SQRT_Y_DAG 16 +S 64 +Z 28 +SQRT_X 95 +S_DAG 18 +Y 89 +SQRT_Y_DAG 96 +Y 29 +SQRT_X_DAG 20 +SQRT_Y_DAG 41 +H 93 +SQRT_Y 14 +CZ 97 33 +CY 29 117 +SQRT_Y_DAG 86 +H 69 +X 60 +SQRT_X 52 +SQRT_X 63 +Z 102 +CY 8 12 +CX 82 34 +Y 14 +SQRT_X 48 +Z 50 +X 45 +CX 94 59 +S 40 +Y 25 +H 76 +Y 108 +X 31 +Z 86 +SQRT_X 44 +CX 23 8 +H 97 +SQRT_X 114 +CZ 86 113 +SQRT_Y_DAG 16 +SQRT_Y 107 +H 91 +CX 94 111 +X 5 +Z 84 +CZ 39 51 +SQRT_X_DAG 1 +Z 36 +SQRT_X 39 +S 35 +Z 52 +S 17 +Z 47 +SQRT_Y 25 +S_DAG 51 +SQRT_X 67 +SQRT_Y_DAG 75 +SQRT_Y_DAG 69 +X 76 +SQRT_X_DAG 31 +X 110 +SQRT_Y 82 +CY 88 29 +S_DAG 92 +X 112 +H 78 +CY 50 53 +SQRT_X_DAG 26 +S 98 +X 56 +SQRT_Y 55 +Y 33 +CY 111 73 +Y 53 +H 42 +X 97 +Z 112 +S 93 +SQRT_X 18 +SQRT_X 49 +X 89 +CX 51 9 +Y 23 +CX 42 110 +SQRT_X 50 +SQRT_Y 95 +X 33 +S 90 +S_DAG 22 +S 108 +S_DAG 77 +S 19 +S 84 +CZ 0 42 +SQRT_Y_DAG 47 +SQRT_X 22 +S 79 +SQRT_X 111 +S_DAG 67 +Y 73 +S 103 +CZ 109 95 +S 55 +CX 101 94 +S 25 +CZ 15 96 +SQRT_Y_DAG 68 +S 42 +CZ 27 1 +S 87 +X 5 +X 89 +Z 63 +S_DAG 23 +Z 19 +SQRT_Y_DAG 110 +SQRT_Y 78 +H 10 +CY 65 51 +SQRT_Y 49 +SQRT_Y_DAG 115 +X 62 +X 72 +Z 56 +CY 24 104 +SQRT_X 93 +X 102 +CX 81 31 +X 100 +SQRT_X 3 +S 43 +CX 44 48 +CY 53 96 +Z 59 +Z 7 +H 90 +SQRT_X 16 +SQRT_Y 24 +X 64 +CZ 76 15 +S 54 +SQRT_Y_DAG 2 +SQRT_X_DAG 18 +Z 9 +S_DAG 32 +X 74 +S_DAG 116 +SQRT_Y 69 +H 13 +CZ 93 36 +CX 5 25 +Y 75 +X 78 +S 82 +SQRT_Y_DAG 40 +CX 70 78 +SQRT_X 71 +CZ 10 18 +H 90 +SQRT_X_DAG 0 +SQRT_X 77 +S 101 +S 101 +S_DAG 111 +SQRT_Y_DAG 29 +SQRT_Y 54 +Z 92 +SQRT_X_DAG 29 +H 9 +H 63 +S_DAG 38 +CZ 56 53 +H 4 +X 106 +H 17 +SQRT_Y_DAG 117 +CX 93 29 +CX 39 85 +CY 12 78 +SQRT_X_DAG 66 +SQRT_X 36 +S_DAG 72 +S_DAG 16 +Z 60 +S_DAG 46 +CZ 23 8 +SQRT_X 70 +S 65 +S 103 +CX 91 53 +SQRT_X_DAG 90 +H 68 +S 112 +SQRT_X 3 +Y 64 +SQRT_Y_DAG 64 +SQRT_X_DAG 8 +CX 32 4 +Z 116 +SQRT_Y_DAG 84 +SQRT_Y 38 +Y 91 +H 26 +CZ 24 63 +SQRT_X_DAG 111 +S 45 +CZ 30 24 +SQRT_Y_DAG 68 +Y 28 +S 11 +CZ 30 11 +S 35 +SQRT_Y_DAG 17 +SQRT_X 67 +SQRT_X_DAG 8 +CZ 12 61 +S_DAG 71 +SQRT_X_DAG 27 +Z 57 +CY 45 59 +CZ 50 87 +CZ 38 88 +SQRT_X_DAG 93 +CY 96 76 +CZ 57 72 +CX 23 94 +SQRT_X_DAG 95 +CZ 78 67 +SQRT_Y_DAG 85 +SQRT_Y 89 +SQRT_X_DAG 32 +SQRT_X_DAG 59 +CZ 117 116 +SQRT_X 86 +SQRT_X_DAG 1 +CY 57 101 +X 2 +S 83 +X 95 +S_DAG 76 +SQRT_X_DAG 16 +SQRT_Y 82 +CY 25 31 +CY 41 16 +SQRT_Y_DAG 34 +Z 58 +CY 7 103 +S_DAG 67 +SQRT_Y 67 +Y 100 +CX 31 117 +SQRT_Y 72 +SQRT_Y_DAG 102 +CZ 12 60 +SQRT_X 56 +S_DAG 67 +SQRT_Y 110 +S_DAG 8 +H 37 +Z 14 +S_DAG 96 +SQRT_Y 59 +SQRT_Y 38 +SQRT_Y_DAG 55 +H 112 +SQRT_Y_DAG 47 +SQRT_Y_DAG 70 +SQRT_X_DAG 28 +CY 62 12 +SQRT_X_DAG 105 +Y 44 +CY 44 25 +CZ 108 32 +SQRT_Y 97 +S_DAG 7 +Y 100 +S 16 +CZ 36 32 +CZ 31 50 +CZ 89 97 +Y 81 +SQRT_X_DAG 23 +SQRT_X_DAG 61 +X 6 +X 83 +S 94 +CZ 24 25 +X 39 +CX 101 30 +S_DAG 2 +H 95 +SQRT_X_DAG 25 +X 37 +X 5 +S_DAG 55 +SQRT_Y_DAG 45 +SQRT_Y_DAG 72 +Z 114 +SQRT_Y 93 +S_DAG 50 +S_DAG 8 +SQRT_Y_DAG 14 +S_DAG 81 +S 38 +Z 22 +SQRT_Y 54 +SQRT_X 40 +Y 77 +SQRT_Y_DAG 71 +S 113 +CZ 17 49 +Y 108 +SQRT_Y 101 +CZ 51 2 +CZ 99 116 +SQRT_Y_DAG 63 +Z 97 +SQRT_X 116 +SQRT_X_DAG 71 +CY 117 4 +CX 90 108 +SQRT_X_DAG 37 +CY 34 60 +SQRT_X_DAG 3 +CX 87 83 +X 105 +SQRT_Y_DAG 3 +SQRT_X 68 +Z 50 +Y 93 +SQRT_Y 26 +CX 3 12 +SQRT_Y_DAG 4 +SQRT_Y 113 +SQRT_X 97 +SQRT_Y 2 +S 35 +CZ 41 70 +Z 55 +SQRT_Y 9 +Z 92 +CY 45 7 +CZ 42 68 +H 15 +CZ 70 26 +CX 113 99 +CY 103 14 +Y 112 +SQRT_X_DAG 94 +SQRT_Y_DAG 92 +S 38 +S 30 +Y 40 +SQRT_Y_DAG 102 +CX 21 87 +H 91 +CX 62 49 +S 57 +Z 22 +S 36 +S_DAG 45 +S 65 +S 54 +SQRT_Y_DAG 105 +CY 46 73 +S_DAG 41 +S 21 +CY 9 91 +H 80 +CY 44 8 +Y 86 +SQRT_X_DAG 74 +Y 5 +CY 83 51 +S_DAG 112 +SQRT_X_DAG 60 +CZ 31 110 +SQRT_Y_DAG 59 +X 101 +CZ 41 14 +Z 105 +S 38 +S 3 +S 24 +Z 10 +SQRT_X_DAG 96 +SQRT_Y 37 +S 51 +S_DAG 117 +SQRT_Y_DAG 32 +CX 105 112 +Y 82 +S_DAG 116 +Y 54 +Z 86 +S 108 +S_DAG 72 +S 8 +Z 61 +S_DAG 108 +X 50 +X 63 +SQRT_Y_DAG 63 +SQRT_X 82 +SQRT_X 11 +SQRT_X 97 +CZ 106 71 +S 106 +CY 48 21 +S_DAG 116 +S 31 +Z 94 +CY 84 73 +SQRT_Y 83 +CZ 101 55 +SQRT_Y_DAG 60 +SQRT_Y 82 +S 106 +CY 112 35 +Y 30 +S 109 +X 33 +CX 55 62 +H 69 +SQRT_X_DAG 72 +Z 113 +S_DAG 56 +Y 50 +CX 75 79 +X 34 +X 113 +Y 90 +SQRT_Y 34 +X 97 +SQRT_X_DAG 92 +SQRT_Y 15 +CY 51 65 +SQRT_X 78 +CX 6 90 +Y 59 +SQRT_X_DAG 57 +X 102 +SQRT_X 36 +SQRT_X_DAG 27 +S_DAG 100 +SQRT_X_DAG 57 +CZ 117 29 +CX 116 39 +Y 46 +X 106 +Y 94 +SQRT_Y_DAG 83 +Z 37 +H 65 +X 53 +SQRT_Y 9 +Y 114 +S 65 +Z 42 +Y 60 +CZ 66 60 +X 82 +Y 100 +SQRT_Y_DAG 14 +H 95 +CY 31 64 +SQRT_Y 41 +CZ 5 20 +H 87 +X 36 +X 15 +SQRT_Y_DAG 46 +SQRT_X 115 +CX 115 17 +X 113 +SQRT_X 20 +CY 112 35 +X 25 +SQRT_Y_DAG 50 +CZ 67 79 +SQRT_X_DAG 20 +Z 116 +CY 112 83 +S 65 +SQRT_X 112 +S 72 +CY 53 111 +CY 46 3 +SQRT_Y_DAG 88 +X 19 +X 89 +SQRT_Y_DAG 76 +S 82 +S_DAG 17 +Z 19 +SQRT_Y 102 +X 109 +CX 43 16 +SQRT_Y 17 +SQRT_X_DAG 117 +CX 61 27 +SQRT_X 116 +Z 113 +CZ 25 3 +CY 45 22 +S 13 +CX 26 39 +CX 60 58 +SQRT_X 114 +CY 102 25 +S 98 +CZ 104 38 +X 18 +S 102 +S_DAG 15 +X 92 +X 98 +CZ 0 107 +S 2 +SQRT_X 41 +X 73 +Y 113 +SQRT_Y 25 +H 116 +X 87 +SQRT_X 15 +CY 45 16 +X 56 +H 88 +CZ 37 49 +SQRT_X_DAG 4 +CY 17 83 +SQRT_X 95 +CX 60 82 +CY 85 39 +SQRT_X 48 +S_DAG 86 +CZ 11 113 +CY 63 6 +S_DAG 5 +H 111 +CZ 20 102 +CY 42 13 +S_DAG 61 +S 19 +CY 117 98 +X 104 +CZ 9 106 +CX 98 90 +Y 32 +H 35 +X 101 +SQRT_X_DAG 83 +CY 29 13 +SQRT_Y 104 +H 44 +SQRT_Y_DAG 84 +X 33 +CZ 90 115 +Y 70 +X 22 +SQRT_X 16 +Z 90 +CY 88 71 +Y 30 +S_DAG 14 +H 59 +CX 30 109 +SQRT_Y_DAG 58 +SQRT_Y_DAG 39 +SQRT_Y_DAG 48 +SQRT_X 112 +S 103 +SQRT_X 76 +SQRT_X 46 +CX 30 14 +SQRT_X 70 +H 84 +CX 31 48 +CY 40 74 +CZ 79 18 +SQRT_Y 12 +SQRT_X 70 +CX 40 0 +Z 67 +SQRT_Y_DAG 73 +S_DAG 62 +CY 116 70 +CY 72 18 +X 10 +S_DAG 24 +S 91 +SQRT_X_DAG 95 +H 42 +X 46 +S_DAG 58 +S 51 +H 3 +Y 19 +H 68 +CY 29 112 +Y 99 +H 113 +SQRT_Y 47 +SQRT_Y 58 +S_DAG 4 +Z 10 +Y 50 +CX 27 98 +SQRT_X 4 +X 80 +SQRT_X 49 +Y 27 +SQRT_X_DAG 68 +CY 86 81 +S_DAG 107 +Y 0 +Y 56 +CX 114 1 +CX 82 34 +SQRT_X_DAG 80 +H 103 +CZ 92 79 +CY 91 66 +S_DAG 103 +Z 48 +Z 11 +Y 9 +X 15 +SQRT_X 50 +SQRT_X_DAG 45 +SQRT_Y 101 +Y 74 +Z 61 +X 35 +SQRT_Y 95 +SQRT_X 19 +CX 38 110 +SQRT_Y 92 +SQRT_X 85 +S 4 +CZ 96 82 +X 6 +CX 40 5 +S_DAG 55 +SQRT_X 46 +CX 38 25 +CY 12 96 +Y 22 +SQRT_Y 71 +Y 74 +Y 84 +S 29 +SQRT_X 101 +S 0 +Z 117 +SQRT_X 68 +Z 1 +CX 78 114 +S_DAG 13 +SQRT_Y_DAG 0 +Z 19 +S 45 +SQRT_Y 117 +CZ 66 24 +CZ 110 92 +SQRT_X 12 +X 6 +SQRT_X_DAG 45 +Z 76 +CY 97 38 +S_DAG 65 +S 71 +SQRT_Y_DAG 81 +CZ 25 70 +CX 36 98 +SQRT_Y 21 +S 98 +SQRT_Y 66 +CY 9 27 +S_DAG 54 +SQRT_Y_DAG 17 +SQRT_Y_DAG 105 +H 110 +CY 41 5 +Y 117 +X 31 +CZ 116 51 +SQRT_Y_DAG 37 +Z 34 +H 113 +S 93 +X 7 +SQRT_X 10 +SQRT_Y 11 +H 44 +CZ 84 48 +CY 86 98 +X 44 +S_DAG 41 +S_DAG 4 +S 75 +Z 73 +SQRT_Y_DAG 82 +X 33 +CX 45 70 +X 86 +CX 73 43 +X 63 +CY 19 76 +H 85 +SQRT_X 35 +CX 30 76 +Z 64 +CZ 37 9 +H 106 +S 70 +CY 115 64 +SQRT_X_DAG 28 +S_DAG 91 +S_DAG 68 +CY 89 21 +Y 2 +CZ 99 17 +X 109 +CX 76 92 +SQRT_X_DAG 53 +X 91 +CY 69 2 +SQRT_Y_DAG 79 +S_DAG 44 +SQRT_Y 80 +Y 10 +X 66 +SQRT_Y 54 +SQRT_Y_DAG 5 +SQRT_X_DAG 1 +SQRT_X_DAG 28 +S 70 +CZ 4 84 +Y 35 +Z 74 +S 23 +Z 62 +CX 16 107 +H 29 +Z 49 +CX 77 68 +H 117 +CX 85 55 +Y 24 +CZ 48 79 +CY 61 38 +CY 18 99 +X 5 +CX 4 101 +X 107 +H 3 +SQRT_X 68 +SQRT_X 55 +SQRT_X_DAG 69 +Y 12 +S_DAG 4 +SQRT_X_DAG 69 +CY 32 62 +H 91 +S 99 +S_DAG 89 +SQRT_Y_DAG 66 +SQRT_X 58 +H 23 +S_DAG 84 +S_DAG 89 +CX 63 90 +CZ 12 79 +Y 6 +S_DAG 66 +Y 52 +X 5 +Z 49 +SQRT_X_DAG 76 +CY 7 22 +X 29 +S 24 +Z 57 +SQRT_X 49 +SQRT_X_DAG 59 +S 4 +SQRT_X_DAG 5 +Z 36 +CY 57 19 +CX 84 71 +X 89 +SQRT_Y 23 +CY 63 34 +CX 45 11 +CZ 62 94 +CZ 99 55 +SQRT_X_DAG 57 +SQRT_Y 72 +SQRT_X 33 +Y 58 +CZ 11 57 +CY 116 49 +S_DAG 99 +SQRT_X 41 +CZ 73 113 +S 18 +S_DAG 77 +H 81 +S_DAG 104 +CX 110 80 +S 59 +SQRT_X 35 +SQRT_Y 76 +H 8 +H 67 +S 53 +SQRT_Y_DAG 46 +S 2 +SQRT_Y 101 +SQRT_X_DAG 13 +S 17 +X 43 +CX 97 109 +CX 108 94 +SQRT_Y 25 +CX 16 48 +CX 15 94 +SQRT_Y_DAG 111 +CX 85 23 +SQRT_X 116 +SQRT_Y 0 +CZ 39 108 +SQRT_Y 41 +Z 83 +S_DAG 83 +SQRT_X_DAG 53 +Y 90 +S_DAG 78 +S 90 +SQRT_Y 33 +S_DAG 58 +SQRT_Y 90 +CX 22 32 +Z 58 +Z 70 +SQRT_X 87 +SQRT_Y_DAG 30 +CZ 13 71 +SQRT_X_DAG 98 +SQRT_X_DAG 108 +CZ 81 23 +H 73 +S 38 +Z 20 +H 33 +S 23 +CX 26 52 +SQRT_Y 30 +CY 84 55 +Z 46 +SQRT_Y_DAG 41 +CY 109 44 +CX 6 55 +X 105 +SQRT_Y_DAG 28 +CY 26 104 +SQRT_Y_DAG 102 +CY 51 38 +X 39 +SQRT_Y_DAG 56 +SQRT_Y 77 +H 2 +CY 42 47 +SQRT_Y 37 +Z 88 +S_DAG 20 +CZ 82 64 +SQRT_X_DAG 6 +CX 103 26 +S_DAG 62 +Z 33 +SQRT_Y_DAG 87 +SQRT_Y_DAG 27 +SQRT_Y 13 +SQRT_Y 109 +CY 36 26 +CZ 114 49 +Y 49 +Y 83 +Z 68 +CZ 49 70 +SQRT_X_DAG 50 +CY 113 38 +CY 72 26 +SQRT_X 8 +CY 80 25 +Z 54 +S 99 +S 96 +X 58 +CX 41 71 +S_DAG 79 +Y 27 +H 27 +CZ 55 5 +Y 12 +CZ 39 57 +S 9 +SQRT_Y_DAG 14 +S_DAG 58 +SQRT_Y 10 +H 115 +X 82 +Y 38 +CZ 53 87 +SQRT_X_DAG 51 +S 97 +SQRT_X_DAG 93 +SQRT_X_DAG 112 +SQRT_Y_DAG 25 +SQRT_X 1 +Z 57 +SQRT_Y_DAG 29 +X 107 +Y 55 +S_DAG 17 +SQRT_Y 108 +Y 70 +CZ 50 61 +SQRT_X 72 +SQRT_X 43 +SQRT_Y 3 +X 115 +CX 64 115 +CX 48 96 +H 84 +SQRT_X 101 +Z 29 +SQRT_Y 58 +X 104 +SQRT_Y_DAG 7 +SQRT_X_DAG 23 +S_DAG 1 +SQRT_X_DAG 114 +SQRT_X 59 +CY 101 111 +Z 63 +Z 48 +H 89 +Z 98 +CZ 28 76 +SQRT_Y_DAG 80 +H 38 +CZ 75 49 +SQRT_Y_DAG 82 +SQRT_X 59 +Y 59 +CY 70 106 +CZ 22 53 +CZ 102 2 +SQRT_X 10 +CX 81 9 +S 72 +CX 55 104 +CZ 20 33 +CZ 96 84 +X 64 +S_DAG 101 +SQRT_Y 25 +SQRT_Y_DAG 62 +CY 33 37 +CZ 85 102 +Y 65 +X 23 +H 80 +Z 107 +H 109 +S 101 +SQRT_Y 12 +S_DAG 29 +Y 78 +CY 99 37 +CZ 105 76 +X 117 +S 62 +SQRT_X_DAG 9 +SQRT_Y 39 +CY 30 61 +CZ 106 11 +CX 102 36 +SQRT_X_DAG 105 +X 23 +CY 112 1 +S_DAG 83 +S 53 +SQRT_X 105 +S 91 +CX 112 107 +X 71 +SQRT_Y_DAG 50 +S 67 +CZ 109 64 +CX 59 112 +CZ 112 49 +Y 23 +S 84 +X 20 +SQRT_Y 46 +SQRT_X 61 +SQRT_Y_DAG 0 +X 75 +S 109 +CX 38 88 +X 108 +Z 81 +SQRT_Y 59 +SQRT_X_DAG 109 +SQRT_Y 13 +S 10 +S_DAG 19 +SQRT_Y_DAG 75 +SQRT_Y_DAG 4 +Z 117 +X 34 +SQRT_Y 25 +SQRT_X_DAG 100 +CZ 13 5 +S_DAG 93 +X 103 +S_DAG 109 +Y 82 +H 84 +S_DAG 39 +Z 9 +CZ 83 3 +CZ 112 55 +Z 1 +S_DAG 42 +CY 90 107 +CZ 4 51 +H 25 +SQRT_X_DAG 102 +SQRT_Y_DAG 46 +X 8 +SQRT_X_DAG 100 +CX 6 59 +H 9 +H 15 +CY 82 1 +CZ 86 117 +Y 38 +Z 94 +SQRT_X 53 +SQRT_Y_DAG 86 +SQRT_Y 29 +H 106 +CZ 62 41 +CX 26 80 +SQRT_X_DAG 12 +CZ 93 112 +CX 39 103 +CY 32 20 +H 88 +CX 37 24 +CY 5 47 +SQRT_X 32 +CY 89 99 +SQRT_Y 104 +H 0 +Y 112 +SQRT_Y 41 +H 104 +CX 88 14 +SQRT_X 57 +X 25 +SQRT_X 45 +SQRT_Y_DAG 63 +CX 20 107 +CX 79 68 +CX 68 70 +SQRT_Y 32 +S 108 +S 57 +Y 95 +S 50 +H 54 +S 93 +SQRT_Y_DAG 27 +Z 59 +H 50 +Y 64 +X 90 +S 108 +S_DAG 59 +CY 3 24 +CY 26 52 +CX 112 14 +S_DAG 51 +S_DAG 78 +X 23 +SQRT_X 27 +S 58 +CZ 39 72 +Y 29 +X 2 +CZ 12 43 +H 12 +SQRT_Y 50 +X 92 +H 34 +SQRT_X_DAG 64 +S_DAG 6 +H 114 +CX 107 72 +SQRT_Y 29 +SQRT_Y 57 +Z 57 +S_DAG 3 +CZ 11 82 +Y 87 +CZ 110 5 +SQRT_X 25 +S_DAG 11 +SQRT_X_DAG 87 +CX 33 15 +X 29 +S 57 +SQRT_Y_DAG 84 +SQRT_X_DAG 52 +SQRT_X 23 +S_DAG 57 +X 117 +SQRT_Y_DAG 11 +S_DAG 56 +CX 75 69 +CZ 63 67 +CX 44 0 +SQRT_X 16 +CZ 71 7 +CX 117 19 +SQRT_Y 85 +S 67 +X 83 +CZ 106 0 +CY 16 102 +Y 117 +Z 29 +Y 107 +SQRT_X_DAG 6 +S 38 +S 6 +Y 11 +CY 21 108 +SQRT_Y_DAG 94 +Y 87 +X 103 +S_DAG 84 +CX 109 16 +SQRT_X_DAG 32 +CY 113 74 +H 60 +S 58 +Y 67 +CY 74 26 +S 1 +CZ 24 104 +SQRT_Y 99 +CY 14 77 +CY 24 89 +S_DAG 88 +SQRT_Y_DAG 76 +S 6 +S_DAG 1 +CX 16 101 +SQRT_X_DAG 83 +SQRT_Y 112 +SQRT_Y 92 +CY 58 70 +S 54 +H 12 +SQRT_X 45 +SQRT_X_DAG 38 +Z 8 +S 72 +SQRT_X_DAG 59 +X 89 +S 87 +SQRT_X_DAG 4 +CY 61 44 +CX 113 27 +SQRT_X_DAG 35 +SQRT_X_DAG 61 +Y 90 +Y 63 +X 25 +Y 9 +SQRT_X 111 +S 87 +SQRT_Y 43 +CX 81 103 +S 116 +S_DAG 3 +SQRT_Y_DAG 94 +S_DAG 15 +S_DAG 102 +SQRT_Y_DAG 30 +SQRT_X 82 +X 97 +CY 77 113 +CX 87 111 +CZ 8 43 +X 30 +X 52 +S 25 +SQRT_Y 13 +Z 72 +Z 27 +S 100 +SQRT_Y_DAG 16 +Y 47 +CX 39 9 +CY 106 64 +S 84 +S_DAG 106 +S 12 +SQRT_Y 5 +S 97 +Z 69 +CX 11 50 +CZ 22 75 +Y 80 +SQRT_X 93 +SQRT_X_DAG 58 +Y 84 +SQRT_Y 88 +CZ 102 1 +Y 39 +CZ 21 109 +H 87 +S_DAG 57 +SQRT_Y_DAG 60 +SQRT_X 18 +S 68 +Z 78 +CY 21 81 +H 31 +Z 66 +S 51 +X 88 +SQRT_Y_DAG 95 diff --git a/crates/ppvm-tableau-2/benches/circuits/gen.py b/crates/ppvm-tableau-2/benches/circuits/gen.py index 2fbe55cce..5cc265b66 100644 --- a/crates/ppvm-tableau-2/benches/circuits/gen.py +++ b/crates/ppvm-tableau-2/benches/circuits/gen.py @@ -4,6 +4,8 @@ - base: H on all qubits (per-shot overhead). - cx: base plus 4096 random CX, in instructions of 16 disjoint pairs. +- gates: base plus 4096 gates drawn uniformly from all 13 Clifford gates, so + no single gate dominates. - mr_batched: 10 rounds of H on all, 128 random CX, one MR over all qubits (random collapses with fan-outs). - mr_split: the same with one MR per instruction (no per-instruction batching). @@ -24,6 +26,18 @@ def write(name, lines): open(f"{name}.stim", "w").write("\n".join(lines) + "\n") write("base", [h_all]) write("cx", [h_all] + cx_layer(4096)) +one_qubit = ["X", "Y", "Z", "H", "S", "S_DAG", "SQRT_X", "SQRT_X_DAG", "SQRT_Y", "SQRT_Y_DAG"] +two_qubit = ["CX", "CZ", "CY"] +gates_rng = random.Random(2) # own seed, so the other circuits stay unchanged +mixed = [] +for _ in range(4096): + g = gates_rng.choice(one_qubit + two_qubit) + if g in two_qubit: + a, b = gates_rng.sample(range(n), 2) + mixed.append(f"{g} {a} {b}") + else: + mixed.append(f"{g} {gates_rng.randrange(n)}") +write("gates", [h_all] + mixed) rounds = [] for _ in range(10): rounds += [h_all] + cx_layer(128) From c4bf473aaa29d7cb0eb6a991b501588bfb9acc1b Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 15:55:33 +0100 Subject: [PATCH 14/60] perf(ppvm-tableau-2): let each gate's row pattern fold to constants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Represent an image's factors as a u8 mask, write the pattern checks as plain integer loops, build images and phases without std::array::from_fn, and track sign changes instead of re-reading rows whose image contains them. The pattern then folds per gate: CX at 118 qubits goes from 24 to 11 ns (a hand-written CX: 10 ns), all 13 gates mixed from 16 to 10 ns, the d=7 surface code from 78 to 50 µs/shot. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/inverse_tableau/physical_gates.rs | 133 +++++++++++++----- 1 file changed, 94 insertions(+), 39 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs index 075aee2d8..189a35632 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs @@ -11,33 +11,43 @@ use super::bitword::{BitWord, ZERO}; /// Where a gate sends one of its generators: `G† P G = (−1)^flip · P(factors)`. /// -/// The generators are X then Z for each gate qubit; `factors[j]` says whether -/// the Hermitian Pauli `P(factors)` contains generator `j`. +/// The generators are X then Z for each gate qubit; bit `j` of `factors` says +/// whether the Hermitian Pauli `P(factors)` contains generator `j`. Plain +/// integer masks, so that for a given gate everything below folds to constants. #[derive(Clone, Copy)] struct Image { - factors: [bool; M], + factors: u8, flip: bool, } impl Image { - fn new((factors, flip): ([bool; M], bool)) -> Self { + fn new((bits, flip): ([bool; M], bool)) -> Self { + let mut factors = 0; + for (j, &b) in bits.iter().enumerate() { + factors |= u8::from(b) << j; + } Self { factors, flip } } + fn has(&self, j: usize) -> bool { + self.factors >> j & 1 == 1 + } + /// Whether generator `g` maps to itself up to sign, so its row keeps its bits. fn keeps_bits(&self, g: usize) -> bool { - (0..M).all(|j| self.factors[j] == (j == g)) + self.factors == 1 << g } - /// Whether row `g` becomes itself times rows that keep their bits, so it can - /// be multiplied in place (CX, CZ, S, √X and their inverses). - fn multiplies_in_place(&self, g: usize, images: &[Image; M]) -> bool { - self.factors[g] && (0..M).all(|j| j == g || !self.factors[j] || images[j].keeps_bits(j)) + /// Whether row `g` becomes itself times rows that keep their bits (`changed` + /// marks the rows that do not), so it can be multiplied in place (CX, CZ, S, + /// √X and their inverses). + fn multiplies_in_place(&self, g: usize, changed: u8) -> bool { + self.has(g) && self.factors & !(1 << g) & changed == 0 } /// Number of Y factors: an X and a Z on the same qubit. fn y_count(&self) -> u32 { - self.factors.chunks(2).filter(|xz| xz[0] && xz[1]).count() as u32 + (self.factors & self.factors >> 1 & 0b0101_0101).count_ones() } /// One word of the product of the old rows in `factors`, and its phase. @@ -45,15 +55,26 @@ impl Image { fn multiply(&self, old_words: &[[BitWord; 2]; M]) -> ([BitWord; 2], u32) { let mut word = [ZERO; 2]; let mut phase = 0u32; - for j in (0..M).filter(|&j| self.factors[j]) { - let (product, k) = word_product(word[0], word[1], old_words[j][0], old_words[j][1]); - word = product; - phase = phase.wrapping_add(k); + for (j, old) in old_words.iter().enumerate() { + if self.has(j) { + let (product, k) = word_product(word[0], word[1], old[0], old[1]); + word = product; + phase = phase.wrapping_add(k); + } } (word, phase) } } +/// Bit `g` set for each row the gate changes. +fn changed_rows(images: &[Image; M]) -> u8 { + let mut changed = 0; + for (g, image) in images.iter().enumerate() { + changed |= u8::from(!image.keeps_bits(g)) << g; + } + changed +} + /// Rewrite row `g` (X words `x[g]`, Z words `z[g]`) as `C⁻¹ (G† P_g G) C`. /// /// Write the image as `(−1)^flip · i^#Y` times its generators in order (a Y is @@ -77,11 +98,10 @@ fn recombine( assert!(x.iter().chain(&z).all(|s| s.len() == len)); for w in 0..len { let old_words: [[BitWord; 2]; M] = std::array::from_fn(|j| [x[j][w], z[j][w]]); - for (g, image) in images - .iter() - .enumerate() - .filter(|(g, image)| !image.keeps_bits(*g)) - { + for (g, image) in images.iter().enumerate() { + if image.keeps_bits(g) { + continue; + } let ([new_x, new_z], phase) = image.multiply(&old_words); x[g][w] = new_x; z[g][w] = new_z; @@ -118,7 +138,10 @@ fn multiply_in_place( if image.keeps_bits(g) { continue; } - for j in (0..M).filter(|&j| j != g && image.factors[j]) { + for j in 0..M { + if j == g || !image.has(j) { + continue; + } let (dst_x, src_x) = pair(&mut x, g, j); let (dst_z, src_z) = pair(&mut z, g, j); let k = row_product(dst_x, dst_z, src_x, src_z); @@ -131,11 +154,42 @@ fn multiply_in_place( impl InverseTableau { /// Starting phase of each new row, as a power of `i`: one `i` per Y in the /// image, `−1` for its flip and `−1` per negative old row it multiplies. + /// A row whose image contains the row itself leaves its own sign out: its + /// phase is then the *change* of its sign (see `set_signs`). + #[inline(always)] fn initial_phases(&self, rows: [usize; M], images: &[Image; M]) -> [u32; M] { - images.map(|image| { - let negatives = (0..M).filter(|&j| image.factors[j] && self.negative(rows[j])); - image.y_count() + 2 * image.flip as u32 + 2 * negatives.count() as u32 - }) + let mut phases = [0; M]; + for (g, image) in images.iter().enumerate() { + phases[g] = image.y_count() + 2 * u32::from(image.flip); + for (j, &r) in rows.iter().enumerate() { + if j != g && image.has(j) && self.negative(r) { + phases[g] += 2; + } + } + } + phases + } + + /// Set each row's sign from its final phase (`i⁰ = +`, `i² = −`): relative + /// to its old sign when its image contains the row itself, else absolute. + #[inline(always)] + fn set_signs( + &mut self, + rows: [usize; M], + images: &[Image; M], + phases: [u32; M], + ) { + for (g, &r) in rows.iter().enumerate() { + debug_assert!(phases[g].is_multiple_of(2), "row is not Hermitian"); + let negative = phases[g] % 4 == 2; + if images[g].has(g) { + if negative { + self.flip_sign(r); + } + } else if self.negative(r) != negative { + self.flip_sign(r); + } + } } /// Replace each row in `rows` by its image's product of old rows: in place @@ -144,25 +198,21 @@ impl InverseTableau { #[inline(always)] fn rewrite_rows(&mut self, rows: [usize; M], images: &[Image; M]) { let mut phases = self.initial_phases(rows, images); - let in_place = - (0..M).all(|g| images[g].keeps_bits(g) || images[g].multiplies_in_place(g, images)); + let in_place = { + let changed = changed_rows(images); + let mut all = true; + for (g, image) in images.iter().enumerate() { + all &= changed >> g & 1 == 0 || image.multiplies_in_place(g, changed); + } + all + }; let (x, z) = self.rows_mut(rows); if in_place { multiply_in_place(x, z, images, &mut phases); } else { recombine(x, z, images, &mut phases); } - self.set_signs(rows, phases); - } - - /// Set each row's sign from its final phase (`i⁰ = +`, `i² = −`). - fn set_signs(&mut self, rows: [usize; M], phases: [u32; M]) { - for (g, &r) in rows.iter().enumerate() { - debug_assert!(phases[g].is_multiple_of(2), "row is not Hermitian"); - if self.negative(r) != (phases[g] % 4 == 2) { - self.flip_sign(r); - } - } + self.set_signs(rows, images, phases); } } @@ -178,8 +228,13 @@ impl ApplyGateRule for InverseTableau { } fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { - let generator = |j: usize| std::array::from_fn(|i| i == j); - let images = std::array::from_fn(|j| Image::new(rule(generator(j)))); + let images = [ + rule([true, false, false, false]), + rule([false, true, false, false]), + rule([false, false, true, false]), + rule([false, false, false, true]), + ] + .map(Image::new); let rows = [a, self.n + a, b, self.n + b]; self.rewrite_rows(rows, &images); } From f31acf14beac1322decca6e2ac30cb619db9af19 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 16:25:17 +0100 Subject: [PATCH 15/60] perf(ppvm-tableau-2): check gate qubits instead of rows qubit_rows_mut takes the gate's qubits and checks only that they are in range and distinct (3 comparisons for a two-qubit gate instead of 10 for four rows); distinct qubits own distinct rows, so the unsafe slicing stays sound. CX at 118 qubits goes from 11.4 to 10.2 ns, mixed gates from 9.8 to 8.9 ns. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/inverse_tableau/data.rs | 39 +++++++++++++------ .../src/inverse_tableau/physical_gates.rs | 13 +++---- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs index cbe0b66ee..6016857a9 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs @@ -101,23 +101,40 @@ impl InverseTableau { ) } - /// The X words and the Z words of distinct rows, mutably. + /// The rows of `qubits` in generator order: `ix_q`, `iz_q` per qubit. `M` + /// is twice the number of qubits. + #[inline(always)] + pub(super) fn qubit_rows(&self, qubits: &[usize]) -> [usize; M] { + let mut rows = [0; M]; + for (k, &q) in qubits.iter().enumerate() { + rows[2 * k] = q; + rows[2 * k + 1] = self.n + q; + } + rows + } + + /// The X words and the Z words of the rows of distinct qubits, in the + /// order of [`qubit_rows`](Self::qubit_rows), mutably. /// - /// Panics unless every row is in range and no row repeats: that is what - /// makes the ranges disjoint, so this is the one place that checks it. + /// Panics unless every qubit is in range and no qubit repeats: distinct + /// qubits own distinct rows, which makes the ranges disjoint, so this is + /// the one place that checks it. #[inline(always)] - pub(super) fn rows_mut( + pub(super) fn qubit_rows_mut( &mut self, - rows: [usize; R], - ) -> ([&mut [BitWord]; R], [&mut [BitWord]; R]) { - for (i, &r) in rows.iter().enumerate() { - assert!(r < 2 * self.n, "row {r} out of range"); - assert!(!rows[..i].contains(&r), "row {r} repeated"); + qubits: &[usize], + ) -> ([&mut [BitWord]; M], [&mut [BitWord]; M]) { + assert_eq!(M, 2 * qubits.len()); + for (i, &q) in qubits.iter().enumerate() { + assert!(q < self.n, "qubit {q} out of range"); + assert!(!qubits[..i].contains(&q), "qubit {q} repeated"); } + let rows: [usize; M] = self.qubit_rows(qubits); let (xs, zs) = (rows.map(|r| self.x_at(r)), rows.map(|r| self.z_at(r))); let (stride, words) = (self.stride, self.words.as_mut_ptr()); - // SAFETY: distinct rows in range own disjoint `stride`-word ranges of - // `words`, and the slices borrow `self` mutably for their lifetime. + // SAFETY: distinct qubits in range own distinct rows, whose + // `stride`-word ranges of `words` are disjoint; the slices borrow + // `self` mutably for their lifetime. unsafe { ( xs.map(|x| std::slice::from_raw_parts_mut(words.add(x), stride)), diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs index 189a35632..e2e7f54e1 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs @@ -192,11 +192,12 @@ impl InverseTableau { } } - /// Replace each row in `rows` by its image's product of old rows: in place + /// Replace each row of `qubits` by its image's product of old rows: in place /// when the pattern allows it, else with the fused pass. The choice depends /// only on the images, which are constants for a given gate. #[inline(always)] - fn rewrite_rows(&mut self, rows: [usize; M], images: &[Image; M]) { + fn rewrite_rows(&mut self, qubits: &[usize], images: &[Image; M]) { + let rows = self.qubit_rows(qubits); let mut phases = self.initial_phases(rows, images); let in_place = { let changed = changed_rows(images); @@ -206,7 +207,7 @@ impl InverseTableau { } all }; - let (x, z) = self.rows_mut(rows); + let (x, z) = self.qubit_rows_mut(qubits); if in_place { multiply_in_place(x, z, images, &mut phases); } else { @@ -223,8 +224,7 @@ impl ApplyGateRule for InverseTableau { fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { let images = [rule(true, false), rule(false, true)].map(Image::new); - let rows = [q, self.n + q]; - self.rewrite_rows(rows, &images); + self.rewrite_rows(&[q], &images); } fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { @@ -235,8 +235,7 @@ impl ApplyGateRule for InverseTableau { rule([false, false, false, true]), ] .map(Image::new); - let rows = [a, self.n + a, b, self.n + b]; - self.rewrite_rows(rows, &images); + self.rewrite_rows(&[a, b], &images); } fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { From 184b6b5d7c7355b93bb5ac48a68fba2cbdd65dd0 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 16:39:56 +0100 Subject: [PATCH 16/60] perf(ppvm-tableau-2): tighten the strided column updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit change_basis1/2 and fan_out visit rows through for_each_row: explicit per-block rows as fixed-length slices instead of an offsets iterator, sign flips collected in a register and written once per 64 rows, and no target Vec allocations. A random measure-reset at 118 qubits goes from ~720 to ~510 ns; the d=7 surface code from 48 to 40 µs/shot. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/inverse_tableau/bitword.rs | 2 + .../src/inverse_tableau/data.rs | 41 +++++++- .../ppvm-tableau-2/src/inverse_tableau/mod.rs | 4 +- .../src/inverse_tableau/virtual_gates.rs | 98 +++++++------------ 4 files changed, 79 insertions(+), 66 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs index ddc999263..e787179f2 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs @@ -9,6 +9,8 @@ pub(super) type BitWord = u64; pub(super) const BITWORD_BITS: usize = BitWord::BITS as usize; pub(super) const ZERO: BitWord = 0; +/// The highest bit of a word. +pub(super) const TOP_BIT: BitWord = 1 << (BITWORD_BITS - 1); /// Bit `i` of a packed bit string. #[inline(always)] diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs index 6016857a9..5b4247d4f 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs @@ -3,7 +3,9 @@ //! The tableau's storage: rows, signs, and where rows sit in the arena. -use super::bitword::{BITWORD_BITS, BitWord, ZERO, flip_bit, get_bit}; +use super::bitword::{ + BITWORD_BITS, BitWord, TOP_BIT, ZERO, bit_position, flip_bit, get_bit, mask_if, +}; /// The inverse tableau of an `n`-qubit Clifford. #[derive(Clone, Debug, PartialEq, Eq)] @@ -162,10 +164,41 @@ impl InverseTableau { Self::offsets(self.n, self.stride, kind, i).1 } - /// `(x_at(r), z_at(r))` for every row in order, without a division per row. - pub(super) fn row_offsets(&self) -> impl Iterator + use<> { + /// Visit every row in order with its X words and Z words; `f` returns + /// whether the row's sign flips. Signs are updated a word (64 rows) at a + /// time. Walks the blocks of `offsets` directly: rows of one kind are + /// `stride` words apart. + #[inline(always)] + pub(super) fn for_each_row( + &mut self, + mut f: impl FnMut(&mut [BitWord], &mut [BitWord]) -> bool, + ) { let (n, stride) = (self.n, self.stride); - (0..2).flat_map(move |kind| (0..n).map(move |i| Self::offsets(n, stride, kind, i))) + if n == 0 { + return; + } + debug_assert_eq!( + Self::offsets(n, stride, 1, 0), + (2 * n * stride, 3 * n * stride) + ); + let (ix, iz) = self.words.split_at_mut(2 * n * stride); + let (ix_x, ix_z) = ix.split_at_mut(n * stride); + let (iz_x, iz_z) = iz.split_at_mut(n * stride); + let (mut flips, mut r) = (ZERO, 0); + for (xs, zs) in [(ix_x, ix_z), (iz_x, iz_z)] { + for (x, z) in xs.chunks_exact_mut(stride).zip(zs.chunks_exact_mut(stride)) { + let (sw, sm) = bit_position(r); + flips |= sm & mask_if(f(x, z)); + if sm == TOP_BIT { + self.signs[sw] ^= flips; + flips = ZERO; + } + r += 1; + } + } + if !r.is_multiple_of(BITWORD_BITS) { + self.signs[r / BITWORD_BITS] ^= flips; + } } } diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs index eaccb7685..ddc1e39ac 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -13,8 +13,8 @@ //! - `virtual_gates`: changes of basis `C ← C·V`, which rewrite columns. //! - `measure`: Z measurement and reset of the stabilizer state. //! -//! Where rows sit in the arena is decided only by `offsets` (used by `x_at`, -//! `z_at` and `row_offsets`). +//! Where rows sit in the arena is decided only in `data`: by `offsets` (used +//! by `x_at`, `z_at`) and by `for_each_row`, which walks the same blocks. mod bitword; mod data; diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs index ca2e9d66c..9ce3f9552 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs @@ -24,15 +24,13 @@ impl InverseTableau { pub fn change_basis1(&mut self, v: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { debug_assert!(v < self.n); let (w, m) = bit_position(v); - for (r, (x, z)) in self.row_offsets().enumerate() { - let (x, z) = (x + w, z + w); - let old = [self.words[x] & m != ZERO, self.words[z] & m != ZERO]; + self.for_each_row(|x, z| { + let old = [x[w] & m != ZERO, z[w] & m != ZERO]; let (new, flip) = rule(old[0], old[1]); - self.words[x] ^= m & mask_if(new[0] != old[0]); - self.words[z] ^= m & mask_if(new[1] != old[1]); - let (sw, sm) = bit_position(r); - self.signs[sw] ^= sm & mask_if(flip); - } + x[w] ^= m & mask_if(new[0] != old[0]); + z[w] ^= m & mask_if(new[1] != old[1]); + flip + }); } /// Change the basis of virtual qubits `a`, `b` by the two-qubit Clifford @@ -50,17 +48,20 @@ impl InverseTableau { ) { debug_assert!(a < self.n && b < self.n && a != b); let [(wa, ma), (wb, mb)] = [bit_position(a), bit_position(b)]; - for (r, (x, z)) in self.row_offsets().enumerate() { - let words = [x + wa, z + wa, x + wb, z + wb]; - let masks = [ma, ma, mb, mb]; - let old: [bool; 4] = std::array::from_fn(|j| self.words[words[j]] & masks[j] != ZERO); + self.for_each_row(|x, z| { + let old = [ + x[wa] & ma != ZERO, + z[wa] & ma != ZERO, + x[wb] & mb != ZERO, + z[wb] & mb != ZERO, + ]; let (new, flip) = rule(old); - for j in 0..4 { - self.words[words[j]] ^= masks[j] & mask_if(new[j] != old[j]); - } - let (sw, sm) = bit_position(r); - self.signs[sw] ^= sm & mask_if(flip); - } + x[wa] ^= ma & mask_if(new[0] != old[0]); + z[wa] ^= ma & mask_if(new[1] != old[1]); + x[wb] ^= mb & mask_if(new[2] != old[2]); + z[wb] ^= mb & mask_if(new[3] != old[3]); + flip + }); } /// Change basis by a pivot fan-out in one pass over the rows: @@ -91,62 +92,39 @@ impl InverseTableau { .all(|&t| t < self.n && t != pivot) ); let (pw, pm) = bit_position(pivot); - let cx = cx_targets - .iter() - .map(|&t| bit_position(t)) - .collect::>(); - let cz = cz_targets - .iter() - .map(|&t| bit_position(t)) - .collect::>(); - for (r, (x, z)) in self.row_offsets().enumerate() { - let old_pivot = [ - self.words[x + pw] & pm != ZERO, - self.words[z + pw] & pm != ZERO, - ]; + self.for_each_row(|x, z| { + let old_pivot = [x[pw] & pm != ZERO, z[pw] & pm != ZERO]; let (mut pivot_bits, mut flip) = (old_pivot, false); - for &(tw, tm) in &cx { - flip ^= target_step( - &mut self.words, - [x + tw, z + tw], - tm, - &mut pivot_bits, - heisenberg::cnot, - ); + for &t in cx_targets { + flip ^= target_step(x, z, t, &mut pivot_bits, heisenberg::cnot); } - for &(tw, tm) in &cz { - flip ^= target_step( - &mut self.words, - [x + tw, z + tw], - tm, - &mut pivot_bits, - heisenberg::cz, - ); + for &t in cz_targets { + flip ^= target_step(x, z, t, &mut pivot_bits, heisenberg::cz); } let (new_pivot, f) = last(pivot_bits[0], pivot_bits[1]); - self.words[x + pw] ^= pm & mask_if(new_pivot[0] != old_pivot[0]); - self.words[z + pw] ^= pm & mask_if(new_pivot[1] != old_pivot[1]); - let (sw, sm) = bit_position(r); - self.signs[sw] ^= sm & mask_if(flip ^ f); - } + x[pw] ^= pm & mask_if(new_pivot[0] != old_pivot[0]); + z[pw] ^= pm & mask_if(new_pivot[1] != old_pivot[1]); + flip ^ f + }); } } -/// Run a two-qubit `rule` on the pivot's bits (held in `pivot_bits`) and one -/// target's bits (at `words[at]`, bit `mask`); store the target's changed bits. +/// Run a two-qubit `rule` on the pivot's bits (held in `pivot_bits`) and +/// target `t`'s bits in one row (`x`, `z`); store the target's changed bits. /// Returns the sign flip. #[inline(always)] fn target_step( - words: &mut [BitWord], - at: [usize; 2], - mask: BitWord, + x: &mut [BitWord], + z: &mut [BitWord], + t: usize, pivot_bits: &mut [bool; 2], rule: impl Fn([bool; 4]) -> ([bool; 4], bool), ) -> bool { - let old = at.map(|i| words[i] & mask != ZERO); + let (w, m) = bit_position(t); + let old = [x[w] & m != ZERO, z[w] & m != ZERO]; let ([px, pz, tx, tz], flip) = rule([pivot_bits[0], pivot_bits[1], old[0], old[1]]); - words[at[0]] ^= mask & mask_if(tx != old[0]); - words[at[1]] ^= mask & mask_if(tz != old[1]); + x[w] ^= m & mask_if(tx != old[0]); + z[w] ^= m & mask_if(tz != old[1]); *pivot_bits = [px, pz]; flip } From 50e192be3f49f3de3425129b3370dbba0a32119e Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 16:54:54 +0100 Subject: [PATCH 17/60] feat(ppvm-tableau-2): add measure_many and CliffordBatch measure_many measures a list of qubits in order (a plain loop for now, so the batched path has an API and a reference); the run_stim harness hands each M/MR instruction to it. InverseTableau gets CliffordBatch with the default loops, since gates on different qubits share no work. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/examples/run_stim.rs | 83 +++++++++++-------- .../src/inverse_tableau/measure.rs | 48 +++++++++++ .../src/inverse_tableau/physical_gates.rs | 6 +- 3 files changed, 103 insertions(+), 34 deletions(-) diff --git a/crates/ppvm-tableau-2/examples/run_stim.rs b/crates/ppvm-tableau-2/examples/run_stim.rs index 5b36b32ea..c65253b4f 100644 --- a/crates/ppvm-tableau-2/examples/run_stim.rs +++ b/crates/ppvm-tableau-2/examples/run_stim.rs @@ -20,13 +20,15 @@ use ppvm_traits_2::Clifford; use stim_parser::instructions::{GateName, MeasureName}; use stim_parser::prelude::{Instruction, Target}; +/// One gate, or one measurement instruction over `targets[at..at + len]` +/// (kept together so they run as one `measure_many`). #[derive(Clone, Copy)] enum Op { One(GateName, usize), Two(GateName, usize, usize), - Measure(usize), + Measure { at: usize, len: usize }, Reset(usize), - MeasureReset(usize), + MeasureReset { at: usize, len: usize }, } fn qubits(targets: &[Target]) -> Vec { @@ -36,8 +38,9 @@ fn qubits(targets: &[Target]) -> Vec { .collect() } -/// Flatten the program into ops, unrolling `REPEAT` blocks. -fn lower(instructions: &[Instruction], ops: &mut Vec) { +/// Flatten the program into ops, unrolling `REPEAT` blocks. Measurement +/// targets go to `targets`. +fn lower(instructions: &[Instruction], ops: &mut Vec, targets: &mut Vec) { for instruction in instructions { match instruction { Instruction::Gate(g) => { @@ -52,16 +55,18 @@ fn lower(instructions: &[Instruction], ops: &mut Vec) { name => ops.extend(qs.iter().map(|&q| Op::One(name, q))), } } - Instruction::Measure(m) => match m.name { - MeasureName::M | MeasureName::MZ => { - ops.extend(m.targets.iter().map(|&q| Op::Measure(q))) - } - MeasureName::MR => ops.extend(m.targets.iter().map(|&q| Op::MeasureReset(q))), - other => panic!("unsupported measurement {other:?}"), - }, + Instruction::Measure(m) => { + let (at, len) = (targets.len(), m.targets.len()); + targets.extend_from_slice(&m.targets); + ops.push(match m.name { + MeasureName::M | MeasureName::MZ => Op::Measure { at, len }, + MeasureName::MR => Op::MeasureReset { at, len }, + other => panic!("unsupported measurement {other:?}"), + }); + } Instruction::Repeat { count, body, .. } => { let mut block = Vec::new(); - lower(body, &mut block); + lower(body, &mut block, targets); for _ in 0..*count { ops.extend_from_slice(&block); } @@ -102,16 +107,20 @@ fn main() { let shots: usize = args.next().map_or(1000, |s| s.parse().expect("shots")); let program = stim_parser::parse(&std::fs::read_to_string(&path).expect("read circuit")) .unwrap_or_else(|e| panic!("parse error: {e:?}")); - let mut ops = Vec::new(); - lower(&program.instructions, &mut ops); + let (mut ops, mut targets) = (Vec::new(), Vec::new()); + lower(&program.instructions, &mut ops, &mut targets); let n = 1 + ops .iter() - .map(|op| match *op { - Op::Two(_, a, b) => a.max(b), - Op::One(_, q) | Op::Measure(q) | Op::Reset(q) | Op::MeasureReset(q) => q, + .flat_map(|op| match *op { + Op::Two(_, a, b) => vec![a, b], + Op::One(_, q) | Op::Reset(q) => vec![q], + Op::Measure { at, len } | Op::MeasureReset { at, len } => { + targets[at..at + len].to_vec() + } }) .max() .expect("empty circuit"); + let mut outcomes = vec![false; targets.len()]; let mut seed = 0x9e37_79b9_7f4a_7c15u64; let mut coin = move || { @@ -132,23 +141,36 @@ fn main() { match op { Op::One(name, q) => apply_one(&mut t, name, q), Op::Two(name, a, b) => apply_two(&mut t, name, a, b), - Op::Measure(q) => parity ^= t.measure(q, &mut flip), + Op::Measure { at, len } => { + let out = &mut outcomes[..len]; + t.measure_many(&targets[at..at + len], &mut flip, out); + parity ^= out.iter().fold(false, |p, &m| p ^ m); + } Op::Reset(q) => t.reset(q, &mut flip), - Op::MeasureReset(q) => { - let m = t.measure(q, &mut flip); - parity ^= m; - if m { - t.x(q); + Op::MeasureReset { at, len } => { + let out = &mut outcomes[..len]; + t.measure_many(&targets[at..at + len], &mut flip, out); + for (&q, &m) in targets[at..at + len].iter().zip(out.iter()) { + parity ^= m; + if m { + t.x(q); + } } } } } } let elapsed = start.elapsed().as_secs_f64(); - let measurements = ops - .iter() - .filter(|op| matches!(op, Op::Measure(_) | Op::MeasureReset(_))) - .count(); + let count = |reset: bool| -> usize { + ops.iter() + .map(|op| match *op { + Op::Measure { len, .. } | Op::MeasureReset { len, .. } => len, + Op::Reset(_) => usize::from(reset), + _ => 0, + }) + .sum() + }; + let measurements = count(false); println!( "{path}: {n} qubits, {} ops ({measurements} measurements) per shot, {shots} shots", ops.len() @@ -157,11 +179,6 @@ fn main() { "{:.1} µs/shot, {:.0} shots/s, {:.1}% of measurements and resets random (parity {parity})", elapsed / shots as f64 * 1e6, shots as f64 / elapsed, - 100.0 * random as f64 - / (shots - * ops - .iter() - .filter(|op| !matches!(op, Op::One(..) | Op::Two(..))) - .count()) as f64, + 100.0 * random as f64 / (shots * count(true)) as f64, ); } diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs index 1155d4090..d692dc180 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs @@ -58,6 +58,22 @@ impl InverseTableau { outcome } + /// Measure `Z_q` for each of `qubits` in order, collapsing after each, and + /// write the outcomes to `outcomes`. `coin` is called once per random + /// outcome. Same result as calling [`measure`](Self::measure) in order; + /// a batch can share setup across its random targets. + pub fn measure_many( + &mut self, + qubits: &[usize], + mut coin: impl FnMut() -> bool, + outcomes: &mut [bool], + ) { + assert_eq!(qubits.len(), outcomes.len()); + for (&q, outcome) in qubits.iter().zip(outcomes) { + *outcome = self.measure(q, &mut coin); + } + } + /// Reset qubit `q` to `|0⟩`: measure it, then flip it if it was `|1⟩`. pub fn reset(&mut self, q: usize, coin: impl FnOnce() -> bool) { if self.measure(q, coin) { @@ -102,6 +118,38 @@ mod tests { } } + /// `measure_many` equals measuring one qubit at a time with the same coins. + #[test] + fn measure_many_matches_measure() { + for (i, n) in SIZES.into_iter().enumerate() { + let mut start = InverseTableau::new(n); + random_circuit(n, 150, 0xba7c + i as u64) + .into_iter() + .for_each(|g| apply(&mut start, g)); + // Every qubit, then a repeat, so later targets see earlier collapses. + let qubits: Vec = (0..n).rev().chain([0]).collect(); + let coins = |seed: u64| { + let mut s = seed | 1; + move || { + s ^= s << 13; + s ^= s >> 7; + s ^= s << 17; + s & 1 == 1 + } + }; + let (mut batched, mut single) = (start.clone(), start); + let mut outcomes = vec![false; qubits.len()]; + batched.measure_many(&qubits, coins(7), &mut outcomes); + let mut coin = coins(7); + let expected: Vec = qubits + .iter() + .map(|&q| single.measure(q, &mut coin)) + .collect(); + assert_eq!(outcomes, expected, "n = {n}"); + assert_eq!(batched, single, "n = {n}"); + } + } + #[test] fn reset_leaves_zero() { for (i, n) in [1, 2, 7, 65].into_iter().enumerate() { diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs index e2e7f54e1..cd49a4603 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs @@ -4,7 +4,7 @@ //! Physical gates, `C ← G·C`: they mix whole rows. use ppvm_pauli_kernels::{row_product, word_product}; -use ppvm_traits_2::ApplyGateRule; +use ppvm_traits_2::{ApplyGateRule, CliffordBatch}; use super::InverseTableau; use super::bitword::{BitWord, ZERO}; @@ -250,6 +250,10 @@ impl ApplyGateRule for InverseTableau { } } +/// The default loops are right here: gates on different qubits touch disjoint +/// rows, so a batch has no shared work to fuse. +impl CliffordBatch for InverseTableau {} + #[cfg(test)] mod tests { use ppvm_pauli_word_2::PauliWord; From cbdd79f4ab94189aaf9f804714d0857783847f8b Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 16:55:11 +0100 Subject: [PATCH 18/60] test(ppvm-tableau-2): avoid a clippy false positive in measure_many test Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/src/inverse_tableau/measure.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs index d692dc180..0b066ab19 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs @@ -141,10 +141,10 @@ mod tests { let mut outcomes = vec![false; qubits.len()]; batched.measure_many(&qubits, coins(7), &mut outcomes); let mut coin = coins(7); - let expected: Vec = qubits - .iter() - .map(|&q| single.measure(q, &mut coin)) - .collect(); + let mut expected = Vec::new(); + for &q in &qubits { + expected.push(single.measure(q, &mut coin)); + } assert_eq!(outcomes, expected, "n = {n}"); assert_eq!(batched, single, "n = {n}"); } From 21e6b3c154acc44fd7dcde1d35aaebc36610940f Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 16:55:43 +0100 Subject: [PATCH 19/60] test(ppvm-tableau-2): allow a clippy false positive in measure_many test Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/src/inverse_tableau/measure.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs index 0b066ab19..01eedcdee 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs @@ -142,6 +142,8 @@ mod tests { batched.measure_many(&qubits, coins(7), &mut outcomes); let mut coin = coins(7); let mut expected = Vec::new(); + // `&mut coin` is needed: `coin` is reused across iterations. + #[allow(clippy::needless_borrows_for_generic_args)] for &q in &qubits { expected.push(single.measure(q, &mut coin)); } From 5c53a29b52bf9e4fd8592ade67a9ad1ffe7e671f Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 17:03:16 +0100 Subject: [PATCH 20/60] perf(ppvm-tableau-2): batch random collapses on a column-major copy measure_many transposes into a column-major copy when a batch has at least 64 random targets: each collapse then runs the same gate rules on 64 rows per word, and two transposes are paid once per batch. The measurement logic is shared between both layouts through a small private trait. At 118 qubits a batch of 118 random MRs goes from ~520 to ~150 ns per collapse; the d=7 surface code stays strided. Also add mr_unfused (TICK-separated single-target MRs, which Stim cannot merge) and note in the design how Stim's instruction merging differs from ppvm's stim-parser (issue 291) when benchmarking. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/benches/circuits/gen.py | 7 +- .../benches/circuits/mr_unfused.stim | 2450 +++++++++++++++++ .../src/inverse_tableau/bitword.rs | 23 + .../src/inverse_tableau/data.rs | 2 +- .../src/inverse_tableau/measure.rs | 218 +- .../ppvm-tableau-2/src/inverse_tableau/mod.rs | 2 + .../src/inverse_tableau/transposed.rs | 184 ++ 7 files changed, 2829 insertions(+), 57 deletions(-) create mode 100644 crates/ppvm-tableau-2/benches/circuits/mr_unfused.stim create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs diff --git a/crates/ppvm-tableau-2/benches/circuits/gen.py b/crates/ppvm-tableau-2/benches/circuits/gen.py index 5cc265b66..79b9a408e 100644 --- a/crates/ppvm-tableau-2/benches/circuits/gen.py +++ b/crates/ppvm-tableau-2/benches/circuits/gen.py @@ -8,7 +8,11 @@ no single gate dominates. - mr_batched: 10 rounds of H on all, 128 random CX, one MR over all qubits (random collapses with fan-outs). -- mr_split: the same with one MR per instruction (no per-instruction batching). +- mr_split: the same with one MR per instruction. Stim's parser merges + consecutive same-gate instructions back into one, so for Stim this equals + mr_batched. +- mr_unfused: mr_split with a TICK after each MR, which Stim cannot merge: + really one target per instruction. Regenerate with `python3 gen.py` in this directory (seeded, deterministic). """ @@ -44,3 +48,4 @@ def write(name, lines): rounds.append("MR " + " ".join(map(str, range(n)))) write("mr_batched", rounds) write("mr_split", [l if not l.startswith("MR") else "\n".join(f"MR {q}" for q in range(n)) for l in rounds]) +write("mr_unfused", [l if not l.startswith("MR") else "\n".join(f"MR {q}\nTICK" for q in range(n)) for l in rounds]) diff --git a/crates/ppvm-tableau-2/benches/circuits/mr_unfused.stim b/crates/ppvm-tableau-2/benches/circuits/mr_unfused.stim new file mode 100644 index 000000000..c6d3ff195 --- /dev/null +++ b/crates/ppvm-tableau-2/benches/circuits/mr_unfused.stim @@ -0,0 +1,2450 @@ +H 0 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 +CX 54 28 63 104 80 47 84 32 92 95 96 31 8 78 33 37 65 98 4 102 61 97 90 20 11 86 75 67 70 25 6 73 +CX 74 66 43 36 8 71 77 28 75 83 21 18 87 92 55 80 78 40 81 90 25 111 59 85 23 0 97 6 2 9 49 46 +CX 115 55 24 107 20 0 104 54 4 31 7 50 14 47 111 70 76 17 48 66 81 12 95 40 62 80 112 59 30 51 71 10 +CX 55 108 76 75 48 2 44 65 37 29 50 63 51 47 6 7 52 77 54 114 20 88 18 42 24 95 62 101 40 0 46 104 +CX 46 93 10 100 96 49 16 91 82 4 18 6 116 39 33 42 5 115 111 17 30 40 26 20 56 25 52 47 99 104 75 23 +CX 58 3 73 22 93 80 15 4 95 18 53 84 37 65 96 27 7 9 77 67 104 69 90 54 113 34 88 60 71 56 59 82 +CX 74 79 115 10 72 43 13 38 29 99 5 30 14 26 22 83 96 50 101 51 81 16 113 109 71 82 104 62 88 75 92 19 +CX 87 94 80 96 14 49 56 45 85 2 77 103 61 81 62 98 7 31 78 36 22 64 107 60 8 40 11 33 89 66 38 86 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 16 69 81 7 40 10 18 64 89 117 22 85 99 3 78 68 0 114 8 96 116 46 61 6 87 67 82 86 102 9 58 93 +CX 72 86 2 35 45 57 10 44 8 22 108 96 73 0 105 50 24 117 85 28 19 62 15 110 67 116 106 76 13 79 69 107 +CX 64 116 39 12 38 11 79 14 73 41 68 56 109 94 74 36 106 102 96 117 101 107 59 75 19 113 103 34 9 85 100 4 +CX 72 111 50 91 16 62 74 83 105 98 93 114 41 85 33 15 73 37 55 28 31 49 69 116 9 51 86 113 32 5 87 89 +CX 98 30 43 10 33 48 13 16 116 62 41 59 52 99 114 65 74 106 4 55 25 50 72 73 87 113 78 79 110 17 112 7 +CX 2 82 107 6 56 0 44 65 104 57 41 102 23 89 85 26 92 113 79 27 84 25 49 42 90 100 35 91 20 38 103 11 +CX 98 79 77 70 9 67 116 61 73 75 69 31 12 63 15 57 64 107 21 14 89 82 41 66 91 48 74 28 94 105 102 29 +CX 14 107 97 11 60 32 73 65 38 105 2 42 77 103 87 101 49 31 55 19 64 72 57 8 113 112 52 69 111 71 94 15 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 116 49 15 107 34 48 109 4 11 69 44 9 50 86 60 61 95 85 82 99 20 51 5 33 105 104 93 117 57 1 92 31 +CX 68 16 9 63 51 26 13 107 98 88 91 59 25 113 81 104 55 3 35 2 5 62 94 53 116 76 22 24 64 48 36 10 +CX 70 5 52 90 18 59 20 38 116 83 110 74 37 4 78 67 41 105 104 62 11 111 94 81 86 76 49 89 80 63 33 57 +CX 100 2 53 4 37 98 6 44 25 43 23 31 17 11 5 12 19 39 40 63 75 73 76 60 116 49 96 69 48 30 67 18 +CX 70 98 13 53 117 49 28 71 1 76 105 73 110 40 52 101 112 107 80 85 27 16 55 22 14 72 93 38 94 45 60 63 +CX 98 111 67 20 113 75 110 34 81 89 30 33 9 91 65 41 31 83 78 71 28 106 114 109 16 7 59 45 93 79 101 38 +CX 94 5 54 57 63 111 95 6 22 71 48 103 85 87 113 83 92 40 39 35 20 45 105 67 93 47 11 115 31 46 12 96 +CX 31 91 8 46 86 68 84 111 87 39 5 72 45 66 1 49 110 12 0 99 73 22 59 13 27 47 115 65 17 51 23 56 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 108 77 12 65 34 90 61 103 23 47 84 19 15 82 17 9 81 50 20 109 28 36 13 6 116 27 0 76 89 92 1 101 +CX 56 88 24 78 48 38 19 108 43 47 79 113 13 103 114 104 115 109 1 54 57 92 101 21 2 64 33 59 29 10 8 27 +CX 76 92 17 31 101 30 91 109 69 46 66 107 2 73 65 25 57 4 12 22 52 16 86 62 64 29 112 7 47 21 70 85 +CX 20 55 104 68 31 34 44 96 97 81 100 76 114 25 37 75 30 13 69 5 42 39 71 22 16 52 40 99 36 106 2 82 +CX 68 31 3 40 61 6 26 51 12 105 56 90 80 102 110 63 117 70 25 96 57 13 86 81 24 83 22 94 32 77 33 0 +CX 8 103 109 85 114 49 2 116 24 64 42 35 81 72 26 98 61 71 18 57 5 48 65 50 51 7 105 92 37 45 29 25 +CX 58 71 60 115 4 98 70 24 15 41 104 91 38 54 93 31 89 94 20 35 75 34 87 111 74 47 13 84 88 46 107 64 +CX 54 57 104 61 28 12 22 66 76 116 79 59 7 13 10 51 102 96 34 24 83 87 62 73 85 86 45 109 48 69 64 14 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 75 1 30 62 49 33 78 107 34 10 20 50 12 100 6 27 44 23 22 37 72 14 29 82 116 53 36 110 18 74 17 77 +CX 41 70 24 15 42 84 63 25 39 79 89 101 5 91 77 115 22 113 112 64 102 97 83 54 14 117 74 72 34 106 0 81 +CX 88 91 106 112 64 38 48 69 49 51 8 40 43 17 33 34 77 56 20 59 24 109 60 11 100 90 15 71 23 32 39 52 +CX 104 37 95 100 38 25 45 83 89 16 85 76 96 27 69 82 13 55 117 33 105 68 97 107 80 30 108 94 72 84 8 43 +CX 6 94 97 57 32 110 81 90 7 84 109 77 31 4 95 45 11 21 85 23 24 115 82 78 5 64 116 91 80 66 1 86 +CX 52 63 99 80 7 69 30 66 111 84 13 104 41 54 22 4 65 79 49 39 45 3 58 43 115 56 116 60 97 83 20 55 +CX 7 86 89 67 42 33 72 6 3 49 19 60 25 108 69 78 93 59 22 28 65 117 102 21 52 58 74 82 0 105 5 111 +CX 79 50 9 90 67 51 0 18 101 28 88 105 112 43 26 13 83 49 40 4 76 115 25 44 45 89 5 82 54 75 39 55 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 37 83 64 20 95 21 101 65 72 17 84 105 88 1 109 0 55 27 28 56 12 102 111 114 70 36 66 59 85 23 79 73 +CX 50 24 102 115 90 110 37 40 14 82 15 71 107 61 6 100 106 49 45 101 7 13 59 34 97 74 41 56 16 39 58 103 +CX 16 36 68 108 14 100 75 15 82 10 107 54 55 65 114 106 45 71 111 21 89 73 63 95 91 83 90 77 64 53 37 59 +CX 59 30 26 75 100 68 24 16 7 35 94 1 97 56 57 85 22 115 37 69 38 18 10 23 67 11 84 21 90 52 89 27 +CX 97 53 103 32 43 99 84 19 107 71 29 116 42 95 117 1 112 96 104 92 109 21 80 37 7 14 28 79 86 3 54 75 +CX 0 66 4 22 87 101 76 2 102 68 56 92 94 54 80 18 46 51 28 96 86 72 63 91 71 81 35 15 64 36 3 37 +CX 81 67 97 7 36 114 11 47 45 14 85 64 57 35 90 4 112 70 34 43 44 31 104 62 33 8 29 52 48 60 20 78 +CX 52 58 55 28 116 99 65 80 10 94 64 50 48 24 25 79 17 4 110 18 113 44 45 87 12 32 30 40 39 2 86 92 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 55 58 33 83 43 100 91 90 10 110 95 1 34 54 89 19 29 76 47 79 49 0 30 69 108 92 22 28 85 15 24 12 +CX 24 93 80 29 5 52 116 43 18 92 86 1 59 81 76 27 7 79 4 91 97 12 99 117 45 8 48 73 103 102 87 40 +CX 50 37 21 76 116 39 35 74 46 29 28 15 94 48 18 109 36 45 114 67 75 100 6 98 7 11 55 43 105 96 69 30 +CX 39 66 110 36 49 116 69 41 21 19 68 12 84 78 108 105 28 89 38 55 54 33 64 80 79 85 82 46 117 56 57 7 +CX 58 71 52 0 73 15 38 78 64 23 82 9 14 30 46 57 103 84 37 19 35 85 13 93 16 87 62 42 70 29 45 4 +CX 7 15 23 63 58 87 65 11 51 117 73 93 19 9 38 1 80 76 113 31 78 94 29 27 91 16 70 74 109 92 90 105 +CX 20 45 86 108 107 114 7 47 3 94 87 85 63 57 11 4 88 90 104 17 48 82 34 111 102 67 93 23 28 78 36 92 +CX 15 97 73 39 23 4 75 13 61 19 116 32 14 71 100 10 86 42 16 29 98 43 111 56 44 72 91 85 1 92 8 34 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 73 70 107 23 1 95 69 83 96 47 76 37 71 21 80 113 62 116 104 46 117 64 98 88 100 54 63 60 44 52 12 86 +CX 9 12 101 105 29 103 41 26 4 89 115 77 75 90 7 114 45 28 98 96 78 53 48 6 85 94 55 102 35 84 74 47 +CX 89 9 13 42 108 22 44 49 59 20 93 112 53 90 60 24 3 64 95 113 25 85 82 23 96 10 76 78 79 101 11 102 +CX 57 109 32 7 100 51 34 105 53 95 30 117 15 8 27 26 103 3 22 52 80 62 56 42 99 86 24 116 11 48 46 87 +CX 11 66 87 58 67 86 71 88 0 25 35 45 106 3 24 31 54 41 47 112 77 56 59 91 92 14 70 4 81 26 43 9 +CX 55 32 90 85 99 31 7 39 12 110 72 18 103 117 46 66 10 100 108 9 56 61 20 30 73 0 68 3 25 26 41 76 +CX 11 74 49 52 65 102 83 48 85 72 43 110 56 36 113 45 24 17 68 80 38 69 12 93 62 44 50 23 84 4 10 63 +CX 87 7 50 19 74 94 17 23 65 8 112 115 71 24 66 106 98 45 89 75 53 22 52 101 35 29 107 47 78 103 1 37 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 36 64 97 104 34 106 18 26 115 59 4 101 98 113 14 92 86 11 6 81 83 8 63 17 62 108 78 49 28 87 99 53 +CX 21 6 37 58 5 19 64 83 110 24 26 80 89 20 27 16 2 48 106 101 96 51 78 47 46 42 112 41 90 68 43 99 +CX 7 41 58 98 110 97 20 11 1 92 3 62 87 104 13 24 49 79 67 52 77 14 33 66 55 5 115 27 108 70 0 37 +CX 53 8 42 44 112 74 51 7 77 84 34 16 26 1 20 57 6 78 72 52 88 13 70 4 67 21 90 83 108 82 58 116 +CX 1 109 70 117 107 18 28 0 68 4 65 82 75 87 30 76 90 55 91 13 62 47 42 20 92 84 27 31 10 83 99 21 +CX 88 38 11 10 117 26 16 95 9 57 96 99 33 46 31 18 22 74 98 30 54 109 47 67 68 101 7 69 86 5 35 0 +CX 71 95 74 61 53 24 75 100 14 45 90 42 64 105 111 116 77 96 84 97 33 21 94 115 36 37 62 65 41 104 82 78 +CX 54 14 6 8 111 114 39 78 53 82 49 47 42 73 11 90 18 71 72 95 56 64 109 28 70 32 98 43 83 113 22 5 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK +H 0 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 +CX 14 92 12 56 15 116 60 97 89 44 35 113 40 48 106 21 66 94 68 57 93 64 11 63 43 72 103 41 115 109 70 31 +CX 63 31 62 43 5 21 107 110 7 1 54 87 42 39 70 79 94 116 45 18 106 95 9 80 24 4 22 90 99 55 97 33 +CX 26 20 117 51 96 109 67 23 22 30 105 59 18 78 79 97 104 98 36 72 41 116 86 40 69 73 103 99 108 24 21 90 +CX 4 18 16 3 55 6 44 108 91 57 116 54 27 20 69 45 94 22 101 8 109 33 47 42 112 66 39 87 19 76 61 82 +CX 6 103 59 80 99 53 78 104 11 2 1 98 5 108 3 106 27 47 26 57 105 90 117 28 38 25 71 22 24 73 92 100 +CX 68 27 93 17 72 90 110 12 40 20 69 102 80 59 84 63 2 107 61 8 1 58 109 73 45 44 52 18 15 13 104 103 +CX 103 112 41 105 108 21 82 8 49 2 64 56 0 85 96 77 17 37 35 38 7 60 117 97 72 19 114 63 76 52 4 31 +CX 14 93 49 86 68 67 3 74 39 0 105 2 30 112 91 81 115 35 80 46 92 108 27 78 97 54 95 57 64 6 16 69 +MR 0 +TICK +MR 1 +TICK +MR 2 +TICK +MR 3 +TICK +MR 4 +TICK +MR 5 +TICK +MR 6 +TICK +MR 7 +TICK +MR 8 +TICK +MR 9 +TICK +MR 10 +TICK +MR 11 +TICK +MR 12 +TICK +MR 13 +TICK +MR 14 +TICK +MR 15 +TICK +MR 16 +TICK +MR 17 +TICK +MR 18 +TICK +MR 19 +TICK +MR 20 +TICK +MR 21 +TICK +MR 22 +TICK +MR 23 +TICK +MR 24 +TICK +MR 25 +TICK +MR 26 +TICK +MR 27 +TICK +MR 28 +TICK +MR 29 +TICK +MR 30 +TICK +MR 31 +TICK +MR 32 +TICK +MR 33 +TICK +MR 34 +TICK +MR 35 +TICK +MR 36 +TICK +MR 37 +TICK +MR 38 +TICK +MR 39 +TICK +MR 40 +TICK +MR 41 +TICK +MR 42 +TICK +MR 43 +TICK +MR 44 +TICK +MR 45 +TICK +MR 46 +TICK +MR 47 +TICK +MR 48 +TICK +MR 49 +TICK +MR 50 +TICK +MR 51 +TICK +MR 52 +TICK +MR 53 +TICK +MR 54 +TICK +MR 55 +TICK +MR 56 +TICK +MR 57 +TICK +MR 58 +TICK +MR 59 +TICK +MR 60 +TICK +MR 61 +TICK +MR 62 +TICK +MR 63 +TICK +MR 64 +TICK +MR 65 +TICK +MR 66 +TICK +MR 67 +TICK +MR 68 +TICK +MR 69 +TICK +MR 70 +TICK +MR 71 +TICK +MR 72 +TICK +MR 73 +TICK +MR 74 +TICK +MR 75 +TICK +MR 76 +TICK +MR 77 +TICK +MR 78 +TICK +MR 79 +TICK +MR 80 +TICK +MR 81 +TICK +MR 82 +TICK +MR 83 +TICK +MR 84 +TICK +MR 85 +TICK +MR 86 +TICK +MR 87 +TICK +MR 88 +TICK +MR 89 +TICK +MR 90 +TICK +MR 91 +TICK +MR 92 +TICK +MR 93 +TICK +MR 94 +TICK +MR 95 +TICK +MR 96 +TICK +MR 97 +TICK +MR 98 +TICK +MR 99 +TICK +MR 100 +TICK +MR 101 +TICK +MR 102 +TICK +MR 103 +TICK +MR 104 +TICK +MR 105 +TICK +MR 106 +TICK +MR 107 +TICK +MR 108 +TICK +MR 109 +TICK +MR 110 +TICK +MR 111 +TICK +MR 112 +TICK +MR 113 +TICK +MR 114 +TICK +MR 115 +TICK +MR 116 +TICK +MR 117 +TICK diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs index e787179f2..65b2bfd17 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs @@ -48,3 +48,26 @@ pub(super) fn set_bits(words: &[BitWord]) -> impl Iterator + '_ { }) }) } + +/// Transpose a 64 × 64 bit matrix in place: bit `j` of word `i` swaps with bit +/// `i` of word `j`. Width-specific (64-bit words). +#[inline(always)] +pub(super) fn transpose_64x64(a: &mut [BitWord; 64]) { + const PASSES: [(BitWord, usize); 6] = [ + (0x5555_5555_5555_5555, 1), + (0x3333_3333_3333_3333, 2), + (0x0F0F_0F0F_0F0F_0F0F, 4), + (0x00FF_00FF_00FF_00FF, 8), + (0x0000_FFFF_0000_FFFF, 16), + (0x0000_0000_FFFF_FFFF, 32), + ]; + for (mask, shift) in PASSES { + for k in 0..64 { + if k & shift == 0 { + let (x, y) = (a[k], a[k + shift]); + a[k] = (x & mask) | ((y & mask) << shift); + a[k + shift] = ((x & !mask) >> shift) | (y & !mask); + } + } + } +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs index 5b4247d4f..b00161210 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs @@ -96,7 +96,7 @@ impl InverseTableau { // Arrangement: blocks by row kind, ix-X, ix-Z, iz-X, iz-Z (#204's order). // Row r is row i = r % n of kind r / n (0 for ix, 1 for iz). #[inline(always)] - fn offsets(n: usize, stride: usize, kind: usize, i: usize) -> (usize, usize) { + pub(super) fn offsets(n: usize, stride: usize, kind: usize, i: usize) -> (usize, usize) { ( ((2 * kind) * n + i) * stride, ((2 * kind + 1) * n + i) * stride, diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs index 01eedcdee..b056fd95a 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs @@ -4,73 +4,177 @@ //! Z measurement and reset of the stabilizer state `C |0…0⟩` (no active //! qubits). -use ppvm_pauli_kernels::heisenberg; +use ppvm_pauli_kernels::{Bits, heisenberg}; use ppvm_traits_2::Clifford; use super::InverseTableau; -use super::bitword::set_bits; +use super::bitword::{BitWord, ZERO, set_bits}; +use super::transposed::TransposedTableau; + +/// Random targets a batch needs before `measure_many` transposes. Measured at +/// 118 qubits: a batch of ~48 small collapses (the d=7 surface code) is faster +/// strided, a batch of 118 is 2.8× faster transposed. The break-even should +/// grow with `n` (transposing costs ~n²/64 words, a strided collapse ~n rows); +/// retune with other sizes. +const TRANSPOSE_THRESHOLD: usize = 64; + +/// What measuring needs from a tableau layout: row `iz_q` and the collapse. +trait Collapse { + /// Columns where row `iz_q` has an X bit, ascending, into `out`. + fn iz_x_support(&self, q: usize, out: &mut Vec); + fn iz_z_bit(&self, q: usize, c: usize) -> bool; + fn iz_negative(&self, q: usize) -> bool; + /// `C ← C·V` with `V = CX(p, targets…) · last(p)`, where `last` is + /// [`collapse_last`] with these flags. + fn collapse(&mut self, pivot: usize, targets: &[usize], has_y: bool, fix_sign: bool); +} + +/// The collapse's final gate on the pivot: `S` if it holds a Y, then `H`, then +/// `X` if the sign calls for it. Generic so both layouts run the same rules. +#[inline(always)] +fn collapse_last(has_y: bool, fix_sign: bool, x: B, z: B) -> ([B; 2], B) { + // `x & !x` is the zero flip (`Bits` has no zero constant). + let ([x, z], f1) = if has_y { + heisenberg::s(x, z) + } else { + ([x, z], x & !x) + }; + let ([x, z], f2) = heisenberg::h(x, z); + let ([x, z], f3) = if fix_sign { + heisenberg::x(x, z) + } else { + ([x, z], x & !x) + }; + ([x, z], f1 ^ f2 ^ f3) +} + +/// Measure `Z_q` on `t` and collapse; `targets` is scratch. +/// +/// Measuring `Z_q` is measuring `O = C⁻¹ Z_q C` (row `iz_q`) on `|0…0⟩`: +/// +/// - **No X in `O`:** `O` is a signed Z string, so the outcome is its sign. +/// - **X on some virtual qubits:** 50/50. Pick the lowest, `p`, as pivot +/// and change basis so that `O` becomes `±Z_p`: `CX(p, v)` clears every +/// other X (trivial on the state, since `p` is in `|0⟩`), leaving +/// `O = s · X_p · Z…` or `s · Y_p · Z…`. The projected state on `p` is +/// `|0⟩ ± |1⟩` (or `|0⟩ ± i|1⟩`), which `C ← C·H` (or `C·S·H`) and an +/// `X` when the sign calls for it map back to `|0⟩`. +fn measure_on( + t: &mut T, + q: usize, + coin: impl FnOnce() -> bool, + targets: &mut Vec, +) -> bool { + targets.clear(); + t.iz_x_support(q, targets); + if targets.is_empty() { + return t.iz_negative(q); + } + let pivot = targets.remove(0); + // The CXs reduce O to s · X_p or s · Y_p (times Z's); find which, and s, + // from row iz_q alone before changing the tableau. + let (mut pivot_bits, mut negative) = ([true, t.iz_z_bit(q, pivot)], t.iz_negative(q)); + for &v in targets.iter() { + let ([px, pz, _, _], flip) = + heisenberg::cnot([pivot_bits[0], pivot_bits[1], true, t.iz_z_bit(q, v)]); + pivot_bits = [px, pz]; + negative ^= flip; + } + let (has_y, outcome) = (pivot_bits[1], coin()); + t.collapse(pivot, targets, has_y, outcome != negative); + outcome +} + +impl Collapse for InverseTableau { + fn iz_x_support(&self, q: usize, out: &mut Vec) { + out.extend(set_bits(self.iz(q).x)); + } + + fn iz_z_bit(&self, q: usize, c: usize) -> bool { + self.iz(q).z_bit(c) + } + + fn iz_negative(&self, q: usize) -> bool { + self.iz(q).negative + } + + fn collapse(&mut self, pivot: usize, targets: &[usize], has_y: bool, fix_sign: bool) { + self.fan_out(pivot, targets, &[], |x, z| { + collapse_last(has_y, fix_sign, x, z) + }); + } +} + +impl Collapse for TransposedTableau { + fn iz_x_support(&self, q: usize, out: &mut Vec) { + out.extend((0..self.num_qubits()).filter(|&c| self.bits(1, q, c)[0])); + } + + fn iz_z_bit(&self, q: usize, c: usize) -> bool { + self.bits(1, q, c)[1] + } + + fn iz_negative(&self, q: usize) -> bool { + self.negative(1, q) + } + + fn collapse(&mut self, pivot: usize, targets: &[usize], has_y: bool, fix_sign: bool) { + self.fan_out(pivot, targets, |x: BitWord, z: BitWord| { + collapse_last(has_y, fix_sign, x, z) + }); + } +} impl InverseTableau { /// Measure `Z_q` on `C |0…0⟩` and collapse; returns the outcome (`true` - /// for `|1⟩`). `coin` is called only if the outcome is random. - /// - /// Measuring `Z_q` is measuring `O = C⁻¹ Z_q C` (row `iz_q`) on `|0…0⟩`: - /// - /// - **No X in `O`:** `O` is a signed Z string, so the outcome is its sign. - /// - **X on some virtual qubits:** 50/50. Pick the lowest, `p`, as pivot - /// and change basis so that `O` becomes `±Z_p`: `CX(p, v)` clears every - /// other X (trivial on the state, since `p` is in `|0⟩`), leaving - /// `O = s · X_p · Z…` or `s · Y_p · Z…`. The projected state on `p` is - /// `|0⟩ ± |1⟩` (or `|0⟩ ± i|1⟩`), which `C ← C·H` (or `C·S·H`) and an - /// `X` when the sign calls for it map back to `|0⟩`. + /// for `|1⟩`). `coin` is called only if the outcome is random. See + /// [`measure_on`] for the method. pub fn measure(&mut self, q: usize, coin: impl FnOnce() -> bool) -> bool { - let mut x_support = set_bits(self.iz(q).x); - let Some(pivot) = x_support.next() else { - return self.iz(q).negative; - }; - let targets: Vec = x_support.collect(); - // The CXs reduce O to s · X_p or s · Y_p (times Z's); find which, and s, - // from row iz_q alone before changing the tableau. - let row = self.iz(q); - let (mut pivot_bits, mut negative) = ([true, row.z_bit(pivot)], row.negative); - for &t in &targets { - let ([px, pz, _, _], flip) = - heisenberg::cnot([pivot_bits[0], pivot_bits[1], row.x_bit(t), row.z_bit(t)]); - pivot_bits = [px, pz]; - negative ^= flip; - } - let (has_y, outcome) = (pivot_bits[1], coin()); - let fix_sign = outcome != negative; - self.fan_out(pivot, &targets, &[], |x, z| { - let ([x, z], f1) = if has_y { - heisenberg::s(x, z) - } else { - ([x, z], false) - }; - let ([x, z], f2) = heisenberg::h(x, z); - let ([x, z], f3) = if fix_sign { - heisenberg::x(x, z) - } else { - ([x, z], false) - }; - ([x, z], f1 ^ f2 ^ f3) - }); - outcome + measure_on(self, q, coin, &mut Vec::new()) } /// Measure `Z_q` for each of `qubits` in order, collapsing after each, and /// write the outcomes to `outcomes`. `coin` is called once per random - /// outcome. Same result as calling [`measure`](Self::measure) in order; - /// a batch can share setup across its random targets. + /// outcome. Same result as calling [`measure`](Self::measure) in order. + /// + /// With at least `TRANSPOSE_THRESHOLD` random targets, the batch runs on a + /// column-major copy: two transposes, then each collapse a few words per + /// 64 rows instead of one bit per row. pub fn measure_many( + &mut self, + qubits: &[usize], + coin: impl FnMut() -> bool, + outcomes: &mut [bool], + ) { + // Count random targets only as far as the decision needs. + let transpose = qubits.len() >= TRANSPOSE_THRESHOLD + && qubits + .iter() + .filter(|&&q| self.iz(q).x.iter().any(|&w| w != ZERO)) + .nth(TRANSPOSE_THRESHOLD - 1) + .is_some(); + self.measure_many_with(qubits, coin, outcomes, transpose); + } + + fn measure_many_with( &mut self, qubits: &[usize], mut coin: impl FnMut() -> bool, outcomes: &mut [bool], + transpose: bool, ) { assert_eq!(qubits.len(), outcomes.len()); - for (&q, outcome) in qubits.iter().zip(outcomes) { - *outcome = self.measure(q, &mut coin); + let mut targets = Vec::new(); + if transpose { + let mut columns = TransposedTableau::from_rows(self); + for (&q, outcome) in qubits.iter().zip(outcomes) { + *outcome = measure_on(&mut columns, q, &mut coin, &mut targets); + } + columns.write_rows(self); + } else { + for (&q, outcome) in qubits.iter().zip(outcomes) { + *outcome = measure_on(self, q, &mut coin, &mut targets); + } } } @@ -118,7 +222,8 @@ mod tests { } } - /// `measure_many` equals measuring one qubit at a time with the same coins. + /// `measure_many` equals measuring one qubit at a time with the same coins, + /// on both the row-major and the column-major path. #[test] fn measure_many_matches_measure() { for (i, n) in SIZES.into_iter().enumerate() { @@ -137,9 +242,7 @@ mod tests { s & 1 == 1 } }; - let (mut batched, mut single) = (start.clone(), start); - let mut outcomes = vec![false; qubits.len()]; - batched.measure_many(&qubits, coins(7), &mut outcomes); + let mut single = start.clone(); let mut coin = coins(7); let mut expected = Vec::new(); // `&mut coin` is needed: `coin` is reused across iterations. @@ -147,8 +250,13 @@ mod tests { for &q in &qubits { expected.push(single.measure(q, &mut coin)); } - assert_eq!(outcomes, expected, "n = {n}"); - assert_eq!(batched, single, "n = {n}"); + for transpose in [false, true] { + let mut batched = start.clone(); + let mut outcomes = vec![false; qubits.len()]; + batched.measure_many_with(&qubits, coins(7), &mut outcomes, transpose); + assert_eq!(outcomes, expected, "n = {n}, transpose = {transpose}"); + assert_eq!(batched, single, "n = {n}, transpose = {transpose}"); + } } } diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs index ddc1e39ac..5aa744397 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -12,6 +12,7 @@ //! - `physical_gates`: physical gates `C ← G·C`, which mix rows. //! - `virtual_gates`: changes of basis `C ← C·V`, which rewrite columns. //! - `measure`: Z measurement and reset of the stabilizer state. +//! - `transposed`: a column-major copy for batches of changes of basis. //! //! Where rows sit in the arena is decided only in `data`: by `offsets` (used //! by `x_at`, `z_at`) and by `for_each_row`, which walks the same blocks. @@ -22,6 +23,7 @@ mod measure; mod physical_gates; #[cfg(test)] mod test_support; +mod transposed; mod virtual_gates; pub use data::{InverseTableau, RowRef}; diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs b/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs new file mode 100644 index 000000000..79df8be11 --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs @@ -0,0 +1,184 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! A column-major copy of the tableau, for batches of changes of basis. +//! +//! Column `c` (virtual qubit `c`) of a row block is `stride` contiguous words, +//! one bit per row, so a change of basis runs the gate rules on 64 rows per +//! word (`B = u64`) instead of one bit per row. Building the copy and writing +//! it back each cost a transpose, so it only pays for a batch. + +use ppvm_pauli_kernels::heisenberg; + +use super::InverseTableau; +use super::bitword::{BitWord, ZERO, flip_bit, get_bit, transpose_64x64}; + +pub(super) struct TransposedTableau { + n: usize, + stride: usize, + /// `x[k][c * stride + w]`: X bits of column `c` for rows `64w..` of + /// block `k` (0: the `ix` rows, 1: the `iz` rows). + x: [Vec; 2], + z: [Vec; 2], + /// `signs[k]`: bit `i` is the sign of row `i` of block `k`. + signs: [Vec; 2], +} + +impl TransposedTableau { + /// The column-major copy of `t`. + pub(super) fn from_rows(t: &InverseTableau) -> Self { + let (n, stride) = (t.n, t.stride); + let empty = || [vec![ZERO; n * stride], vec![ZERO; n * stride]]; + let mut c = Self { + n, + stride, + x: empty(), + z: empty(), + signs: [vec![ZERO; stride], vec![ZERO; stride]], + }; + for k in 0..2 { + for plane in 0..2 { + let dst = if plane == 0 { &mut c.x[k] } else { &mut c.z[k] }; + for_each_block(n, stride, |rows, cols| { + let mut block = [ZERO; 64]; + for (r, word) in block.iter_mut().enumerate() { + let i = rows + r; + if i < n { + let (x, z) = InverseTableau::offsets(n, stride, k, i); + *word = t.words[if plane == 0 { x } else { z } + cols / 64]; + } + } + transpose_64x64(&mut block); + for (j, &word) in block.iter().enumerate() { + if cols + j < n { + dst[(cols + j) * stride + rows / 64] = word; + } + } + }); + } + for i in 0..n { + if t.negative(k * n + i) { + flip_bit(&mut c.signs[k], i); + } + } + } + c + } + + /// Write this copy back into the rows of `t`. + pub(super) fn write_rows(&self, t: &mut InverseTableau) { + let (n, stride) = (self.n, self.stride); + for k in 0..2 { + for plane in 0..2 { + let src = if plane == 0 { &self.x[k] } else { &self.z[k] }; + for_each_block(n, stride, |rows, cols| { + let mut block = [ZERO; 64]; + for (j, word) in block.iter_mut().enumerate() { + if cols + j < n { + *word = src[(cols + j) * stride + rows / 64]; + } + } + transpose_64x64(&mut block); + for (r, &word) in block.iter().enumerate() { + let i = rows + r; + if i < n { + let (x, z) = InverseTableau::offsets(n, stride, k, i); + t.words[if plane == 0 { x } else { z } + cols / 64] = word; + } + } + }); + } + for i in 0..n { + if t.negative(k * n + i) != get_bit(&self.signs[k], i) { + t.flip_sign(k * n + i); + } + } + } + } + + /// X and Z bits of row `i` of block `k` at column `c`. + pub(super) fn bits(&self, k: usize, i: usize, c: usize) -> [bool; 2] { + let w = c * self.stride; + [ + get_bit(&self.x[k][w..w + self.stride], i), + get_bit(&self.z[k][w..w + self.stride], i), + ] + } + + pub(super) fn negative(&self, k: usize, i: usize) -> bool { + get_bit(&self.signs[k], i) + } + + pub(super) fn num_qubits(&self) -> usize { + self.n + } + + /// [`InverseTableau::fan_out`] without CZ targets, on columns: the rules + /// run on 64 rows per word. + pub(super) fn fan_out( + &mut self, + pivot: usize, + cx_targets: &[usize], + last: impl Fn(BitWord, BitWord) -> ([BitWord; 2], BitWord), + ) { + let s = self.stride; + for k in 0..2 { + let (x, z) = (&mut self.x[k], &mut self.z[k]); + for w in 0..s { + let p = pivot * s + w; + let (mut px, mut pz, mut flips) = (x[p], z[p], ZERO); + for &t in cx_targets { + let i = t * s + w; + let ([nx, nz, tx, tz], f) = heisenberg::cnot([px, pz, x[i], z[i]]); + (x[i], z[i], px, pz) = (tx, tz, nx, nz); + flips ^= f; + } + let ([nx, nz], f) = last(px, pz); + (x[p], z[p]) = (nx, nz); + self.signs[k][w] ^= flips ^ f; + } + } + } +} + +/// Visit the 64 × 64 blocks of an `n × n` bit matrix by their first row and +/// first column. +fn for_each_block(n: usize, stride: usize, mut f: impl FnMut(usize, usize)) { + for rows in (0..stride).map(|w| 64 * w).filter(|&r| r < n) { + for cols in (0..stride).map(|w| 64 * w).filter(|&c| c < n) { + f(rows, cols); + } + } +} + +#[cfg(test)] +mod tests { + use super::super::test_support::*; + use super::*; + + /// Building the copy and writing it back restores the tableau exactly, + /// and the copy's bits and signs are the rows' bits and signs. + #[test] + fn round_trip_and_bits_match() { + for (i, n) in SIZES.into_iter().enumerate() { + let mut t = InverseTableau::new(n); + random_circuit(n, 150, 0x7a + i as u64) + .into_iter() + .for_each(|g| apply(&mut t, g)); + let c = TransposedTableau::from_rows(&t); + for q in 0..n { + for col in 0..n { + assert_eq!(c.bits(0, q, col), [t.ix(q).x_bit(col), t.ix(q).z_bit(col)]); + assert_eq!(c.bits(1, q, col), [t.iz(q).x_bit(col), t.iz(q).z_bit(col)]); + } + assert_eq!(c.negative(1, q), t.iz(q).negative); + } + let mut back = InverseTableau::new(n); + random_circuit(n, 20, 0x99) + .into_iter() + .for_each(|g| apply(&mut back, g)); + c.write_rows(&mut back); + assert_eq!(back, t, "n = {n}"); + } + } +} From da76493bb68e1c698383fb8fdda5363d2a473a0a Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 17:16:09 +0100 Subject: [PATCH 21/60] bench(ppvm-tableau-2): port the Stim comparison from PR 236 A size sweep over Stim-generated surface, repetition and color memory circuits (noise removed, C_XYZ/MX/MY rewritten), a per-gate CX/H benchmark over n, and an official-Stim timing script, all driven through run_stim. Self-contained under benches/stim-compare; may not be shipped. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../benches/stim-compare/README.md | 42 ++++++++++ .../benches/stim-compare/circuits.py | 56 +++++++++++++ .../benches/stim-compare/common.py | 59 ++++++++++++++ .../benches/stim-compare/gates.py | 61 ++++++++++++++ .../benches/stim-compare/stim_bench.py | 32 ++++++++ .../benches/stim-compare/sweep.py | 80 +++++++++++++++++++ 6 files changed, 330 insertions(+) create mode 100644 crates/ppvm-tableau-2/benches/stim-compare/README.md create mode 100644 crates/ppvm-tableau-2/benches/stim-compare/circuits.py create mode 100644 crates/ppvm-tableau-2/benches/stim-compare/common.py create mode 100644 crates/ppvm-tableau-2/benches/stim-compare/gates.py create mode 100644 crates/ppvm-tableau-2/benches/stim-compare/stim_bench.py create mode 100644 crates/ppvm-tableau-2/benches/stim-compare/sweep.py diff --git a/crates/ppvm-tableau-2/benches/stim-compare/README.md b/crates/ppvm-tableau-2/benches/stim-compare/README.md new file mode 100644 index 000000000..e6e3e5cf7 --- /dev/null +++ b/crates/ppvm-tableau-2/benches/stim-compare/README.md @@ -0,0 +1,42 @@ +# ppvm-tableau-2 vs Stim + +Development benchmarks comparing the stabilizer core of `ppvm-tableau-2` +(`InverseTableau`, through the `run_stim` example) with official Stim's +`TableauSimulator`. Ported from PR 236's `bench/stim-compare`. Possibly not +shipped; nothing in the crate depends on this directory. + +```bash +cd crates/ppvm-tableau-2/benches/stim-compare +uv run --no-project --with stim python sweep.py # sizes, three code families +uv run --no-project --with stim python gates.py # CX and H cost over n +uv run --no-project --with stim python stim_bench.py +``` + +Outputs (circuits, `sweep.csv`) go to `target/stim-compare/`. `--extra +NAME=PATH` adds a reference binary with `run_stim`'s CLI +(` `, printing ` µs/shot`). + +## What is measured + +- Time per shot of gates, measurements and resets on a fresh simulator per + shot. Noise is removed (`circuits.py`), so both sides run the same + noise-free circuit; noise sampling is the circuit runner's job. +- **sweep.py**: Stim-generated memory circuits (surface d = 3…31, repetition + d = 3…675, color d = 3…43, rounds = d), alternating tools over rounds, + median per tool. The color code's `C_XYZ`, `MX`, `MY` are rewritten into + gates ppvm's harness supports, for both tools. +- **gates.py**: a brickwork `CX` layer and a full `H` layer, timed at two + repeat counts; the difference gives ns per gate without per-shot overhead. + +## Caveats + +- **PyPI Stim** is a generic build and is called from Python once per shot, + which adds a few µs per shot. Per-shot comparisons on small circuits favor + ppvm; `gates.py` cancels the overhead. A natively built Stim + (`-march=native`) is the stricter baseline; pass it with `--extra`. +- **Instruction batching differs**: Stim merges consecutive same-gate + instructions and shares measurement setup per instruction; ppvm's + `stim-parser` does not merge (issue 291). See the design doc's Benchmarks + section. +- This compares the stabilizer core. Once `GeneralizedTableau` exists, its + `k = 0` overhead (active-mask checks) should be compared separately. diff --git a/crates/ppvm-tableau-2/benches/stim-compare/circuits.py b/crates/ppvm-tableau-2/benches/stim-compare/circuits.py new file mode 100644 index 000000000..868164704 --- /dev/null +++ b/crates/ppvm-tableau-2/benches/stim-compare/circuits.py @@ -0,0 +1,56 @@ +"""Stim-generated Clifford memory circuits, as both simulators run them: noise +removed, and the two instructions ppvm's harness lacks rewritten. + +- `C_XYZ` is `H` then `SQRT_X_DAG` (checked with `stim.Tableau`). +- `MX` and `MY` (only the color code's final data measurements) are `H`, `M` + and `S_DAG`, `H`, `M`: they map X or Y to Z first; the state afterwards is + not used. +""" + +import re + +import stim + +# (family, Stim generator task, distances); rounds = distance. +FAMILIES = [ + ("surface", "surface_code:rotated_memory_z", [3, 5, 7, 9, 11, 15, 19, 23, 27, 31]), + ("repetition", "repetition_code:memory", [3, 9, 25, 75, 225, 675]), + ("color", "color_code:memory_xyz", [3, 5, 7, 9, 13, 17, 21, 25, 31, 37, 43]), +] + + +def generate(task: str, distance: int) -> str: + """The circuit text for `task` at `distance`, noise-free and rewritten.""" + noise = 0.001 # only so the generator emits the full structure; removed below + circuit = stim.Circuit.generated( + task, + distance=distance, + rounds=distance, + after_clifford_depolarization=noise, + before_round_data_depolarization=noise, + before_measure_flip_probability=noise, + after_reset_flip_probability=noise, + ).without_noise() + return rewrite(str(circuit)) + + +def rewrite(text: str) -> str: + def c_xyz(m: re.Match) -> str: + targets = m.group(1) + return f"H {targets}\nSQRT_X_DAG {targets}" + + def mx(m: re.Match) -> str: + targets = m.group(1) + return f"H {targets}\nM {targets}" + + def my(m: re.Match) -> str: + targets = m.group(1) + return f"S_DAG {targets}\nH {targets}\nM {targets}" + + text = re.sub(r"^\s*C_XYZ ([\d ]+)$", c_xyz, text, flags=re.MULTILINE) + text = re.sub(r"^\s*MX ([\d ]+)$", mx, text, flags=re.MULTILINE) + return re.sub(r"^\s*MY ([\d ]+)$", my, text, flags=re.MULTILINE) + + +def num_qubits(text: str) -> int: + return stim.Circuit(text).num_qubits diff --git a/crates/ppvm-tableau-2/benches/stim-compare/common.py b/crates/ppvm-tableau-2/benches/stim-compare/common.py new file mode 100644 index 000000000..72309132c --- /dev/null +++ b/crates/ppvm-tableau-2/benches/stim-compare/common.py @@ -0,0 +1,59 @@ +"""Running the timing tools: ppvm's `run_stim`, official Stim, and optional +extra reference binaries with the same CLI (` `, +printing ` µs/shot` or ` us/shot`).""" + +import pathlib +import re +import subprocess +import sys + +HERE = pathlib.Path(__file__).resolve().parent +REPO = HERE.parents[3] +OUT = REPO / "target" / "stim-compare" +RUN_STIM = REPO / "target" / "release" / "examples" / "run_stim" + + +def build_ppvm() -> None: + subprocess.run( + [ + "cargo", + "build", + "--release", + "-q", + "-p", + "ppvm-tableau-2", + "--example", + "run_stim", + ], + cwd=REPO, + check=True, + ) + + +def tools(extras: list[str]) -> dict[str, list[str]]: + """`ppvm`, `stim`, and `NAME=PATH` extras.""" + found = { + "ppvm": [str(RUN_STIM)], + "stim": [sys.executable, str(HERE / "stim_bench.py")], + } + for extra in extras: + name, path = extra.split("=", 1) + found[name] = [path] + return found + + +def time_per_shot(cmd: list[str], circuit: pathlib.Path, shots: int) -> float: + """µs per shot.""" + out = subprocess.run( + [*cmd, str(circuit), str(shots)], capture_output=True, text=True, check=True + ).stdout + match = re.search(r"([\d.]+) (?:µs|us)/shot", out) + if not match: + raise RuntimeError(f"no timing in output of {cmd}: {out!r}") + return float(match.group(1)) + + +def calibrated_shots(cmd: list[str], circuit: pathlib.Path, seconds: float) -> int: + """Shots for about `seconds` of runtime.""" + estimate = time_per_shot(cmd, circuit, 3) + return max(3, min(50_000, int(seconds * 1e6 / max(estimate, 1e-3)))) diff --git a/crates/ppvm-tableau-2/benches/stim-compare/gates.py b/crates/ppvm-tableau-2/benches/stim-compare/gates.py new file mode 100644 index 000000000..f7512abca --- /dev/null +++ b/crates/ppvm-tableau-2/benches/stim-compare/gates.py @@ -0,0 +1,61 @@ +"""Per-gate cost over size: a brickwork `CX` layer and a full `H` layer, on +ppvm-tableau-2's `run_stim` and on official Stim. Each circuit is timed at two +repeat counts; the difference removes per-shot overhead (simulator +construction, the Python call). + + uv run --no-project --with stim python gates.py [--extra NAME=PATH]... +""" + +import argparse +import statistics + +import common + +SIZES = [32, 64, 128, 274, 512, 1024, 2048] +REPEATS = (50, 150) + + +def layer(gate: str, n: int) -> tuple[str, int]: + """(one layer, gates in it).""" + if gate == "CX": + pairs = [(q, q + 1) for q in range(0, n - 1, 2)] + [ + (q, q + 1) for q in range(1, n - 1, 2) + ] + even, odd = pairs[: (n // 2)], pairs[(n // 2) :] + lines = [ + f"CX {' '.join(f'{a} {b}' for a, b in even)}", + f"CX {' '.join(f'{a} {b}' for a, b in odd)}", + ] + return "\n".join(lines), len(pairs) + return f"H {' '.join(map(str, range(n)))}", n + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--extra", action="append", default=[]) + parser.add_argument("--rounds", type=int, default=3) + args = parser.parse_args() + common.build_ppvm() + tools = common.tools(args.extra) + (common.OUT / "gates").mkdir(parents=True, exist_ok=True) + for gate in ("CX", "H"): + for n in SIZES: + body, count = layer(gate, n) + paths = [] + for reps in REPEATS: + path = common.OUT / "gates" / f"{gate}_{n}_{reps}.stim" + path.write_text(f"REPEAT {reps} {{\n{body}\n}}\n") + paths.append(path) + cells = [] + for name, cmd in tools.items(): + shots = common.calibrated_shots(cmd, paths[1], 0.2) + deltas = [] + for _ in range(args.rounds): + lo, hi = (common.time_per_shot(cmd, p, shots) for p in paths) + deltas.append((hi - lo) * 1e3 / ((REPEATS[1] - REPEATS[0]) * count)) + cells.append(f"{name} {statistics.median(deltas):6.2f}") + print(f"{gate:2} n={n:<5} ns/gate: " + " ".join(cells), flush=True) + + +if __name__ == "__main__": + main() diff --git a/crates/ppvm-tableau-2/benches/stim-compare/stim_bench.py b/crates/ppvm-tableau-2/benches/stim-compare/stim_bench.py new file mode 100644 index 000000000..4cc7ebf6c --- /dev/null +++ b/crates/ppvm-tableau-2/benches/stim-compare/stim_bench.py @@ -0,0 +1,32 @@ +"""Time official Stim's TableauSimulator on a circuit, the way ppvm's +`run_stim` runs it: a fresh simulator per shot, one `do_circuit` per shot. + +Usage: `stim_bench.py `. Prints ` µs/shot`. +The per-shot Python call adds about a microsecond; it matters only for the +smallest circuits. +""" + +import pathlib +import sys +import time + +import stim + + +def main() -> None: + circuit = stim.Circuit(pathlib.Path(sys.argv[1]).read_text()) + shots = int(sys.argv[2]) + n = circuit.num_qubits + for _ in range(2): # warm-up + stim.TableauSimulator().do_circuit(circuit) + start = time.perf_counter() + for _ in range(shots): + sim = stim.TableauSimulator() + sim.set_num_qubits(n) + sim.do_circuit(circuit) + elapsed = time.perf_counter() - start + print(f"{elapsed / shots * 1e6:.2f} µs/shot (stim {stim.__version__}, {n} qubits)") + + +if __name__ == "__main__": + main() diff --git a/crates/ppvm-tableau-2/benches/stim-compare/sweep.py b/crates/ppvm-tableau-2/benches/stim-compare/sweep.py new file mode 100644 index 000000000..8cc25c82e --- /dev/null +++ b/crates/ppvm-tableau-2/benches/stim-compare/sweep.py @@ -0,0 +1,80 @@ +"""Size sweep: Stim-generated Clifford memory circuits (see `circuits.py`) on +ppvm-tableau-2's `run_stim` and on official Stim, alternating tools over a few +rounds; median per tool. Writes the circuits and `sweep.csv` to +`target/stim-compare/`. + + uv run --no-project --with stim python sweep.py [--rounds 3] [--max-qubits N] + [--family surface] [--extra NAME=PATH]... + +`--extra` adds a reference binary with `run_stim`'s CLI. Times are per shot, +noise-free, gates/measurements/resets only; see the design doc's Benchmarks +section for how instruction batching differs between the tools. +""" + +import argparse +import csv +import statistics + +import circuits +import common + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--rounds", type=int, default=3) + parser.add_argument("--max-qubits", type=int, default=10**9) + parser.add_argument("--family", action="append") + parser.add_argument("--extra", action="append", default=[]) + parser.add_argument( + "--seconds", type=float, default=0.3, help="target runtime per measurement" + ) + args = parser.parse_args() + + common.build_ppvm() + tools = common.tools(args.extra) + (common.OUT / "circuits").mkdir(parents=True, exist_ok=True) + rows = [] + for family, task, distances in circuits.FAMILIES: + if args.family and family not in args.family: + continue + for d in distances: + text = circuits.generate(task, d) + n = circuits.num_qubits(text) + if n > args.max_qubits: + continue + path = common.OUT / "circuits" / f"{family}_d{d}.stim" + path.write_text(text) + shots = { + name: common.calibrated_shots(cmd, path, args.seconds) + for name, cmd in tools.items() + } + times = {name: [] for name in tools} + for _ in range(args.rounds): + for name, cmd in tools.items(): + times[name].append(common.time_per_shot(cmd, path, shots[name])) + row = {"family": family, "distance": d, "qubits": n} + row |= { + f"{name}_us": round(statistics.median(ts), 3) + for name, ts in times.items() + } + row["ppvm_over_stim"] = round(row["ppvm_us"] / row["stim_us"], 3) + rows.append(row) + others = " ".join( + f"{name} {row[f'{name}_us']:.1f}" + for name in tools + if name not in ("ppvm", "stim") + ) + print( + f"{family:10} d={d:<4} {n:5} q ppvm {row['ppvm_us']:10.1f} µs stim {row['stim_us']:10.1f} µs" + f" ppvm/stim {row['ppvm_over_stim']:.2f} {others}", + flush=True, + ) + with open(common.OUT / "sweep.csv", "w", newline="") as f: + writer = csv.DictWriter(f, fieldnames=list(rows[0])) + writer.writeheader() + writer.writerows(rows) + print(f"wrote {common.OUT / 'sweep.csv'}") + + +if __name__ == "__main__": + main() From 1ad3b2de48ab7d41431f0dbe3393634fc98ee4f6 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 17:20:36 +0100 Subject: [PATCH 22/60] bench(ppvm-tableau-2): compare against a native Stim build Python Stim now only generates circuits; timing is against reference binaries (--reference NAME=PATH). Add stim_driver.cc, a TableauSimulator driver with run_stim's CLI that builds against any Stim checkout, and --case / --size filters for running selected benchmarks. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../benches/stim-compare/README.md | 27 ++++++++----- .../benches/stim-compare/common.py | 21 +++++----- .../benches/stim-compare/gates.py | 15 +++---- .../benches/stim-compare/stim_bench.py | 32 --------------- .../benches/stim-compare/stim_driver.cc | 38 ++++++++++++++++++ .../benches/stim-compare/sweep.py | 40 +++++++++++-------- 6 files changed, 96 insertions(+), 77 deletions(-) delete mode 100644 crates/ppvm-tableau-2/benches/stim-compare/stim_bench.py create mode 100644 crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc diff --git a/crates/ppvm-tableau-2/benches/stim-compare/README.md b/crates/ppvm-tableau-2/benches/stim-compare/README.md index e6e3e5cf7..0439d2d6e 100644 --- a/crates/ppvm-tableau-2/benches/stim-compare/README.md +++ b/crates/ppvm-tableau-2/benches/stim-compare/README.md @@ -6,15 +6,23 @@ Development benchmarks comparing the stabilizer core of `ppvm-tableau-2` shipped; nothing in the crate depends on this directory. ```bash +# A reference: stim_driver.cc built against any Stim checkout (C++, -march=native). +cmake -S -B target/stim-build -DCMAKE_BUILD_TYPE=Release +cmake --build target/stim-build --target libstim -j +clang++ -O3 -std=c++20 -march=native -fno-strict-aliasing -I /src \ + crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc \ + target/stim-build/out/libstim.a -o target/stim-compare/stim_driver + cd crates/ppvm-tableau-2/benches/stim-compare -uv run --no-project --with stim python sweep.py # sizes, three code families -uv run --no-project --with stim python gates.py # CX and H cost over n -uv run --no-project --with stim python stim_bench.py +uv run --no-project --with stim python sweep.py --reference stim=$PWD/../../../../target/stim-compare/stim_driver \ + --case surface:7 --case color:25 # or no --case for the full sweep +uv run --no-project --with stim python gates.py --reference stim=... --size 64 --size 2048 ``` -Outputs (circuits, `sweep.csv`) go to `target/stim-compare/`. `--extra -NAME=PATH` adds a reference binary with `run_stim`'s CLI -(` `, printing ` µs/shot`). +Python Stim (`uv run --with stim`) only generates circuits. Outputs (circuits, +`sweep.csv`) go to `target/stim-compare/`. Each `--reference NAME=PATH` is a +binary with `run_stim`'s CLI (` `, printing +` µs/shot`); ratios are against the first. ## What is measured @@ -30,10 +38,9 @@ NAME=PATH` adds a reference binary with `run_stim`'s CLI ## Caveats -- **PyPI Stim** is a generic build and is called from Python once per shot, - which adds a few µs per shot. Per-shot comparisons on small circuits favor - ppvm; `gates.py` cancels the overhead. A natively built Stim - (`-march=native`) is the stricter baseline; pass it with `--extra`. +- Compare against a native C++ build, as above. PyPI Stim is a generic build + and its per-shot Python call adds a few µs, which flatters ppvm on small + circuits. - **Instruction batching differs**: Stim merges consecutive same-gate instructions and shares measurement setup per instruction; ppvm's `stim-parser` does not merge (issue 291). See the design doc's Benchmarks diff --git a/crates/ppvm-tableau-2/benches/stim-compare/common.py b/crates/ppvm-tableau-2/benches/stim-compare/common.py index 72309132c..8e7d2cfff 100644 --- a/crates/ppvm-tableau-2/benches/stim-compare/common.py +++ b/crates/ppvm-tableau-2/benches/stim-compare/common.py @@ -1,6 +1,6 @@ -"""Running the timing tools: ppvm's `run_stim`, official Stim, and optional -extra reference binaries with the same CLI (` `, -printing ` µs/shot` or ` us/shot`).""" +"""Running the timing tools: ppvm's `run_stim` and reference binaries with +the same CLI (` `, printing ` µs/shot` or +` us/shot`), such as `stim_driver` built against a Stim checkout.""" import pathlib import re @@ -30,14 +30,13 @@ def build_ppvm() -> None: ) -def tools(extras: list[str]) -> dict[str, list[str]]: - """`ppvm`, `stim`, and `NAME=PATH` extras.""" - found = { - "ppvm": [str(RUN_STIM)], - "stim": [sys.executable, str(HERE / "stim_bench.py")], - } - for extra in extras: - name, path = extra.split("=", 1) +def tools(references: list[str]) -> dict[str, list[str]]: + """`ppvm`, then the `NAME=PATH` references in order.""" + if not references: + sys.exit("give at least one --reference NAME=PATH (see README.md)") + found = {"ppvm": [str(RUN_STIM)]} + for reference in references: + name, path = reference.split("=", 1) found[name] = [path] return found diff --git a/crates/ppvm-tableau-2/benches/stim-compare/gates.py b/crates/ppvm-tableau-2/benches/stim-compare/gates.py index f7512abca..c995e1bc2 100644 --- a/crates/ppvm-tableau-2/benches/stim-compare/gates.py +++ b/crates/ppvm-tableau-2/benches/stim-compare/gates.py @@ -1,9 +1,9 @@ """Per-gate cost over size: a brickwork `CX` layer and a full `H` layer, on -ppvm-tableau-2's `run_stim` and on official Stim. Each circuit is timed at two -repeat counts; the difference removes per-shot overhead (simulator -construction, the Python call). +ppvm-tableau-2's `run_stim` and on reference binaries. Each circuit is timed at +two repeat counts; the difference removes per-shot overhead (simulator +construction). - uv run --no-project --with stim python gates.py [--extra NAME=PATH]... + python gates.py --reference stim=PATH [--size 64]... [--rounds 3] """ import argparse @@ -32,14 +32,15 @@ def layer(gate: str, n: int) -> tuple[str, int]: def main() -> None: parser = argparse.ArgumentParser() - parser.add_argument("--extra", action="append", default=[]) + parser.add_argument("--reference", action="append", default=[]) + parser.add_argument("--size", action="append", type=int) parser.add_argument("--rounds", type=int, default=3) args = parser.parse_args() common.build_ppvm() - tools = common.tools(args.extra) + tools = common.tools(args.reference) (common.OUT / "gates").mkdir(parents=True, exist_ok=True) for gate in ("CX", "H"): - for n in SIZES: + for n in args.size or SIZES: body, count = layer(gate, n) paths = [] for reps in REPEATS: diff --git a/crates/ppvm-tableau-2/benches/stim-compare/stim_bench.py b/crates/ppvm-tableau-2/benches/stim-compare/stim_bench.py deleted file mode 100644 index 4cc7ebf6c..000000000 --- a/crates/ppvm-tableau-2/benches/stim-compare/stim_bench.py +++ /dev/null @@ -1,32 +0,0 @@ -"""Time official Stim's TableauSimulator on a circuit, the way ppvm's -`run_stim` runs it: a fresh simulator per shot, one `do_circuit` per shot. - -Usage: `stim_bench.py `. Prints ` µs/shot`. -The per-shot Python call adds about a microsecond; it matters only for the -smallest circuits. -""" - -import pathlib -import sys -import time - -import stim - - -def main() -> None: - circuit = stim.Circuit(pathlib.Path(sys.argv[1]).read_text()) - shots = int(sys.argv[2]) - n = circuit.num_qubits - for _ in range(2): # warm-up - stim.TableauSimulator().do_circuit(circuit) - start = time.perf_counter() - for _ in range(shots): - sim = stim.TableauSimulator() - sim.set_num_qubits(n) - sim.do_circuit(circuit) - elapsed = time.perf_counter() - start - print(f"{elapsed / shots * 1e6:.2f} µs/shot (stim {stim.__version__}, {n} qubits)") - - -if __name__ == "__main__": - main() diff --git a/crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc b/crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc new file mode 100644 index 000000000..9a0a8d01c --- /dev/null +++ b/crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc @@ -0,0 +1,38 @@ +// Times Stim's TableauSimulator on a circuit the way ppvm-tableau-2's run_stim +// runs it: noise removed, a fresh simulator per shot. Builds against any Stim +// checkout's libstim; see README.md. Usage: stim_driver [shots]. +#include +#include +#include +#include + +#include "stim.h" + +int main(int argc, char **argv) { + if (argc < 2) { + std::cerr << "usage: stim_driver [shots]\n"; + return 1; + } + std::ifstream in(argv[1]); + std::stringstream text; + text << in.rdbuf(); + stim::Circuit circuit = stim::Circuit(text.str()).without_noise(); + size_t shots = argc > 2 ? std::stoul(argv[2]) : 1000; + size_t n = circuit.count_qubits(); + + std::mt19937_64 rng(0x9e3779b97f4a7c15ULL); + bool parity = false; + auto start = std::chrono::steady_clock::now(); + for (size_t s = 0; s < shots; s++) { + stim::TableauSimulator sim(std::move(rng), n); + sim.safe_do_circuit(circuit); + if (!sim.measurement_record.storage.empty()) { + parity ^= sim.measurement_record.storage.back(); + } + rng = std::move(sim.rng); + } + double elapsed = std::chrono::duration(std::chrono::steady_clock::now() - start).count(); + std::cout << argv[1] << ": " << n << " qubits, " << shots << " shots, word width " + << stim::MAX_BITWORD_WIDTH << "\n" + << elapsed / shots * 1e6 << " us/shot (parity " << parity << ")\n"; +} diff --git a/crates/ppvm-tableau-2/benches/stim-compare/sweep.py b/crates/ppvm-tableau-2/benches/stim-compare/sweep.py index 8cc25c82e..f9dcadfa4 100644 --- a/crates/ppvm-tableau-2/benches/stim-compare/sweep.py +++ b/crates/ppvm-tableau-2/benches/stim-compare/sweep.py @@ -1,14 +1,15 @@ """Size sweep: Stim-generated Clifford memory circuits (see `circuits.py`) on -ppvm-tableau-2's `run_stim` and on official Stim, alternating tools over a few -rounds; median per tool. Writes the circuits and `sweep.csv` to +ppvm-tableau-2's `run_stim` and on reference binaries, alternating tools over a +few rounds; median per tool. Writes the circuits and `sweep.csv` to `target/stim-compare/`. - uv run --no-project --with stim python sweep.py [--rounds 3] [--max-qubits N] - [--family surface] [--extra NAME=PATH]... + uv run --no-project --with stim python sweep.py --reference stim=PATH + [--case surface:7]... [--family surface] [--max-qubits N] [--rounds 3] -`--extra` adds a reference binary with `run_stim`'s CLI. Times are per shot, -noise-free, gates/measurements/resets only; see the design doc's Benchmarks -section for how instruction batching differs between the tools. +Python Stim only generates the circuits; timing is against the references +(e.g. `stim_driver`, see README.md). Times are per shot, noise-free, +gates/measurements/resets only; see the design doc's Benchmarks section for how +instruction batching differs between the tools. """ import argparse @@ -24,20 +25,27 @@ def main() -> None: parser.add_argument("--rounds", type=int, default=3) parser.add_argument("--max-qubits", type=int, default=10**9) parser.add_argument("--family", action="append") - parser.add_argument("--extra", action="append", default=[]) + parser.add_argument( + "--case", action="append", help="family:distance, e.g. surface:7" + ) + parser.add_argument("--reference", action="append", default=[]) parser.add_argument( "--seconds", type=float, default=0.3, help="target runtime per measurement" ) args = parser.parse_args() common.build_ppvm() - tools = common.tools(args.extra) + tools = common.tools(args.reference) + reference = list(tools)[1] + cases = {(f, int(d)) for f, d in (c.split(":") for c in args.case or [])} (common.OUT / "circuits").mkdir(parents=True, exist_ok=True) rows = [] for family, task, distances in circuits.FAMILIES: if args.family and family not in args.family: continue for d in distances: + if cases and (family, d) not in cases: + continue text = circuits.generate(task, d) n = circuits.num_qubits(text) if n > args.max_qubits: @@ -57,16 +65,14 @@ def main() -> None: f"{name}_us": round(statistics.median(ts), 3) for name, ts in times.items() } - row["ppvm_over_stim"] = round(row["ppvm_us"] / row["stim_us"], 3) - rows.append(row) - others = " ".join( - f"{name} {row[f'{name}_us']:.1f}" - for name in tools - if name not in ("ppvm", "stim") + row[f"ppvm_over_{reference}"] = round( + row["ppvm_us"] / row[f"{reference}_us"], 3 ) + rows.append(row) + cells = " ".join(f"{name} {row[f'{name}_us']:10.1f} µs" for name in tools) + ratio = row[f"ppvm_over_{reference}"] print( - f"{family:10} d={d:<4} {n:5} q ppvm {row['ppvm_us']:10.1f} µs stim {row['stim_us']:10.1f} µs" - f" ppvm/stim {row['ppvm_over_stim']:.2f} {others}", + f"{family:10} d={d:<4} {n:5} q {cells} ppvm/{reference} {ratio:.2f}", flush=True, ) with open(common.OUT / "sweep.csv", "w", newline="") as f: From 2022ef2e299f8960afc5248f05d385dcb38f1e09 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 11 Nov 2026 17:40:09 +0100 Subject: [PATCH 23/60] feat(ppvm-tableau-2): add the active state with a dense backend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ActiveState is the interface of the active part φ: activate, rotate, branch_weights, collapse. DenseState stores 2^k amplitudes. AxisPauli is a Pauli on the axes; Rotation is the collapse's change of basis V, built once by Rotation::collapse and shared with the tableau, with V†PV and its sign computed by the gate rules. Checked against plain matrices. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/ppvm-tableau-2/Cargo.toml | 2 +- crates/ppvm-tableau-2/src/active/axis.rs | 157 ++++++++ crates/ppvm-tableau-2/src/active/dense.rs | 464 ++++++++++++++++++++++ crates/ppvm-tableau-2/src/active/mod.rs | 18 + crates/ppvm-tableau-2/src/active/state.rs | 39 ++ crates/ppvm-tableau-2/src/lib.rs | 2 + 6 files changed, 681 insertions(+), 1 deletion(-) create mode 100644 crates/ppvm-tableau-2/src/active/axis.rs create mode 100644 crates/ppvm-tableau-2/src/active/dense.rs create mode 100644 crates/ppvm-tableau-2/src/active/mod.rs create mode 100644 crates/ppvm-tableau-2/src/active/state.rs diff --git a/crates/ppvm-tableau-2/Cargo.toml b/crates/ppvm-tableau-2/Cargo.toml index ee04b94fe..1fe9deaf6 100644 --- a/crates/ppvm-tableau-2/Cargo.toml +++ b/crates/ppvm-tableau-2/Cargo.toml @@ -6,11 +6,11 @@ license.workspace = true repository.workspace = true [dependencies] +num-complex = "0.4.6" ppvm-pauli-kernels = { path = "../ppvm-pauli-kernels" } ppvm-traits-2 = { path = "../ppvm-traits-2" } [dev-dependencies] ppvm-pauli-word-2 = { path = "../ppvm-pauli-word-2" } ppvm-phased-pauli-word-2 = { path = "../ppvm-phased-pauli-word-2" } -num-complex = "0.4.6" stim-parser = { path = "../stim-parser" } diff --git a/crates/ppvm-tableau-2/src/active/axis.rs b/crates/ppvm-tableau-2/src/active/axis.rs new file mode 100644 index 000000000..9c91f5b9f --- /dev/null +++ b/crates/ppvm-tableau-2/src/active/axis.rs @@ -0,0 +1,157 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Paulis on the active axes, and the rotation that collapses onto one axis. + +use ppvm_pauli_kernels::heisenberg; + +/// A Pauli on the active axes, unsigned: bit `j` of `x` / `z` is the X / Z +/// factor on axis `j`, and both set is Y. +/// `P|α⟩ = i^{|x∧z|} (−1)^{|α∧z|} |α ⊕ x⟩`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct AxisPauli { + pub x: u64, + pub z: u64, +} + +impl AxisPauli { + pub fn is_identity(&self) -> bool { + self.x == 0 && self.z == 0 + } + + fn get(&self, j: usize) -> [bool; 2] { + [self.x >> j & 1 == 1, self.z >> j & 1 == 1] + } + + fn set(&mut self, j: usize, [x, z]: [bool; 2]) { + self.x = self.x & !(1 << j) | u64::from(x) << j; + self.z = self.z & !(1 << j) | u64::from(z) << j; + } +} + +/// The change of basis `V` of a collapse, shared by the tableau (`C ← C·V`) +/// and the active state (`φ ← V† φ`), as a gate sequence applied in this +/// order: CX from `pivot` to every axis in `fan_out`, CX from every axis in +/// `fan_in` to `pivot`, CZ between `pivot` and every axis in `cz`, then `S`, +/// `H`, `X` on `pivot` where set. +/// +/// Built by [`Rotation::collapse`] so that `V† P V = ±Z_pivot` and the +/// measured branch leaves `pivot` in `|0⟩`; one place decides the gates. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Rotation { + pub pivot: usize, + pub fan_out: u64, + pub fan_in: u64, + pub cz: u64, + pub s: bool, + pub h: bool, + pub x: bool, +} + +impl Rotation { + /// The rotation for measuring `p` with outcome `m` (eigenvalue `(−1)^m`): + /// after `V`, the projection onto that outcome leaves `pivot` in `|0⟩`. + /// + /// With an X part, the pivot is its lowest X axis: CX from it clears the + /// other X bits, CZ clears the remaining Z bits, and `H` (`S`, `H` for a + /// Y) turns the pivot's X into Z. A Z-only `p` collects its Z bits onto its + /// lowest Z axis with CX into it. + pub fn collapse(p: AxisPauli, m: bool) -> Self { + assert!(!p.is_identity(), "measuring the identity"); + let mut v = if p.x != 0 { + let pivot = p.x.trailing_zeros() as usize; + let fan_out = p.x & !(1 << pivot); + let cx_only = Self { + pivot, + fan_out, + fan_in: 0, + cz: 0, + s: false, + h: false, + x: false, + }; + // After the CXs, the Z bits left on other axes are the CZ targets, + // and a Z left on the pivot makes it a Y. + let after_cx = cx_only.conjugate(p).0; + Self { + cz: after_cx.z & !(1 << pivot), + s: after_cx.z >> pivot & 1 == 1, + h: true, + ..cx_only + } + } else { + let pivot = p.z.trailing_zeros() as usize; + Self { + pivot, + fan_out: 0, + fan_in: p.z & !(1 << pivot), + cz: 0, + s: false, + h: false, + x: false, + } + }; + let (reduced, negative) = v.conjugate(p); + debug_assert_eq!( + reduced, + AxisPauli { + x: 0, + z: 1 << v.pivot + } + ); + // The projection leaves the pivot in |b⟩ with (−1)^b = ±(−1)^m; X maps it to |0⟩. + v.x = m != negative; + v + } + + /// `V† p V` and whether it picks up a sign, by running the gate rules on + /// `p`'s bits in gate order. + pub fn conjugate(&self, mut p: AxisPauli) -> (AxisPauli, bool) { + let mut negative = false; + let mut two = + |p: &mut AxisPauli, c: usize, t: usize, rule: fn([bool; 4]) -> ([bool; 4], bool)| { + let ([xc, zc], [xt, zt]) = (p.get(c), p.get(t)); + let ([xc, zc, xt, zt], flip) = rule([xc, zc, xt, zt]); + p.set(c, [xc, zc]); + p.set(t, [xt, zt]); + negative ^= flip; + }; + let p_ = self.pivot; + for t in bits(self.fan_out) { + two(&mut p, p_, t, heisenberg::cnot); + } + for c in bits(self.fan_in) { + two(&mut p, c, p_, heisenberg::cnot); + } + for t in bits(self.cz) { + two(&mut p, p_, t, heisenberg::cz); + } + let one = |p: &mut AxisPauli, rule: fn(bool, bool) -> ([bool; 2], bool)| { + let [x, z] = p.get(p_); + let (bits, flip) = rule(x, z); + p.set(p_, bits); + flip + }; + for (on, rule) in [ + (self.s, heisenberg::s as fn(bool, bool) -> ([bool; 2], bool)), + (self.h, heisenberg::h), + (self.x, heisenberg::x), + ] { + if on { + negative ^= one(&mut p, rule); + } + } + (p, negative) + } +} + +/// The set bits of `mask`, ascending. +pub(super) fn bits(mut mask: u64) -> impl Iterator { + std::iter::from_fn(move || { + (mask != 0).then(|| { + let j = mask.trailing_zeros() as usize; + mask &= mask - 1; + j + }) + }) +} diff --git a/crates/ppvm-tableau-2/src/active/dense.rs b/crates/ppvm-tableau-2/src/active/dense.rs new file mode 100644 index 000000000..f6df8cc5a --- /dev/null +++ b/crates/ppvm-tableau-2/src/active/dense.rs @@ -0,0 +1,464 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! The exact dense backend: `2^k` amplitudes, bit `j` of an index is axis `j`. + +use num_complex::Complex64; + +use super::axis::{AxisPauli, Rotation, bits}; +use super::state::ActiveState; + +#[derive(Clone, Debug, PartialEq)] +pub struct DenseState { + amplitudes: Vec, +} + +impl Default for DenseState { + /// No axes: the single amplitude 1. + fn default() -> Self { + Self { + amplitudes: vec![Complex64::new(1.0, 0.0)], + } + } +} + +/// `i^e`. +fn i_pow(e: u32) -> Complex64 { + [ + Complex64::new(1.0, 0.0), + Complex64::new(0.0, 1.0), + Complex64::new(-1.0, 0.0), + Complex64::new(0.0, -1.0), + ][(e % 4) as usize] +} + +/// `c` with `P|α⟩ = c · |α ⊕ x⟩`. +fn pauli_factor(p: AxisPauli, alpha: usize) -> Complex64 { + let negative = (alpha as u64 & p.z).count_ones() % 2 == 1; + let c = i_pow((p.x & p.z).count_ones()); + if negative { -c } else { c } +} + +impl DenseState { + pub fn amplitudes(&self) -> &[Complex64] { + &self.amplitudes + } + + /// `φ ← G† φ` for each gate of `v` in order. + fn apply_rotation_dagger(&mut self, v: &Rotation) { + let p = 1usize << v.pivot; + for t in bits(v.fan_out) { + self.cx(p, 1 << t); + } + for c in bits(v.fan_in) { + self.cx(1 << c, p); + } + for t in bits(v.cz) { + let both = p | 1 << t; + self.for_each_index(|a, amp| { + if a & both == both { + *amp = -*amp; + } + }); + } + if v.s { + // S† = diag(1, −i). + self.for_each_index(|a, amp| { + if a & p != 0 { + *amp *= Complex64::new(0.0, -1.0); + } + }); + } + if v.h { + let r = std::f64::consts::FRAC_1_SQRT_2; + for a in (0..self.amplitudes.len()).filter(|a| a & p == 0) { + let (lo, hi) = (self.amplitudes[a], self.amplitudes[a | p]); + self.amplitudes[a] = (lo + hi) * r; + self.amplitudes[a | p] = (lo - hi) * r; + } + } + if v.x { + for a in (0..self.amplitudes.len()).filter(|a| a & p == 0) { + self.amplitudes.swap(a, a | p); + } + } + } + + /// CX with control mask `c` and target mask `t` (its own inverse). + fn cx(&mut self, c: usize, t: usize) { + for a in (0..self.amplitudes.len()).filter(|a| a & c != 0 && a & t == 0) { + self.amplitudes.swap(a, a | t); + } + } + + fn for_each_index(&mut self, mut f: impl FnMut(usize, &mut Complex64)) { + for (a, amp) in self.amplitudes.iter_mut().enumerate() { + f(a, amp); + } + } +} + +impl ActiveState for DenseState { + fn num_axes(&self) -> usize { + self.amplitudes.len().trailing_zeros() as usize + } + + fn activate(&mut self) { + let len = self.amplitudes.len(); + self.amplitudes.resize(2 * len, Complex64::new(0.0, 0.0)); + } + + fn rotate(&mut self, p: AxisPauli, a: Complex64, b: Complex64) { + let x = p.x as usize; + if x == 0 { + self.for_each_index(|alpha, amp| *amp *= a + b * pauli_factor(p, alpha)); + return; + } + // Pair α with β = α ⊕ x: each new amplitude mixes the two old ones. + for alpha in 0..self.amplitudes.len() { + let beta = alpha ^ x; + if alpha < beta { + let (ua, ub) = (self.amplitudes[alpha], self.amplitudes[beta]); + self.amplitudes[alpha] = a * ua + b * pauli_factor(p, beta) * ub; + self.amplitudes[beta] = a * ub + b * pauli_factor(p, alpha) * ua; + } + } + } + + fn branch_weights(&self, p: AxisPauli) -> [f64; 2] { + let x = p.x as usize; + let norm: f64 = self.amplitudes.iter().map(|a| a.norm_sqr()).sum(); + // ⟨φ|P|φ⟩ = Σ_α conj(φ[α ⊕ x]) · c(α) · φ[α], real for Hermitian P. + let expectation: f64 = self + .amplitudes + .iter() + .enumerate() + .map(|(alpha, amp)| { + (self.amplitudes[alpha ^ x].conj() * pauli_factor(p, alpha) * amp).re + }) + .sum(); + [0.5 * (norm + expectation), 0.5 * (norm - expectation)] + } + + fn collapse(&mut self, p: AxisPauli, m: bool, v: &Rotation) { + // Project onto eigenvalue (−1)^m: φ ← ½(φ ± P φ). + let sign = if m { -0.5 } else { 0.5 }; + self.rotate(p, Complex64::new(0.5, 0.0), Complex64::new(sign, 0.0)); + self.apply_rotation_dagger(v); + // The pivot is |0⟩ now: keep those amplitudes, removing its bit. + let h = v.pivot; + let low = (1usize << h) - 1; + let kept: Vec = (0..self.amplitudes.len() / 2) + .map(|a| self.amplitudes[(a & low) | (a & !low) << 1]) + .collect(); + let norm = kept.iter().map(|a| a.norm_sqr()).sum::().sqrt(); + assert!(norm > 0.0, "collapsed onto a branch of zero weight"); + self.amplitudes = kept.into_iter().map(|a| a / norm).collect(); + } +} + +#[cfg(test)] +mod tests { + //! Checked against plain matrices: Paulis as tensor products of 2 × 2 + //! matrices, `V` as a product of gate matrices. Bit `j` of an index is axis + //! `j`. No code is shared with the backend. + use super::*; + + type C = Complex64; + type Mat = Vec>; + + fn c(re: f64, im: f64) -> C { + C::new(re, im) + } + + fn identity(d: usize) -> Mat { + (0..d) + .map(|i| { + (0..d) + .map(|j| c(f64::from(u8::from(i == j)), 0.0)) + .collect() + }) + .collect() + } + + fn mul(a: &Mat, b: &Mat) -> Mat { + let d = a.len(); + (0..d) + .map(|i| { + (0..d) + .map(|j| (0..d).map(|k| a[i][k] * b[k][j]).sum()) + .collect() + }) + .collect() + } + + fn dagger(a: &Mat) -> Mat { + let d = a.len(); + (0..d) + .map(|i| (0..d).map(|j| a[j][i].conj()).collect()) + .collect() + } + + fn apply(a: &Mat, v: &[C]) -> Vec { + a.iter() + .map(|row| row.iter().zip(v).map(|(x, y)| x * y).sum()) + .collect() + } + + /// The one-qubit matrix `u` on axis `j` of `k`. + fn on_axis(u: [[C; 2]; 2], j: usize, k: usize) -> Mat { + let d = 1 << k; + (0..d) + .map(|r| { + (0..d) + .map(|s| { + if (r ^ s) & !(1 << j) == 0 { + u[r >> j & 1][s >> j & 1] + } else { + c(0.0, 0.0) + } + }) + .collect() + }) + .collect() + } + + fn pauli(p: AxisPauli, k: usize) -> Mat { + let (o, l, i) = (c(0.0, 0.0), c(1.0, 0.0), c(0.0, 1.0)); + (0..k).fold(identity(1 << k), |m, j| { + let u = match (p.x >> j & 1, p.z >> j & 1) { + (0, 0) => return m, + (1, 0) => [[o, l], [l, o]], + (0, _) => [[l, o], [o, -l]], + _ => [[o, -i], [i, o]], + }; + mul(&m, &on_axis(u, j, k)) + }) + } + + /// `V` = the product of `v`'s gate matrices in gate order. + fn rotation_matrix(v: &Rotation, k: usize) -> Mat { + let d = 1 << k; + let (o, l, i) = (c(0.0, 0.0), c(1.0, 0.0), c(0.0, 1.0)); + let r = std::f64::consts::FRAC_1_SQRT_2; + let cx = |ctl: usize, tgt: usize| -> Mat { + (0..d) + .map(|row| { + (0..d) + .map(|col| { + let image = if col >> ctl & 1 == 1 { + col ^ 1 << tgt + } else { + col + }; + c(f64::from(u8::from(row == image)), 0.0) + }) + .collect() + }) + .collect() + }; + let cz = |a: usize, b: usize| -> Mat { + (0..d) + .map(|row| { + (0..d) + .map(|col| { + let s = if col >> a & 1 == 1 && col >> b & 1 == 1 { + -1.0 + } else { + 1.0 + }; + c(if row == col { s } else { 0.0 }, 0.0) + }) + .collect() + }) + .collect() + }; + let mut gates = Vec::new(); + bits(v.fan_out).for_each(|t| gates.push(cx(v.pivot, t))); + bits(v.fan_in).for_each(|s| gates.push(cx(s, v.pivot))); + bits(v.cz).for_each(|t| gates.push(cz(v.pivot, t))); + if v.s { + gates.push(on_axis([[l, o], [o, i]], v.pivot, k)); + } + if v.h { + gates.push(on_axis( + [[c(r, 0.0), c(r, 0.0)], [c(r, 0.0), c(-r, 0.0)]], + v.pivot, + k, + )); + } + if v.x { + gates.push(on_axis([[o, l], [l, o]], v.pivot, k)); + } + gates.iter().fold(identity(d), |m, g| mul(&m, g)) + } + + struct Rng(u64); + + impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + + fn unit(&mut self) -> f64 { + (self.next() >> 11) as f64 / (1u64 << 53) as f64 - 0.5 + } + + fn state(&mut self, k: usize) -> DenseState { + let amps: Vec = (0..1 << k).map(|_| c(self.unit(), self.unit())).collect(); + let norm = amps.iter().map(|a| a.norm_sqr()).sum::().sqrt(); + DenseState { + amplitudes: amps.into_iter().map(|a| a / norm).collect(), + } + } + + fn pauli(&mut self, k: usize) -> AxisPauli { + loop { + let mask = (1u64 << k) - 1; + let p = AxisPauli { + x: self.next() & mask, + z: self.next() & mask, + }; + if !p.is_identity() { + return p; + } + } + } + } + + fn assert_close(a: &[C], b: &[C]) { + assert_eq!(a.len(), b.len()); + for (x, y) in a.iter().zip(b) { + assert!((x - y).norm() < 1e-12, "{a:?} != {b:?}"); + } + } + + #[test] + fn activate_appends_zero() { + let mut rng = Rng(3); + let phi = rng.state(3); + let mut grown = phi.clone(); + grown.activate(); + assert_eq!(grown.num_axes(), 4); + let zeros = vec![c(0.0, 0.0); 8]; + assert_close(&grown.amplitudes[..8], &phi.amplitudes); + assert_close(&grown.amplitudes[8..], &zeros); + } + + #[test] + fn rotate_matches_matrices() { + let mut rng = Rng(5); + for k in 1..=5 { + for _ in 0..20 { + let (phi, p) = (rng.state(k), rng.pauli(k)); + let (a, b) = (c(rng.unit(), rng.unit()), c(rng.unit(), rng.unit())); + let mut got = phi.clone(); + got.rotate(p, a, b); + let pm = pauli(p, k); + let op: Mat = (0..1 << k) + .map(|i| { + (0..1 << k) + .map(|j| a * identity(1 << k)[i][j] + b * pm[i][j]) + .collect() + }) + .collect(); + assert_close(&got.amplitudes, &apply(&op, &phi.amplitudes)); + } + } + } + + #[test] + fn branch_weights_match_matrices() { + let mut rng = Rng(7); + for k in 1..=5 { + for _ in 0..20 { + let (phi, p) = (rng.state(k), rng.pauli(k)); + let pv = apply(&pauli(p, k), &phi.amplitudes); + let e: C = phi + .amplitudes + .iter() + .zip(&pv) + .map(|(x, y)| x.conj() * y) + .sum(); + let [w0, w1] = phi.branch_weights(p); + assert!( + (w0 - 0.5 * (1.0 + e.re)).abs() < 1e-12 + && (w1 - 0.5 * (1.0 - e.re)).abs() < 1e-12 + ); + } + } + } + + /// `V† ½(1 + (−1)^m P) V` is the projector onto "pivot in |0⟩". + #[test] + fn rotation_maps_the_branch_to_pivot_zero() { + let mut rng = Rng(11); + for k in 1..=5 { + for _ in 0..30 { + let (p, m) = (rng.pauli(k), rng.next() & 1 == 1); + let v = Rotation::collapse(p, m); + let vm = rotation_matrix(&v, k); + let (pm, id) = (pauli(p, k), identity(1 << k)); + let s = if m { -0.5 } else { 0.5 }; + let proj: Mat = (0..1 << k) + .map(|i| (0..1 << k).map(|j| 0.5 * id[i][j] + s * pm[i][j]).collect()) + .collect(); + let got = mul(&mul(&dagger(&vm), &proj), &vm); + let z = pauli( + AxisPauli { + x: 0, + z: 1 << v.pivot, + }, + k, + ); + for i in 0..1 << k { + for j in 0..1 << k { + let want = 0.5 * id[i][j] + 0.5 * z[i][j]; + assert!( + (got[i][j] - want).norm() < 1e-12, + "k = {k}, p = {p:?}, m = {m}" + ); + } + } + } + } + } + + #[test] + fn collapse_matches_matrices() { + let mut rng = Rng(13); + for k in 1..=5 { + for _ in 0..30 { + let (phi, p) = (rng.state(k), rng.pauli(k)); + let m = rng.next() & 1 == 1; + if phi.branch_weights(p)[usize::from(m)] < 1e-6 { + continue; + } + let v = Rotation::collapse(p, m); + let mut got = phi.clone(); + got.collapse(p, m, &v); + // Project, apply V†, keep the pivot's |0⟩ half, renormalize. + let s = if m { -0.5 } else { 0.5 }; + let pv = apply(&pauli(p, k), &phi.amplitudes); + let projected: Vec = phi + .amplitudes + .iter() + .zip(&pv) + .map(|(x, y)| 0.5 * x + s * y) + .collect(); + let rotated = apply(&dagger(&rotation_matrix(&v, k)), &projected); + let kept: Vec = (0..1usize << k) + .filter(|a| a >> v.pivot & 1 == 0) + .map(|a| rotated[a]) + .collect(); + let norm = kept.iter().map(|a| a.norm_sqr()).sum::().sqrt(); + let want: Vec = kept.iter().map(|a| a / norm).collect(); + assert_eq!(got.num_axes(), k - 1); + assert_close(&got.amplitudes, &want); + } + } + } +} diff --git a/crates/ppvm-tableau-2/src/active/mod.rs b/crates/ppvm-tableau-2/src/active/mod.rs new file mode 100644 index 000000000..607167850 --- /dev/null +++ b/crates/ppvm-tableau-2/src/active/mod.rs @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! The active part `φ` of the state `C (|φ⟩ ⊗ |0…0⟩)`: a vector over the +//! active axes `0..k`, behind the [`ActiveState`] interface. +//! +//! - `axis`: Paulis on the axes ([`AxisPauli`]) and the collapse rotation +//! ([`Rotation`]) shared with the tableau. +//! - `state`: the [`ActiveState`] trait. +//! - `dense`: [`DenseState`], an exact vector of `2^k` amplitudes. + +mod axis; +mod dense; +mod state; + +pub use axis::{AxisPauli, Rotation}; +pub use dense::DenseState; +pub use state::ActiveState; diff --git a/crates/ppvm-tableau-2/src/active/state.rs b/crates/ppvm-tableau-2/src/active/state.rs new file mode 100644 index 000000000..12e421ed3 --- /dev/null +++ b/crates/ppvm-tableau-2/src/active/state.rs @@ -0,0 +1,39 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! The interface of the active part. + +use num_complex::Complex64; + +use super::axis::{AxisPauli, Rotation}; + +/// Storage for `φ` over the active axes `0..k`. Only three things happen to +/// it: a Pauli rotation, an activation, and a collapse; every other change of +/// basis is trivial on the state. It sees axes only, never the tableau. +/// +/// `P` is unsigned: the caller folds signs into the coefficients and the +/// outcome. Weights are unnormalized, so `rotate` need not be unitary. +pub trait ActiveState: Clone { + fn num_axes(&self) -> usize; + + /// Append axis `k` in `|0⟩`. + fn activate(&mut self); + + /// `φ ← a·φ + b·P φ`. + fn rotate(&mut self, p: AxisPauli, a: Complex64, b: Complex64); + + /// Unnormalized weights `‖½(1 + P) φ‖²` and `‖½(1 − P) φ‖²` of the + /// outcomes `+1` and `−1`. + fn branch_weights(&self, p: AxisPauli) -> [f64; 2]; + + /// Project onto outcome `m` of `p` (eigenvalue `(−1)^m`), apply `V†` for + /// `v = Rotation::collapse(p, m)`, drop axis `v.pivot` (now `|0⟩`), and + /// renormalize. Axes above the pivot shift down by one. The branch must + /// have nonzero weight. + fn collapse(&mut self, p: AxisPauli, m: bool, v: &Rotation); + + /// Weight dropped by truncation so far; exact backends return 0. + fn truncation_error(&self) -> f64 { + 0.0 + } +} diff --git a/crates/ppvm-tableau-2/src/lib.rs b/crates/ppvm-tableau-2/src/lib.rs index 43315bd6f..8c574d113 100644 --- a/crates/ppvm-tableau-2/src/lib.rs +++ b/crates/ppvm-tableau-2/src/lib.rs @@ -4,6 +4,8 @@ //! Hybrid stabilizer simulation: an inverse tableau plus a dense vector over //! the active qubits. Design: `docs/design/hybrid-tableau.md`. +mod active; mod inverse_tableau; +pub use active::{ActiveState, AxisPauli, DenseState, Rotation}; pub use inverse_tableau::{InverseTableau, RowRef}; From 94d5ee3d01dfdc3a1cf9b6a30934f44183109632 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Thu, 12 Nov 2026 16:32:36 +0100 Subject: [PATCH 24/60] feat(ppvm-tableau-2): add GeneralizedTableau over a VirtualState Introduce the VirtualState trait, which sees the tableau only through a VirtualView (row reads and changes of basis), and move the active state under virtual_state/active_dormant as ActiveDormant. GeneralizedTableau combines the inverse tableau, the virtual state and an RNG: physical gates, measurement and reset with no active qubits (step 7b). Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/ppvm-tableau-2/Cargo.toml | 1 + crates/ppvm-tableau-2/src/active/mod.rs | 18 --- .../ppvm-tableau-2/src/generalized_tableau.rs | 141 ++++++++++++++++++ .../ppvm-tableau-2/src/inverse_tableau/mod.rs | 3 + .../src/inverse_tableau/view.rs | 67 +++++++++ crates/ppvm-tableau-2/src/lib.rs | 20 ++- .../active_dormant}/axis.rs | 82 ++++++---- .../active_dormant}/dense.rs | 0 .../src/virtual_state/active_dormant/mod.rs | 91 +++++++++++ .../active_dormant}/state.rs | 0 .../ppvm-tableau-2/src/virtual_state/mod.rs | 45 ++++++ 12 files changed, 413 insertions(+), 56 deletions(-) delete mode 100644 crates/ppvm-tableau-2/src/active/mod.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau.rs create mode 100644 crates/ppvm-tableau-2/src/inverse_tableau/view.rs rename crates/ppvm-tableau-2/src/{active => virtual_state/active_dormant}/axis.rs (73%) rename crates/ppvm-tableau-2/src/{active => virtual_state/active_dormant}/dense.rs (100%) create mode 100644 crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs rename crates/ppvm-tableau-2/src/{active => virtual_state/active_dormant}/state.rs (100%) create mode 100644 crates/ppvm-tableau-2/src/virtual_state/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 5e988753b..78c7e1776 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2362,6 +2362,7 @@ dependencies = [ "ppvm-pauli-word-2", "ppvm-phased-pauli-word-2", "ppvm-traits-2", + "rand 0.10.3", "stim-parser", ] diff --git a/crates/ppvm-tableau-2/Cargo.toml b/crates/ppvm-tableau-2/Cargo.toml index 1fe9deaf6..6a600ffef 100644 --- a/crates/ppvm-tableau-2/Cargo.toml +++ b/crates/ppvm-tableau-2/Cargo.toml @@ -9,6 +9,7 @@ repository.workspace = true num-complex = "0.4.6" ppvm-pauli-kernels = { path = "../ppvm-pauli-kernels" } ppvm-traits-2 = { path = "../ppvm-traits-2" } +rand = "0.10.1" [dev-dependencies] ppvm-pauli-word-2 = { path = "../ppvm-pauli-word-2" } diff --git a/crates/ppvm-tableau-2/src/active/mod.rs b/crates/ppvm-tableau-2/src/active/mod.rs deleted file mode 100644 index 607167850..000000000 --- a/crates/ppvm-tableau-2/src/active/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-FileCopyrightText: 2026 The PPVM Authors -// SPDX-License-Identifier: Apache-2.0 - -//! The active part `φ` of the state `C (|φ⟩ ⊗ |0…0⟩)`: a vector over the -//! active axes `0..k`, behind the [`ActiveState`] interface. -//! -//! - `axis`: Paulis on the axes ([`AxisPauli`]) and the collapse rotation -//! ([`Rotation`]) shared with the tableau. -//! - `state`: the [`ActiveState`] trait. -//! - `dense`: [`DenseState`], an exact vector of `2^k` amplitudes. - -mod axis; -mod dense; -mod state; - -pub use axis::{AxisPauli, Rotation}; -pub use dense::DenseState; -pub use state::ActiveState; diff --git a/crates/ppvm-tableau-2/src/generalized_tableau.rs b/crates/ppvm-tableau-2/src/generalized_tableau.rs new file mode 100644 index 000000000..345cb33c7 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau.rs @@ -0,0 +1,141 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! [`GeneralizedTableau`]: the simulator, `|ψ⟩ = C |χ⟩`. + +use rand::rngs::SmallRng; +use rand::{RngExt, SeedableRng}; + +use ppvm_traits_2::{ApplyGateRule, Clifford, CliffordBatch}; + +use crate::{InverseTableau, VirtualState}; + +/// The state `|ψ⟩ = C |χ⟩`: the Clifford `C` as an [`InverseTableau`], and the +/// virtual state `|χ⟩` in the representation `V`. +/// +/// Physical gates `C ← G·C` touch only the tableau. Measurements ask `V` for +/// the outcome weights, sample here, and let `V` collapse. +#[derive(Clone, Debug)] +pub struct GeneralizedTableau { + tableau: InverseTableau, + state: V, + rng: SmallRng, +} + +impl GeneralizedTableau { + /// `|0…0⟩` on `n` qubits, with the RNG seeded from OS entropy. + pub fn new(n: usize) -> Self { + Self::with_rng(n, rand::make_rng()) + } + + /// Same as [`new`](Self::new), but seed the RNG deterministically. + pub fn new_with_seed(n: usize, seed: u64) -> Self { + Self::with_rng(n, SmallRng::seed_from_u64(seed)) + } + + fn with_rng(n: usize, rng: SmallRng) -> Self { + Self { + tableau: InverseTableau::new(n), + state: V::new(n), + rng, + } + } + + pub fn num_qubits(&self) -> usize { + self.tableau.num_qubits() + } + + pub fn tableau(&self) -> &InverseTableau { + &self.tableau + } + + pub fn state(&self) -> &V { + &self.state + } + + /// Measure `Z_q` and collapse; returns the outcome (`true` for `|1⟩`). + pub fn measure(&mut self, q: usize) -> bool { + let mut view = self.tableau.virtual_view(); + let [w0, w1] = self.state.z_weights(&mut view, q); + let m = self.rng.random::() * (w0 + w1) < w1; + self.state.collapse(&mut view, q, m); + m + } + + /// Reset qubit `q` to `|0⟩`: measure it, then flip it if it was `|1⟩`. + pub fn reset(&mut self, q: usize) { + if self.measure(q) { + self.x(q); + } + } +} + +/// Physical gates `C ← G·C` go to the tableau; `|χ⟩` is untouched. +impl ApplyGateRule for GeneralizedTableau { + type B = bool; + + fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + self.tableau.apply1(q, rule); + } + + fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { + self.tableau.apply2(a, b, rule); + } + + fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + self.tableau.apply_sign1(q, rule); + } +} + +impl CliffordBatch for GeneralizedTableau {} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ActiveDormant, DenseState}; + + type Sim = GeneralizedTableau>; + + /// Bell pair: the two outcomes always agree, and both occur. + #[test] + fn bell_pair_outcomes_agree() { + let mut ones = 0; + for seed in 0..200 { + let mut s = Sim::new_with_seed(2, seed); + s.h(0); + s.cnot(0, 1); + let (a, b) = (s.measure(0), s.measure(1)); + assert_eq!(a, b); + ones += usize::from(a); + } + assert!((60..140).contains(&ones), "{ones} of 200"); + } + + /// A measured qubit stays in its outcome, and reset leaves `|0⟩`. + #[test] + fn measurement_repeats_and_reset_clears() { + for seed in 0..50 { + let mut s = Sim::new_with_seed(3, seed); + s.h(0); + s.cnot(0, 1); + s.sqrt_x(2); + s.cz(1, 2); + for q in 0..3 { + let m = s.measure(q); + assert_eq!(s.measure(q), m); + } + for q in 0..3 { + s.reset(q); + assert!(!s.measure(q)); + } + } + } + + /// `X` then measure is deterministic: `Z|1⟩ = −|1⟩`. + #[test] + fn x_flips_the_outcome() { + let mut s = Sim::new_with_seed(1, 0); + s.x(0); + assert!(s.measure(0)); + } +} diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs index 5aa744397..bc858d147 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -13,6 +13,7 @@ //! - `virtual_gates`: changes of basis `C ← C·V`, which rewrite columns. //! - `measure`: Z measurement and reset of the stabilizer state. //! - `transposed`: a column-major copy for batches of changes of basis. +//! - `view`: [`VirtualView`], the virtual side handed to a virtual state. //! //! Where rows sit in the arena is decided only in `data`: by `offsets` (used //! by `x_at`, `z_at`) and by `for_each_row`, which walks the same blocks. @@ -24,6 +25,8 @@ mod physical_gates; #[cfg(test)] mod test_support; mod transposed; +mod view; mod virtual_gates; pub use data::{InverseTableau, RowRef}; +pub use view::VirtualView; diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/view.rs b/crates/ppvm-tableau-2/src/inverse_tableau/view.rs new file mode 100644 index 000000000..a8f222a60 --- /dev/null +++ b/crates/ppvm-tableau-2/src/inverse_tableau/view.rs @@ -0,0 +1,67 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! [`VirtualView`]: the part of the tableau a virtual state may use. + +use super::{InverseTableau, RowRef}; + +/// The virtual side of an [`InverseTableau`]: row reads and changes of basis +/// `C ← C·V`, but no physical gates. A virtual state gets this view, so it can +/// change basis together with its own state while `|ψ⟩` stays the same. +pub struct VirtualView<'a> { + t: &'a mut InverseTableau, +} + +impl InverseTableau { + pub fn virtual_view(&mut self) -> VirtualView<'_> { + VirtualView { t: self } + } +} + +impl VirtualView<'_> { + pub fn num_qubits(&self) -> usize { + self.t.num_qubits() + } + + /// Row `ix_q = C⁻¹ X_q C`. + pub fn ix(&self, q: usize) -> RowRef<'_> { + self.t.ix(q) + } + + /// Row `iz_q = C⁻¹ Z_q C`. + pub fn iz(&self, q: usize) -> RowRef<'_> { + self.t.iz(q) + } + + /// See [`InverseTableau::change_basis1`]. + pub fn change_basis1(&mut self, v: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + self.t.change_basis1(v, rule); + } + + /// See [`InverseTableau::change_basis2`]. + pub fn change_basis2( + &mut self, + a: usize, + b: usize, + rule: impl Fn([bool; 4]) -> ([bool; 4], bool), + ) { + self.t.change_basis2(a, b, rule); + } + + /// See [`InverseTableau::fan_out`]. + pub fn fan_out( + &mut self, + pivot: usize, + cx_targets: &[usize], + cz_targets: &[usize], + last: impl Fn(bool, bool) -> ([bool; 2], bool), + ) { + self.t.fan_out(pivot, cx_targets, cz_targets, last); + } + + /// Measure `Z_q` with every virtual qubit dormant, i.e. on `C |0…0⟩`. See + /// [`InverseTableau::measure`]; the collapse is a change of basis only. + pub fn measure(&mut self, q: usize, coin: impl FnOnce() -> bool) -> bool { + self.t.measure(q, coin) + } +} diff --git a/crates/ppvm-tableau-2/src/lib.rs b/crates/ppvm-tableau-2/src/lib.rs index 8c574d113..54e008add 100644 --- a/crates/ppvm-tableau-2/src/lib.rs +++ b/crates/ppvm-tableau-2/src/lib.rs @@ -1,11 +1,21 @@ // SPDX-FileCopyrightText: 2026 The PPVM Authors // SPDX-License-Identifier: Apache-2.0 -//! Hybrid stabilizer simulation: an inverse tableau plus a dense vector over -//! the active qubits. Design: `docs/design/hybrid-tableau.md`. +//! Hybrid stabilizer simulation: `|ψ⟩ = C |χ⟩`, an inverse tableau for the +//! Clifford `C` plus the state `|χ⟩` of the virtual qubits in an exchangeable +//! representation. Design: `docs/design/hybrid-tableau.md`. +//! +//! - `inverse_tableau`: [`InverseTableau`] and its [`VirtualView`]. +//! - `virtual_state`: the [`VirtualState`] trait and [`ActiveDormant`], which +//! keeps amplitudes only for the active virtual qubits. +//! - `generalized_tableau`: [`GeneralizedTableau`], the simulator. -mod active; +mod generalized_tableau; mod inverse_tableau; +mod virtual_state; -pub use active::{ActiveState, AxisPauli, DenseState, Rotation}; -pub use inverse_tableau::{InverseTableau, RowRef}; +pub use generalized_tableau::GeneralizedTableau; +pub use inverse_tableau::{InverseTableau, RowRef, VirtualView}; +pub use virtual_state::{ + ActiveDormant, ActiveState, AxisPauli, DenseState, Rotation, VirtualState, +}; diff --git a/crates/ppvm-tableau-2/src/active/axis.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/axis.rs similarity index 73% rename from crates/ppvm-tableau-2/src/active/axis.rs rename to crates/ppvm-tableau-2/src/virtual_state/active_dormant/axis.rs index 9c91f5b9f..5f6b80bb3 100644 --- a/crates/ppvm-tableau-2/src/active/axis.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/axis.rs @@ -3,7 +3,7 @@ //! Paulis on the active axes, and the rotation that collapses onto one axis. -use ppvm_pauli_kernels::heisenberg; +use ppvm_traits_2::{ApplyGateRule, Clifford}; /// A Pauli on the active axes, unsigned: bit `j` of `x` / `z` is the X / Z /// factor on axis `j`, and both set is Y. @@ -29,6 +29,31 @@ impl AxisPauli { } } +/// An [`AxisPauli`] with a sign, so the gate rules can run on it. +struct Signed { + p: AxisPauli, + negative: bool, +} + +impl ApplyGateRule for Signed { + type B = bool; + + fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + let [x, z] = self.p.get(q); + let (bits, flip) = rule(x, z); + self.p.set(q, bits); + self.negative ^= flip; + } + + fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { + let ([xa, za], [xb, zb]) = (self.p.get(a), self.p.get(b)); + let ([xa, za, xb, zb], flip) = rule([xa, za, xb, zb]); + self.p.set(a, [xa, za]); + self.p.set(b, [xb, zb]); + self.negative ^= flip; + } +} + /// The change of basis `V` of a collapse, shared by the tableau (`C ← C·V`) /// and the active state (`φ ← V† φ`), as a gate sequence applied in this /// order: CX from `pivot` to every axis in `fan_out`, CX from every axis in @@ -104,44 +129,35 @@ impl Rotation { v } - /// `V† p V` and whether it picks up a sign, by running the gate rules on - /// `p`'s bits in gate order. - pub fn conjugate(&self, mut p: AxisPauli) -> (AxisPauli, bool) { - let mut negative = false; - let mut two = - |p: &mut AxisPauli, c: usize, t: usize, rule: fn([bool; 4]) -> ([bool; 4], bool)| { - let ([xc, zc], [xt, zt]) = (p.get(c), p.get(t)); - let ([xc, zc, xt, zt], flip) = rule([xc, zc, xt, zt]); - p.set(c, [xc, zc]); - p.set(t, [xt, zt]); - negative ^= flip; - }; - let p_ = self.pivot; + /// Apply the gates of `V` to `c`, in order. + pub(crate) fn apply_gates(&self, c: &mut impl Clifford) { + let p = self.pivot; for t in bits(self.fan_out) { - two(&mut p, p_, t, heisenberg::cnot); + c.cnot(p, t); } - for c in bits(self.fan_in) { - two(&mut p, c, p_, heisenberg::cnot); + for ctrl in bits(self.fan_in) { + c.cnot(ctrl, p); } for t in bits(self.cz) { - two(&mut p, p_, t, heisenberg::cz); + c.cz(p, t); } - let one = |p: &mut AxisPauli, rule: fn(bool, bool) -> ([bool; 2], bool)| { - let [x, z] = p.get(p_); - let (bits, flip) = rule(x, z); - p.set(p_, bits); - flip - }; - for (on, rule) in [ - (self.s, heisenberg::s as fn(bool, bool) -> ([bool; 2], bool)), - (self.h, heisenberg::h), - (self.x, heisenberg::x), - ] { - if on { - negative ^= one(&mut p, rule); - } + if self.s { + c.s(p); + } + if self.h { + c.h(p); } - (p, negative) + if self.x { + c.x(p); + } + } + + /// `V† p V` and whether it picks up a sign, by running the gate rules on + /// `p` in gate order. + pub fn conjugate(&self, p: AxisPauli) -> (AxisPauli, bool) { + let mut signed = Signed { p, negative: false }; + self.apply_gates(&mut signed); + (signed.p, signed.negative) } } diff --git a/crates/ppvm-tableau-2/src/active/dense.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs similarity index 100% rename from crates/ppvm-tableau-2/src/active/dense.rs rename to crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs new file mode 100644 index 000000000..b4b164113 --- /dev/null +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs @@ -0,0 +1,91 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! [`ActiveDormant`]: `|χ⟩ = |φ⟩ ⊗ |0…0⟩`, with `φ` a vector over the active +//! virtual qubits behind the [`ActiveState`] interface, and every other +//! (dormant) virtual qubit in `|0⟩`. +//! +//! - `axis`: Paulis on the axes ([`AxisPauli`]) and the collapse rotation +//! ([`Rotation`]) shared with the tableau. +//! - `state`: the [`ActiveState`] trait. +//! - `dense`: [`DenseState`], an exact vector of `2^k` amplitudes. + +mod axis; +mod dense; +mod state; + +use num_complex::Complex64; + +use super::VirtualState; +use crate::VirtualView; + +pub use axis::{AxisPauli, Rotation}; +pub use dense::DenseState; +pub use state::ActiveState; + +/// `|χ⟩ = |φ⟩ ⊗ |0…0⟩`: amplitudes only for the active virtual qubits. +/// +/// Non-Clifford gates activate virtual qubits (localization), and measurements +/// retire them; with no active qubits it is the stabilizer state `C |0…0⟩`. +#[derive(Clone, Debug, PartialEq)] +pub struct ActiveDormant { + /// Virtual qubit of each axis of `φ`: axis `j` is qubit `active[j]`. + active: Vec, + phi: S, +} + +impl ActiveDormant { + /// The virtual qubit of each axis of `φ`. + pub fn active(&self) -> &[usize] { + &self.active + } + + pub fn phi(&self) -> &S { + &self.phi + } +} + +impl VirtualState for ActiveDormant { + fn new(_num_qubits: usize) -> Self { + Self { + active: Vec::new(), + phi: S::default(), + } + } + + fn rotate( + &mut self, + _t: &mut VirtualView<'_>, + _qubits: &[usize], + _a: Complex64, + _b: Complex64, + ) { + todo!("localization and the Pauli rotation on φ (step 7c)") + } + + /// Deterministic if `O` has no X/Y, else 50/50 (a dormant pivot). + fn z_weights(&mut self, t: &mut VirtualView<'_>, q: usize) -> [f64; 2] { + debug_assert_eq!(self.active.len(), self.phi.num_axes()); + if !self.active.is_empty() { + todo!("measurement with active qubits (step 7d)"); + } + let o = t.iz(q); + if o.x.iter().all(|&w| w == 0) { + if o.negative { [0.0, 1.0] } else { [1.0, 0.0] } + } else { + [0.5, 0.5] + } + } + + fn collapse(&mut self, t: &mut VirtualView<'_>, q: usize, m: bool) { + if !self.active.is_empty() { + todo!("measurement with active qubits (step 7d)"); + } + let outcome = t.measure(q, || m); + debug_assert_eq!(outcome, m, "collapsing onto an impossible outcome"); + } + + fn truncation_error(&self) -> f64 { + self.phi.truncation_error() + } +} diff --git a/crates/ppvm-tableau-2/src/active/state.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs similarity index 100% rename from crates/ppvm-tableau-2/src/active/state.rs rename to crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs diff --git a/crates/ppvm-tableau-2/src/virtual_state/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/mod.rs new file mode 100644 index 000000000..0739e7a3b --- /dev/null +++ b/crates/ppvm-tableau-2/src/virtual_state/mod.rs @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! The state `|χ⟩` of the virtual qubits, with `|ψ⟩ = C |χ⟩`, behind the +//! [`VirtualState`] interface. +//! +//! - `active_dormant`: [`ActiveDormant`], `|χ⟩ = |φ⟩ ⊗ |0…0⟩` with `φ` over +//! the active qubits, and the storage of `φ`. + +mod active_dormant; + +use num_complex::Complex64; + +use crate::VirtualView; + +pub use active_dormant::{ActiveDormant, ActiveState, AxisPauli, DenseState, Rotation}; + +/// The `2ⁿ`-dimensional state `|χ⟩` of the `n` virtual qubits, in a +/// representation of the implementor's choice. +/// +/// It sees the tableau only through a [`VirtualView`]: it may change basis +/// (`C ← C·V` with `χ ← V† χ`, so `|ψ⟩` is unchanged), but never applies a +/// physical gate. Arguments are physical qubits; the implementor reads +/// `O = C⁻¹ Z_q C` from the view. The caller samples outcomes, so weights are +/// unnormalized and `rotate` need not be unitary. +pub trait VirtualState: Clone { + /// `|0…0⟩` on `num_qubits` virtual qubits. + fn new(num_qubits: usize) -> Self; + + /// `χ ← (a·I + b·O) χ` with `O = C⁻¹ (∏ Z_q) C` over `qubits`. + fn rotate(&mut self, t: &mut VirtualView<'_>, qubits: &[usize], a: Complex64, b: Complex64); + + /// Unnormalized weights of the outcomes `0` and `1` of `Z_q`. Takes `&mut` + /// so an implementor may change basis here and reuse it in `collapse`. + fn z_weights(&mut self, t: &mut VirtualView<'_>, q: usize) -> [f64; 2]; + + /// Project onto outcome `m` of `Z_q` and renormalize. The outcome must have + /// nonzero weight. + fn collapse(&mut self, t: &mut VirtualView<'_>, q: usize, m: bool); + + /// Weight dropped by truncation so far; exact representations return 0. + fn truncation_error(&self) -> f64 { + 0.0 + } +} From f94103b0586b5ce1f31a23d578f4ce5e8f61cc96 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Fri, 13 Nov 2026 13:38:26 +0100 Subject: [PATCH 25/60] feat(ppvm-tableau-2): add T and Pauli rotations with localization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ActiveDormant::rotate localizes O = C⁻¹ ∏Z C by one fan-out from its lowest dormant X qubit, which becomes active. GeneralizedTableau gets TGate, RotationOne and RotationTwo, mapping X/Y axes to Z by physical Cliffords. Checked against a dense state vector. Co-Authored-By: Claude Opus 5.5 --- .../ppvm-tableau-2/src/generalized_tableau.rs | 84 +++++++++++- .../src/inverse_tableau/data.rs | 9 +- .../ppvm-tableau-2/src/inverse_tableau/mod.rs | 2 +- .../src/inverse_tableau/view.rs | 41 ++++++ crates/ppvm-tableau-2/src/lib.rs | 2 +- .../src/virtual_state/active_dormant/mod.rs | 34 +++-- crates/ppvm-tableau-2/tests/dense_matrix.rs | 123 +++++++++++++++++- 7 files changed, 276 insertions(+), 19 deletions(-) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau.rs b/crates/ppvm-tableau-2/src/generalized_tableau.rs index 345cb33c7..68ac83926 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau.rs @@ -6,7 +6,10 @@ use rand::rngs::SmallRng; use rand::{RngExt, SeedableRng}; -use ppvm_traits_2::{ApplyGateRule, Clifford, CliffordBatch}; +use num_complex::Complex64; +use ppvm_traits_2::{ + ApplyGateRule, Clifford, CliffordBatch, Pauli, RotationOne, RotationTwo, TGate, +}; use crate::{InverseTableau, VirtualState}; @@ -68,6 +71,85 @@ impl GeneralizedTableau { self.x(q); } } + + /// `|ψ⟩ ← (a·I + b·∏ Z_q) |ψ⟩` over `qubits`, which must be distinct. + fn rotate_z(&mut self, qubits: &[usize], a: Complex64, b: Complex64) { + let mut view = self.tableau.virtual_view(); + self.state.rotate(&mut view, qubits, a, b); + } + + /// `exp(−i θ/2 ∏ Z_q) = cos(θ/2)·I − i sin(θ/2)·∏ Z_q`. + fn rz_product(&mut self, qubits: &[usize], theta: f64) { + let (s, c) = (theta / 2.0).sin_cos(); + self.rotate_z(qubits, Complex64::new(c, 0.0), Complex64::new(0.0, -s)); + } + + /// Map `axis` on `q` to `Z`: apply `W` with `W·axis·W† = Z`. + fn axis_to_z(&mut self, axis: Pauli, q: usize) { + match axis { + Pauli::X => self.h(q), + Pauli::Y => { + self.s_dag(q); + self.h(q); + } + Pauli::Z | Pauli::I => {} + } + } + + /// Undo [`axis_to_z`](Self::axis_to_z): apply `W†`. + fn z_to_axis(&mut self, axis: Pauli, q: usize) { + match axis { + Pauli::X => self.h(q), + Pauli::Y => { + self.h(q); + self.s(q); + } + Pauli::Z | Pauli::I => {} + } + } +} + +/// `T = diag(1, ω) = ½(1 + ω)·I + ½(1 − ω)·Z` with `ω = e^{iπ/4}`, exact +/// including the global phase. +impl TGate for GeneralizedTableau { + fn t(&mut self, q: usize) { + let w = Complex64::from_polar(1.0, std::f64::consts::FRAC_PI_4); + self.rotate_z(&[q], (1.0 + w) / 2.0, (1.0 - w) / 2.0); + } + + fn t_dag(&mut self, q: usize) { + let w = Complex64::from_polar(1.0, -std::f64::consts::FRAC_PI_4); + self.rotate_z(&[q], (1.0 + w) / 2.0, (1.0 - w) / 2.0); + } +} + +/// `exp(−i θ/2 P)`: `P` is mapped to `Z` by a physical Clifford. +impl RotationOne for GeneralizedTableau { + fn rotate_1(&mut self, axis: Pauli, q: usize, theta: f64) { + if axis == Pauli::I { + return; + } + self.axis_to_z(axis, q); + self.rz_product(&[q], theta); + self.z_to_axis(axis, q); + } +} + +/// `exp(−i θ/2 P_a ⊗ P_b)`: each factor is mapped to `Z` by a physical Clifford. +impl RotationTwo for GeneralizedTableau { + fn rotate_2(&mut self, axis_a: Pauli, axis_b: Pauli, a: usize, b: usize, theta: f64) { + assert_ne!(a, b, "two-qubit rotation on one qubit"); + let qubits: Vec = [(axis_a, a), (axis_b, b)] + .into_iter() + .filter(|&(axis, _)| axis != Pauli::I) + .map(|(_, q)| q) + .collect(); + self.axis_to_z(axis_a, a); + self.axis_to_z(axis_b, b); + self.rz_product(&qubits, theta); + self.z_to_axis(axis_a, a); + self.z_to_axis(axis_b, b); + } } /// Physical gates `C ← G·C` go to the tableau; `|χ⟩` is untouched. diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs index b00161210..f32dbc0d6 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs @@ -4,7 +4,7 @@ //! The tableau's storage: rows, signs, and where rows sit in the arena. use super::bitword::{ - BITWORD_BITS, BitWord, TOP_BIT, ZERO, bit_position, flip_bit, get_bit, mask_if, + BITWORD_BITS, BitWord, TOP_BIT, ZERO, bit_position, flip_bit, get_bit, mask_if, set_bits, }; /// The inverse tableau of an `n`-qubit Clifford. @@ -28,7 +28,7 @@ pub struct RowRef<'a> { pub negative: bool, } -impl RowRef<'_> { +impl<'a> RowRef<'a> { /// X bit of the row at qubit `q`. pub fn x_bit(&self, q: usize) -> bool { get_bit(self.x, q) @@ -38,6 +38,11 @@ impl RowRef<'_> { pub fn z_bit(&self, q: usize) -> bool { get_bit(self.z, q) } + + /// Qubits where the row has an X or Y, ascending. + pub fn x_support(&self) -> impl Iterator + 'a { + set_bits(self.x) + } } impl InverseTableau { diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs index bc858d147..9da950595 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -29,4 +29,4 @@ mod view; mod virtual_gates; pub use data::{InverseTableau, RowRef}; -pub use view::VirtualView; +pub use view::{OwnedRow, VirtualView}; diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/view.rs b/crates/ppvm-tableau-2/src/inverse_tableau/view.rs index a8f222a60..f17c0bc0b 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/view.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/view.rs @@ -3,8 +3,29 @@ //! [`VirtualView`]: the part of the tableau a virtual state may use. +use ppvm_pauli_kernels::row_product; + +use super::bitword::{BitWord, ZERO}; use super::{InverseTableau, RowRef}; +/// An owned row, e.g. a product of rows: its X words, Z words and sign. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct OwnedRow { + x: Vec, + z: Vec, + negative: bool, +} + +impl OwnedRow { + pub fn row(&self) -> RowRef<'_> { + RowRef { + x: &self.x, + z: &self.z, + negative: self.negative, + } + } +} + /// The virtual side of an [`InverseTableau`]: row reads and changes of basis /// `C ← C·V`, but no physical gates. A virtual state gets this view, so it can /// change basis together with its own state while `|ψ⟩` stays the same. @@ -33,6 +54,26 @@ impl VirtualView<'_> { self.t.iz(q) } + /// `O = C⁻¹ (∏ Z_q) C = ∏ iz_q` over `qubits`, which must be distinct. The + /// `Z_q` commute, so `O` is a Hermitian Pauli string with sign ±1. + pub fn iz_product(&self, qubits: &[usize]) -> OwnedRow { + let stride = self.t.stride; + let (mut x, mut z) = (vec![ZERO; stride], vec![ZERO; stride]); + // Power of `i` in front of the product of the bare rows. + let mut phase = 0u8; + for &q in qubits { + let r = self.t.iz(q); + let k = row_product(&mut x, &mut z, r.x, r.z) + 2 * u8::from(r.negative); + phase = phase.wrapping_add(k); + } + debug_assert_eq!(phase % 2, 0, "product of commuting rows is Hermitian"); + OwnedRow { + x, + z, + negative: phase % 4 == 2, + } + } + /// See [`InverseTableau::change_basis1`]. pub fn change_basis1(&mut self, v: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { self.t.change_basis1(v, rule); diff --git a/crates/ppvm-tableau-2/src/lib.rs b/crates/ppvm-tableau-2/src/lib.rs index 54e008add..df9be5a0b 100644 --- a/crates/ppvm-tableau-2/src/lib.rs +++ b/crates/ppvm-tableau-2/src/lib.rs @@ -15,7 +15,7 @@ mod inverse_tableau; mod virtual_state; pub use generalized_tableau::GeneralizedTableau; -pub use inverse_tableau::{InverseTableau, RowRef, VirtualView}; +pub use inverse_tableau::{InverseTableau, OwnedRow, RowRef, VirtualView}; pub use virtual_state::{ ActiveDormant, ActiveState, AxisPauli, DenseState, Rotation, VirtualState, }; diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs index b4b164113..dc4eaf85a 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs @@ -53,14 +53,32 @@ impl VirtualState for ActiveDormant { } } - fn rotate( - &mut self, - _t: &mut VirtualView<'_>, - _qubits: &[usize], - _a: Complex64, - _b: Complex64, - ) { - todo!("localization and the Pauli rotation on φ (step 7c)") + /// Localization: if `O` has X/Y on dormant qubits, CX from the lowest of + /// them (in `|0⟩`, so `χ` is unchanged) clears the others, and it becomes + /// active. Then `O` acts as `±P` on the active axes and as `+1` elsewhere. + fn rotate(&mut self, t: &mut VirtualView<'_>, qubits: &[usize], a: Complex64, b: Complex64) { + debug_assert_eq!(self.active.len(), self.phi.num_axes()); + let mut o = t.iz_product(qubits); + let dormant_x: Vec = o + .row() + .x_support() + .filter(|v| !self.active.contains(v)) + .collect(); + if let Some((&pivot, rest)) = dormant_x.split_first() { + t.fan_out(pivot, rest, &[], |x, z| ([x, z], false)); + assert!(self.active.len() < 64, "at most 64 active axes"); + self.active.push(pivot); + self.phi.activate(); + o = t.iz_product(qubits); + } + let o = o.row(); + let mut p = AxisPauli { x: 0, z: 0 }; + for (j, &v) in self.active.iter().enumerate() { + p.x |= u64::from(o.x_bit(v)) << j; + p.z |= u64::from(o.z_bit(v)) << j; + } + debug_assert_eq!(o.x_support().count(), p.x.count_ones() as usize); + self.phi.rotate(p, a, if o.negative { -b } else { b }); } /// Deterministic if `O` has no X/Y, else 50/50 (a dormant pivot). diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs index cee9d9ab8..e361437c4 100644 --- a/crates/ppvm-tableau-2/tests/dense_matrix.rs +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -9,8 +9,8 @@ use std::collections::{HashSet, VecDeque}; use num_complex::Complex64 as C; -use ppvm_tableau_2::InverseTableau; -use ppvm_traits_2::Clifford; +use ppvm_tableau_2::{ActiveDormant, DenseState, GeneralizedTableau, InverseTableau}; +use ppvm_traits_2::{Clifford, Pauli, RotationOne, RotationTwo, TGate}; /// A dense `2^n × 2^n` matrix; basis index bit `q` is qubit `q`. #[derive(Clone)] @@ -123,7 +123,7 @@ enum Gate { Two(usize, usize, usize), } -fn apply(t: &mut InverseTableau, gate: Gate) { +fn apply(t: &mut impl Clifford, gate: Gate) { match gate { Gate::One(g, q) => match g { 0 => t.x(q), @@ -347,9 +347,14 @@ fn row_matrix(t: &InverseTableau, q: usize, z: bool, paulis: &Paulis) -> Mat { m } -/// `⟨0…0| C⁻¹ P C |0…0⟩` for the Pauli string `code`, from the rows alone: -/// `P = i^#Y · ∏ X_q^x Z_q^z`, so `C⁻¹ P C = i^#Y · ∏ ix_q^x iz_q^z`. +/// `⟨0…0| C⁻¹ P C |0…0⟩` for the Pauli string `code`, from the rows alone. fn tableau_expectation(t: &InverseTableau, code: usize, paulis: &Paulis) -> C { + conjugated(t, code, paulis).a[0] +} + +/// `C⁻¹ P C` for the Pauli string `code`, from the rows alone: +/// `P = i^#Y · ∏ X_q^x Z_q^z`, so `C⁻¹ P C = i^#Y · ∏ ix_q^x iz_q^z`. +fn conjugated(t: &InverseTableau, code: usize, paulis: &Paulis) -> Mat { let n = t.num_qubits(); let mut m = Mat::identity(n); let mut phase = c(1.0, 0.0); @@ -365,7 +370,8 @@ fn tableau_expectation(t: &InverseTableau, code: usize, paulis: &Paulis) -> C { phase *= c(0.0, 1.0); } } - phase * m.a[0] + m.a.iter_mut().for_each(|e| *e *= phase); + m } fn apply_to_state(m: &Mat, v: &[C]) -> Vec { @@ -477,3 +483,108 @@ fn measurements_match_the_state_vector() { } } } + +type Sim = GeneralizedTableau>; + +/// `|χ⟩ = |φ⟩ ⊗ |0…0⟩` as a vector over all virtual qubits. +fn virtual_state(s: &Sim) -> Vec { + let (active, phi) = (s.state().active(), s.state().phi().amplitudes()); + let mut chi = vec![C::default(); 1 << s.num_qubits()]; + for (alpha, &) in phi.iter().enumerate() { + let i: usize = (0..active.len()) + .map(|j| ((alpha >> j) & 1) << active[j]) + .sum(); + chi[i] = amp; + } + chi +} + +/// Code (`x | z << n`) of the Pauli `axis` on qubit `q`. +fn pauli_code(axis: Pauli, q: usize, n: usize) -> usize { + let (x, z) = match axis { + Pauli::I => (0, 0), + Pauli::X => (1, 0), + Pauli::Y => (1, 1), + Pauli::Z => (0, 1), + }; + x << q | z << (n + q) +} + +/// `exp(−i θ/2 P) = cos(θ/2)·I − i sin(θ/2)·P`. +fn rotation(p: &Mat, theta: f64) -> Mat { + let (s, co) = (theta / 2.0).sin_cos(); + let mut m = p.clone(); + for (k, e) in m.a.iter_mut().enumerate() { + let identity = f64::from(u8::from(k % (p.d + 1) == 0)); + *e = c(co * identity, 0.0) + c(0.0, -s) * *e; + } + m +} + +/// Random three-qubit circuits of Cliffords, `T`, `T†` and one- and two-qubit +/// Pauli rotations: after every gate, all 64 Pauli expectations of +/// `C |χ⟩` match the state vector. +#[test] +fn rotations_match_the_state_vector() { + let n = 3; + let gates = all_gates(n); + let paulis = Paulis::new(n); + let axes = [Pauli::I, Pauli::X, Pauli::Y, Pauli::Z]; + let w = C::from_polar(1.0, std::f64::consts::FRAC_PI_4); + let mut seed = 0x2545_f491_4f6c_dd1du64; + let mut next = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed as usize + }; + let mut max_axes = 0; + for round in 0..40 { + let mut s = Sim::new_with_seed(n, round); + let mut v = vec![C::default(); 1 << n]; + v[0] = c(1.0, 0.0); + for step in 0..40 { + let (a, theta) = (next() % n, (next() % 1000) as f64 * 0.00628); + let b = (a + 1 + next() % (n - 1)) % n; + let (pa, pb) = (axes[next() % 4], axes[next() % 4]); + let m = match next() % 8 { + 0..3 => { + let gate = gates[next() % gates.len()]; + apply(&mut s, gate); + matrix(gate, n) + } + 3 => { + s.t(a); + single([[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), w]], a, n) + } + 4 => { + s.t_dag(a); + single([[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), w.conj()]], a, n) + } + 5 | 6 => { + s.rotate_1(pa, a, theta); + rotation(&paulis.table[pauli_code(pa, a, n)], theta) + } + _ => { + s.rotate_2(pa, pb, a, b, theta); + let code = pauli_code(pa, a, n) | pauli_code(pb, b, n); + rotation(&paulis.table[code], theta) + } + }; + v = apply_to_state(&m, &v); + max_axes = max_axes.max(s.state().active().len()); + let chi = virtual_state(&s); + for code in 0..1usize << (2 * n) { + let (from_sim, from_state) = ( + state_expectation(&chi, &conjugated(s.tableau(), code, &paulis)), + state_expectation(&v, &paulis.table[code]), + ); + assert!( + (from_sim - from_state).norm() < 1e-9, + "round {round} step {step}: Pauli {code:#b}: simulator {from_sim}, state {from_state}" + ); + } + } + } + assert_eq!(max_axes, n, "the circuits activate every qubit"); +} From f36bc8e69c3b1698484bfde12fdcc6eeda1754a1 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Fri, 13 Nov 2026 13:59:18 +0100 Subject: [PATCH 26/60] feat(ppvm-tableau-2): measure with active qubits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ActiveDormant classifies O = C⁻¹ Z_q C word-wise against a QubitMask of active qubits: deterministic, dormant pivot (one fan-out with CZs to active Z's, φ untouched), or active (branch weights, Rotation applied to the tableau, pivot axis retired). Checked against a dense state vector, including outcome weights against Born probabilities. Co-Authored-By: Claude Opus 5.5 --- .../src/inverse_tableau/bitword.rs | 7 +- .../src/inverse_tableau/data.rs | 12 ++ .../src/inverse_tableau/measure.rs | 37 +++++- .../ppvm-tableau-2/src/inverse_tableau/mod.rs | 2 +- .../src/inverse_tableau/view.rs | 32 ++++- crates/ppvm-tableau-2/src/lib.rs | 2 +- .../src/virtual_state/active_dormant/mod.rs | 124 ++++++++++++++---- crates/ppvm-tableau-2/tests/dense_matrix.rs | 37 +++++- 8 files changed, 215 insertions(+), 38 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs index 65b2bfd17..1f007c573 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs @@ -37,7 +37,12 @@ pub(super) fn mask_if(b: bool) -> BitWord { /// Indices of the set bits of a packed bit string, ascending. pub(super) fn set_bits(words: &[BitWord]) -> impl Iterator + '_ { - words.iter().enumerate().flat_map(|(w, &word)| { + set_bits_of(words.iter().copied()) +} + +/// [`set_bits`] over words computed on the fly (e.g. masked). +pub(super) fn set_bits_of(words: impl Iterator) -> impl Iterator { + words.enumerate().flat_map(|(w, word)| { let mut rest = word; std::iter::from_fn(move || { (rest != ZERO).then(|| { diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs index f32dbc0d6..6294f554c 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs @@ -5,7 +5,9 @@ use super::bitword::{ BITWORD_BITS, BitWord, TOP_BIT, ZERO, bit_position, flip_bit, get_bit, mask_if, set_bits, + set_bits_of, }; +use super::view::QubitMask; /// The inverse tableau of an `n`-qubit Clifford. #[derive(Clone, Debug, PartialEq, Eq)] @@ -43,6 +45,16 @@ impl<'a> RowRef<'a> { pub fn x_support(&self) -> impl Iterator + 'a { set_bits(self.x) } + + /// Qubits outside `mask` where the row has an X or Y, ascending. + pub fn x_outside(&self, mask: &'a QubitMask) -> impl Iterator + 'a { + set_bits_of(self.x.iter().zip(&mask.words).map(|(&x, &m)| x & !m)) + } + + /// Qubits in `mask` where the row has a Z or Y, ascending. + pub fn z_inside(&self, mask: &'a QubitMask) -> impl Iterator + 'a { + set_bits_of(self.z.iter().zip(&mask.words).map(|(&z, &m)| z & m)) + } } impl InverseTableau { diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs index b056fd95a..075362a9d 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs @@ -7,9 +7,9 @@ use ppvm_pauli_kernels::{Bits, heisenberg}; use ppvm_traits_2::Clifford; -use super::InverseTableau; use super::bitword::{BitWord, ZERO, set_bits}; use super::transposed::TransposedTableau; +use super::{InverseTableau, QubitMask}; /// Random targets a batch needs before `measure_many` transposes. Measured at /// 118 qubits: a batch of ~48 small collapses (the d=7 surface code) is faster @@ -178,6 +178,41 @@ impl InverseTableau { } } + /// Collapse `O = C⁻¹ Z_q C` onto outcome `m` on `|χ⟩ = |φ⟩ ⊗ |0…0⟩`, where + /// `φ` lives on the `active` qubits and `O` has an X or Y on a dormant one. + /// + /// Stim's collapse with the pivot `p` restricted to dormant qubits: `p` is + /// the lowest of them, `CX(p, v)` clears every other X (active ones too), + /// and `CZ(p, c)` clears the Z on every active `c`. Both are trivial on + /// `χ`, since `p` is in `|0⟩`, so `φ` is unchanged. What is left is + /// `±X_p` or `±Y_p` times Z's on dormant qubits, which act as `+1`, so the + /// final gates are those of [`measure`](Self::measure). + pub fn collapse_dormant(&mut self, q: usize, active: &QubitMask, m: bool) { + let o = self.iz(q); + let pivot = o.x_outside(active).next().expect("no dormant X in O"); + let cx_targets: Vec = o.x_support().filter(|&v| v != pivot).collect(); + let cz_targets: Vec = o.z_inside(active).collect(); + // Pivot bits and sign of O after the CXs and CZs, from row iz_q alone. + // The CXs leave the other qubits' Z bits and clear their X bits. + let (mut pivot_bits, mut negative) = ([true, o.z_bit(pivot)], o.negative); + for &v in &cx_targets { + let ([px, pz, _, _], flip) = + heisenberg::cnot([pivot_bits[0], pivot_bits[1], true, o.z_bit(v)]); + pivot_bits = [px, pz]; + negative ^= flip; + } + for _ in &cz_targets { + let ([px, pz, _, _], flip) = + heisenberg::cz([pivot_bits[0], pivot_bits[1], false, true]); + pivot_bits = [px, pz]; + negative ^= flip; + } + let has_y = pivot_bits[1]; + self.fan_out(pivot, &cx_targets, &cz_targets, |x, z| { + collapse_last(has_y, m != negative, x, z) + }); + } + /// Reset qubit `q` to `|0⟩`: measure it, then flip it if it was `|1⟩`. pub fn reset(&mut self, q: usize, coin: impl FnOnce() -> bool) { if self.measure(q, coin) { diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs index 9da950595..a9f8d024b 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/mod.rs @@ -29,4 +29,4 @@ mod view; mod virtual_gates; pub use data::{InverseTableau, RowRef}; -pub use view::{OwnedRow, VirtualView}; +pub use view::{OwnedRow, QubitMask, VirtualView}; diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/view.rs b/crates/ppvm-tableau-2/src/inverse_tableau/view.rs index f17c0bc0b..1ad37baa5 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/view.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/view.rs @@ -5,9 +5,34 @@ use ppvm_pauli_kernels::row_product; -use super::bitword::{BitWord, ZERO}; +use super::bitword::{BITWORD_BITS, BitWord, ZERO, bit_position, get_bit, mask_if}; use super::{InverseTableau, RowRef}; +/// A set of qubits as packed bits, e.g. the active virtual qubits, so rows +/// can be classified against it word by word. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct QubitMask { + pub(super) words: Vec, +} + +impl QubitMask { + /// The empty set over `n` qubits. + pub fn new(n: usize) -> Self { + Self { + words: vec![ZERO; n.div_ceil(BITWORD_BITS)], + } + } + + pub fn contains(&self, q: usize) -> bool { + get_bit(&self.words, q) + } + + pub fn set(&mut self, q: usize, member: bool) { + let (w, m) = bit_position(q); + self.words[w] = self.words[w] & !m | m & mask_if(member); + } +} + /// An owned row, e.g. a product of rows: its X words, Z words and sign. #[derive(Clone, Debug, PartialEq, Eq)] pub struct OwnedRow { @@ -105,4 +130,9 @@ impl VirtualView<'_> { pub fn measure(&mut self, q: usize, coin: impl FnOnce() -> bool) -> bool { self.t.measure(q, coin) } + + /// See [`InverseTableau::collapse_dormant`]. + pub fn collapse_dormant(&mut self, q: usize, active: &QubitMask, m: bool) { + self.t.collapse_dormant(q, active, m); + } } diff --git a/crates/ppvm-tableau-2/src/lib.rs b/crates/ppvm-tableau-2/src/lib.rs index df9be5a0b..ebe25eed7 100644 --- a/crates/ppvm-tableau-2/src/lib.rs +++ b/crates/ppvm-tableau-2/src/lib.rs @@ -15,7 +15,7 @@ mod inverse_tableau; mod virtual_state; pub use generalized_tableau::GeneralizedTableau; -pub use inverse_tableau::{InverseTableau, OwnedRow, RowRef, VirtualView}; +pub use inverse_tableau::{InverseTableau, OwnedRow, QubitMask, RowRef, VirtualView}; pub use virtual_state::{ ActiveDormant, ActiveState, AxisPauli, DenseState, Rotation, VirtualState, }; diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs index dc4eaf85a..987c00ca8 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs @@ -16,8 +16,10 @@ mod state; use num_complex::Complex64; +use ppvm_traits_2::ApplyGateRule; + use super::VirtualState; -use crate::VirtualView; +use crate::{QubitMask, RowRef, VirtualView}; pub use axis::{AxisPauli, Rotation}; pub use dense::DenseState; @@ -31,9 +33,21 @@ pub use state::ActiveState; pub struct ActiveDormant { /// Virtual qubit of each axis of `φ`: axis `j` is qubit `active[j]`. active: Vec, + /// The qubits of `active`, for word-wise classification. + mask: QubitMask, phi: S, } +/// How `O = C⁻¹ Z_q C` acts on `|φ⟩ ⊗ |0…0⟩`. +enum Case { + /// No X/Y on dormant qubits and none on the active axes: `O = ±1` there. + Deterministic { outcome: bool }, + /// X/Y on a dormant qubit: 50/50, `φ` untouched. + DormantPivot, + /// `O = ±P` with `P` a nontrivial Pauli on the active axes. + Active { p: AxisPauli, negative: bool }, +} + impl ActiveDormant { /// The virtual qubit of each axis of `φ`. pub fn active(&self) -> &[usize] { @@ -43,12 +57,60 @@ impl ActiveDormant { pub fn phi(&self) -> &S { &self.phi } + + /// `o` on the active axes; its X bits must all be on active qubits. + fn axis_pauli(&self, o: RowRef<'_>) -> AxisPauli { + let mut p = AxisPauli { x: 0, z: 0 }; + for (j, &v) in self.active.iter().enumerate() { + p.x |= u64::from(o.x_bit(v)) << j; + p.z |= u64::from(o.z_bit(v)) << j; + } + debug_assert_eq!(o.x_support().count(), p.x.count_ones() as usize); + p + } + + fn classify(&self, o: RowRef<'_>) -> Case { + if o.x_outside(&self.mask).next().is_some() { + return Case::DormantPivot; + } + let p = self.axis_pauli(o); + if p.is_identity() { + Case::Deterministic { + outcome: o.negative, + } + } else { + Case::Active { + p, + negative: o.negative, + } + } + } +} + +/// The view with axis `j` as virtual qubit `active[j]`, so a [`Rotation`]'s +/// gates change the tableau's basis. +struct OnAxes<'v, 'a> { + t: &'v mut VirtualView<'a>, + active: &'v [usize], +} + +impl ApplyGateRule for OnAxes<'_, '_> { + type B = bool; + + fn apply1(&mut self, j: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + self.t.change_basis1(self.active[j], rule); + } + + fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { + self.t.change_basis2(self.active[a], self.active[b], rule); + } } impl VirtualState for ActiveDormant { - fn new(_num_qubits: usize) -> Self { + fn new(num_qubits: usize) -> Self { Self { active: Vec::new(), + mask: QubitMask::new(num_qubits), phi: S::default(), } } @@ -59,48 +121,58 @@ impl VirtualState for ActiveDormant { fn rotate(&mut self, t: &mut VirtualView<'_>, qubits: &[usize], a: Complex64, b: Complex64) { debug_assert_eq!(self.active.len(), self.phi.num_axes()); let mut o = t.iz_product(qubits); - let dormant_x: Vec = o - .row() - .x_support() - .filter(|v| !self.active.contains(v)) - .collect(); + let dormant_x: Vec = o.row().x_outside(&self.mask).collect(); if let Some((&pivot, rest)) = dormant_x.split_first() { t.fan_out(pivot, rest, &[], |x, z| ([x, z], false)); assert!(self.active.len() < 64, "at most 64 active axes"); self.active.push(pivot); + self.mask.set(pivot, true); self.phi.activate(); o = t.iz_product(qubits); } let o = o.row(); - let mut p = AxisPauli { x: 0, z: 0 }; - for (j, &v) in self.active.iter().enumerate() { - p.x |= u64::from(o.x_bit(v)) << j; - p.z |= u64::from(o.z_bit(v)) << j; - } - debug_assert_eq!(o.x_support().count(), p.x.count_ones() as usize); + let p = self.axis_pauli(o); self.phi.rotate(p, a, if o.negative { -b } else { b }); } - /// Deterministic if `O` has no X/Y, else 50/50 (a dormant pivot). + /// Deterministic, 50/50 for a dormant pivot, or the branch weights of `±P`. fn z_weights(&mut self, t: &mut VirtualView<'_>, q: usize) -> [f64; 2] { debug_assert_eq!(self.active.len(), self.phi.num_axes()); - if !self.active.is_empty() { - todo!("measurement with active qubits (step 7d)"); - } - let o = t.iz(q); - if o.x.iter().all(|&w| w == 0) { - if o.negative { [0.0, 1.0] } else { [1.0, 0.0] } - } else { - [0.5, 0.5] + match self.classify(t.iz(q)) { + Case::Deterministic { outcome: false } => [1.0, 0.0], + Case::Deterministic { outcome: true } => [0.0, 1.0], + Case::DormantPivot => [0.5, 0.5], + Case::Active { p, negative } => { + let [plus, minus] = self.phi.branch_weights(p); + if negative { + [minus, plus] + } else { + [plus, minus] + } + } } } + /// The active case measures `P` with outcome `m ⊕ negative`, changes basis + /// so that its pivot axis ends in `|0⟩`, and retires that axis. fn collapse(&mut self, t: &mut VirtualView<'_>, q: usize, m: bool) { - if !self.active.is_empty() { - todo!("measurement with active qubits (step 7d)"); + match self.classify(t.iz(q)) { + Case::Deterministic { outcome } => { + debug_assert_eq!(outcome, m, "collapsing onto an impossible outcome"); + } + Case::DormantPivot => t.collapse_dormant(q, &self.mask, m), + Case::Active { p, negative } => { + let m = m != negative; + let v = Rotation::collapse(p, m); + v.apply_gates(&mut OnAxes { + t, + active: &self.active, + }); + self.phi.collapse(p, m, &v); + let retired = self.active.remove(v.pivot); + self.mask.set(retired, false); + } } - let outcome = t.measure(q, || m); - debug_assert_eq!(outcome, m, "collapsing onto an impossible outcome"); } fn truncation_error(&self) -> f64 { diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs index e361437c4..54e4d2732 100644 --- a/crates/ppvm-tableau-2/tests/dense_matrix.rs +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -9,7 +9,7 @@ use std::collections::{HashSet, VecDeque}; use num_complex::Complex64 as C; -use ppvm_tableau_2::{ActiveDormant, DenseState, GeneralizedTableau, InverseTableau}; +use ppvm_tableau_2::{ActiveDormant, DenseState, GeneralizedTableau, InverseTableau, VirtualState}; use ppvm_traits_2::{Clifford, Pauli, RotationOne, RotationTwo, TGate}; /// A dense `2^n × 2^n` matrix; basis index bit `q` is qubit `q`. @@ -521,9 +521,10 @@ fn rotation(p: &Mat, theta: f64) -> Mat { m } -/// Random three-qubit circuits of Cliffords, `T`, `T†` and one- and two-qubit -/// Pauli rotations: after every gate, all 64 Pauli expectations of -/// `C |χ⟩` match the state vector. +/// Random three-qubit circuits of Cliffords, `T`, `T†`, one- and two-qubit +/// Pauli rotations and measurements: after every step, all 64 Pauli +/// expectations of `C |χ⟩` match the state vector, and before every +/// measurement the outcome weights match the Born probabilities. #[test] fn rotations_match_the_state_vector() { let n = 3; @@ -538,7 +539,7 @@ fn rotations_match_the_state_vector() { seed ^= seed << 17; seed as usize }; - let mut max_axes = 0; + let (mut max_axes, mut retired) = (0, 0); for round in 0..40 { let mut s = Sim::new_with_seed(n, round); let mut v = vec![C::default(); 1 << n]; @@ -547,7 +548,8 @@ fn rotations_match_the_state_vector() { let (a, theta) = (next() % n, (next() % 1000) as f64 * 0.00628); let b = (a + 1 + next() % (n - 1)) % n; let (pa, pb) = (axes[next() % 4], axes[next() % 4]); - let m = match next() % 8 { + let axes_before = s.state().active().len(); + let m = match next() % 10 { 0..3 => { let gate = gates[next() % gates.len()]; apply(&mut s, gate); @@ -565,12 +567,32 @@ fn rotations_match_the_state_vector() { s.rotate_1(pa, a, theta); rotation(&paulis.table[pauli_code(pa, a, n)], theta) } - _ => { + 7 => { s.rotate_2(pa, pb, a, b, theta); let code = pauli_code(pa, a, n) | pauli_code(pb, b, n); rotation(&paulis.table[code], theta) } + _ => { + let [w0, w1] = s + .state() + .clone() + .z_weights(&mut s.tableau().clone().virtual_view(), a); + let p1 = probability_one(&v, a); + assert!( + (w1 / (w0 + w1) - p1).abs() < 1e-9, + "round {round} step {step}: P(1) simulator {}, state {p1}", + w1 / (w0 + w1) + ); + let outcome = s.measure(a); + assert!( + (if outcome { p1 } else { 1.0 - p1 }) > 1e-9, + "round {round} step {step}: sampled an impossible outcome" + ); + project(&mut v, a, outcome); + Mat::identity(n) + } }; + retired += axes_before.saturating_sub(s.state().active().len()); v = apply_to_state(&m, &v); max_axes = max_axes.max(s.state().active().len()); let chi = virtual_state(&s); @@ -587,4 +609,5 @@ fn rotations_match_the_state_vector() { } } assert_eq!(max_axes, n, "the circuits activate every qubit"); + assert!(retired > 0, "measurements retire active axes"); } From 487d2d70328483514e2bc11d2924c3c25af7f1fb Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Fri, 13 Nov 2026 14:02:23 +0100 Subject: [PATCH 27/60] refactor(ppvm-tableau-2): implement Measure and Reset on GeneralizedTableau Replace the inherent measure/reset with the ppvm-traits-2 Measure, ResetState and Reset traits. Outcomes are always Some until loss is modelled. InverseTableau keeps its coin-taking methods: it has no RNG by design. Co-Authored-By: Claude Opus 5.5 --- .../ppvm-tableau-2/src/generalized_tableau.rs | 53 ++++++++++++------- crates/ppvm-tableau-2/tests/dense_matrix.rs | 4 +- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau.rs b/crates/ppvm-tableau-2/src/generalized_tableau.rs index 68ac83926..e0bac7616 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau.rs @@ -8,7 +8,8 @@ use rand::{RngExt, SeedableRng}; use num_complex::Complex64; use ppvm_traits_2::{ - ApplyGateRule, Clifford, CliffordBatch, Pauli, RotationOne, RotationTwo, TGate, + ApplyGateRule, Clifford, CliffordBatch, Measure, Pauli, Reset, ResetState, RotationOne, + RotationTwo, TGate, }; use crate::{InverseTableau, VirtualState}; @@ -56,22 +57,6 @@ impl GeneralizedTableau { &self.state } - /// Measure `Z_q` and collapse; returns the outcome (`true` for `|1⟩`). - pub fn measure(&mut self, q: usize) -> bool { - let mut view = self.tableau.virtual_view(); - let [w0, w1] = self.state.z_weights(&mut view, q); - let m = self.rng.random::() * (w0 + w1) < w1; - self.state.collapse(&mut view, q, m); - m - } - - /// Reset qubit `q` to `|0⟩`: measure it, then flip it if it was `|1⟩`. - pub fn reset(&mut self, q: usize) { - if self.measure(q) { - self.x(q); - } - } - /// `|ψ⟩ ← (a·I + b·∏ Z_q) |ψ⟩` over `qubits`, which must be distinct. fn rotate_z(&mut self, qubits: &[usize], a: Complex64, b: Complex64) { let mut view = self.tableau.virtual_view(); @@ -171,6 +156,34 @@ impl ApplyGateRule for GeneralizedTableau { impl CliffordBatch for GeneralizedTableau {} +/// Measure `Z_q` and collapse; `Some(true)` for `|1⟩`. Every qubit is live +/// until loss is modelled, so the outcome is never `None`. +impl Measure for GeneralizedTableau { + fn measure(&mut self, q: usize) -> Option { + let mut view = self.tableau.virtual_view(); + let [w0, w1] = self.state.z_weights(&mut view, q); + let m = self.rng.random::() * (w0 + w1) < w1; + self.state.collapse(&mut view, q, m); + Some(m) + } +} + +/// Measure, then flip the qubit if it was `|1⟩`. +impl ResetState for GeneralizedTableau { + fn reset_state(&mut self, q: usize) { + if self.measure(q) == Some(true) { + self.x(q); + } + } +} + +/// No loss or leakage yet, so a full reset is a state reset. +impl Reset for GeneralizedTableau { + fn reset(&mut self, q: usize) { + self.reset_state(q); + } +} + #[cfg(test)] mod tests { use super::*; @@ -186,7 +199,7 @@ mod tests { let mut s = Sim::new_with_seed(2, seed); s.h(0); s.cnot(0, 1); - let (a, b) = (s.measure(0), s.measure(1)); + let (a, b) = (s.measure(0).unwrap(), s.measure(1).unwrap()); assert_eq!(a, b); ones += usize::from(a); } @@ -208,7 +221,7 @@ mod tests { } for q in 0..3 { s.reset(q); - assert!(!s.measure(q)); + assert_eq!(s.measure(q), Some(false)); } } } @@ -218,6 +231,6 @@ mod tests { fn x_flips_the_outcome() { let mut s = Sim::new_with_seed(1, 0); s.x(0); - assert!(s.measure(0)); + assert_eq!(s.measure(0), Some(true)); } } diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs index 54e4d2732..21521c414 100644 --- a/crates/ppvm-tableau-2/tests/dense_matrix.rs +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -10,7 +10,7 @@ use std::collections::{HashSet, VecDeque}; use num_complex::Complex64 as C; use ppvm_tableau_2::{ActiveDormant, DenseState, GeneralizedTableau, InverseTableau, VirtualState}; -use ppvm_traits_2::{Clifford, Pauli, RotationOne, RotationTwo, TGate}; +use ppvm_traits_2::{Clifford, Measure, Pauli, RotationOne, RotationTwo, TGate}; /// A dense `2^n × 2^n` matrix; basis index bit `q` is qubit `q`. #[derive(Clone)] @@ -583,7 +583,7 @@ fn rotations_match_the_state_vector() { "round {round} step {step}: P(1) simulator {}, state {p1}", w1 / (w0 + w1) ); - let outcome = s.measure(a); + let outcome = s.measure(a).expect("no loss"); assert!( (if outcome { p1 } else { 1.0 - p1 }) > 1e-9, "round {round} step {step}: sampled an impossible outcome" From a3fc56085c9ce2e1791cfc4564e3d24a88ece648 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Fri, 13 Nov 2026 14:14:49 +0100 Subject: [PATCH 28/60] feat(ppvm-tableau-2): add rotation, U3 and Pauli-noise traits to GeneralizedTableau Split GeneralizedTableau into gates, measure and noise modules like ppvm-tableau. Add RotationOneBatch, RotationTwoBatch, RotXY, U3Gate, PauliError, TwoQubitPauliError, Depolarizing and Depolarizing2. Noise is a sampled Pauli applied as a physical gate. Co-Authored-By: Claude Opus 5.5 --- .../ppvm-tableau-2/src/generalized_tableau.rs | 236 ------------------ .../src/generalized_tableau/gates/clifford.rs | 26 ++ .../src/generalized_tableau/gates/mod.rs | 9 + .../src/generalized_tableau/gates/reset.rs | 23 ++ .../src/generalized_tableau/gates/rot1.rs | 31 +++ .../src/generalized_tableau/gates/rot2.rs | 26 ++ .../src/generalized_tableau/gates/tgate.rs | 22 ++ .../src/generalized_tableau/gates/u3.rs | 17 ++ .../src/generalized_tableau/measure.rs | 74 ++++++ .../src/generalized_tableau/mod.rs | 100 ++++++++ .../src/generalized_tableau/noise.rs | 157 ++++++++++++ crates/ppvm-tableau-2/tests/dense_matrix.rs | 27 +- 12 files changed, 509 insertions(+), 239 deletions(-) delete mode 100644 crates/ppvm-tableau-2/src/generalized_tableau.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/gates/mod.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/gates/rot1.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/gates/rot2.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/gates/tgate.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/gates/u3.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/measure.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/mod.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/noise.rs diff --git a/crates/ppvm-tableau-2/src/generalized_tableau.rs b/crates/ppvm-tableau-2/src/generalized_tableau.rs deleted file mode 100644 index e0bac7616..000000000 --- a/crates/ppvm-tableau-2/src/generalized_tableau.rs +++ /dev/null @@ -1,236 +0,0 @@ -// SPDX-FileCopyrightText: 2026 The PPVM Authors -// SPDX-License-Identifier: Apache-2.0 - -//! [`GeneralizedTableau`]: the simulator, `|ψ⟩ = C |χ⟩`. - -use rand::rngs::SmallRng; -use rand::{RngExt, SeedableRng}; - -use num_complex::Complex64; -use ppvm_traits_2::{ - ApplyGateRule, Clifford, CliffordBatch, Measure, Pauli, Reset, ResetState, RotationOne, - RotationTwo, TGate, -}; - -use crate::{InverseTableau, VirtualState}; - -/// The state `|ψ⟩ = C |χ⟩`: the Clifford `C` as an [`InverseTableau`], and the -/// virtual state `|χ⟩` in the representation `V`. -/// -/// Physical gates `C ← G·C` touch only the tableau. Measurements ask `V` for -/// the outcome weights, sample here, and let `V` collapse. -#[derive(Clone, Debug)] -pub struct GeneralizedTableau { - tableau: InverseTableau, - state: V, - rng: SmallRng, -} - -impl GeneralizedTableau { - /// `|0…0⟩` on `n` qubits, with the RNG seeded from OS entropy. - pub fn new(n: usize) -> Self { - Self::with_rng(n, rand::make_rng()) - } - - /// Same as [`new`](Self::new), but seed the RNG deterministically. - pub fn new_with_seed(n: usize, seed: u64) -> Self { - Self::with_rng(n, SmallRng::seed_from_u64(seed)) - } - - fn with_rng(n: usize, rng: SmallRng) -> Self { - Self { - tableau: InverseTableau::new(n), - state: V::new(n), - rng, - } - } - - pub fn num_qubits(&self) -> usize { - self.tableau.num_qubits() - } - - pub fn tableau(&self) -> &InverseTableau { - &self.tableau - } - - pub fn state(&self) -> &V { - &self.state - } - - /// `|ψ⟩ ← (a·I + b·∏ Z_q) |ψ⟩` over `qubits`, which must be distinct. - fn rotate_z(&mut self, qubits: &[usize], a: Complex64, b: Complex64) { - let mut view = self.tableau.virtual_view(); - self.state.rotate(&mut view, qubits, a, b); - } - - /// `exp(−i θ/2 ∏ Z_q) = cos(θ/2)·I − i sin(θ/2)·∏ Z_q`. - fn rz_product(&mut self, qubits: &[usize], theta: f64) { - let (s, c) = (theta / 2.0).sin_cos(); - self.rotate_z(qubits, Complex64::new(c, 0.0), Complex64::new(0.0, -s)); - } - - /// Map `axis` on `q` to `Z`: apply `W` with `W·axis·W† = Z`. - fn axis_to_z(&mut self, axis: Pauli, q: usize) { - match axis { - Pauli::X => self.h(q), - Pauli::Y => { - self.s_dag(q); - self.h(q); - } - Pauli::Z | Pauli::I => {} - } - } - - /// Undo [`axis_to_z`](Self::axis_to_z): apply `W†`. - fn z_to_axis(&mut self, axis: Pauli, q: usize) { - match axis { - Pauli::X => self.h(q), - Pauli::Y => { - self.h(q); - self.s(q); - } - Pauli::Z | Pauli::I => {} - } - } -} - -/// `T = diag(1, ω) = ½(1 + ω)·I + ½(1 − ω)·Z` with `ω = e^{iπ/4}`, exact -/// including the global phase. -impl TGate for GeneralizedTableau { - fn t(&mut self, q: usize) { - let w = Complex64::from_polar(1.0, std::f64::consts::FRAC_PI_4); - self.rotate_z(&[q], (1.0 + w) / 2.0, (1.0 - w) / 2.0); - } - - fn t_dag(&mut self, q: usize) { - let w = Complex64::from_polar(1.0, -std::f64::consts::FRAC_PI_4); - self.rotate_z(&[q], (1.0 + w) / 2.0, (1.0 - w) / 2.0); - } -} - -/// `exp(−i θ/2 P)`: `P` is mapped to `Z` by a physical Clifford. -impl RotationOne for GeneralizedTableau { - fn rotate_1(&mut self, axis: Pauli, q: usize, theta: f64) { - if axis == Pauli::I { - return; - } - self.axis_to_z(axis, q); - self.rz_product(&[q], theta); - self.z_to_axis(axis, q); - } -} - -/// `exp(−i θ/2 P_a ⊗ P_b)`: each factor is mapped to `Z` by a physical Clifford. -impl RotationTwo for GeneralizedTableau { - fn rotate_2(&mut self, axis_a: Pauli, axis_b: Pauli, a: usize, b: usize, theta: f64) { - assert_ne!(a, b, "two-qubit rotation on one qubit"); - let qubits: Vec = [(axis_a, a), (axis_b, b)] - .into_iter() - .filter(|&(axis, _)| axis != Pauli::I) - .map(|(_, q)| q) - .collect(); - self.axis_to_z(axis_a, a); - self.axis_to_z(axis_b, b); - self.rz_product(&qubits, theta); - self.z_to_axis(axis_a, a); - self.z_to_axis(axis_b, b); - } -} - -/// Physical gates `C ← G·C` go to the tableau; `|χ⟩` is untouched. -impl ApplyGateRule for GeneralizedTableau { - type B = bool; - - fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - self.tableau.apply1(q, rule); - } - - fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { - self.tableau.apply2(a, b, rule); - } - - fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - self.tableau.apply_sign1(q, rule); - } -} - -impl CliffordBatch for GeneralizedTableau {} - -/// Measure `Z_q` and collapse; `Some(true)` for `|1⟩`. Every qubit is live -/// until loss is modelled, so the outcome is never `None`. -impl Measure for GeneralizedTableau { - fn measure(&mut self, q: usize) -> Option { - let mut view = self.tableau.virtual_view(); - let [w0, w1] = self.state.z_weights(&mut view, q); - let m = self.rng.random::() * (w0 + w1) < w1; - self.state.collapse(&mut view, q, m); - Some(m) - } -} - -/// Measure, then flip the qubit if it was `|1⟩`. -impl ResetState for GeneralizedTableau { - fn reset_state(&mut self, q: usize) { - if self.measure(q) == Some(true) { - self.x(q); - } - } -} - -/// No loss or leakage yet, so a full reset is a state reset. -impl Reset for GeneralizedTableau { - fn reset(&mut self, q: usize) { - self.reset_state(q); - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{ActiveDormant, DenseState}; - - type Sim = GeneralizedTableau>; - - /// Bell pair: the two outcomes always agree, and both occur. - #[test] - fn bell_pair_outcomes_agree() { - let mut ones = 0; - for seed in 0..200 { - let mut s = Sim::new_with_seed(2, seed); - s.h(0); - s.cnot(0, 1); - let (a, b) = (s.measure(0).unwrap(), s.measure(1).unwrap()); - assert_eq!(a, b); - ones += usize::from(a); - } - assert!((60..140).contains(&ones), "{ones} of 200"); - } - - /// A measured qubit stays in its outcome, and reset leaves `|0⟩`. - #[test] - fn measurement_repeats_and_reset_clears() { - for seed in 0..50 { - let mut s = Sim::new_with_seed(3, seed); - s.h(0); - s.cnot(0, 1); - s.sqrt_x(2); - s.cz(1, 2); - for q in 0..3 { - let m = s.measure(q); - assert_eq!(s.measure(q), m); - } - for q in 0..3 { - s.reset(q); - assert_eq!(s.measure(q), Some(false)); - } - } - } - - /// `X` then measure is deterministic: `Z|1⟩ = −|1⟩`. - #[test] - fn x_flips_the_outcome() { - let mut s = Sim::new_with_seed(1, 0); - s.x(0); - assert_eq!(s.measure(0), Some(true)); - } -} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs new file mode 100644 index 000000000..e92fd8503 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +use ppvm_traits_2::{ApplyGateRule, CliffordBatch}; + +use super::super::GeneralizedTableau; +use crate::VirtualState; + +/// Physical gates `C ← G·C` go to the tableau; `|χ⟩` is untouched. +impl ApplyGateRule for GeneralizedTableau { + type B = bool; + + fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + self.tableau.apply1(q, rule); + } + + fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { + self.tableau.apply2(a, b, rule); + } + + fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + self.tableau.apply_sign1(q, rule); + } +} + +impl CliffordBatch for GeneralizedTableau {} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/mod.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/mod.rs new file mode 100644 index 000000000..901ad87e7 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/mod.rs @@ -0,0 +1,9 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +mod clifford; +mod reset; +mod rot1; +mod rot2; +mod tgate; +mod u3; diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs new file mode 100644 index 000000000..2f4e2ec1f --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +use ppvm_traits_2::{Clifford, Measure, Reset, ResetState}; + +use super::super::GeneralizedTableau; +use crate::VirtualState; + +/// Measure, then flip the qubit if it was `|1⟩`. +impl ResetState for GeneralizedTableau { + fn reset_state(&mut self, q: usize) { + if self.measure(q) == Some(true) { + self.x(q); + } + } +} + +/// No loss or leakage yet, so a full reset is a state reset. +impl Reset for GeneralizedTableau { + fn reset(&mut self, q: usize) { + self.reset_state(q); + } +} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/rot1.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/rot1.rs new file mode 100644 index 000000000..c4b84623e --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/rot1.rs @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +use ppvm_traits_2::{Pauli, RotXY, RotationOne, RotationOneBatch}; + +use super::super::GeneralizedTableau; +use crate::VirtualState; + +/// `exp(−i θ/2 P)`: `P` is mapped to `Z` by a physical Clifford. +impl RotationOne for GeneralizedTableau { + fn rotate_1(&mut self, axis: Pauli, q: usize, theta: f64) { + if axis == Pauli::I { + return; + } + self.axis_to_z(axis, q); + self.rz_product(&[q], theta); + self.z_to_axis(axis, q); + } +} + +impl RotationOneBatch for GeneralizedTableau {} + +/// `R(φ, θ) = RZ(φ)·RX(θ)·RZ(−φ)`, exact: `RZ(φ)` turns `X` into +/// `cos φ·X + sin φ·Y`. Gates apply in circuit order, `RZ(−φ)` first. +impl RotXY for GeneralizedTableau { + fn r(&mut self, q: usize, axis_angle: f64, theta: f64) { + self.rz(q, -axis_angle); + self.rx(q, theta); + self.rz(q, axis_angle); + } +} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/rot2.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/rot2.rs new file mode 100644 index 000000000..3b2989ea1 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/rot2.rs @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +use ppvm_traits_2::{Pauli, RotationTwo, RotationTwoBatch}; + +use super::super::GeneralizedTableau; +use crate::VirtualState; + +/// `exp(−i θ/2 P_a ⊗ P_b)`: each factor is mapped to `Z` by a physical Clifford. +impl RotationTwo for GeneralizedTableau { + fn rotate_2(&mut self, axis_a: Pauli, axis_b: Pauli, a: usize, b: usize, theta: f64) { + assert_ne!(a, b, "two-qubit rotation on one qubit"); + let qubits: Vec = [(axis_a, a), (axis_b, b)] + .into_iter() + .filter(|&(axis, _)| axis != Pauli::I) + .map(|(_, q)| q) + .collect(); + self.axis_to_z(axis_a, a); + self.axis_to_z(axis_b, b); + self.rz_product(&qubits, theta); + self.z_to_axis(axis_a, a); + self.z_to_axis(axis_b, b); + } +} + +impl RotationTwoBatch for GeneralizedTableau {} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/tgate.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/tgate.rs new file mode 100644 index 000000000..9f4d9e2c4 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/tgate.rs @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +use num_complex::Complex64; +use ppvm_traits_2::TGate; + +use super::super::GeneralizedTableau; +use crate::VirtualState; + +/// `T = diag(1, ω) = ½(1 + ω)·I + ½(1 − ω)·Z` with `ω = e^{iπ/4}`, exact +/// including the global phase. +impl TGate for GeneralizedTableau { + fn t(&mut self, q: usize) { + let w = Complex64::from_polar(1.0, std::f64::consts::FRAC_PI_4); + self.rotate_z(&[q], (1.0 + w) / 2.0, (1.0 - w) / 2.0); + } + + fn t_dag(&mut self, q: usize) { + let w = Complex64::from_polar(1.0, -std::f64::consts::FRAC_PI_4); + self.rotate_z(&[q], (1.0 + w) / 2.0, (1.0 - w) / 2.0); + } +} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/u3.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/u3.rs new file mode 100644 index 000000000..159d86951 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/u3.rs @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +use ppvm_traits_2::{RotationOne, U3Gate}; + +use super::super::GeneralizedTableau; +use crate::VirtualState; + +/// `U3(θ, φ, λ) = RZ(φ)·RY(θ)·RZ(λ)` up to the global phase `e^{i(φ+λ)/2}`. +/// Gates apply in circuit order, `RZ(λ)` first. +impl U3Gate for GeneralizedTableau { + fn u3(&mut self, q: usize, theta: f64, phi: f64, lambda: f64) { + self.rz(q, lambda); + self.ry(q, theta); + self.rz(q, phi); + } +} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs new file mode 100644 index 000000000..bf8d78020 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +use rand::RngExt; + +use ppvm_traits_2::Measure; + +use super::GeneralizedTableau; +use crate::VirtualState; + +/// Measure `Z_q` and collapse; `Some(true)` for `|1⟩`. Every qubit is live +/// until loss is modelled, so the outcome is never `None`. +impl Measure for GeneralizedTableau { + fn measure(&mut self, q: usize) -> Option { + let mut view = self.tableau.virtual_view(); + let [w0, w1] = self.state.z_weights(&mut view, q); + let m = self.rng.random::() * (w0 + w1) < w1; + self.state.collapse(&mut view, q, m); + Some(m) + } +} + +#[cfg(test)] +mod tests { + use ppvm_traits_2::{Clifford, Reset}; + + use super::*; + use crate::{ActiveDormant, DenseState}; + + type Sim = GeneralizedTableau>; + + /// Bell pair: the two outcomes always agree, and both occur. + #[test] + fn bell_pair_outcomes_agree() { + let mut ones = 0; + for seed in 0..200 { + let mut s = Sim::new_with_seed(2, seed); + s.h(0); + s.cnot(0, 1); + let (a, b) = (s.measure(0).unwrap(), s.measure(1).unwrap()); + assert_eq!(a, b); + ones += usize::from(a); + } + assert!((60..140).contains(&ones), "{ones} of 200"); + } + + /// A measured qubit stays in its outcome, and reset leaves `|0⟩`. + #[test] + fn measurement_repeats_and_reset_clears() { + for seed in 0..50 { + let mut s = Sim::new_with_seed(3, seed); + s.h(0); + s.cnot(0, 1); + s.sqrt_x(2); + s.cz(1, 2); + for q in 0..3 { + let m = s.measure(q); + assert_eq!(s.measure(q), m); + } + for q in 0..3 { + s.reset(q); + assert_eq!(s.measure(q), Some(false)); + } + } + } + + /// `X` then measure is deterministic: `Z|1⟩ = −|1⟩`. + #[test] + fn x_flips_the_outcome() { + let mut s = Sim::new_with_seed(1, 0); + s.x(0); + assert_eq!(s.measure(0), Some(true)); + } +} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs b/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs new file mode 100644 index 000000000..8c73b7de7 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs @@ -0,0 +1,100 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! [`GeneralizedTableau`]: the simulator, `|ψ⟩ = C |χ⟩`. +//! +//! - `gates`: Clifford, T, rotation and `U3` gates, and reset. +//! - `measure`: Z measurement. +//! - `noise`: Pauli and depolarizing channels. + +mod gates; +mod measure; +mod noise; + +use rand::SeedableRng; +use rand::rngs::SmallRng; + +use num_complex::Complex64; +use ppvm_traits_2::{Clifford, Pauli}; + +use crate::{InverseTableau, VirtualState}; + +/// The state `|ψ⟩ = C |χ⟩`: the Clifford `C` as an [`InverseTableau`], and the +/// virtual state `|χ⟩` in the representation `V`. +/// +/// Physical gates `C ← G·C` touch only the tableau. Measurements ask `V` for +/// the outcome weights, sample here, and let `V` collapse. +#[derive(Clone, Debug)] +pub struct GeneralizedTableau { + tableau: InverseTableau, + state: V, + rng: SmallRng, +} + +impl GeneralizedTableau { + /// `|0…0⟩` on `n` qubits, with the RNG seeded from OS entropy. + pub fn new(n: usize) -> Self { + Self::with_rng(n, rand::make_rng()) + } + + /// Same as [`new`](Self::new), but seed the RNG deterministically. + pub fn new_with_seed(n: usize, seed: u64) -> Self { + Self::with_rng(n, SmallRng::seed_from_u64(seed)) + } + + fn with_rng(n: usize, rng: SmallRng) -> Self { + Self { + tableau: InverseTableau::new(n), + state: V::new(n), + rng, + } + } + + pub fn num_qubits(&self) -> usize { + self.tableau.num_qubits() + } + + pub fn tableau(&self) -> &InverseTableau { + &self.tableau + } + + pub fn state(&self) -> &V { + &self.state + } + + /// `|ψ⟩ ← (a·I + b·∏ Z_q) |ψ⟩` over `qubits`, which must be distinct. + fn rotate_z(&mut self, qubits: &[usize], a: Complex64, b: Complex64) { + let mut view = self.tableau.virtual_view(); + self.state.rotate(&mut view, qubits, a, b); + } + + /// `exp(−i θ/2 ∏ Z_q) = cos(θ/2)·I − i sin(θ/2)·∏ Z_q`. + fn rz_product(&mut self, qubits: &[usize], theta: f64) { + let (s, c) = (theta / 2.0).sin_cos(); + self.rotate_z(qubits, Complex64::new(c, 0.0), Complex64::new(0.0, -s)); + } + + /// Map `axis` on `q` to `Z`: apply `W` with `W·axis·W† = Z`. + fn axis_to_z(&mut self, axis: Pauli, q: usize) { + match axis { + Pauli::X => self.h(q), + Pauli::Y => { + self.s_dag(q); + self.h(q); + } + Pauli::Z | Pauli::I => {} + } + } + + /// Undo [`axis_to_z`](Self::axis_to_z): apply `W†`. + fn z_to_axis(&mut self, axis: Pauli, q: usize) { + match axis { + Pauli::X => self.h(q), + Pauli::Y => { + self.h(q); + self.s(q); + } + Pauli::Z | Pauli::I => {} + } + } +} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs b/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs new file mode 100644 index 000000000..0db480ba4 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs @@ -0,0 +1,157 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Pauli channels as trajectories: draw one Pauli and apply it as a physical +//! gate, so `|χ⟩` is untouched. + +use rand::RngExt; + +use ppvm_traits_2::{Clifford, Depolarizing, Depolarizing2, PauliError, TwoQubitPauliError}; + +use super::GeneralizedTableau; +use crate::VirtualState; + +impl GeneralizedTableau { + /// Draw event `k` with probability `probabilities[k]`, or `None` with the + /// remaining probability. Uses one random number. + fn draw(&mut self, probabilities: &[f64]) -> Option { + debug_assert!(probabilities.iter().all(|&p| p >= 0.0)); + debug_assert!(probabilities.iter().sum::() <= 1.0 + 1e-9); + let r = self.rng.random::(); + let mut cumulative = 0.0; + probabilities.iter().position(|&p| { + cumulative += p; + r < cumulative + }) + } + + /// Apply Pauli `k` (0 = I, 1 = X, 2 = Y, 3 = Z) to `q`. + fn pauli(&mut self, q: usize, k: usize) { + match k { + 0 => {} + 1 => self.x(q), + 2 => self.y(q), + _ => self.z(q), + } + } +} + +/// `X`, `Y`, `Z` with probabilities `p[0]`, `p[1]`, `p[2]`. +impl PauliError for GeneralizedTableau { + fn pauli_error(&mut self, q: usize, p: [f64; 3]) { + if let Some(k) = self.draw(&p) { + self.pauli(q, k + 1); + } + } +} + +/// `p[k]` is the probability of `P_a ⊗ P_b` with `4a + b = k + 1`, in the +/// trait's order `IX, IY, IZ, XI, …, ZZ`. +impl TwoQubitPauliError for GeneralizedTableau { + fn two_qubit_pauli_error(&mut self, a: usize, b: usize, p: [f64; 15]) { + if let Some(k) = self.draw(&p) { + self.pauli(a, (k + 1) / 4); + self.pauli(b, (k + 1) % 4); + } + } +} + +/// Stim's `DEPOLARIZE1(p)`: each of `X`, `Y`, `Z` with probability `p/3`. +impl Depolarizing for GeneralizedTableau { + fn depolarize1(&mut self, q: usize, p: f64) { + self.pauli_error(q, [p / 3.0; 3]); + } +} + +/// Stim's `DEPOLARIZE2(p)`: each of the 15 non-identity Paulis with +/// probability `p/15`. +impl Depolarizing2 for GeneralizedTableau { + fn depolarize2(&mut self, a: usize, b: usize, p: f64) { + self.two_qubit_pauli_error(a, b, [p / 15.0; 15]); + } +} + +#[cfg(test)] +mod tests { + use ppvm_traits_2::Measure; + + use super::*; + use crate::{ActiveDormant, DenseState}; + + type Sim = GeneralizedTableau>; + + /// The `(x, z)` bits of the Pauli applied to each qubit of a fresh `n`-qubit + /// state by `channel`: X bits flip `|0⟩`, Z bits flip `|+⟩`. + fn applied(n: usize, seed: u64, channel: impl Fn(&mut Sim)) -> Vec<(bool, bool)> { + let (mut zero, mut plus) = (Sim::new_with_seed(n, seed), Sim::new_with_seed(n, seed)); + (0..n).for_each(|q| plus.h(q)); + channel(&mut zero); + channel(&mut plus); + (0..n) + .map(|q| { + plus.h(q); + (zero.measure(q).unwrap(), plus.measure(q).unwrap()) + }) + .collect() + } + + fn bits(name: char) -> (bool, bool) { + match name { + 'I' => (false, false), + 'X' => (true, false), + 'Y' => (true, true), + _ => (false, true), + } + } + + /// With all probability on one entry, exactly that Pauli is applied, in + /// the order the traits document. + #[test] + fn one_hot_channels_apply_the_documented_pauli() { + for (k, name) in "XYZ".chars().enumerate() { + let mut p = [0.0; 3]; + p[k] = 1.0; + assert_eq!(applied(1, 0, |s| s.pauli_error(0, p)), [bits(name)]); + } + let order = "IX IY IZ XI XX XY XZ YI YX YY YZ ZI ZX ZY ZZ"; + for (k, pair) in order.split(' ').enumerate() { + let mut p = [0.0; 15]; + p[k] = 1.0; + let names: Vec = pair.chars().collect(); + assert_eq!( + applied(2, 0, |s| s.two_qubit_pauli_error(0, 1, p)), + [bits(names[0]), bits(names[1])], + "{pair}" + ); + } + } + + /// Flip frequencies on `|0…0⟩` match the channels: `DEPOLARIZE1(p)` flips + /// with `2p/3` (X, Y), `DEPOLARIZE2(p)` flips a given qubit with `8p/15` + /// and both with `4p/15`. + #[test] + fn depolarizing_flip_rates() { + let (p, shots) = (0.3, 20_000); + let within = |count: usize, rate: f64| { + let sigma = (rate * (1.0 - rate) / shots as f64).sqrt(); + ((count as f64 / shots as f64) - rate).abs() < 5.0 * sigma + }; + let mut flips = 0; + for seed in 0..shots { + let mut s = Sim::new_with_seed(1, seed as u64); + s.depolarize1(0, p); + flips += usize::from(s.measure(0).unwrap()); + } + assert!(within(flips, 2.0 * p / 3.0), "{flips} of {shots}"); + let (mut first, mut both) = (0, 0); + for seed in 0..shots { + let mut s = Sim::new_with_seed(2, seed as u64); + s.depolarize2(0, 1, p); + let (a, b) = (s.measure(0).unwrap(), s.measure(1).unwrap()); + first += usize::from(a); + both += usize::from(a && b); + } + assert!(within(first, 8.0 * p / 15.0), "{first} of {shots}"); + assert!(within(both, 4.0 * p / 15.0), "{both} of {shots}"); + } +} diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs index 21521c414..aebe22f71 100644 --- a/crates/ppvm-tableau-2/tests/dense_matrix.rs +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -10,7 +10,7 @@ use std::collections::{HashSet, VecDeque}; use num_complex::Complex64 as C; use ppvm_tableau_2::{ActiveDormant, DenseState, GeneralizedTableau, InverseTableau, VirtualState}; -use ppvm_traits_2::{Clifford, Measure, Pauli, RotationOne, RotationTwo, TGate}; +use ppvm_traits_2::{Clifford, Measure, Pauli, RotXY, RotationOne, RotationTwo, TGate, U3Gate}; /// A dense `2^n × 2^n` matrix; basis index bit `q` is qubit `q`. #[derive(Clone)] @@ -522,7 +522,7 @@ fn rotation(p: &Mat, theta: f64) -> Mat { } /// Random three-qubit circuits of Cliffords, `T`, `T†`, one- and two-qubit -/// Pauli rotations and measurements: after every step, all 64 Pauli +/// Pauli rotations, `U3`, `R(φ, θ)` and measurements: after every step, all 64 Pauli /// expectations of `C |χ⟩` match the state vector, and before every /// measurement the outcome weights match the Born probabilities. #[test] @@ -549,7 +549,11 @@ fn rotations_match_the_state_vector() { let b = (a + 1 + next() % (n - 1)) % n; let (pa, pb) = (axes[next() % 4], axes[next() % 4]); let axes_before = s.state().active().len(); - let m = match next() % 10 { + let (phi, lambda) = ( + (next() % 1000) as f64 * 0.00628, + (next() % 1000) as f64 * 0.00628, + ); + let m = match next() % 12 { 0..3 => { let gate = gates[next() % gates.len()]; apply(&mut s, gate); @@ -572,6 +576,23 @@ fn rotations_match_the_state_vector() { let code = pauli_code(pa, a, n) | pauli_code(pb, b, n); rotation(&paulis.table[code], theta) } + 8 => { + s.u3(a, theta, phi, lambda); + let (sn, co) = (theta / 2.0).sin_cos(); + let e = |x: f64| C::from_polar(1.0, x); + let u = [ + [c(co, 0.0), -e(lambda) * sn], + [e(phi) * sn, e(phi + lambda) * co], + ]; + single(u, a, n) + } + 9 => { + // R(φ, θ) = cos(θ/2)·I − i sin(θ/2)·(cos φ·X + sin φ·Y). + s.r(a, phi, theta); + let (sn, co) = (theta / 2.0).sin_cos(); + let off = |x: f64| c(0.0, -sn) * C::from_polar(1.0, x); + single([[c(co, 0.0), off(-phi)], [off(phi), c(co, 0.0)]], a, n) + } _ => { let [w0, w1] = s .state() From 96e0b31baf42b9f1fe567c50fb72e3b1fccb2602 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Fri, 13 Nov 2026 16:09:36 +0100 Subject: [PATCH 29/60] bench(ppvm-tableau-2): add a clifft-bench runner on GeneralizedTableau run_clifft executes a Stim circuit with noise one shot at a time on GeneralizedTableau>. Clifft's plain R_X/R_Y/ R_Z and U3 lines (half-turns) are rewritten into stim-parser's I[...] tags; MPP is a basis change and CX ladder; classically controlled Paulis read the record. --records dumps measurement records for detector checks outside the timed loop. Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/examples/run_clifft.rs | 441 +++++++++++++++++++ 1 file changed, 441 insertions(+) create mode 100644 crates/ppvm-tableau-2/examples/run_clifft.rs diff --git a/crates/ppvm-tableau-2/examples/run_clifft.rs b/crates/ppvm-tableau-2/examples/run_clifft.rs new file mode 100644 index 000000000..04f52d812 --- /dev/null +++ b/crates/ppvm-tableau-2/examples/run_clifft.rs @@ -0,0 +1,441 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Benchmark harness for the clifft-bench workloads: runs a Stim circuit, +//! noise included, on `GeneralizedTableau` one shot at a time with a fresh +//! simulator per shot. +//! +//! ```text +//! cargo run --release -p ppvm-tableau-2 --example run_clifft -- [shots] [--records ] +//! ``` +//! +//! Clifft's plain `R_X(α)`, `R_Y(α)`, `R_Z(α)` and `U3(θ, φ, λ)` / `U(…)` +//! lines (angles in half-turns) are rewritten into `stim-parser`'s `I[…]` tags before parsing. +//! `stim-parser` drops `rec[-k]` targets, so detectors are not evaluated here; +//! `--records` writes each shot's measurement record as a line of `0`/`1` to +//! check detector statistics outside the timed loop. Workloads are fetched by +//! `scripts/fetch_clifft_bench.sh`. + +use std::fmt::Write as _; +use std::time::Instant; + +use rand::rngs::SmallRng; +use rand::{RngExt, SeedableRng}; + +use ppvm_tableau_2::{ActiveDormant, DenseState, GeneralizedTableau}; +use ppvm_traits_2::{ + Clifford, Depolarizing2, Measure, PauliError, Reset, RotationOne, TGate, U3Gate, +}; +use stim_parser::ast::{Axis, ExtendedInstruction, PauliAxis, PauliFactor, Target}; +use stim_parser::instructions::{AnnotationKind, GateName, MeasureName, NoiseName}; + +type Sim = GeneralizedTableau>; + +#[derive(Clone, Copy)] +enum Basis { + X, + Y, + Z, +} + +/// One operation on one qubit or pair; `REPEAT` blocks are unrolled. +#[derive(Clone, Copy)] +enum Op { + One(GateName, usize), + Two(GateName, usize, usize), + T(usize), + TDag(usize), + Rotation(Axis, f64, usize), + U3([f64; 3], usize), + /// `X`, `Y`, `Z` with these probabilities. + Pauli([f64; 3], usize), + Depolarize2(f64, usize, usize), + /// Recorded outcome flipped with probability `flip`; `reset` returns the + /// qubit to the basis' `+1` state afterwards. + Measure { + basis: Basis, + q: usize, + flip: f64, + reset: bool, + }, + Reset(Basis, usize), + /// Classically controlled Pauli: apply it to `q` if `rec[-back]` is 1. + Feedback { + pauli: Basis, + back: usize, + q: usize, + }, + /// Pauli product `factors[at..at + len]`. + Mpp { + at: usize, + len: usize, + flip: f64, + }, +} + +/// Rewrite Clifft's plain rotation and `U3` lines into `I[…]` tags. Both write +/// angles in half-turns (`R_Z(α)` rotates by `α·π`), so the numbers carry over. +fn rewrite_dialect(src: &str) -> String { + let mut out = String::new(); + for line in src.lines() { + let body = line.trim_start(); + let indent = &line[..line.len() - body.len()]; + let rewritten = ["R_X", "R_Y", "R_Z", "U3", "U"] + .into_iter() + .find_map(|name| { + let rest = body.strip_prefix(name)?.strip_prefix('(')?; + let (args, targets) = rest.split_once(')')?; + let turns: Vec = args + .split(',') + .map(|a| a.trim().parse::().expect("angle")) + .collect(); + let params = match (name, turns.as_slice()) { + ("U3" | "U", [t, p, l]) => { + format!("U3(theta={t}*pi, phi={p}*pi, lambda={l}*pi)") + } + (_, [t]) => format!("{name}(theta={t}*pi)"), + _ => panic!("bad arguments in {line:?}"), + }; + Some(format!("{indent}I[{params}]{targets}")) + }); + writeln!(out, "{}", rewritten.as_deref().unwrap_or(line)).unwrap(); + } + out +} + +fn flip_arg(args: &[f64]) -> f64 { + args.first().copied().unwrap_or(0.0) +} + +/// Flatten the program into ops, unrolling `REPEAT` blocks. MPP factors go to +/// `factors`. +fn lower(instructions: &[ExtendedInstruction], ops: &mut Vec, factors: &mut Vec) { + for instruction in instructions { + match instruction { + ExtendedInstruction::Gate(g) if g.targets.iter().any(|t| t.as_qubit().is_none()) => ops + .extend( + g.targets + .chunks(2) + .map(|pair| feedback(g.name, pair[0], pair[1])), + ), + ExtendedInstruction::Gate(g) => { + let qs: Vec = g + .targets + .iter() + .map(|t| t.as_qubit().expect("only qubit targets are supported")) + .collect(); + match g.name { + GateName::Reset | GateName::ResetZ => { + ops.extend(qs.iter().map(|&q| Op::Reset(Basis::Z, q))) + } + GateName::ResetX => ops.extend(qs.iter().map(|&q| Op::Reset(Basis::X, q))), + GateName::ResetY => ops.extend(qs.iter().map(|&q| Op::Reset(Basis::Y, q))), + GateName::CX + | GateName::ZCX + | GateName::CNot + | GateName::CY + | GateName::ZCY + | GateName::CZ + | GateName::ZCZ => { + ops.extend(qs.chunks(2).map(|p| Op::Two(g.name, p[0], p[1]))) + } + GateName::Identity => {} + name => ops.extend(qs.iter().map(|&q| Op::One(name, q))), + } + } + ExtendedInstruction::T { targets, .. } => ops.extend(targets.iter().map(|&q| Op::T(q))), + ExtendedInstruction::TDag { targets, .. } => { + ops.extend(targets.iter().map(|&q| Op::TDag(q))) + } + ExtendedInstruction::Rotation { + axis, + theta, + targets, + .. + } => ops.extend(targets.iter().map(|&q| Op::Rotation(*axis, *theta, q))), + ExtendedInstruction::U3 { + theta, + phi, + lambda, + targets, + .. + } => ops.extend(targets.iter().map(|&q| Op::U3([*theta, *phi, *lambda], q))), + ExtendedInstruction::Noise(n) => { + let p = flip_arg(&n.args); + let one = |probabilities: [f64; 3]| { + n.targets.iter().map(move |&q| Op::Pauli(probabilities, q)) + }; + match n.name { + NoiseName::XError => ops.extend(one([p, 0.0, 0.0])), + NoiseName::YError => ops.extend(one([0.0, p, 0.0])), + NoiseName::ZError => ops.extend(one([0.0, 0.0, p])), + NoiseName::Depolarize1 => ops.extend(one([p / 3.0; 3])), + NoiseName::PauliChannel1 => ops.extend(one([n.args[0], n.args[1], n.args[2]])), + NoiseName::Depolarize2 => ops.extend( + n.targets + .chunks(2) + .map(|pair| Op::Depolarize2(p, pair[0], pair[1])), + ), + other => panic!("unsupported noise {other:?}"), + } + } + ExtendedInstruction::Measure(m) => { + let (basis, reset) = match m.name { + MeasureName::M | MeasureName::MZ => (Basis::Z, false), + MeasureName::MX => (Basis::X, false), + MeasureName::MY => (Basis::Y, false), + MeasureName::MR => (Basis::Z, true), + MeasureName::MRX => (Basis::X, true), + MeasureName::MRY => (Basis::Y, true), + other => panic!("unsupported measurement {other:?}"), + }; + let flip = flip_arg(&m.args); + ops.extend(m.targets.iter().map(|&q| Op::Measure { + basis, + q, + flip, + reset, + })); + } + ExtendedInstruction::Mpp(mpp) => { + let flip = flip_arg(&mpp.args); + for product in &mpp.products { + ops.push(Op::Mpp { + at: factors.len(), + len: product.len(), + flip, + }); + factors.extend_from_slice(product); + } + } + ExtendedInstruction::Repeat { count, body, .. } => { + let mut block = Vec::new(); + lower(body, &mut block, factors); + for _ in 0..*count { + ops.extend_from_slice(&block); + } + } + ExtendedInstruction::Annotation(a) => match a.kind { + AnnotationKind::Detector + | AnnotationKind::ObservableInclude + | AnnotationKind::QubitCoords + | AnnotationKind::ShiftCoords + | AnnotationKind::Tick => {} + }, + other => panic!("unsupported instruction {other:?}"), + } + } +} + +/// `CX`/`CY`/`CZ` with a `rec[-k]` control (either side for `CZ`). +fn feedback(name: GateName, a: Target, b: Target) -> Op { + let pauli = match name { + GateName::CX | GateName::ZCX | GateName::CNot => Basis::X, + GateName::CY | GateName::ZCY => Basis::Y, + GateName::CZ | GateName::ZCZ => Basis::Z, + other => panic!("unsupported classically controlled gate {other:?}"), + }; + match (a, b, pauli) { + (Target::Rec(back), Target::Qubit(q), _) + | (Target::Qubit(q), Target::Rec(back), Basis::Z) => Op::Feedback { pauli, back, q }, + _ => panic!("unsupported targets {a:?} {b:?} for {name:?}"), + } +} + +fn apply_one(s: &mut Sim, name: GateName, q: usize) { + match name { + GateName::X => s.x(q), + GateName::Y => s.y(q), + GateName::Z => s.z(q), + GateName::H | GateName::HXZ => s.h(q), + GateName::S | GateName::SqrtZ => s.s(q), + GateName::SDag | GateName::SqrtZDag => s.s_dag(q), + GateName::SqrtX => s.sqrt_x(q), + GateName::SqrtXDag => s.sqrt_x_dag(q), + GateName::SqrtY => s.sqrt_y(q), + GateName::SqrtYDag => s.sqrt_y_dag(q), + GateName::T => s.t(q), + GateName::TDag => s.t_dag(q), + other => panic!("unsupported gate {other:?}"), + } +} + +fn apply_two(s: &mut Sim, name: GateName, a: usize, b: usize) { + match name { + GateName::CX | GateName::ZCX | GateName::CNot => s.cnot(a, b), + GateName::CY | GateName::ZCY => s.cy(a, b), + GateName::CZ | GateName::ZCZ => s.cz(a, b), + other => panic!("unsupported gate {other:?}"), + } +} + +/// Map `basis` on `q` to `Z` (`to_z`) or back. +fn change_basis(s: &mut Sim, basis: Basis, q: usize, to_z: bool) { + match (basis, to_z) { + (Basis::Z, _) => {} + (Basis::X, _) => s.h(q), + (Basis::Y, true) => { + s.s_dag(q); + s.h(q); + } + (Basis::Y, false) => { + s.h(q); + s.s(q); + } + } +} + +fn pauli_basis(axis: PauliAxis) -> Basis { + match axis { + PauliAxis::X => Basis::X, + PauliAxis::Y => Basis::Y, + PauliAxis::Z => Basis::Z, + } +} + +/// Run `op`, appending measurement outcomes to `record`; `rng` flips recorded +/// outcomes. +fn run(s: &mut Sim, op: Op, factors: &[PauliFactor], rng: &mut SmallRng, record: &mut Vec) { + let mut noisy = |m: bool, flip: f64| m ^ (flip > 0.0 && rng.random::() < flip); + match op { + Op::One(name, q) => apply_one(s, name, q), + Op::Two(name, a, b) => apply_two(s, name, a, b), + Op::T(q) => s.t(q), + Op::TDag(q) => s.t_dag(q), + Op::Rotation(axis, theta, q) => match axis { + Axis::X => s.rx(q, theta), + Axis::Y => s.ry(q, theta), + Axis::Z => s.rz(q, theta), + }, + Op::U3([theta, phi, lambda], q) => s.u3(q, theta, phi, lambda), + Op::Pauli(p, q) => s.pauli_error(q, p), + Op::Depolarize2(p, a, b) => s.depolarize2(a, b, p), + Op::Measure { + basis, + q, + flip, + reset, + } => { + change_basis(s, basis, q, true); + let m = s.measure(q).expect("no loss"); + if reset && m { + s.x(q); + } + change_basis(s, basis, q, false); + record.push(noisy(m, flip)); + } + Op::Feedback { pauli, back, q } => { + if record[record.len() - back] { + match pauli { + Basis::X => s.x(q), + Basis::Y => s.y(q), + Basis::Z => s.z(q), + } + } + } + Op::Reset(basis, q) => match basis { + Basis::Z => s.reset(q), + Basis::X => s.reset_x(q), + Basis::Y => s.reset_y(q), + }, + // Basis changes to Z, a CX ladder onto the first qubit, measure, undo. + Op::Mpp { at, len, flip } => { + let product = &factors[at..at + len]; + let first = product[0].qubit; + for f in product { + change_basis(s, pauli_basis(f.axis), f.qubit, true); + } + for f in &product[1..] { + s.cnot(f.qubit, first); + } + let m = s.measure(first).expect("no loss"); + for f in product[1..].iter().rev() { + s.cnot(f.qubit, first); + } + for f in product { + change_basis(s, pauli_basis(f.axis), f.qubit, false); + } + record.push(noisy(m, flip)); + } + } +} + +fn qubits(op: &Op, factors: &[PauliFactor]) -> Vec { + match *op { + Op::Two(_, a, b) | Op::Depolarize2(_, a, b) => vec![a, b], + Op::One(_, q) + | Op::T(q) + | Op::TDag(q) + | Op::Rotation(_, _, q) + | Op::U3(_, q) + | Op::Pauli(_, q) + | Op::Measure { q, .. } + | Op::Reset(_, q) + | Op::Feedback { q, .. } => vec![q], + Op::Mpp { at, len, .. } => factors[at..at + len].iter().map(|f| f.qubit).collect(), + } +} + +fn main() { + let mut args = std::env::args().skip(1); + let path = args + .next() + .expect("usage: run_clifft [shots] [--records ]"); + let (mut shots, mut records_path) = (1000usize, None); + while let Some(arg) = args.next() { + if arg == "--records" { + records_path = Some(args.next().expect("--records needs a path")); + } else { + shots = arg.parse().expect("shots"); + } + } + let src = rewrite_dialect(&std::fs::read_to_string(&path).expect("read circuit")); + let program = + stim_parser::parse_extended(&src).unwrap_or_else(|e| panic!("parse error: {e:?}")); + let (mut ops, mut factors) = (Vec::new(), Vec::new()); + lower(&program.instructions, &mut ops, &mut factors); + let n = 1 + ops + .iter() + .flat_map(|op| qubits(op, &factors)) + .max() + .expect("empty circuit"); + + let mut rng = SmallRng::seed_from_u64(0x9e37_79b9_7f4a_7c15); + let (mut record, mut records) = (Vec::new(), String::new()); + let (mut parity, mut max_k, mut k_at_measurements, mut measurements) = + (false, 0, 0usize, 0usize); + let start = Instant::now(); + for _ in 0..shots { + let mut s = Sim::new_with_seed(n, rng.random()); + record.clear(); + for &op in &ops { + run(&mut s, op, &factors, &mut rng, &mut record); + let k = s.state().active().len(); + max_k = max_k.max(k); + if matches!(op, Op::Measure { .. } | Op::Mpp { .. }) { + k_at_measurements += k; + measurements += 1; + } + } + parity ^= record.iter().fold(false, |p, &m| p ^ m); + if records_path.is_some() { + records.extend(record.iter().map(|&m| if m { '1' } else { '0' })); + records.push('\n'); + } + } + let elapsed = start.elapsed().as_secs_f64(); + if let Some(p) = records_path { + std::fs::write(p, records).expect("write records"); + } + println!( + "{path}: {n} qubits, {} ops ({} measurements) per shot, {shots} shots", + ops.len(), + record.len() + ); + println!( + "{:.1} µs/shot, {:.0} shots/s, max k {max_k}, mean k at measurements {:.2} (parity {parity})", + elapsed / shots as f64 * 1e6, + shots as f64 / elapsed, + k_at_measurements as f64 / measurements.max(1) as f64, + ); +} From 2288b46ce28a7fbe2a7d0b94f59e433a3e150827 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 08:54:25 +0100 Subject: [PATCH 30/60] bench(ppvm-tableau-2): add the clifft-bench comparison script clifft.py runs clifft-bench workloads, noise included, on the run_clifft example and on reference binaries with the same CLI, and writes target/stim-compare/clifft.csv. common.py now builds and runs either ppvm example. Co-Authored-By: Claude Opus 5.5 --- .../benches/stim-compare/clifft.py | 84 +++++++++++++++++++ .../benches/stim-compare/common.py | 19 +++-- 2 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 crates/ppvm-tableau-2/benches/stim-compare/clifft.py diff --git a/crates/ppvm-tableau-2/benches/stim-compare/clifft.py b/crates/ppvm-tableau-2/benches/stim-compare/clifft.py new file mode 100644 index 000000000..f50e634cd --- /dev/null +++ b/crates/ppvm-tableau-2/benches/stim-compare/clifft.py @@ -0,0 +1,84 @@ +"""clifft-bench workloads, noise included, on ppvm-tableau-2's `run_clifft` and +on reference binaries with the same CLI. Follows clifft-bench's probe contract +loosely: per tool and workload, a calibration run (also the warmup) picks the +shots for about `--seconds`, then `--probes` timed runs alternate over the +tools; the median is reported. A probe is at least one shot, so on slow +workloads it runs longer than `--seconds`. Writes `clifft.csv` to +`target/stim-compare/`. + + python clifft.py --reference NAME=PATH... [--workloads DIR] [--case NAME]... + [--skip NAME]... [--seconds 1] [--probes 3] + +Workloads are clifft-bench's `.stim` files (`scripts/fetch_clifft_bench.sh` +fetches them into `target/clifft-bench/`). Every tool reads the original file: +the ppvm runners rewrite Clifft's rotation and `U3` lines themselves. + +Stim merges consecutive instructions with the same gate and arguments, and +shares setup across an instruction's targets; ppvm's `stim-parser` does not, so +ppvm runs one instruction per line. See the design doc's Benchmarks section. +""" + +import argparse +import csv +import pathlib +import statistics + +import common + + +def calibrated_shots(cmd: list[str], circuit: pathlib.Path, seconds: float) -> int: + """Shots for about `seconds`: one shot, then a ~10% run if that is fast.""" + estimate = common.time_per_shot(cmd, circuit, 1) + if estimate * 1e-6 < seconds / 20: + shots = max(1, int(seconds / 10 * 1e6 / estimate)) + estimate = common.time_per_shot(cmd, circuit, shots) + return max(1, int(seconds * 1e6 / estimate)) + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--reference", action="append", default=[]) + parser.add_argument( + "--workloads", type=pathlib.Path, default=common.REPO / "target" / "clifft-bench" + ) + parser.add_argument("--case", action="append", help="workload name, e.g. coherent_d3_r1") + parser.add_argument("--skip", action="append", default=[], help="workload name to leave out") + parser.add_argument("--seconds", type=float, default=1.0, help="target time per probe") + parser.add_argument("--probes", type=int, default=3) + args = parser.parse_args() + + common.build_ppvm("run_clifft") + tools = common.tools(args.reference, "run_clifft") + references = list(tools)[1:] + paths = sorted(args.workloads.glob("*.stim")) + if args.case: + paths = [p for p in paths if p.stem in args.case] + paths = [p for p in paths if p.stem not in args.skip] + if not paths: + raise SystemExit(f"no workloads in {args.workloads}") + rows = [] + for path in paths: + shots = {name: calibrated_shots(cmd, path, args.seconds) for name, cmd in tools.items()} + times = {name: [] for name in tools} + for _ in range(args.probes): + for name, cmd in tools.items(): + times[name].append(common.time_per_shot(cmd, path, shots[name])) + row = {"workload": path.stem} + row |= {f"{name}_us": round(statistics.median(ts), 3) for name, ts in times.items()} + row |= {f"{name}_shots": shots[name] for name in tools} + for ref in references: + row[f"ppvm_over_{ref}"] = round(row["ppvm_us"] / row[f"{ref}_us"], 3) + rows.append(row) + cells = " ".join(f"{name} {row[f'{name}_us']:12.1f} µs" for name in tools) + ratios = " ".join(f"ppvm/{ref} {row[f'ppvm_over_{ref}']:.2f}" for ref in references) + print(f"{path.stem:32} {cells} {ratios}", flush=True) + common.OUT.mkdir(parents=True, exist_ok=True) + with open(common.OUT / "clifft.csv", "w", newline="") as f: + writer = csv.DictWriter(f, fieldnames=list(rows[0])) + writer.writeheader() + writer.writerows(rows) + print(f"wrote {common.OUT / 'clifft.csv'}") + + +if __name__ == "__main__": + main() diff --git a/crates/ppvm-tableau-2/benches/stim-compare/common.py b/crates/ppvm-tableau-2/benches/stim-compare/common.py index 8e7d2cfff..59d30973d 100644 --- a/crates/ppvm-tableau-2/benches/stim-compare/common.py +++ b/crates/ppvm-tableau-2/benches/stim-compare/common.py @@ -1,6 +1,7 @@ -"""Running the timing tools: ppvm's `run_stim` and reference binaries with -the same CLI (` `, printing ` µs/shot` or -` us/shot`), such as `stim_driver` built against a Stim checkout.""" +"""Running the timing tools: a ppvm example (`run_stim`, `run_clifft`) and +reference binaries with the same CLI (` `, printing +` µs/shot` or ` us/shot`), such as `stim_driver` built against a Stim +checkout.""" import pathlib import re @@ -10,10 +11,10 @@ HERE = pathlib.Path(__file__).resolve().parent REPO = HERE.parents[3] OUT = REPO / "target" / "stim-compare" -RUN_STIM = REPO / "target" / "release" / "examples" / "run_stim" +EXAMPLES = REPO / "target" / "release" / "examples" -def build_ppvm() -> None: +def build_ppvm(example: str = "run_stim") -> None: subprocess.run( [ "cargo", @@ -23,18 +24,18 @@ def build_ppvm() -> None: "-p", "ppvm-tableau-2", "--example", - "run_stim", + example, ], cwd=REPO, check=True, ) -def tools(references: list[str]) -> dict[str, list[str]]: - """`ppvm`, then the `NAME=PATH` references in order.""" +def tools(references: list[str], example: str = "run_stim") -> dict[str, list[str]]: + """`ppvm` (the example), then the `NAME=PATH` references in order.""" if not references: sys.exit("give at least one --reference NAME=PATH (see README.md)") - found = {"ppvm": [str(RUN_STIM)]} + found = {"ppvm": [str(EXAMPLES / example)]} for reference in references: name, path = reference.split("=", 1) found[name] = [path] From 0e08ea01c248c5842e3b3352d5b7007a31025a21 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 09:05:06 +0100 Subject: [PATCH 31/60] docs(ppvm-tableau-2): drop the design-doc reference from the crate docs Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ppvm-tableau-2/src/lib.rs b/crates/ppvm-tableau-2/src/lib.rs index ebe25eed7..a4ab728b8 100644 --- a/crates/ppvm-tableau-2/src/lib.rs +++ b/crates/ppvm-tableau-2/src/lib.rs @@ -3,7 +3,7 @@ //! Hybrid stabilizer simulation: `|ψ⟩ = C |χ⟩`, an inverse tableau for the //! Clifford `C` plus the state `|χ⟩` of the virtual qubits in an exchangeable -//! representation. Design: `docs/design/hybrid-tableau.md`. +//! representation. //! //! - `inverse_tableau`: [`InverseTableau`] and its [`VirtualView`]. //! - `virtual_state`: the [`VirtualState`] trait and [`ActiveDormant`], which From 66c442122560bcf1567c52bdb6615e273c568c4b Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 09:25:12 +0100 Subject: [PATCH 32/60] refactor(ppvm-tableau-2): drop the unused truncation_error hook It was meant for a sparse backend that is no longer planned; every implementation returned 0 and nothing read it. Co-Authored-By: Claude Opus 5.5 --- .../ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs | 4 ---- .../ppvm-tableau-2/src/virtual_state/active_dormant/state.rs | 5 ----- crates/ppvm-tableau-2/src/virtual_state/mod.rs | 5 ----- 3 files changed, 14 deletions(-) diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs index 987c00ca8..1ca2b6ed1 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs @@ -174,8 +174,4 @@ impl VirtualState for ActiveDormant { } } } - - fn truncation_error(&self) -> f64 { - self.phi.truncation_error() - } } diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs index 12e421ed3..b0ddbeffb 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs @@ -31,9 +31,4 @@ pub trait ActiveState: Clone { /// renormalize. Axes above the pivot shift down by one. The branch must /// have nonzero weight. fn collapse(&mut self, p: AxisPauli, m: bool, v: &Rotation); - - /// Weight dropped by truncation so far; exact backends return 0. - fn truncation_error(&self) -> f64 { - 0.0 - } } diff --git a/crates/ppvm-tableau-2/src/virtual_state/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/mod.rs index 0739e7a3b..e3554389b 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/mod.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/mod.rs @@ -37,9 +37,4 @@ pub trait VirtualState: Clone { /// Project onto outcome `m` of `Z_q` and renormalize. The outcome must have /// nonzero weight. fn collapse(&mut self, t: &mut VirtualView<'_>, q: usize, m: bool); - - /// Weight dropped by truncation so far; exact representations return 0. - fn truncation_error(&self) -> f64 { - 0.0 - } } From e07221e0c4b3bbc2988845986f1ec06ba3fac803 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 09:42:47 +0100 Subject: [PATCH 33/60] feat(ppvm-traits-2): add leakage channel traits LeakageChannel and ResetLeakageChannel, ported from ppvm-traits. The AsymmetricLossChannel doc no longer claims every backend omits the survival back-action. Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-traits-2/src/gates/channel.rs | 21 +++++++++++++++++++-- crates/ppvm-traits-2/src/gates/mod.rs | 3 ++- crates/ppvm-traits-2/src/lib.rs | 7 ++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/crates/ppvm-traits-2/src/gates/channel.rs b/crates/ppvm-traits-2/src/gates/channel.rs index 3ab12f9f9..dac3f15aa 100644 --- a/crates/ppvm-traits-2/src/gates/channel.rs +++ b/crates/ppvm-traits-2/src/gates/channel.rs @@ -130,7 +130,24 @@ pub trait ResetLossChannel { /// The total loss probability depends on the populations, so the channel reads `⟨Z⟩`. pub trait AsymmetricLossChannel { /// Apply asymmetric loss to `qubit`, with `p0` / `p1` the loss probabilities - /// from `|0⟩` / `|1⟩`. See the backend impl for the trajectory approximation - /// used (the survival back-action is omitted). + /// from `|0⟩` / `|1⟩`. Backends document whether they apply the survival + /// back-action. fn asymmetric_loss_channel(&mut self, qubit: usize, p0: C, p1: C); } + +/// Single-qubit leakage: with probability `p0` the qubit leaks and is pinned to +/// `|0⟩`, with `p1` to `|1⟩`. A leaked qubit stays measurable and returns the +/// pinned bit; gates skip it. +pub trait LeakageChannel { + /// Apply leakage to `qubit`, with `p0` / `p1` the probabilities of leaking + /// into `|0⟩` / `|1⟩`. + fn leakage_channel(&mut self, qubit: usize, p0: C, p1: C); +} + +/// Return a leaked qubit to the computational subspace in `|0⟩`, modelling a +/// leakage-reduction step. +pub trait ResetLeakageChannel { + /// Clear leakage at `qubit` and reset it to `|0⟩`. No-op on a live qubit; a + /// lost qubit is not recovered. + fn reset_leakage_channel(&mut self, qubit: usize); +} diff --git a/crates/ppvm-traits-2/src/gates/mod.rs b/crates/ppvm-traits-2/src/gates/mod.rs index d2df415c5..253fba25d 100644 --- a/crates/ppvm-traits-2/src/gates/mod.rs +++ b/crates/ppvm-traits-2/src/gates/mod.rs @@ -12,7 +12,8 @@ mod rot; pub use apply_gate_rule::ApplyGateRule; pub use channel::{ AmplitudeDamping, AsymmetricLossChannel, CorrelatedLossChannel, Depolarizing, Depolarizing2, - LossChannel, PauliError, ResetLossChannel, TwoQubitPauliError, + LeakageChannel, LossChannel, PauliError, ResetLeakageChannel, ResetLossChannel, + TwoQubitPauliError, }; pub use clifford::{Clifford, CliffordBatch}; pub use measure::{Measure, Projection, Reset, ResetState}; diff --git a/crates/ppvm-traits-2/src/lib.rs b/crates/ppvm-traits-2/src/lib.rs index 56842b500..65ea43520 100644 --- a/crates/ppvm-traits-2/src/lib.rs +++ b/crates/ppvm-traits-2/src/lib.rs @@ -21,9 +21,10 @@ pub use containers::{ }; pub use gates::{ AmplitudeDamping, ApplyGateRule, AsymmetricLossChannel, Bits, CRx, Clifford, CliffordBatch, - CorrelatedLossChannel, Depolarizing, Depolarizing2, LossChannel, Measure, PauliError, - Projection, Reset, ResetLossChannel, ResetState, RotXY, RotationOne, RotationOneBatch, - RotationTwo, RotationTwoBatch, TGate, TwoQubitPauliError, U3Gate, + CorrelatedLossChannel, Depolarizing, Depolarizing2, LeakageChannel, LossChannel, Measure, + PauliError, Projection, Reset, ResetLeakageChannel, ResetLossChannel, ResetState, RotXY, + RotationOne, RotationOneBatch, RotationTwo, RotationTwoBatch, TGate, TwoQubitPauliError, + U3Gate, }; pub use loss::{LossState, LossySite}; pub use pauli::Pauli; From 31b15a55aa11d49ded1bd4786c9a1049b065779d Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 09:51:11 +0100 Subject: [PATCH 34/60] feat(ppvm-tableau-2): track lost qubits and skip gates on them GeneralizedTableau keeps a Status (inactive and lost masks, plus a count). Gates skip inactive qubits, two-qubit Cliffords as a whole; rotations drop the inactive operand. A lost qubit measures None. Add LossChannel and ResetLossChannel; Reset revives a lost qubit. Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/gates/clifford.rs | 15 +- .../src/generalized_tableau/gates/reset.rs | 10 +- .../src/generalized_tableau/loss.rs | 136 ++++++++++++++++++ .../src/generalized_tableau/measure.rs | 35 ++++- .../src/generalized_tableau/mod.rs | 34 ++++- .../src/generalized_tableau/status.rs | 59 ++++++++ 6 files changed, 273 insertions(+), 16 deletions(-) create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/loss.rs create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/status.rs diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs index e92fd8503..150a33445 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs @@ -6,20 +6,27 @@ use ppvm_traits_2::{ApplyGateRule, CliffordBatch}; use super::super::GeneralizedTableau; use crate::VirtualState; -/// Physical gates `C ← G·C` go to the tableau; `|χ⟩` is untouched. +/// Physical gates `C ← G·C` go to the tableau; `|χ⟩` is untouched. A gate on a +/// lost or leaked qubit is skipped, a two-qubit gate as a whole. impl ApplyGateRule for GeneralizedTableau { type B = bool; fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - self.tableau.apply1(q, rule); + if !self.status.is_inactive(q) { + self.tableau.apply1(q, rule); + } } fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { - self.tableau.apply2(a, b, rule); + if !self.status.is_inactive(a) && !self.status.is_inactive(b) { + self.tableau.apply2(a, b, rule); + } } fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - self.tableau.apply_sign1(q, rule); + if !self.status.is_inactive(q) { + self.tableau.apply_sign1(q, rule); + } } } diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs index 2f4e2ec1f..bd52ea65b 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs @@ -1,23 +1,25 @@ // SPDX-FileCopyrightText: 2026 The PPVM Authors // SPDX-License-Identifier: Apache-2.0 -use ppvm_traits_2::{Clifford, Measure, Reset, ResetState}; +use ppvm_traits_2::{Clifford, Reset, ResetState}; use super::super::GeneralizedTableau; use crate::VirtualState; -/// Measure, then flip the qubit if it was `|1⟩`. +/// Measure, then flip the qubit if it was `|1⟩`. Lost and leaked qubits are +/// left alone. impl ResetState for GeneralizedTableau { fn reset_state(&mut self, q: usize) { - if self.measure(q) == Some(true) { + if !self.status.is_inactive(q) && self.sample_z(q) { self.x(q); } } } -/// No loss or leakage yet, so a full reset is a state reset. +/// A full reset: a lost or leaked qubit becomes live, then its state is reset. impl Reset for GeneralizedTableau { fn reset(&mut self, q: usize) { + self.status.set_live(q); self.reset_state(q); } } diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs new file mode 100644 index 000000000..1cdb1bca6 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs @@ -0,0 +1,136 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Loss and leakage. A lost qubit is collapsed, reset to `|0⟩` and skipped by +//! gates; measuring it gives `None`. A leaked qubit is collapsed and pinned to +//! `|0⟩` or `|1⟩`; gates skip it, and measuring it gives the pinned bit. Loss +//! overwrites leakage. Neither event is a measurement outcome. + +use rand::RngExt; + +use ppvm_traits_2::{Clifford, LossChannel, ResetLossChannel}; + +use super::GeneralizedTableau; +use crate::VirtualState; + +impl GeneralizedTableau { + /// Collapse `q` by sampling `Z_q`, reset it to `|0⟩` and mark it lost. The + /// `X` goes straight to the tableau, as `q` may be leaked. + fn lose(&mut self, q: usize) { + if self.sample_z(q) { + self.tableau.x(q); + } + self.status.set_lost(q); + } +} + +/// With probability `p`, lose `q`. A lost qubit stays lost. +impl LossChannel for GeneralizedTableau { + fn loss_channel(&mut self, q: usize, p: f64) { + if !self.status.is_lost(q) && self.rng.random::() < p { + self.lose(q); + } + } +} + +/// Bring a lost qubit back as live; it is already in `|0⟩`. +impl ResetLossChannel for GeneralizedTableau { + fn reset_loss_channel(&mut self, q: usize) { + if self.status.is_lost(q) { + self.status.set_live(q); + } + } +} + +#[cfg(test)] +mod tests { + use std::f64::consts::PI; + + use ppvm_traits_2::{Measure, Pauli, Reset, RotationOne, RotationTwo, TGate}; + + use super::*; + use crate::{ActiveDormant, DenseState}; + + type Sim = GeneralizedTableau>; + + /// Whether `count` of `shots` is within 5σ of `rate`. + fn within(count: usize, shots: usize, rate: f64) -> bool { + let sigma = (rate * (1.0 - rate) / shots as f64).sqrt(); + ((count as f64 / shots as f64) - rate).abs() <= 5.0 * sigma + } + + /// A lost qubit reads `None` and no gate reaches it, including a CX from a + /// live `|1⟩` control. Clearing the flag finds it still in `|0⟩`. + #[test] + fn lost_qubit_is_skipped_and_reads_none() { + let mut s = Sim::new_with_seed(2, 0); + s.loss_channel(0, 1.0); + assert!(s.is_lost(0)); + s.x(1); + s.cnot(1, 0); + s.x(0); + s.h(0); + s.rx(0, 1.0); + s.t(0); + assert_eq!(s.measure(0), None); + assert_eq!(s.measure(1), Some(true)); + s.reset_loss_channel(0); + assert!(!s.is_lost(0)); + assert_eq!(s.measure(0), Some(false)); + } + + /// `exp(−iπ/2 X⊗X)` with the second qubit lost acts as `exp(−iπ/2 X)` on + /// the first and flips it; skipping it whole would leave `|0⟩`. + #[test] + fn rotation_with_a_lost_partner_acts_on_the_survivor() { + let mut s = Sim::new_with_seed(2, 0); + s.loss_channel(1, 1.0); + s.rotate_2(Pauli::X, Pauli::X, 0, 1, PI); + assert_eq!(s.measure(0), Some(true)); + assert_eq!(s.measure(1), None); + } + + /// Loss is an unrecorded measurement: losing half of a Bell pair leaves the + /// partner in a Z eigenstate, `|1⟩` half of the time. + #[test] + fn loss_collapses_the_partner() { + let shots = 2000; + let mut ones = 0; + for seed in 0..shots { + let mut s = Sim::new_with_seed(2, seed as u64); + s.h(0); + s.cnot(0, 1); + s.loss_channel(0, 1.0); + let [w0, w1] = s.z_weights(1); + assert!(w0 == 0.0 || w1 == 0.0, "partner is deterministic"); + ones += usize::from(s.measure(1).unwrap()); + } + assert!(within(ones, shots, 0.5), "{ones} of {shots}"); + } + + /// `loss_channel(p)` loses a qubit with probability `p`, whatever its state. + #[test] + fn loss_rate() { + let (p, shots) = (0.3, 20_000); + let mut lost = 0; + for seed in 0..shots { + let mut s = Sim::new_with_seed(1, seed as u64); + s.h(0); + s.loss_channel(0, p); + lost += usize::from(s.is_lost(0)); + } + assert!(within(lost, shots, p), "{lost} of {shots}"); + } + + /// A full reset brings a lost qubit back as live `|0⟩`, so gates reach it. + #[test] + fn reset_revives_a_lost_qubit() { + let mut s = Sim::new_with_seed(1, 0); + s.loss_channel(0, 1.0); + s.reset(0); + assert!(!s.is_lost(0)); + assert_eq!(s.measure(0), Some(false)); + s.x(0); + assert_eq!(s.measure(0), Some(true)); + } +} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs index bf8d78020..d5209fc79 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs @@ -8,15 +8,36 @@ use ppvm_traits_2::Measure; use super::GeneralizedTableau; use crate::VirtualState; -/// Measure `Z_q` and collapse; `Some(true)` for `|1⟩`. Every qubit is live -/// until loss is modelled, so the outcome is never `None`. -impl Measure for GeneralizedTableau { - fn measure(&mut self, q: usize) -> Option { +impl GeneralizedTableau { + /// Unnormalized weights of the outcomes `0` and `1` of `Z_q`. + pub(super) fn z_weights(&mut self, q: usize) -> [f64; 2] { + let mut view = self.tableau.virtual_view(); + self.state.z_weights(&mut view, q) + } + + /// Project onto outcome `m` of `Z_q`, which must have nonzero weight. + pub(super) fn collapse(&mut self, q: usize, m: bool) { let mut view = self.tableau.virtual_view(); - let [w0, w1] = self.state.z_weights(&mut view, q); - let m = self.rng.random::() * (w0 + w1) < w1; self.state.collapse(&mut view, q, m); - Some(m) + } + + /// Sample `Z_q` and collapse, whatever the status of `q`. + pub(super) fn sample_z(&mut self, q: usize) -> bool { + let [w0, w1] = self.z_weights(q); + let m = self.rng.random::() * (w0 + w1) < w1; + self.collapse(q, m); + m + } +} + +/// Measure `Z_q` and collapse; `Some(true)` for `|1⟩`. A lost qubit gives +/// `None`, a leaked one its pinned bit. +impl Measure for GeneralizedTableau { + fn measure(&mut self, q: usize) -> Option { + if self.status.is_lost(q) { + return None; + } + Some(self.sample_z(q)) } } diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs b/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs index 8c73b7de7..2bf348c3a 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs @@ -5,11 +5,14 @@ //! //! - `gates`: Clifford, T, rotation and `U3` gates, and reset. //! - `measure`: Z measurement. +//! - `loss`: loss and leakage channels. //! - `noise`: Pauli and depolarizing channels. mod gates; +mod loss; mod measure; mod noise; +mod status; use rand::SeedableRng; use rand::rngs::SmallRng; @@ -18,16 +21,19 @@ use num_complex::Complex64; use ppvm_traits_2::{Clifford, Pauli}; use crate::{InverseTableau, VirtualState}; +use status::Status; /// The state `|ψ⟩ = C |χ⟩`: the Clifford `C` as an [`InverseTableau`], and the /// virtual state `|χ⟩` in the representation `V`. /// /// Physical gates `C ← G·C` touch only the tableau. Measurements ask `V` for -/// the outcome weights, sample here, and let `V` collapse. +/// the outcome weights, sample here, and let `V` collapse. Lost and leaked +/// qubits are tracked here; the tableau and `V` never see them. #[derive(Clone, Debug)] pub struct GeneralizedTableau { tableau: InverseTableau, state: V, + status: Status, rng: SmallRng, } @@ -46,6 +52,7 @@ impl GeneralizedTableau { Self { tableau: InverseTableau::new(n), state: V::new(n), + status: Status::new(n), rng, } } @@ -62,8 +69,33 @@ impl GeneralizedTableau { &self.state } + /// Whether `q` is lost: gates skip it and measuring it gives `None`. + pub fn is_lost(&self, q: usize) -> bool { + self.status.is_lost(q) + } + + /// Whether `q` is leaked: gates skip it and measuring it gives the pinned bit. + pub fn is_leaked(&self, q: usize) -> bool { + self.status.is_leaked(q) + } + /// `|ψ⟩ ← (a·I + b·∏ Z_q) |ψ⟩` over `qubits`, which must be distinct. + /// Inactive qubits are dropped, as gates skip them. fn rotate_z(&mut self, qubits: &[usize], a: Complex64, b: Complex64) { + let live: Vec; + let qubits = if self.status.any() { + live = qubits + .iter() + .copied() + .filter(|&q| !self.status.is_inactive(q)) + .collect(); + if live.is_empty() { + return; + } + &live[..] + } else { + qubits + }; let mut view = self.tableau.virtual_view(); self.state.rotate(&mut view, qubits, a, b); } diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/status.rs b/crates/ppvm-tableau-2/src/generalized_tableau/status.rs new file mode 100644 index 000000000..1d3140099 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/status.rs @@ -0,0 +1,59 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! Which qubits are lost or leaked. Both are inactive: outside the +//! computational subspace, so gates skip them. + +use crate::QubitMask; + +/// Inactive and lost qubits as masks; leaked = inactive and not lost. +#[derive(Clone, Debug)] +pub(super) struct Status { + inactive: QubitMask, + lost: QubitMask, + /// Number of inactive qubits, checked first so a loss-free circuit pays + /// one predictable branch per gate. + count: usize, +} + +impl Status { + pub(super) fn new(n: usize) -> Self { + Self { + inactive: QubitMask::new(n), + lost: QubitMask::new(n), + count: 0, + } + } + + /// Whether any qubit is inactive. + pub(super) fn any(&self) -> bool { + self.count != 0 + } + + pub(super) fn is_inactive(&self, q: usize) -> bool { + self.any() && self.inactive.contains(q) + } + + pub(super) fn is_lost(&self, q: usize) -> bool { + self.any() && self.lost.contains(q) + } + + pub(super) fn is_leaked(&self, q: usize) -> bool { + self.is_inactive(q) && !self.lost.contains(q) + } + + pub(super) fn set_live(&mut self, q: usize) { + self.mark(q, false, false); + } + + /// Mark `q` lost; overwrites leakage. + pub(super) fn set_lost(&mut self, q: usize) { + self.mark(q, true, true); + } + + fn mark(&mut self, q: usize, inactive: bool, lost: bool) { + self.count = self.count - usize::from(self.inactive.contains(q)) + usize::from(inactive); + self.inactive.set(q, inactive); + self.lost.set(q, lost); + } +} From 303ef9854b4130ac3123637e8aa5004f2041196e Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 10:05:33 +0100 Subject: [PATCH 35/60] feat(ppvm-tableau-2): add leakage and leakage reduction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LeakageChannel pins a qubit to |0⟩ or |1⟩ with p0 / p1 and marks it leaked; ResetLeakageChannel returns it as live |0⟩. Qubit status is now a plain Vec instead of two masks and a count. Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/gates/clifford.rs | 6 +- .../src/generalized_tableau/gates/reset.rs | 5 +- .../src/generalized_tableau/loss.rs | 110 +++++++++++++++++- .../src/generalized_tableau/measure.rs | 2 +- .../src/generalized_tableau/mod.rs | 19 +-- .../src/generalized_tableau/status.rs | 64 ++-------- 6 files changed, 136 insertions(+), 70 deletions(-) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs index 150a33445..31c719731 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/clifford.rs @@ -12,19 +12,19 @@ impl ApplyGateRule for GeneralizedTableau { type B = bool; fn apply1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - if !self.status.is_inactive(q) { + if !self.is_inactive(q) { self.tableau.apply1(q, rule); } } fn apply2(&mut self, a: usize, b: usize, rule: impl Fn([bool; 4]) -> ([bool; 4], bool)) { - if !self.status.is_inactive(a) && !self.status.is_inactive(b) { + if !self.is_inactive(a) && !self.is_inactive(b) { self.tableau.apply2(a, b, rule); } } fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { - if !self.status.is_inactive(q) { + if !self.is_inactive(q) { self.tableau.apply_sign1(q, rule); } } diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs b/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs index bd52ea65b..5f821755d 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/gates/reset.rs @@ -4,13 +4,14 @@ use ppvm_traits_2::{Clifford, Reset, ResetState}; use super::super::GeneralizedTableau; +use super::super::status::QubitStatus; use crate::VirtualState; /// Measure, then flip the qubit if it was `|1⟩`. Lost and leaked qubits are /// left alone. impl ResetState for GeneralizedTableau { fn reset_state(&mut self, q: usize) { - if !self.status.is_inactive(q) && self.sample_z(q) { + if !self.is_inactive(q) && self.sample_z(q) { self.x(q); } } @@ -19,7 +20,7 @@ impl ResetState for GeneralizedTableau { /// A full reset: a lost or leaked qubit becomes live, then its state is reset. impl Reset for GeneralizedTableau { fn reset(&mut self, q: usize) { - self.status.set_live(q); + self.status[q] = QubitStatus::Live; self.reset_state(q); } } diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs index 1cdb1bca6..133cba3a3 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs @@ -8,9 +8,12 @@ use rand::RngExt; -use ppvm_traits_2::{Clifford, LossChannel, ResetLossChannel}; +use ppvm_traits_2::{ + Clifford, LeakageChannel, LossChannel, ResetLeakageChannel, ResetLossChannel, ResetState, +}; use super::GeneralizedTableau; +use super::status::QubitStatus; use crate::VirtualState; impl GeneralizedTableau { @@ -20,14 +23,14 @@ impl GeneralizedTableau { if self.sample_z(q) { self.tableau.x(q); } - self.status.set_lost(q); + self.status[q] = QubitStatus::Lost; } } /// With probability `p`, lose `q`. A lost qubit stays lost. impl LossChannel for GeneralizedTableau { fn loss_channel(&mut self, q: usize, p: f64) { - if !self.status.is_lost(q) && self.rng.random::() < p { + if !self.is_lost(q) && self.rng.random::() < p { self.lose(q); } } @@ -36,8 +39,38 @@ impl LossChannel for GeneralizedTableau { /// Bring a lost qubit back as live; it is already in `|0⟩`. impl ResetLossChannel for GeneralizedTableau { fn reset_loss_channel(&mut self, q: usize) { - if self.status.is_lost(q) { - self.status.set_live(q); + if self.is_lost(q) { + self.status[q] = QubitStatus::Live; + } + } +} + +/// With probability `p0` pin `q` to `|0⟩` and mark it leaked, with `p1` to +/// `|1⟩`. Lost and leaked qubits are left alone. +impl LeakageChannel for GeneralizedTableau { + fn leakage_channel(&mut self, q: usize, p0: f64, p1: f64) { + debug_assert!(p0 >= 0.0 && p1 >= 0.0 && p0 + p1 <= 1.0 + 1e-9); + if self.is_inactive(q) { + return; + } + let r = self.rng.random::(); + if r >= p0 + p1 { + return; + } + let pin = r >= p0; + if self.sample_z(q) != pin { + self.tableau.x(q); + } + self.status[q] = QubitStatus::Leaked; + } +} + +/// Bring a leaked qubit back as live `|0⟩`. Live and lost qubits are untouched. +impl ResetLeakageChannel for GeneralizedTableau { + fn reset_leakage_channel(&mut self, q: usize) { + if self.is_leaked(q) { + self.status[q] = QubitStatus::Live; + self.reset_state(q); } } } @@ -133,4 +166,71 @@ mod tests { s.x(0); assert_eq!(s.measure(0), Some(true)); } + + /// `leakage_channel(p0, p1)` pins to `|0⟩` with `p0` and `|1⟩` with `p1`, + /// whatever the state. Gates skip a leaked qubit, so it keeps reading the + /// pinned bit. + #[test] + fn leakage_pins_with_the_branch_probabilities() { + let (p0, p1, shots) = (0.2, 0.3, 20_000); + let (mut zeros, mut ones) = (0, 0); + for seed in 0..shots { + let mut s = Sim::new_with_seed(1, seed as u64); + s.h(0); + s.leakage_channel(0, p0, p1); + if s.is_leaked(0) { + let m = s.measure(0).unwrap(); + s.x(0); + s.h(0); + s.rx(0, 1.0); + assert_eq!(s.measure(0), Some(m), "gates skip a leaked qubit"); + if m { + ones += 1; + } else { + zeros += 1; + } + } + } + assert!(within(zeros, shots, p0), "{zeros} of {shots}"); + assert!(within(ones, shots, p1), "{ones} of {shots}"); + } + + /// Leakage reduction returns a leaked qubit as live `|0⟩`; it leaves a + /// lost qubit lost. + #[test] + fn reset_leakage_revives_only_leaked_qubits() { + let mut s = Sim::new_with_seed(2, 0); + s.leakage_channel(0, 0.0, 1.0); + assert_eq!(s.measure(0), Some(true)); + s.reset_leakage_channel(0); + assert!(!s.is_leaked(0)); + assert_eq!(s.measure(0), Some(false)); + s.x(0); + assert_eq!(s.measure(0), Some(true)); + s.loss_channel(1, 1.0); + s.reset_leakage_channel(1); + assert!(s.is_lost(1)); + } + + /// Loss overwrites leakage: the pinned `|1⟩` is reset to `|0⟩`. + #[test] + fn loss_overwrites_leakage() { + let mut s = Sim::new_with_seed(1, 0); + s.leakage_channel(0, 0.0, 1.0); + s.loss_channel(0, 1.0); + assert!(s.is_lost(0) && !s.is_leaked(0)); + assert_eq!(s.measure(0), None); + s.reset_loss_channel(0); + assert_eq!(s.measure(0), Some(false)); + } + + /// A full reset brings a leaked qubit back as live `|0⟩`. + #[test] + fn reset_revives_a_leaked_qubit() { + let mut s = Sim::new_with_seed(1, 0); + s.leakage_channel(0, 0.0, 1.0); + s.reset(0); + assert!(!s.is_leaked(0)); + assert_eq!(s.measure(0), Some(false)); + } } diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs index d5209fc79..145c165f6 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs @@ -34,7 +34,7 @@ impl GeneralizedTableau { /// `None`, a leaked one its pinned bit. impl Measure for GeneralizedTableau { fn measure(&mut self, q: usize) -> Option { - if self.status.is_lost(q) { + if self.is_lost(q) { return None; } Some(self.sample_z(q)) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs b/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs index 2bf348c3a..cc0fdbcf1 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs @@ -21,7 +21,7 @@ use num_complex::Complex64; use ppvm_traits_2::{Clifford, Pauli}; use crate::{InverseTableau, VirtualState}; -use status::Status; +use status::QubitStatus; /// The state `|ψ⟩ = C |χ⟩`: the Clifford `C` as an [`InverseTableau`], and the /// virtual state `|χ⟩` in the representation `V`. @@ -33,7 +33,7 @@ use status::Status; pub struct GeneralizedTableau { tableau: InverseTableau, state: V, - status: Status, + status: Vec, rng: SmallRng, } @@ -52,7 +52,7 @@ impl GeneralizedTableau { Self { tableau: InverseTableau::new(n), state: V::new(n), - status: Status::new(n), + status: vec![QubitStatus::Live; n], rng, } } @@ -71,23 +71,28 @@ impl GeneralizedTableau { /// Whether `q` is lost: gates skip it and measuring it gives `None`. pub fn is_lost(&self, q: usize) -> bool { - self.status.is_lost(q) + self.status[q] == QubitStatus::Lost } /// Whether `q` is leaked: gates skip it and measuring it gives the pinned bit. pub fn is_leaked(&self, q: usize) -> bool { - self.status.is_leaked(q) + self.status[q] == QubitStatus::Leaked + } + + /// Whether `q` is lost or leaked, so gates skip it. + fn is_inactive(&self, q: usize) -> bool { + self.status[q].is_inactive() } /// `|ψ⟩ ← (a·I + b·∏ Z_q) |ψ⟩` over `qubits`, which must be distinct. /// Inactive qubits are dropped, as gates skip them. fn rotate_z(&mut self, qubits: &[usize], a: Complex64, b: Complex64) { let live: Vec; - let qubits = if self.status.any() { + let qubits = if qubits.iter().any(|&q| self.is_inactive(q)) { live = qubits .iter() .copied() - .filter(|&q| !self.status.is_inactive(q)) + .filter(|&q| !self.is_inactive(q)) .collect(); if live.is_empty() { return; diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/status.rs b/crates/ppvm-tableau-2/src/generalized_tableau/status.rs index 1d3140099..8f8d43142 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/status.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/status.rs @@ -1,59 +1,19 @@ // SPDX-FileCopyrightText: 2026 The PPVM Authors // SPDX-License-Identifier: Apache-2.0 -//! Which qubits are lost or leaked. Both are inactive: outside the -//! computational subspace, so gates skip them. - -use crate::QubitMask; - -/// Inactive and lost qubits as masks; leaked = inactive and not lost. -#[derive(Clone, Debug)] -pub(super) struct Status { - inactive: QubitMask, - lost: QubitMask, - /// Number of inactive qubits, checked first so a loss-free circuit pays - /// one predictable branch per gate. - count: usize, +/// A qubit relative to the computational subspace. Lost and leaked qubits are +/// inactive: gates skip them. Measuring a lost qubit gives `None`, a leaked +/// one its pinned bit. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(super) enum QubitStatus { + #[default] + Live, + Lost, + Leaked, } -impl Status { - pub(super) fn new(n: usize) -> Self { - Self { - inactive: QubitMask::new(n), - lost: QubitMask::new(n), - count: 0, - } - } - - /// Whether any qubit is inactive. - pub(super) fn any(&self) -> bool { - self.count != 0 - } - - pub(super) fn is_inactive(&self, q: usize) -> bool { - self.any() && self.inactive.contains(q) - } - - pub(super) fn is_lost(&self, q: usize) -> bool { - self.any() && self.lost.contains(q) - } - - pub(super) fn is_leaked(&self, q: usize) -> bool { - self.is_inactive(q) && !self.lost.contains(q) - } - - pub(super) fn set_live(&mut self, q: usize) { - self.mark(q, false, false); - } - - /// Mark `q` lost; overwrites leakage. - pub(super) fn set_lost(&mut self, q: usize) { - self.mark(q, true, true); - } - - fn mark(&mut self, q: usize, inactive: bool, lost: bool) { - self.count = self.count - usize::from(self.inactive.contains(q)) + usize::from(inactive); - self.inactive.set(q, inactive); - self.lost.set(q, lost); +impl QubitStatus { + pub(super) fn is_inactive(self) -> bool { + self != QubitStatus::Live } } From 75123938803fc3139548b706da5ca8038b637bc9 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 10:09:17 +0100 Subject: [PATCH 36/60] refactor(ppvm-tableau-2): move GeneralizedTableau into data.rs mod.rs keeps the module list and the re-export. Fields and the helpers shared by the sibling modules become pub(super). Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/data.rs | 124 ++++++++++++++++++ .../src/generalized_tableau/mod.rs | 124 +----------------- 2 files changed, 127 insertions(+), 121 deletions(-) create mode 100644 crates/ppvm-tableau-2/src/generalized_tableau/data.rs diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/data.rs b/crates/ppvm-tableau-2/src/generalized_tableau/data.rs new file mode 100644 index 000000000..238e941c7 --- /dev/null +++ b/crates/ppvm-tableau-2/src/generalized_tableau/data.rs @@ -0,0 +1,124 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +use rand::SeedableRng; +use rand::rngs::SmallRng; + +use num_complex::Complex64; +use ppvm_traits_2::{Clifford, Pauli}; + +use super::status::QubitStatus; +use crate::{InverseTableau, VirtualState}; + +/// The state `|ψ⟩ = C |χ⟩`: the Clifford `C` as an [`InverseTableau`], and the +/// virtual state `|χ⟩` in the representation `V`. +/// +/// Physical gates `C ← G·C` touch only the tableau. Measurements ask `V` for +/// the outcome weights, sample here, and let `V` collapse. Lost and leaked +/// qubits are tracked here; the tableau and `V` never see them. +#[derive(Clone, Debug)] +pub struct GeneralizedTableau { + pub(super) tableau: InverseTableau, + pub(super) state: V, + pub(super) status: Vec, + pub(super) rng: SmallRng, +} + +impl GeneralizedTableau { + /// `|0…0⟩` on `n` qubits, with the RNG seeded from OS entropy. + pub fn new(n: usize) -> Self { + Self::with_rng(n, rand::make_rng()) + } + + /// Same as [`new`](Self::new), but seed the RNG deterministically. + pub fn new_with_seed(n: usize, seed: u64) -> Self { + Self::with_rng(n, SmallRng::seed_from_u64(seed)) + } + + fn with_rng(n: usize, rng: SmallRng) -> Self { + Self { + tableau: InverseTableau::new(n), + state: V::new(n), + status: vec![QubitStatus::Live; n], + rng, + } + } + + pub fn num_qubits(&self) -> usize { + self.tableau.num_qubits() + } + + pub fn tableau(&self) -> &InverseTableau { + &self.tableau + } + + pub fn state(&self) -> &V { + &self.state + } + + /// Whether `q` is lost: gates skip it and measuring it gives `None`. + pub fn is_lost(&self, q: usize) -> bool { + self.status[q] == QubitStatus::Lost + } + + /// Whether `q` is leaked: gates skip it and measuring it gives the pinned bit. + pub fn is_leaked(&self, q: usize) -> bool { + self.status[q] == QubitStatus::Leaked + } + + /// Whether `q` is lost or leaked, so gates skip it. + pub(super) fn is_inactive(&self, q: usize) -> bool { + self.status[q].is_inactive() + } + + /// `|ψ⟩ ← (a·I + b·∏ Z_q) |ψ⟩` over `qubits`, which must be distinct. + /// Inactive qubits are dropped, as gates skip them. + pub(super) fn rotate_z(&mut self, qubits: &[usize], a: Complex64, b: Complex64) { + let live: Vec; + let qubits = if qubits.iter().any(|&q| self.is_inactive(q)) { + live = qubits + .iter() + .copied() + .filter(|&q| !self.is_inactive(q)) + .collect(); + if live.is_empty() { + return; + } + &live[..] + } else { + qubits + }; + let mut view = self.tableau.virtual_view(); + self.state.rotate(&mut view, qubits, a, b); + } + + /// `exp(−i θ/2 ∏ Z_q) = cos(θ/2)·I − i sin(θ/2)·∏ Z_q`. + pub(super) fn rz_product(&mut self, qubits: &[usize], theta: f64) { + let (s, c) = (theta / 2.0).sin_cos(); + self.rotate_z(qubits, Complex64::new(c, 0.0), Complex64::new(0.0, -s)); + } + + /// Map `axis` on `q` to `Z`: apply `W` with `W·axis·W† = Z`. + pub(super) fn axis_to_z(&mut self, axis: Pauli, q: usize) { + match axis { + Pauli::X => self.h(q), + Pauli::Y => { + self.s_dag(q); + self.h(q); + } + Pauli::Z | Pauli::I => {} + } + } + + /// Undo [`axis_to_z`](Self::axis_to_z): apply `W†`. + pub(super) fn z_to_axis(&mut self, axis: Pauli, q: usize) { + match axis { + Pauli::X => self.h(q), + Pauli::Y => { + self.h(q); + self.s(q); + } + Pauli::Z | Pauli::I => {} + } + } +} diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs b/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs index cc0fdbcf1..4387626d0 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/mod.rs @@ -3,135 +3,17 @@ //! [`GeneralizedTableau`]: the simulator, `|ψ⟩ = C |χ⟩`. //! +//! - `data`: the struct, constructors and shared helpers. //! - `gates`: Clifford, T, rotation and `U3` gates, and reset. //! - `measure`: Z measurement. //! - `loss`: loss and leakage channels. //! - `noise`: Pauli and depolarizing channels. +mod data; mod gates; mod loss; mod measure; mod noise; mod status; -use rand::SeedableRng; -use rand::rngs::SmallRng; - -use num_complex::Complex64; -use ppvm_traits_2::{Clifford, Pauli}; - -use crate::{InverseTableau, VirtualState}; -use status::QubitStatus; - -/// The state `|ψ⟩ = C |χ⟩`: the Clifford `C` as an [`InverseTableau`], and the -/// virtual state `|χ⟩` in the representation `V`. -/// -/// Physical gates `C ← G·C` touch only the tableau. Measurements ask `V` for -/// the outcome weights, sample here, and let `V` collapse. Lost and leaked -/// qubits are tracked here; the tableau and `V` never see them. -#[derive(Clone, Debug)] -pub struct GeneralizedTableau { - tableau: InverseTableau, - state: V, - status: Vec, - rng: SmallRng, -} - -impl GeneralizedTableau { - /// `|0…0⟩` on `n` qubits, with the RNG seeded from OS entropy. - pub fn new(n: usize) -> Self { - Self::with_rng(n, rand::make_rng()) - } - - /// Same as [`new`](Self::new), but seed the RNG deterministically. - pub fn new_with_seed(n: usize, seed: u64) -> Self { - Self::with_rng(n, SmallRng::seed_from_u64(seed)) - } - - fn with_rng(n: usize, rng: SmallRng) -> Self { - Self { - tableau: InverseTableau::new(n), - state: V::new(n), - status: vec![QubitStatus::Live; n], - rng, - } - } - - pub fn num_qubits(&self) -> usize { - self.tableau.num_qubits() - } - - pub fn tableau(&self) -> &InverseTableau { - &self.tableau - } - - pub fn state(&self) -> &V { - &self.state - } - - /// Whether `q` is lost: gates skip it and measuring it gives `None`. - pub fn is_lost(&self, q: usize) -> bool { - self.status[q] == QubitStatus::Lost - } - - /// Whether `q` is leaked: gates skip it and measuring it gives the pinned bit. - pub fn is_leaked(&self, q: usize) -> bool { - self.status[q] == QubitStatus::Leaked - } - - /// Whether `q` is lost or leaked, so gates skip it. - fn is_inactive(&self, q: usize) -> bool { - self.status[q].is_inactive() - } - - /// `|ψ⟩ ← (a·I + b·∏ Z_q) |ψ⟩` over `qubits`, which must be distinct. - /// Inactive qubits are dropped, as gates skip them. - fn rotate_z(&mut self, qubits: &[usize], a: Complex64, b: Complex64) { - let live: Vec; - let qubits = if qubits.iter().any(|&q| self.is_inactive(q)) { - live = qubits - .iter() - .copied() - .filter(|&q| !self.is_inactive(q)) - .collect(); - if live.is_empty() { - return; - } - &live[..] - } else { - qubits - }; - let mut view = self.tableau.virtual_view(); - self.state.rotate(&mut view, qubits, a, b); - } - - /// `exp(−i θ/2 ∏ Z_q) = cos(θ/2)·I − i sin(θ/2)·∏ Z_q`. - fn rz_product(&mut self, qubits: &[usize], theta: f64) { - let (s, c) = (theta / 2.0).sin_cos(); - self.rotate_z(qubits, Complex64::new(c, 0.0), Complex64::new(0.0, -s)); - } - - /// Map `axis` on `q` to `Z`: apply `W` with `W·axis·W† = Z`. - fn axis_to_z(&mut self, axis: Pauli, q: usize) { - match axis { - Pauli::X => self.h(q), - Pauli::Y => { - self.s_dag(q); - self.h(q); - } - Pauli::Z | Pauli::I => {} - } - } - - /// Undo [`axis_to_z`](Self::axis_to_z): apply `W†`. - fn z_to_axis(&mut self, axis: Pauli, q: usize) { - match axis { - Pauli::X => self.h(q), - Pauli::Y => { - self.h(q); - self.s(q); - } - Pauli::Z | Pauli::I => {} - } - } -} +pub use data::GeneralizedTableau; From 249a2f424fe05fae980f5d0808e5a36e92d4ad74 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 10:12:12 +0100 Subject: [PATCH 37/60] feat(ppvm-tableau-2): add correlated loss p = [p_LL, p_LQ, p_LN]: with one of the pair already lost the other is lost with p_LN; otherwise one draw picks both, either named one, or none. Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/loss.rs | 60 ++++++++++++++++++- .../src/generalized_tableau/noise.rs | 2 +- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs index 133cba3a3..04fbc3b2c 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs @@ -9,7 +9,8 @@ use rand::RngExt; use ppvm_traits_2::{ - Clifford, LeakageChannel, LossChannel, ResetLeakageChannel, ResetLossChannel, ResetState, + Clifford, CorrelatedLossChannel, LeakageChannel, LossChannel, ResetLeakageChannel, + ResetLossChannel, ResetState, }; use super::GeneralizedTableau; @@ -75,6 +76,34 @@ impl ResetLeakageChannel for GeneralizedTableau { } } +/// `p = [p_LL, p_LQ, p_LN]` as the trait documents: if one of the pair is +/// already lost, the other is lost with `p[2]`; otherwise both with `p[0]` and +/// each named one alone with `p[1]`. A leaked partner counts as present. +impl CorrelatedLossChannel for GeneralizedTableau { + fn correlated_loss_channel(&mut self, a: usize, b: usize, p: [f64; 3]) { + debug_assert!( + p[0] >= 0.0 && p[1] >= 0.0 && p[0] + 2.0 * p[1] <= 1.0 + 1e-9, + "correlated loss needs p0, p1 >= 0 and p0 + 2·p1 <= 1; got {p:?}" + ); + debug_assert!((0.0..=1.0).contains(&p[2]), "p2 in [0, 1]; got {p:?}"); + if self.is_lost(a) { + self.loss_channel(b, p[2]); + } else if self.is_lost(b) { + self.loss_channel(a, p[2]); + } else { + match self.draw(&[p[0], p[1], p[1]]) { + Some(0) => { + self.lose(a); + self.lose(b); + } + Some(1) => self.lose(a), + Some(_) => self.lose(b), + None => {} + } + } + } +} + #[cfg(test)] mod tests { use std::f64::consts::PI; @@ -233,4 +262,33 @@ mod tests { assert!(!s.is_leaked(0)); assert_eq!(s.measure(0), Some(false)); } + + /// `p = [p_LL, p_LQ, p_LN]`: from two present qubits both are lost with + /// `p_LL`, each named one alone with `p_LQ`; with one already lost, the + /// other is lost with `p_LN`. + #[test] + fn correlated_loss_event_rates() { + let (p, shots) = ([0.1, 0.2, 0.5], 20_000); + let mut counts = [0; 4]; + let mut second = 0; + for seed in 0..shots { + let mut s = Sim::new_with_seed(2, seed as u64); + s.h(0); + s.cnot(0, 1); + s.correlated_loss_channel(0, 1, p); + counts[usize::from(s.is_lost(0)) | usize::from(s.is_lost(1)) << 1] += 1; + let mut t = Sim::new_with_seed(2, seed as u64); + t.loss_channel(0, 1.0); + t.correlated_loss_channel(0, 1, p); + second += usize::from(t.is_lost(1)); + } + let rates = [1.0 - p[0] - 2.0 * p[1], p[1], p[1], p[0]]; + for (k, (&count, rate)) in counts.iter().zip(rates).enumerate() { + assert!( + within(count, shots, rate), + "event {k:#b}: {count} of {shots}" + ); + } + assert!(within(second, shots, p[2]), "{second} of {shots}"); + } } diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs b/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs index 0db480ba4..8faa6067f 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs @@ -14,7 +14,7 @@ use crate::VirtualState; impl GeneralizedTableau { /// Draw event `k` with probability `probabilities[k]`, or `None` with the /// remaining probability. Uses one random number. - fn draw(&mut self, probabilities: &[f64]) -> Option { + pub(super) fn draw(&mut self, probabilities: &[f64]) -> Option { debug_assert!(probabilities.iter().all(|&p| p >= 0.0)); debug_assert!(probabilities.iter().sum::() <= 1.0 + 1e-9); let r = self.rng.random::(); From 3567165cadf0050d0cf333c8ecc5dcf4c382cca0 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 10:23:13 +0100 Subject: [PATCH 38/60] feat(ppvm-tableau-2): add asymmetric loss with its survival back-action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The qubit is lost from |m⟩ with probability p_m·P(m); otherwise the survival Kraus operator K₀ = a·I + b·Z_q is applied through the T-gate path and the state renormalized (#249). Checked against the Kraus operators on random three-qubit states. Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/loss.rs | 71 +++++++++++++-- crates/ppvm-tableau-2/tests/dense_matrix.rs | 91 ++++++++++++++++++- 2 files changed, 155 insertions(+), 7 deletions(-) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs index 04fbc3b2c..f39f424a2 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs @@ -6,11 +6,12 @@ //! `|0⟩` or `|1⟩`; gates skip it, and measuring it gives the pinned bit. Loss //! overwrites leakage. Neither event is a measurement outcome. +use num_complex::Complex64; use rand::RngExt; use ppvm_traits_2::{ - Clifford, CorrelatedLossChannel, LeakageChannel, LossChannel, ResetLeakageChannel, - ResetLossChannel, ResetState, + AsymmetricLossChannel, Clifford, CorrelatedLossChannel, LeakageChannel, LossChannel, + ResetLeakageChannel, ResetLossChannel, ResetState, }; use super::GeneralizedTableau; @@ -18,14 +19,20 @@ use super::status::QubitStatus; use crate::VirtualState; impl GeneralizedTableau { - /// Collapse `q` by sampling `Z_q`, reset it to `|0⟩` and mark it lost. The - /// `X` goes straight to the tableau, as `q` may be leaked. - fn lose(&mut self, q: usize) { - if self.sample_z(q) { + /// Reset `q`, collapsed onto outcome `m`, to `|0⟩` and mark it lost. The `X` + /// goes straight to the tableau, as `q` may be leaked. + fn mark_lost(&mut self, q: usize, m: bool) { + if m { self.tableau.x(q); } self.status[q] = QubitStatus::Lost; } + + /// Collapse `q` by sampling `Z_q`, then reset it and mark it lost. + fn lose(&mut self, q: usize) { + let m = self.sample_z(q); + self.mark_lost(q, m); + } } /// With probability `p`, lose `q`. A lost qubit stays lost. @@ -104,6 +111,36 @@ impl CorrelatedLossChannel for GeneralizedTableau { } } +/// State-dependent loss, with its back-action (issue #249). With Born weights +/// `P(m)` of `Z_q`, `q` is lost from `|m⟩` with probability `p_m·P(m)`; +/// otherwise the survival Kraus operator +/// `K₀ = √(1−p0)|0⟩⟨0| + √(1−p1)|1⟩⟨1| = a·I + b·Z_q` is applied and the state +/// renormalized. On a qubit in superposition `K₀` is non-Clifford and +/// activates an axis. +impl AsymmetricLossChannel for GeneralizedTableau { + fn asymmetric_loss_channel(&mut self, q: usize, p0: f64, p1: f64) { + debug_assert!((0.0..=1.0).contains(&p0) && (0.0..=1.0).contains(&p1)); + if self.is_lost(q) { + return; + } + let [w0, w1] = self.z_weights(q); + let pop = [w0 / (w0 + w1), w1 / (w0 + w1)]; + if let Some(k) = self.draw(&[p0 * pop[0], p1 * pop[1]]) { + // Lost from |m⟩: the outcome comes from the branch, not from `P(m)`. + let m = k == 1; + self.collapse(q, m); + self.mark_lost(q, m); + return; + } + // Scaled so the norm is unchanged: ‖K₀ψ‖² = Σ (1 − p_m)·P(m). + let scale = ((1.0 - p0) * pop[0] + (1.0 - p1) * pop[1]).sqrt().recip(); + let (s0, s1) = ((1.0 - p0).sqrt(), (1.0 - p1).sqrt()); + let a = Complex64::from((s0 + s1) / 2.0 * scale); + let b = Complex64::from((s0 - s1) / 2.0 * scale); + self.rotate_z(&[q], a, b); + } +} + #[cfg(test)] mod tests { use std::f64::consts::PI; @@ -291,4 +328,26 @@ mod tests { } assert!(within(second, shots, p[2]), "{second} of {shots}"); } + + /// The Bell-pair example of #249 with `p0 = 0, p1 = 1`: the qubit is lost + /// exactly from `|1⟩`, so a loss leaves the partner in `|1⟩`; survival + /// projects onto `|0⟩`, leaving `|00⟩`. Each happens half of the time. + #[test] + fn asymmetric_loss_on_a_bell_pair() { + let shots = 4000; + let mut lost = 0; + for seed in 0..shots { + let mut s = Sim::new_with_seed(2, seed as u64); + s.h(0); + s.cnot(0, 1); + s.asymmetric_loss_channel(0, 0.0, 1.0); + if s.is_lost(0) { + lost += 1; + assert_eq!(s.measure(1), Some(true), "lost from |1⟩"); + } else { + assert_eq!((s.measure(0), s.measure(1)), (Some(false), Some(false))); + } + } + assert!(within(lost, shots, 0.5), "{lost} of {shots}"); + } } diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs index aebe22f71..eee042d9a 100644 --- a/crates/ppvm-tableau-2/tests/dense_matrix.rs +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -10,7 +10,9 @@ use std::collections::{HashSet, VecDeque}; use num_complex::Complex64 as C; use ppvm_tableau_2::{ActiveDormant, DenseState, GeneralizedTableau, InverseTableau, VirtualState}; -use ppvm_traits_2::{Clifford, Measure, Pauli, RotXY, RotationOne, RotationTwo, TGate, U3Gate}; +use ppvm_traits_2::{ + AsymmetricLossChannel, Clifford, Measure, Pauli, RotXY, RotationOne, RotationTwo, TGate, U3Gate, +}; /// A dense `2^n × 2^n` matrix; basis index bit `q` is qubit `q`. #[derive(Clone)] @@ -632,3 +634,90 @@ fn rotations_match_the_state_vector() { assert_eq!(max_axes, n, "the circuits activate every qubit"); assert!(retired > 0, "measurements retire active axes"); } + +/// Asymmetric loss against its Kraus operators on random three-qubit states +/// from Cliffords and `T`: the survival branch equals `K₀|v⟩/‖K₀|v⟩‖` with +/// `K₀ = √(1−p0)|0⟩⟨0| + √(1−p1)|1⟩⟨1|`, a loss branch equals `|v⟩` projected +/// onto some `|m⟩` with `q` reset, `|χ⟩` stays normalized, and losses occur +/// with probability `p0·P(0) + p1·P(1)`. +#[test] +fn asymmetric_loss_matches_the_kraus_operators() { + let n = 3; + let gates = all_gates(n); + let paulis = Paulis::new(n); + let x = gate_matrices()[0]; + let w = C::from_polar(1.0, std::f64::consts::FRAC_PI_4); + let mut seed = 0x6a09_e667_f3bc_c908u64; + let mut next = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed as usize + }; + let (mut expected, mut variance, mut observed) = (0.0, 0.0, 0); + for round in 0..600 { + let (p0, p1) = [(0.15, 0.6), (0.3, 0.3), (0.0, 1.0)][round % 3]; + let mut s = Sim::new_with_seed(n, round as u64); + let mut v = vec![C::default(); 1 << n]; + v[0] = c(1.0, 0.0); + for _ in 0..12 { + let q = next() % n; + let m = if next() % 3 == 0 { + s.t(q); + single([[c(1.0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), w]], q, n) + } else { + let gate = gates[next() % gates.len()]; + apply(&mut s, gate); + matrix(gate, n) + }; + v = apply_to_state(&m, &v); + } + let q = next() % n; + let pop = [1.0 - probability_one(&v, q), probability_one(&v, q)]; + let p_lost = p0 * pop[0] + p1 * pop[1]; + (expected, variance) = (expected + p_lost, variance + p_lost * (1.0 - p_lost)); + + s.asymmetric_loss_channel(q, p0, p1); + + let candidates: Vec> = if s.is_lost(q) { + observed += 1; + [false, true] + .into_iter() + .filter(|&m| pop[usize::from(m)] > 1e-9) + .map(|m| { + let mut u = v.clone(); + project(&mut u, q, m); + if m { + u = apply_to_state(&single(x, q, n), &u); + } + u + }) + .collect() + } else { + let (s0, s1) = ((1.0 - p0).sqrt(), (1.0 - p1).sqrt()); + let k0 = single([[c(s0, 0.0), c(0.0, 0.0)], [c(0.0, 0.0), c(s1, 0.0)]], q, n); + let mut u = apply_to_state(&k0, &v); + let norm = u.iter().map(|a| a.norm_sqr()).sum::().sqrt(); + u.iter_mut().for_each(|a| *a /= norm); + vec![u] + }; + let chi = virtual_state(&s); + let norm: f64 = chi.iter().map(|a| a.norm_sqr()).sum(); + assert!((norm - 1.0).abs() < 1e-9, "round {round}: ‖χ‖² = {norm}"); + let matches = |u: &Vec| { + (0..1usize << (2 * n)).all(|code| { + let from_sim = state_expectation(&chi, &conjugated(s.tableau(), code, &paulis)); + (from_sim - state_expectation(u, &paulis.table[code])).norm() < 1e-9 + }) + }; + assert!( + candidates.iter().any(matches), + "round {round}: lost = {}, the state matches no Kraus branch", + s.is_lost(q) + ); + } + assert!( + (observed as f64 - expected).abs() < 5.0 * variance.sqrt(), + "{observed} losses, {expected:.1} expected" + ); +} From 3c63093a0c7958f1839bb372b3c1092aaa613b6c Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 10:28:23 +0100 Subject: [PATCH 39/60] perf(ppvm-tableau-2): skip the survival rotation when p0 == p1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit K₀ is then proportional to I, so the renormalized state is unchanged; rotating would activate an axis on a qubit in superposition for nothing. Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/loss.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs index f39f424a2..821d6f042 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/loss.rs @@ -132,6 +132,11 @@ impl AsymmetricLossChannel for GeneralizedTableau { self.mark_lost(q, m); return; } + // K₀ ∝ I: the renormalized state is unchanged, and rotating would + // activate an axis for nothing. + if p0 == p1 { + return; + } // Scaled so the norm is unchanged: ‖K₀ψ‖² = Σ (1 − p_m)·P(m). let scale = ((1.0 - p0) * pop[0] + (1.0 - p1) * pop[1]).sqrt().recip(); let (s0, s1) = ((1.0 - p0).sqrt(), (1.0 - p1).sqrt()); @@ -350,4 +355,17 @@ mod tests { } assert!(within(lost, shots, 0.5), "{lost} of {shots}"); } + + /// With `p0 = p1` the survival operator is proportional to `I`, so a + /// surviving qubit in superposition activates no axis. + #[test] + fn symmetric_asymmetric_loss_activates_no_axis() { + for seed in 0..50 { + let mut s = Sim::new_with_seed(2, seed); + s.h(0); + s.cnot(0, 1); + s.asymmetric_loss_channel(0, 0.3, 0.3); + assert!(s.state().active().is_empty(), "seed {seed}"); + } + } } From f58b3354e4317a3bc1e93c30994f4568e313376e Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 14:09:05 +0100 Subject: [PATCH 40/60] feat(ppvm-tableau-2): add Display for the simulator and its parts InverseTableau prints its rows as signed Pauli strings, DenseState its nonzero amplitudes, ActiveDormant the virtual qubit of each axis, and GeneralizedTableau combines them with the lost and leaked qubits. Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/src/display.rs | 83 ++++++++++++++++++++++++++++ crates/ppvm-tableau-2/src/lib.rs | 1 + 2 files changed, 84 insertions(+) create mode 100644 crates/ppvm-tableau-2/src/display.rs diff --git a/crates/ppvm-tableau-2/src/display.rs b/crates/ppvm-tableau-2/src/display.rs new file mode 100644 index 000000000..e14cf9e1e --- /dev/null +++ b/crates/ppvm-tableau-2/src/display.rs @@ -0,0 +1,83 @@ +// SPDX-FileCopyrightText: 2026 The PPVM Authors +// SPDX-License-Identifier: Apache-2.0 + +//! `Display` for the simulator and its parts, built from their public +//! accessors. + +use std::fmt::{Display, Formatter, Result}; + +use crate::{ + ActiveDormant, ActiveState, DenseState, GeneralizedTableau, InverseTableau, RowRef, + VirtualState, +}; + +/// A row as a signed Pauli string over `n` qubits, qubit 0 first: `-XZI`. +fn write_row(f: &mut Formatter<'_>, row: RowRef<'_>, n: usize) -> Result { + write!(f, "{}", if row.negative { '-' } else { '+' })?; + for q in 0..n { + let c = match (row.x_bit(q), row.z_bit(q)) { + (false, false) => 'I', + (true, false) => 'X', + (true, true) => 'Y', + (false, true) => 'Z', + }; + write!(f, "{c}")?; + } + Ok(()) +} + +/// One line per qubit: the rows `C⁻¹ X_q C` and `C⁻¹ Z_q C`. +impl Display for InverseTableau { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + let n = self.num_qubits(); + writeln!( + f, + "InverseTableau ({n} qubits, rows C⁻¹ X_q C and C⁻¹ Z_q C):" + )?; + for q in 0..n { + write!(f, " {q}: ")?; + write_row(f, self.ix(q), n)?; + write!(f, " ")?; + write_row(f, self.iz(q), n)?; + writeln!(f)?; + } + Ok(()) + } +} + +/// The nonzero amplitudes, axis 0 as the rightmost bit. +impl Display for DenseState { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + let k = self.num_axes(); + for (alpha, c) in self.amplitudes().iter().enumerate() { + if c.norm_sqr() > 0.0 { + writeln!(f, " |{alpha:0k$b}⟩: {c}")?; + } + } + Ok(()) + } +} + +/// The virtual qubit of each axis, then `φ` over the axes. +impl Display for ActiveDormant { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + writeln!( + f, + "Active virtual qubits (axis 0 first): {:?}", + self.active() + )?; + write!(f, "{}", self.phi()) + } +} + +impl Display for GeneralizedTableau { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + let n = self.num_qubits(); + writeln!(f, "GeneralizedTableau ({n} qubits):")?; + write!(f, "{}", self.tableau())?; + write!(f, "{}", self.state())?; + let lost: Vec = (0..n).filter(|&q| self.is_lost(q)).collect(); + let leaked: Vec = (0..n).filter(|&q| self.is_leaked(q)).collect(); + writeln!(f, "Lost: {lost:?}, leaked: {leaked:?}") + } +} diff --git a/crates/ppvm-tableau-2/src/lib.rs b/crates/ppvm-tableau-2/src/lib.rs index a4ab728b8..10cd2ed91 100644 --- a/crates/ppvm-tableau-2/src/lib.rs +++ b/crates/ppvm-tableau-2/src/lib.rs @@ -10,6 +10,7 @@ //! keeps amplitudes only for the active virtual qubits. //! - `generalized_tableau`: [`GeneralizedTableau`], the simulator. +mod display; mod generalized_tableau; mod inverse_tableau; mod virtual_state; From 6fee2eca1e83774013abae5a5a308c24c7807349 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 14:09:20 +0100 Subject: [PATCH 41/60] chore(ppvm-tableau-2): mark unpublished and drop the version It depends on the unpublished ppvm-traits-2 and ppvm-pauli-kernels, so it cannot be published; without a version it stays out of release-plz's workspace version bumps. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 2 +- crates/ppvm-tableau-2/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78c7e1776..de899fe54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2355,7 +2355,7 @@ dependencies = [ [[package]] name = "ppvm-tableau-2" -version = "0.3.0" +version = "0.0.0" dependencies = [ "num-complex", "ppvm-pauli-kernels", diff --git a/crates/ppvm-tableau-2/Cargo.toml b/crates/ppvm-tableau-2/Cargo.toml index 6a600ffef..5af4001dc 100644 --- a/crates/ppvm-tableau-2/Cargo.toml +++ b/crates/ppvm-tableau-2/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "ppvm-tableau-2" -version.workspace = true edition.workspace = true license.workspace = true repository.workspace = true +publish = false [dependencies] num-complex = "0.4.6" From c525666f8653ad9e5e5d4c15f4906632baf529ed Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 16:04:29 +0100 Subject: [PATCH 42/60] feat(ppvm-tableau-2): add fork to GeneralizedTableau Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/data.rs | 11 +++++++ .../src/generalized_tableau/measure.rs | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/data.rs b/crates/ppvm-tableau-2/src/generalized_tableau/data.rs index 238e941c7..3c45a8363 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/data.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/data.rs @@ -35,6 +35,17 @@ impl GeneralizedTableau { Self::with_rng(n, SmallRng::seed_from_u64(seed)) } + /// Clone the state but reseed the RNG, giving an independent simulation + /// branch. `Some(seed)` seeds it deterministically; `None` from OS entropy. + pub fn fork(&self, seed: Option) -> Self { + let mut forked = self.clone(); + forked.rng = match seed { + Some(s) => SmallRng::seed_from_u64(s), + None => rand::make_rng(), + }; + forked + } + fn with_rng(n: usize, rng: SmallRng) -> Self { Self { tableau: InverseTableau::new(n), diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs index 145c165f6..1254de047 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs @@ -85,6 +85,35 @@ mod tests { } } + /// A fork carries the collapsed state: after measuring one half of a Bell + /// pair, the fork's other half agrees with it. + #[test] + fn fork_keeps_the_collapsed_state() { + for seed in 0..50 { + let mut s = Sim::new_with_seed(2, seed); + s.h(0); + s.cnot(0, 1); + let a = s.measure(0); + let mut f = s.fork(Some(seed + 1000)); + assert_eq!(f.measure(1), a); + } + } + + /// Forks of a superposition sample independently: one seed repeats its + /// outcome, different seeds give both outcomes. + #[test] + fn forks_sample_independently() { + let mut s = Sim::new_with_seed(1, 0); + s.h(0); + let mut ones = 0; + for seed in 0..200 { + let m = s.fork(Some(seed)).measure(0); + assert_eq!(s.fork(Some(seed)).measure(0), m); + ones += usize::from(m.unwrap()); + } + assert!((60..140).contains(&ones), "{ones} of 200"); + } + /// `X` then measure is deterministic: `Z|1⟩ = −|1⟩`. #[test] fn x_flips_the_outcome() { From 8566728409b9c838b87052de495b56855091543e Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 16:25:47 +0100 Subject: [PATCH 43/60] perf(ppvm-tableau-2): split the fan-out on the pivot's X bit With the pivot's X bit a constant, the CX and CZ rules fold so rows without it touch no target. LLVM did this split only in some callers. Co-Authored-By: Claude Opus 5.5 --- .../src/inverse_tableau/virtual_gates.rs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs index 9ce3f9552..a84b04a70 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/virtual_gates.rs @@ -95,11 +95,22 @@ impl InverseTableau { self.for_each_row(|x, z| { let old_pivot = [x[pw] & pm != ZERO, z[pw] & pm != ZERO]; let (mut pivot_bits, mut flip) = (old_pivot, false); - for &t in cx_targets { - flip ^= target_step(x, z, t, &mut pivot_bits, heisenberg::cnot); - } - for &t in cz_targets { - flip ^= target_step(x, z, t, &mut pivot_bits, heisenberg::cz); + if old_pivot[0] { + pivot_bits[0] = true; + for &t in cx_targets { + flip ^= target_step(x, z, t, &mut pivot_bits, heisenberg::cnot); + } + for &t in cz_targets { + flip ^= target_step(x, z, t, &mut pivot_bits, heisenberg::cz); + } + } else { + pivot_bits[0] = false; + for &t in cx_targets { + flip ^= target_step(x, z, t, &mut pivot_bits, heisenberg::cnot); + } + for &t in cz_targets { + flip ^= target_step(x, z, t, &mut pivot_bits, heisenberg::cz); + } } let (new_pivot, f) = last(pivot_bits[0], pivot_bits[1]); x[pw] ^= pm & mask_if(new_pivot[0] != old_pivot[0]); From 9411c15f08954c3c6e3b33a25101e6c952be7587 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 16:25:51 +0100 Subject: [PATCH 44/60] perf(ppvm-tableau-2): measure on the tableau alone with no active qubits Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/src/generalized_tableau/measure.rs | 5 +++++ .../ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs | 4 ++++ crates/ppvm-tableau-2/src/virtual_state/mod.rs | 6 ++++++ crates/ppvm-tableau-2/tests/dense_matrix.rs | 7 ++++++- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs index 1254de047..36e50631f 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs @@ -23,6 +23,11 @@ impl GeneralizedTableau { /// Sample `Z_q` and collapse, whatever the status of `q`. pub(super) fn sample_z(&mut self, q: usize) -> bool { + // Hot path: a stabilizer state measures on the tableau alone. + if self.state.is_zero() { + let rng = &mut self.rng; + return self.tableau.measure(q, || rng.random()); + } let [w0, w1] = self.z_weights(q); let m = self.rng.random::() * (w0 + w1) < w1; self.collapse(q, m); diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs index 1ca2b6ed1..fa17c81cd 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs @@ -115,6 +115,10 @@ impl VirtualState for ActiveDormant { } } + fn is_zero(&self) -> bool { + self.active.is_empty() + } + /// Localization: if `O` has X/Y on dormant qubits, CX from the lowest of /// them (in `|0⟩`, so `χ` is unchanged) clears the others, and it becomes /// active. Then `O` acts as `±P` on the active axes and as `+1` elsewhere. diff --git a/crates/ppvm-tableau-2/src/virtual_state/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/mod.rs index e3554389b..baa2cebb8 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/mod.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/mod.rs @@ -27,6 +27,12 @@ pub trait VirtualState: Clone { /// `|0…0⟩` on `num_qubits` virtual qubits. fn new(num_qubits: usize) -> Self; + /// Whether `χ = |0…0⟩`, so `|ψ⟩ = C |0…0⟩` is a stabilizer state and the + /// caller may measure on the tableau alone. `false` is always correct. + fn is_zero(&self) -> bool { + false + } + /// `χ ← (a·I + b·O) χ` with `O = C⁻¹ (∏ Z_q) C` over `qubits`. fn rotate(&mut self, t: &mut VirtualView<'_>, qubits: &[usize], a: Complex64, b: Complex64); diff --git a/crates/ppvm-tableau-2/tests/dense_matrix.rs b/crates/ppvm-tableau-2/tests/dense_matrix.rs index eee042d9a..28b45f432 100644 --- a/crates/ppvm-tableau-2/tests/dense_matrix.rs +++ b/crates/ppvm-tableau-2/tests/dense_matrix.rs @@ -541,7 +541,7 @@ fn rotations_match_the_state_vector() { seed ^= seed << 17; seed as usize }; - let (mut max_axes, mut retired) = (0, 0); + let (mut max_axes, mut retired, mut stabilizer_measurements) = (0, 0, 0); for round in 0..40 { let mut s = Sim::new_with_seed(n, round); let mut v = vec![C::default(); 1 << n]; @@ -606,6 +606,7 @@ fn rotations_match_the_state_vector() { "round {round} step {step}: P(1) simulator {}, state {p1}", w1 / (w0 + w1) ); + stabilizer_measurements += usize::from(s.state().active().is_empty()); let outcome = s.measure(a).expect("no loss"); assert!( (if outcome { p1 } else { 1.0 - p1 }) > 1e-9, @@ -633,6 +634,10 @@ fn rotations_match_the_state_vector() { } assert_eq!(max_axes, n, "the circuits activate every qubit"); assert!(retired > 0, "measurements retire active axes"); + assert!( + stabilizer_measurements > 0, + "some measurements see a stabilizer state" + ); } /// Asymmetric loss against its Kraus operators on random three-qubit states From 44897843fd21d533a4083ee5e0dced4871b1d286 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 16:30:28 +0100 Subject: [PATCH 45/60] perf(ppvm-tableau-2): batch measurements on GeneralizedTableau With no active and no lost qubits, measure_many runs on the tableau's batched measurement; run_clifft keeps each Z-basis instruction together. Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/examples/run_clifft.rs | 75 +++++++++++++++---- .../src/generalized_tableau/measure.rs | 66 +++++++++++++++- 2 files changed, 127 insertions(+), 14 deletions(-) diff --git a/crates/ppvm-tableau-2/examples/run_clifft.rs b/crates/ppvm-tableau-2/examples/run_clifft.rs index 04f52d812..a56b086d7 100644 --- a/crates/ppvm-tableau-2/examples/run_clifft.rs +++ b/crates/ppvm-tableau-2/examples/run_clifft.rs @@ -50,6 +50,14 @@ enum Op { /// `X`, `Y`, `Z` with these probabilities. Pauli([f64; 3], usize), Depolarize2(f64, usize, usize), + /// `M`/`MR` on `measured[at..at + len]`, as one `measure_many`; flips and + /// resets as for `Measure`. + MeasureZ { + at: usize, + len: usize, + flip: f64, + reset: bool, + }, /// Recorded outcome flipped with probability `flip`; `reset` returns the /// qubit to the basis' `+1` state afterwards. Measure { @@ -108,8 +116,13 @@ fn flip_arg(args: &[f64]) -> f64 { } /// Flatten the program into ops, unrolling `REPEAT` blocks. MPP factors go to -/// `factors`. -fn lower(instructions: &[ExtendedInstruction], ops: &mut Vec, factors: &mut Vec) { +/// `factors`, Z-basis measurement targets to `measured`. +fn lower( + instructions: &[ExtendedInstruction], + ops: &mut Vec, + factors: &mut Vec, + measured: &mut Vec, +) { for instruction in instructions { match instruction { ExtendedInstruction::Gate(g) if g.targets.iter().any(|t| t.as_qubit().is_none()) => ops @@ -190,6 +203,16 @@ fn lower(instructions: &[ExtendedInstruction], ops: &mut Vec, factors: &mut other => panic!("unsupported measurement {other:?}"), }; let flip = flip_arg(&m.args); + if let Basis::Z = basis { + ops.push(Op::MeasureZ { + at: measured.len(), + len: m.targets.len(), + flip, + reset, + }); + measured.extend_from_slice(&m.targets); + continue; + } ops.extend(m.targets.iter().map(|&q| Op::Measure { basis, q, @@ -210,7 +233,7 @@ fn lower(instructions: &[ExtendedInstruction], ops: &mut Vec, factors: &mut } ExtendedInstruction::Repeat { count, body, .. } => { let mut block = Vec::new(); - lower(body, &mut block, factors); + lower(body, &mut block, factors, measured); for _ in 0..*count { ops.extend_from_slice(&block); } @@ -295,7 +318,13 @@ fn pauli_basis(axis: PauliAxis) -> Basis { /// Run `op`, appending measurement outcomes to `record`; `rng` flips recorded /// outcomes. -fn run(s: &mut Sim, op: Op, factors: &[PauliFactor], rng: &mut SmallRng, record: &mut Vec) { +fn run( + s: &mut Sim, + op: Op, + (factors, measured): (&[PauliFactor], &[usize]), + rng: &mut SmallRng, + record: &mut Vec, +) { let mut noisy = |m: bool, flip: f64| m ^ (flip > 0.0 && rng.random::() < flip); match op { Op::One(name, q) => apply_one(s, name, q), @@ -324,6 +353,21 @@ fn run(s: &mut Sim, op: Op, factors: &[PauliFactor], rng: &mut SmallRng, record: change_basis(s, basis, q, false); record.push(noisy(m, flip)); } + Op::MeasureZ { + at, + len, + flip, + reset, + } => { + let targets = &measured[at..at + len]; + for (m, &q) in s.measure_many(targets).into_iter().zip(targets) { + let m = m.expect("no loss"); + if reset && m { + s.x(q); + } + record.push(noisy(m, flip)); + } + } Op::Feedback { pauli, back, q } => { if record[record.len() - back] { match pauli { @@ -360,7 +404,7 @@ fn run(s: &mut Sim, op: Op, factors: &[PauliFactor], rng: &mut SmallRng, record: } } -fn qubits(op: &Op, factors: &[PauliFactor]) -> Vec { +fn qubits(op: &Op, (factors, measured): (&[PauliFactor], &[usize])) -> Vec { match *op { Op::Two(_, a, b) | Op::Depolarize2(_, a, b) => vec![a, b], Op::One(_, q) @@ -373,6 +417,7 @@ fn qubits(op: &Op, factors: &[PauliFactor]) -> Vec { | Op::Reset(_, q) | Op::Feedback { q, .. } => vec![q], Op::Mpp { at, len, .. } => factors[at..at + len].iter().map(|f| f.qubit).collect(), + Op::MeasureZ { at, len, .. } => measured[at..at + len].to_vec(), } } @@ -392,11 +437,12 @@ fn main() { let src = rewrite_dialect(&std::fs::read_to_string(&path).expect("read circuit")); let program = stim_parser::parse_extended(&src).unwrap_or_else(|e| panic!("parse error: {e:?}")); - let (mut ops, mut factors) = (Vec::new(), Vec::new()); - lower(&program.instructions, &mut ops, &mut factors); + let (mut ops, mut factors, mut measured) = (Vec::new(), Vec::new(), Vec::new()); + lower(&program.instructions, &mut ops, &mut factors, &mut measured); + let pools = (&factors[..], &measured[..]); let n = 1 + ops .iter() - .flat_map(|op| qubits(op, &factors)) + .flat_map(|op| qubits(op, pools)) .max() .expect("empty circuit"); @@ -409,13 +455,16 @@ fn main() { let mut s = Sim::new_with_seed(n, rng.random()); record.clear(); for &op in &ops { - run(&mut s, op, &factors, &mut rng, &mut record); + run(&mut s, op, pools, &mut rng, &mut record); let k = s.state().active().len(); max_k = max_k.max(k); - if matches!(op, Op::Measure { .. } | Op::Mpp { .. }) { - k_at_measurements += k; - measurements += 1; - } + let count = match op { + Op::Measure { .. } | Op::Mpp { .. } => 1, + Op::MeasureZ { len, .. } => len, + _ => 0, + }; + k_at_measurements += k * count; + measurements += count; } parity ^= record.iter().fold(false, |p, &m| p ^ m); if records_path.is_some() { diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs index 36e50631f..b66e7abcf 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs @@ -44,11 +44,24 @@ impl Measure for GeneralizedTableau { } Some(self.sample_z(q)) } + + /// With no active and no lost qubits, the whole batch runs on the + /// tableau's `measure_many`; otherwise one target at a time. + fn measure_many(&mut self, targets: &[usize]) -> Vec> { + if !self.state.is_zero() || targets.iter().any(|&q| self.is_lost(q)) { + return targets.iter().map(|&q| self.measure(q)).collect(); + } + let mut outcomes = vec![false; targets.len()]; + let rng = &mut self.rng; + self.tableau + .measure_many(targets, || rng.random(), &mut outcomes); + outcomes.into_iter().map(Some).collect() + } } #[cfg(test)] mod tests { - use ppvm_traits_2::{Clifford, Reset}; + use ppvm_traits_2::{Clifford, LossChannel, Reset, TGate}; use super::*; use crate::{ActiveDormant, DenseState}; @@ -70,6 +83,57 @@ mod tests { assert!((60..140).contains(&ones), "{ones} of 200"); } + /// `n` Bell pairs `(2i, 2i + 1)`, with `T` on each control when `t`. + fn bell_pairs(n: usize, t: bool, seed: u64) -> Sim { + let mut s = Sim::new_with_seed(2 * n, seed); + for i in 0..n { + s.h(2 * i); + if t { + s.t(2 * i); + } + s.cnot(2 * i, 2 * i + 1); + } + s + } + + /// Batched Bell pairs, enough for the tableau's column-major path: each + /// pair agrees, about half are `1`, and measuring again repeats. + #[test] + fn measure_many_on_bell_pairs() { + let n = 100; + let targets: Vec = (0..2 * n).collect(); + for seed in 0..5 { + let mut s = bell_pairs(n, false, seed); + let m: Vec = s.measure_many(&targets).into_iter().flatten().collect(); + assert_eq!(m.len(), 2 * n); + assert!(m.chunks(2).all(|p| p[0] == p[1])); + let ones = m.iter().step_by(2).filter(|&&b| b).count(); + assert!((25..75).contains(&ones), "{ones} of {n}"); + let again: Vec = s.measure_many(&targets).into_iter().flatten().collect(); + assert_eq!(again, m); + } + } + + /// The per-target fallback: active qubits (from `T`) and a lost qubit, + /// which gives `None` while its partner is still measured. + #[test] + fn measure_many_with_active_or_lost_qubits() { + let targets: Vec = (0..8).collect(); + for seed in 0..20 { + let mut s = bell_pairs(4, true, seed); + assert!(!s.state().is_zero()); + let m = s.measure_many(&targets); + assert!(m.chunks(2).all(|p| p[0].is_some() && p[0] == p[1])); + + let mut s = bell_pairs(4, false, seed); + s.loss_channel(0, 1.0); + let m = s.measure_many(&targets); + assert_eq!(m[0], None); + assert!(m[1].is_some()); + assert!(m[2..].chunks(2).all(|p| p[0].is_some() && p[0] == p[1])); + } + } + /// A measured qubit stays in its outcome, and reset leaves `|0⟩`. #[test] fn measurement_repeats_and_reset_clears() { From b11e9cccf1852499783b0356c33b9fe515ae8539 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Mon, 16 Nov 2026 16:42:28 +0100 Subject: [PATCH 46/60] chore(ppvm-tableau-2): drop the Stim driver source and its README Co-Authored-By: Claude Opus 5.5 --- .../benches/stim-compare/README.md | 49 ------------------- .../benches/stim-compare/common.py | 5 +- .../benches/stim-compare/stim_driver.cc | 38 -------------- .../benches/stim-compare/sweep.py | 2 +- 4 files changed, 3 insertions(+), 91 deletions(-) delete mode 100644 crates/ppvm-tableau-2/benches/stim-compare/README.md delete mode 100644 crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc diff --git a/crates/ppvm-tableau-2/benches/stim-compare/README.md b/crates/ppvm-tableau-2/benches/stim-compare/README.md deleted file mode 100644 index 0439d2d6e..000000000 --- a/crates/ppvm-tableau-2/benches/stim-compare/README.md +++ /dev/null @@ -1,49 +0,0 @@ -# ppvm-tableau-2 vs Stim - -Development benchmarks comparing the stabilizer core of `ppvm-tableau-2` -(`InverseTableau`, through the `run_stim` example) with official Stim's -`TableauSimulator`. Ported from PR 236's `bench/stim-compare`. Possibly not -shipped; nothing in the crate depends on this directory. - -```bash -# A reference: stim_driver.cc built against any Stim checkout (C++, -march=native). -cmake -S -B target/stim-build -DCMAKE_BUILD_TYPE=Release -cmake --build target/stim-build --target libstim -j -clang++ -O3 -std=c++20 -march=native -fno-strict-aliasing -I /src \ - crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc \ - target/stim-build/out/libstim.a -o target/stim-compare/stim_driver - -cd crates/ppvm-tableau-2/benches/stim-compare -uv run --no-project --with stim python sweep.py --reference stim=$PWD/../../../../target/stim-compare/stim_driver \ - --case surface:7 --case color:25 # or no --case for the full sweep -uv run --no-project --with stim python gates.py --reference stim=... --size 64 --size 2048 -``` - -Python Stim (`uv run --with stim`) only generates circuits. Outputs (circuits, -`sweep.csv`) go to `target/stim-compare/`. Each `--reference NAME=PATH` is a -binary with `run_stim`'s CLI (` `, printing -` µs/shot`); ratios are against the first. - -## What is measured - -- Time per shot of gates, measurements and resets on a fresh simulator per - shot. Noise is removed (`circuits.py`), so both sides run the same - noise-free circuit; noise sampling is the circuit runner's job. -- **sweep.py**: Stim-generated memory circuits (surface d = 3…31, repetition - d = 3…675, color d = 3…43, rounds = d), alternating tools over rounds, - median per tool. The color code's `C_XYZ`, `MX`, `MY` are rewritten into - gates ppvm's harness supports, for both tools. -- **gates.py**: a brickwork `CX` layer and a full `H` layer, timed at two - repeat counts; the difference gives ns per gate without per-shot overhead. - -## Caveats - -- Compare against a native C++ build, as above. PyPI Stim is a generic build - and its per-shot Python call adds a few µs, which flatters ppvm on small - circuits. -- **Instruction batching differs**: Stim merges consecutive same-gate - instructions and shares measurement setup per instruction; ppvm's - `stim-parser` does not merge (issue 291). See the design doc's Benchmarks - section. -- This compares the stabilizer core. Once `GeneralizedTableau` exists, its - `k = 0` overhead (active-mask checks) should be compared separately. diff --git a/crates/ppvm-tableau-2/benches/stim-compare/common.py b/crates/ppvm-tableau-2/benches/stim-compare/common.py index 59d30973d..98a7f0782 100644 --- a/crates/ppvm-tableau-2/benches/stim-compare/common.py +++ b/crates/ppvm-tableau-2/benches/stim-compare/common.py @@ -1,7 +1,6 @@ """Running the timing tools: a ppvm example (`run_stim`, `run_clifft`) and reference binaries with the same CLI (` `, printing -` µs/shot` or ` us/shot`), such as `stim_driver` built against a Stim -checkout.""" +` µs/shot` or ` us/shot`), such as a native Stim build.""" import pathlib import re @@ -34,7 +33,7 @@ def build_ppvm(example: str = "run_stim") -> None: def tools(references: list[str], example: str = "run_stim") -> dict[str, list[str]]: """`ppvm` (the example), then the `NAME=PATH` references in order.""" if not references: - sys.exit("give at least one --reference NAME=PATH (see README.md)") + sys.exit("give at least one --reference NAME=PATH") found = {"ppvm": [str(EXAMPLES / example)]} for reference in references: name, path = reference.split("=", 1) diff --git a/crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc b/crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc deleted file mode 100644 index 9a0a8d01c..000000000 --- a/crates/ppvm-tableau-2/benches/stim-compare/stim_driver.cc +++ /dev/null @@ -1,38 +0,0 @@ -// Times Stim's TableauSimulator on a circuit the way ppvm-tableau-2's run_stim -// runs it: noise removed, a fresh simulator per shot. Builds against any Stim -// checkout's libstim; see README.md. Usage: stim_driver [shots]. -#include -#include -#include -#include - -#include "stim.h" - -int main(int argc, char **argv) { - if (argc < 2) { - std::cerr << "usage: stim_driver [shots]\n"; - return 1; - } - std::ifstream in(argv[1]); - std::stringstream text; - text << in.rdbuf(); - stim::Circuit circuit = stim::Circuit(text.str()).without_noise(); - size_t shots = argc > 2 ? std::stoul(argv[2]) : 1000; - size_t n = circuit.count_qubits(); - - std::mt19937_64 rng(0x9e3779b97f4a7c15ULL); - bool parity = false; - auto start = std::chrono::steady_clock::now(); - for (size_t s = 0; s < shots; s++) { - stim::TableauSimulator sim(std::move(rng), n); - sim.safe_do_circuit(circuit); - if (!sim.measurement_record.storage.empty()) { - parity ^= sim.measurement_record.storage.back(); - } - rng = std::move(sim.rng); - } - double elapsed = std::chrono::duration(std::chrono::steady_clock::now() - start).count(); - std::cout << argv[1] << ": " << n << " qubits, " << shots << " shots, word width " - << stim::MAX_BITWORD_WIDTH << "\n" - << elapsed / shots * 1e6 << " us/shot (parity " << parity << ")\n"; -} diff --git a/crates/ppvm-tableau-2/benches/stim-compare/sweep.py b/crates/ppvm-tableau-2/benches/stim-compare/sweep.py index f9dcadfa4..024e1e017 100644 --- a/crates/ppvm-tableau-2/benches/stim-compare/sweep.py +++ b/crates/ppvm-tableau-2/benches/stim-compare/sweep.py @@ -7,7 +7,7 @@ [--case surface:7]... [--family surface] [--max-qubits N] [--rounds 3] Python Stim only generates the circuits; timing is against the references -(e.g. `stim_driver`, see README.md). Times are per shot, noise-free, +(e.g. a native Stim build). Times are per shot, noise-free, gates/measurements/resets only; see the design doc's Benchmarks section for how instruction batching differs between the tools. """ From 63860be565fab1ed0aecea355532bc1ac3944568 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:02:47 +0100 Subject: [PATCH 47/60] Remove stim files --- .../benches/circuits/.gitignore | 1 + .../ppvm-tableau-2/benches/circuits/base.stim | 1 - .../ppvm-tableau-2/benches/circuits/cx.stim | 257 -- .../benches/circuits/gates.stim | 4097 ----------------- crates/ppvm-tableau-2/benches/circuits/gen.py | 3 +- .../benches/circuits/mr_batched.stim | 100 - .../benches/circuits/mr_split.stim | 1270 ----- .../benches/circuits/mr_unfused.stim | 2450 ---------- 8 files changed, 3 insertions(+), 8176 deletions(-) create mode 100644 crates/ppvm-tableau-2/benches/circuits/.gitignore delete mode 100644 crates/ppvm-tableau-2/benches/circuits/base.stim delete mode 100644 crates/ppvm-tableau-2/benches/circuits/cx.stim delete mode 100644 crates/ppvm-tableau-2/benches/circuits/gates.stim delete mode 100644 crates/ppvm-tableau-2/benches/circuits/mr_batched.stim delete mode 100644 crates/ppvm-tableau-2/benches/circuits/mr_split.stim delete mode 100644 crates/ppvm-tableau-2/benches/circuits/mr_unfused.stim diff --git a/crates/ppvm-tableau-2/benches/circuits/.gitignore b/crates/ppvm-tableau-2/benches/circuits/.gitignore new file mode 100644 index 000000000..11f7e0b31 --- /dev/null +++ b/crates/ppvm-tableau-2/benches/circuits/.gitignore @@ -0,0 +1 @@ +*.stim diff --git a/crates/ppvm-tableau-2/benches/circuits/base.stim b/crates/ppvm-tableau-2/benches/circuits/base.stim deleted file mode 100644 index 76a4ec815..000000000 --- a/crates/ppvm-tableau-2/benches/circuits/base.stim +++ /dev/null @@ -1 +0,0 @@ -H 0 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 diff --git a/crates/ppvm-tableau-2/benches/circuits/cx.stim b/crates/ppvm-tableau-2/benches/circuits/cx.stim deleted file mode 100644 index 009a35aca..000000000 --- a/crates/ppvm-tableau-2/benches/circuits/cx.stim +++ /dev/null @@ -1,257 +0,0 @@ -H 0 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 -CX 17 72 108 102 97 8 32 15 63 113 57 60 83 48 100 26 12 62 3 49 55 77 0 89 107 34 29 75 13 40 99 2 -CX 3 83 69 1 112 48 87 27 54 92 117 67 28 97 56 63 70 29 44 100 86 105 58 37 2 53 71 82 12 23 80 94 -CX 15 95 42 114 92 91 64 54 111 106 85 24 38 36 75 63 109 50 103 4 61 31 116 51 53 107 22 46 70 86 47 11 -CX 56 84 65 13 99 20 66 107 50 47 62 93 3 60 5 39 90 78 75 74 109 82 21 95 64 29 1 25 69 70 92 51 -CX 65 44 108 73 45 58 34 84 70 77 93 0 49 100 94 117 16 66 99 71 26 54 7 61 46 72 109 25 64 52 62 113 -CX 53 44 0 68 69 79 100 78 42 58 76 3 102 29 81 22 70 74 23 11 101 32 4 86 9 10 2 57 1 35 31 34 -CX 14 102 79 23 44 37 8 21 20 32 67 110 84 34 82 91 112 58 89 41 63 60 117 3 39 49 43 53 24 33 13 108 -CX 115 93 65 26 77 55 104 2 28 110 50 18 4 92 20 57 90 64 86 54 69 109 80 88 66 102 96 67 83 3 107 99 -CX 73 102 41 84 80 54 7 94 38 16 27 6 39 9 104 105 109 95 20 53 72 32 108 1 71 4 75 107 97 58 21 79 -CX 65 4 48 25 44 12 26 73 86 55 75 24 63 13 85 49 37 64 105 2 41 78 51 36 98 20 114 97 72 17 43 54 -CX 27 34 86 12 107 48 70 44 113 87 68 62 98 109 30 8 92 5 10 17 21 97 104 117 116 42 76 64 32 47 43 108 -CX 14 37 30 111 77 99 91 62 17 74 70 98 13 41 5 52 9 48 18 16 43 117 78 75 100 101 73 107 28 72 10 34 -CX 46 114 37 72 68 14 58 35 13 100 5 105 115 1 78 85 104 11 52 112 107 24 30 75 53 20 98 57 21 87 95 92 -CX 95 108 13 55 48 103 69 104 37 70 32 91 61 40 12 26 83 110 5 3 1 109 92 76 100 57 50 93 51 8 88 90 -CX 76 58 14 32 27 100 79 99 69 88 60 84 45 33 23 109 26 39 25 31 46 10 35 11 57 94 83 73 82 43 29 49 -CX 39 5 41 23 40 101 108 74 38 31 42 12 69 78 110 76 11 111 28 2 100 51 9 34 70 95 92 98 81 1 37 45 -CX 63 60 110 109 19 12 64 99 101 41 9 65 85 22 104 115 113 18 40 39 13 90 106 77 37 16 26 100 69 4 102 79 -CX 102 86 70 107 95 88 26 22 38 55 68 20 6 91 85 31 32 99 8 87 57 108 115 101 69 56 114 58 1 50 43 21 -CX 33 62 3 101 82 53 73 2 7 88 45 74 17 75 16 105 117 35 50 72 51 22 78 11 29 116 0 96 67 40 64 83 -CX 117 56 87 81 93 28 30 40 63 115 61 112 91 52 43 71 78 113 83 35 82 106 6 9 65 97 47 20 100 26 39 38 -CX 88 38 108 70 47 21 89 111 94 59 76 10 15 77 65 73 48 22 19 32 54 27 72 92 6 63 87 50 81 44 49 103 -CX 108 21 69 93 5 67 11 103 32 80 12 34 94 10 17 99 78 84 87 89 104 56 30 48 55 50 116 41 96 16 79 62 -CX 27 15 55 76 68 52 116 84 37 35 31 48 95 71 0 24 67 56 74 2 3 80 77 107 33 26 22 36 18 69 25 34 -CX 39 74 96 32 106 87 57 101 103 21 69 45 62 53 15 98 26 73 49 110 36 13 3 109 72 1 107 37 86 83 17 9 -CX 64 47 73 103 39 55 117 86 45 97 67 41 0 15 56 91 57 44 113 69 51 43 93 87 115 63 14 82 48 89 26 71 -CX 0 35 81 76 92 112 94 106 93 65 25 59 114 66 52 95 91 39 89 21 57 79 85 67 107 46 111 117 86 49 74 54 -CX 51 43 110 79 74 93 89 95 8 63 115 31 81 83 37 80 2 52 92 102 19 105 50 34 22 9 77 1 44 33 100 69 -CX 38 19 59 106 33 62 21 115 65 5 34 109 12 95 75 54 8 45 101 84 56 2 111 64 90 20 88 11 51 81 35 77 -CX 38 26 67 116 30 42 34 8 9 89 106 66 84 47 59 65 71 94 6 21 117 83 100 91 101 111 45 78 29 50 93 51 -CX 22 61 101 33 110 78 42 91 28 114 112 90 31 84 3 79 51 40 55 97 105 34 24 9 80 21 74 56 113 18 77 108 -CX 58 67 20 17 99 114 91 56 46 39 96 51 30 14 111 26 103 87 108 8 13 29 50 41 63 12 23 5 7 76 2 27 -CX 87 4 63 90 67 104 92 78 56 43 84 35 15 110 88 22 12 28 51 29 115 57 48 21 98 30 36 59 70 74 49 27 -CX 57 91 33 42 63 75 14 27 10 5 1 102 0 61 40 49 74 36 25 51 20 82 19 3 107 106 18 85 69 7 72 48 -CX 32 16 10 59 83 107 38 1 4 68 7 67 116 5 35 99 15 55 11 24 3 63 81 105 103 87 98 84 57 49 42 80 -CX 34 33 82 81 31 113 7 75 100 110 22 44 54 77 89 71 114 66 111 45 70 52 68 25 91 95 105 84 8 117 78 9 -CX 32 22 12 19 7 26 109 54 111 5 6 81 11 104 65 60 64 47 115 40 108 16 68 4 56 85 96 50 57 3 67 34 -CX 11 32 102 41 10 38 4 110 49 7 93 33 40 94 16 106 101 48 14 86 112 12 54 31 64 71 26 42 43 65 50 74 -CX 61 13 16 83 104 57 67 71 92 108 106 74 89 66 68 3 37 95 20 25 47 49 113 41 12 52 44 115 73 8 5 38 -CX 104 102 83 68 40 53 38 113 45 34 41 95 106 66 64 1 67 15 19 110 93 107 96 73 8 57 35 61 58 46 48 10 -CX 74 102 7 17 6 67 62 73 109 32 100 31 89 110 95 43 46 82 47 51 39 59 76 116 68 64 21 3 18 108 87 28 -CX 72 17 14 23 98 52 93 79 6 103 12 69 87 34 91 13 26 33 8 80 73 67 82 10 9 27 95 22 65 55 2 75 -CX 47 115 108 62 90 103 36 28 25 76 63 30 54 57 86 46 69 24 61 92 9 32 52 109 1 68 48 65 114 97 51 78 -CX 112 65 101 74 114 54 5 45 108 58 0 24 38 89 88 82 107 69 15 105 116 95 40 100 102 73 70 36 67 52 94 66 -CX 52 77 80 74 39 57 38 16 64 56 75 17 70 98 20 32 81 1 54 94 84 72 4 47 53 51 36 97 85 2 11 87 -CX 108 0 49 34 59 114 101 100 47 81 95 61 98 43 115 58 14 106 45 18 53 105 2 22 33 109 16 75 36 52 93 65 -CX 36 94 53 88 35 55 42 99 62 27 91 106 109 51 107 54 11 8 16 26 19 29 93 3 13 32 97 61 12 104 83 23 -CX 106 0 11 54 78 6 70 27 68 114 44 112 83 13 94 111 86 53 85 103 15 33 87 35 22 61 90 117 110 101 82 115 -CX 110 49 15 85 57 37 87 65 63 50 14 77 61 13 19 116 78 89 25 21 66 32 53 68 36 109 81 69 27 79 43 62 -CX 13 1 96 93 84 44 90 34 7 69 80 56 38 97 12 29 65 35 110 111 31 52 18 16 32 24 115 71 107 76 109 68 -CX 106 77 65 19 52 34 35 61 89 39 112 62 27 63 47 76 60 30 43 22 116 23 94 74 88 57 68 114 7 64 41 67 -CX 88 17 82 97 103 27 40 79 63 61 42 15 16 116 89 32 28 11 81 68 113 6 72 22 87 14 101 95 25 64 90 84 -CX 113 39 54 41 0 99 2 105 116 110 78 28 10 95 106 35 87 80 43 34 76 92 66 48 111 15 42 44 17 14 32 18 -CX 87 73 5 44 9 11 92 13 38 40 31 34 67 6 46 3 10 17 51 47 111 81 88 30 12 86 42 35 1 65 41 14 -CX 45 102 101 82 92 107 16 77 34 51 11 86 73 79 113 67 60 72 53 68 50 38 28 80 96 70 17 6 76 65 14 22 -CX 30 27 114 55 35 69 2 32 68 34 67 33 60 16 51 90 13 95 47 8 83 112 46 96 71 92 64 87 74 3 79 39 -CX 57 87 16 19 9 74 18 86 105 27 61 102 98 42 46 37 20 114 48 56 51 15 76 111 34 106 85 116 81 77 1 68 -CX 1 104 82 16 48 95 71 12 58 3 99 55 76 86 54 35 47 52 51 77 59 6 110 60 4 115 90 89 0 5 14 75 -CX 17 67 65 97 45 70 34 100 72 83 113 102 60 104 89 31 79 30 13 71 107 20 14 5 90 40 54 44 32 84 80 7 -CX 78 55 53 48 45 37 96 104 43 56 102 89 30 81 117 66 18 7 109 86 14 65 22 69 82 80 62 99 15 74 2 61 -CX 114 26 49 80 106 22 50 91 29 12 31 42 113 84 107 100 86 59 95 60 47 63 83 104 92 24 55 56 51 69 15 73 -CX 62 34 107 16 19 1 48 53 13 102 3 83 9 23 58 98 111 85 64 36 113 97 67 109 32 2 59 50 81 29 68 90 -CX 0 69 102 31 54 20 84 22 43 111 30 9 99 68 71 112 110 48 74 2 65 27 113 107 5 66 24 89 64 88 78 83 -CX 68 9 31 50 99 59 15 72 82 6 49 11 71 12 109 61 5 66 30 1 2 39 112 35 92 53 21 76 17 105 40 117 -CX 81 57 64 102 53 70 21 89 50 110 49 103 25 63 35 46 19 33 72 106 22 92 79 10 93 114 43 18 100 32 88 44 -CX 49 35 72 59 1 19 16 32 28 25 9 102 74 68 79 108 69 54 91 30 73 17 70 58 50 99 106 10 80 107 112 85 -CX 7 3 95 51 48 53 87 17 75 76 16 86 68 69 9 30 113 110 36 25 84 92 50 45 22 28 38 90 18 44 62 105 -CX 37 11 65 105 38 26 90 59 2 117 102 103 79 75 13 78 47 96 56 32 114 7 6 40 20 16 80 106 14 55 81 104 -CX 31 95 26 64 114 50 15 90 27 104 49 84 66 17 91 74 32 92 0 103 111 25 72 48 106 61 69 78 29 34 4 81 -CX 21 85 116 70 64 29 109 52 35 98 84 53 51 34 63 12 115 16 23 71 2 58 5 62 27 50 68 43 31 102 9 86 -CX 95 5 108 54 106 56 24 22 76 64 111 65 49 66 46 25 29 103 84 75 96 8 43 6 58 116 78 110 18 36 60 92 -CX 74 64 8 110 106 72 50 11 51 101 65 73 82 38 111 34 45 60 6 70 61 2 54 104 75 40 19 76 93 71 35 115 -CX 110 77 100 101 99 46 53 50 66 114 3 73 74 14 4 106 67 1 12 42 43 47 70 103 81 96 105 9 62 93 10 69 -CX 57 42 64 103 69 0 20 41 46 27 18 74 107 75 13 51 40 65 53 109 43 33 77 47 4 91 8 80 31 96 50 70 -CX 36 73 100 79 10 9 90 21 34 52 113 16 117 70 92 82 33 30 26 12 35 103 61 6 65 38 99 69 112 104 40 43 -CX 37 110 66 17 4 56 104 46 102 95 113 3 40 53 108 20 71 5 90 75 89 84 80 67 54 23 25 29 14 98 16 88 -CX 64 15 92 34 58 25 100 7 46 113 42 78 115 45 28 81 1 101 62 4 21 32 70 5 111 29 10 67 22 98 90 112 -CX 26 56 36 31 62 64 47 41 50 83 9 24 76 23 106 87 79 38 74 54 78 60 46 2 113 94 13 84 80 73 85 101 -CX 108 55 105 90 74 43 112 9 82 53 24 89 65 102 63 77 72 84 70 64 61 76 87 94 73 57 104 60 21 34 86 67 -CX 38 72 97 103 50 77 69 33 32 39 1 112 96 5 100 58 102 45 29 65 56 26 89 60 42 95 80 18 49 55 6 82 -CX 14 45 111 100 1 32 96 69 94 6 39 48 113 41 43 107 75 114 108 26 91 10 42 15 85 82 8 16 88 37 52 77 -CX 43 29 3 82 89 88 23 96 98 110 64 95 73 114 46 38 37 48 53 67 59 9 25 52 116 77 5 79 30 80 28 31 -CX 91 50 48 26 79 19 92 38 95 111 46 0 117 90 87 39 56 63 21 86 18 3 47 55 70 43 65 62 40 77 14 74 -CX 82 37 103 70 84 35 54 1 107 39 96 11 81 62 14 64 28 77 95 117 99 33 55 47 29 6 13 76 65 89 88 20 -CX 16 37 6 113 8 27 0 86 7 54 93 91 2 114 109 1 4 68 43 42 105 78 102 71 112 60 25 34 116 74 70 66 -CX 32 113 29 23 26 50 7 30 71 89 57 4 42 41 52 15 2 72 114 64 81 11 99 27 28 22 38 12 111 40 18 8 -CX 106 56 19 29 5 95 36 44 7 75 11 116 25 101 114 85 23 15 109 105 6 112 93 14 107 28 111 32 67 54 31 4 -CX 92 32 97 24 41 44 45 58 115 84 78 48 86 49 11 54 31 62 43 22 77 83 14 30 9 55 35 68 38 42 47 52 -CX 58 46 45 40 50 60 65 2 47 16 38 21 107 72 108 70 91 93 19 106 117 82 80 99 17 20 10 78 32 30 115 96 -CX 40 21 35 109 60 39 9 54 19 70 45 57 13 114 87 117 8 103 23 61 68 4 5 92 24 83 107 46 64 91 89 80 -CX 103 85 47 43 83 15 23 48 4 34 78 91 102 26 7 31 38 41 72 51 105 46 6 29 37 89 99 0 25 12 17 28 -CX 47 64 113 34 18 20 29 9 39 73 65 107 69 76 105 101 55 56 74 106 60 23 98 45 25 102 110 35 26 111 115 17 -CX 98 26 2 20 62 46 23 6 100 112 10 78 30 86 89 27 11 56 82 83 25 77 43 21 73 88 85 90 115 102 40 61 -CX 70 4 115 6 109 46 63 71 44 17 62 8 65 40 85 94 72 103 39 77 104 73 11 61 43 53 9 33 106 84 82 41 -CX 2 23 41 28 40 33 106 104 32 39 62 53 1 37 20 81 110 6 14 55 98 78 27 35 45 83 72 63 73 36 77 109 -CX 86 22 41 18 45 12 50 113 66 95 72 89 24 111 57 19 61 106 31 4 93 81 99 10 9 98 109 64 60 107 101 115 -CX 117 66 101 21 72 90 63 50 1 49 70 92 71 94 57 114 75 115 47 6 106 99 45 56 30 88 82 84 69 38 11 104 -CX 97 111 45 24 20 17 56 106 5 46 72 43 103 22 107 62 61 1 73 29 78 7 116 83 113 65 26 51 59 15 40 33 -CX 17 21 42 16 23 103 94 79 67 39 29 70 90 54 59 58 65 106 108 116 66 78 64 99 75 26 36 86 19 87 0 43 -CX 15 54 48 91 83 65 94 22 79 56 57 104 68 108 46 26 6 10 92 13 12 105 49 17 106 50 23 60 107 66 75 4 -CX 75 24 117 57 62 49 37 44 99 96 22 76 34 23 109 3 71 7 85 8 70 29 114 40 56 42 13 112 6 59 35 52 -CX 59 42 64 12 21 51 69 110 54 78 94 61 115 19 40 18 44 17 108 24 28 27 58 83 104 13 89 92 109 6 95 93 -CX 47 71 41 35 50 1 49 62 91 57 38 94 109 107 82 74 111 40 96 37 22 12 110 23 108 19 58 13 68 15 89 100 -CX 40 105 63 86 71 81 43 92 74 117 113 75 102 59 41 62 88 50 68 27 21 30 99 25 76 31 6 103 79 7 42 53 -CX 3 44 46 115 76 113 84 52 26 100 36 28 40 50 89 49 85 99 22 1 102 83 116 77 78 106 29 8 93 105 97 109 -CX 90 111 37 12 55 0 103 44 11 116 52 19 14 68 101 93 22 96 43 18 48 113 41 69 81 89 66 35 26 24 20 21 -CX 68 20 18 15 56 74 66 16 55 17 42 77 101 93 89 87 40 76 108 2 45 22 28 30 88 63 75 62 4 83 11 99 -CX 68 60 72 18 26 46 90 17 35 94 44 8 49 116 3 67 59 25 92 30 113 88 0 99 89 38 5 34 66 24 9 13 -CX 104 102 98 109 14 110 51 42 13 57 91 73 66 90 83 61 85 35 18 55 47 82 44 49 52 115 97 70 26 25 8 99 -CX 30 117 2 116 85 50 58 100 78 56 72 12 6 22 67 0 5 55 35 53 16 114 88 84 47 98 43 74 105 64 111 97 -CX 88 66 46 74 7 44 15 107 31 81 108 111 55 19 103 2 115 16 104 36 3 60 110 97 61 8 75 105 11 96 69 77 -CX 64 12 16 68 108 86 90 50 82 77 69 52 30 66 48 61 93 40 56 14 8 26 75 78 89 47 13 116 45 91 25 98 -CX 88 83 75 11 0 65 55 30 114 39 62 78 7 73 54 71 38 50 80 5 85 76 3 35 79 61 56 28 34 41 92 91 -CX 68 7 34 65 22 95 90 56 58 37 74 75 23 41 114 84 50 97 85 88 52 87 71 76 101 61 80 28 39 2 8 18 -CX 63 105 114 14 116 46 33 108 107 39 101 69 38 17 13 64 104 58 4 57 60 93 72 41 106 47 16 1 68 25 34 79 -CX 103 8 100 59 36 1 82 34 92 64 88 2 72 51 14 12 87 41 77 79 81 89 107 73 57 11 78 63 67 43 75 86 -CX 5 24 21 7 78 14 103 117 15 71 67 39 96 100 25 20 68 19 29 27 11 64 45 88 72 55 34 113 17 36 73 31 -CX 8 109 76 33 7 2 55 78 36 107 60 54 111 103 117 23 27 98 86 4 81 96 106 53 45 93 65 18 22 28 29 113 -CX 46 8 111 57 41 104 27 28 32 19 88 90 66 48 13 61 87 93 78 0 60 39 33 89 37 26 16 107 81 112 84 4 -CX 100 98 48 58 68 110 3 16 29 63 82 12 37 89 79 55 25 66 42 106 31 97 62 73 14 22 108 45 107 76 81 103 -CX 55 51 70 53 107 96 3 80 116 18 54 16 7 37 49 78 117 81 12 25 76 34 61 97 113 33 65 13 41 19 71 68 -CX 100 33 86 115 3 71 84 93 12 98 47 58 116 96 109 36 17 10 52 90 48 113 61 74 92 16 112 50 62 29 65 104 -CX 48 102 7 52 76 10 31 86 5 58 112 37 78 109 44 98 104 8 9 101 74 39 45 96 11 68 60 79 95 41 21 80 -CX 45 112 67 31 41 77 29 114 81 98 89 27 39 105 68 113 91 38 75 0 84 61 32 97 111 18 30 20 10 33 51 25 -CX 17 21 104 70 101 109 107 79 9 40 49 90 27 20 4 57 105 51 14 89 39 28 92 81 37 65 83 102 43 10 8 112 -CX 105 29 15 67 59 91 117 71 58 1 76 21 109 55 69 14 24 108 31 39 27 66 77 37 98 33 44 34 36 6 3 100 -CX 80 99 109 56 5 26 9 40 57 84 38 14 31 86 106 24 3 102 81 17 79 76 87 85 101 114 93 71 29 60 22 68 -CX 117 1 28 17 8 2 111 114 41 73 10 66 69 33 24 50 116 105 35 45 104 100 49 51 67 93 68 59 99 11 23 61 -CX 72 50 17 79 26 67 3 66 6 108 40 18 28 107 116 5 52 93 75 60 64 8 88 4 16 71 101 69 49 90 34 99 -CX 5 27 24 38 90 48 114 66 2 72 34 115 68 110 94 105 20 29 11 26 61 21 6 84 51 36 1 19 12 117 75 55 -CX 60 22 104 27 72 59 103 84 13 110 51 28 8 16 43 64 117 63 65 108 47 55 74 31 56 33 107 45 49 113 29 48 -CX 115 79 110 13 23 87 111 76 80 103 44 9 3 53 75 63 7 97 58 14 109 82 29 99 107 65 11 42 86 4 35 108 -CX 67 98 78 108 42 16 73 21 55 87 39 91 56 93 31 62 95 49 3 64 32 15 36 33 99 72 10 41 80 65 84 23 -CX 28 37 94 10 21 58 47 51 81 104 99 57 103 86 60 85 84 13 72 62 107 114 102 4 7 2 35 115 34 39 22 68 -CX 61 79 91 87 43 2 58 113 30 108 28 44 94 115 7 112 56 65 25 50 19 22 29 10 98 5 96 40 0 111 68 116 -CX 67 20 4 107 54 28 104 32 86 66 56 24 5 77 91 48 52 51 65 113 34 114 43 72 3 10 60 53 116 55 89 69 -CX 65 99 64 93 115 79 22 34 52 95 61 36 44 90 58 50 70 48 106 30 45 69 96 91 104 67 28 33 2 84 9 114 -CX 90 112 49 20 33 98 102 75 32 62 2 114 105 61 14 28 19 103 115 7 22 8 12 59 70 83 94 3 116 34 6 67 -CX 60 113 94 82 26 45 76 56 14 43 40 48 83 49 37 10 29 92 110 71 44 54 55 90 93 95 75 34 23 19 6 42 -CX 45 48 8 80 75 101 40 73 22 18 97 92 114 14 68 26 61 90 29 117 78 67 105 91 20 102 38 21 17 82 51 54 -CX 62 44 90 99 4 68 9 3 47 31 19 27 50 56 65 75 34 54 76 43 61 98 10 102 78 7 17 71 60 22 11 1 -CX 8 3 23 35 24 92 58 51 91 69 65 34 89 87 33 71 49 13 90 50 59 30 9 93 94 40 17 104 77 116 80 48 -CX 81 7 37 44 98 84 110 2 88 79 56 40 74 1 97 68 106 93 50 109 103 100 6 105 87 57 96 113 83 12 54 51 -CX 94 15 109 72 112 2 1 99 71 76 52 97 44 22 51 117 5 18 36 66 89 78 107 82 21 114 60 37 74 108 32 86 -CX 4 99 112 50 69 75 52 18 41 21 58 114 73 71 84 16 64 82 10 77 115 78 106 33 95 62 117 80 37 20 81 34 -CX 49 35 15 32 1 115 106 85 101 107 13 59 19 111 30 100 103 5 28 10 108 12 93 4 74 84 14 102 114 53 18 44 -CX 14 6 107 102 49 101 78 79 105 28 20 68 73 62 21 45 77 50 65 72 85 103 41 67 9 80 116 1 109 38 12 57 -CX 11 0 111 84 6 94 35 70 39 75 79 97 100 32 58 49 15 82 28 109 105 85 16 65 64 3 46 90 57 12 55 86 -CX 19 35 14 47 32 97 105 27 42 79 18 71 28 78 0 29 90 61 45 82 16 52 85 43 54 108 56 115 113 6 67 37 -CX 91 65 40 25 26 29 107 94 30 48 44 32 0 62 98 64 17 54 97 61 11 66 35 12 28 13 100 51 18 14 85 56 -CX 105 66 85 102 27 20 113 34 46 107 90 41 44 95 32 72 96 19 3 28 103 61 76 68 2 43 93 22 89 25 33 82 -CX 116 29 9 54 88 47 113 112 96 104 98 24 13 0 50 43 73 42 87 52 102 75 111 32 51 78 35 45 92 115 55 28 -CX 78 60 112 44 98 36 91 3 13 76 67 6 21 79 96 28 113 68 56 37 54 51 104 0 8 50 19 75 26 116 85 92 -CX 63 12 52 100 82 21 91 84 117 27 83 39 71 99 4 38 37 114 17 32 113 65 102 60 104 55 42 66 41 26 35 5 -CX 39 64 110 73 37 63 38 33 20 113 115 102 42 19 107 49 86 56 84 112 92 21 106 5 11 74 26 40 6 66 117 94 -CX 53 101 13 79 80 89 41 16 1 44 30 114 45 66 55 91 107 104 10 4 42 2 83 56 3 21 35 85 78 26 54 37 -CX 80 21 5 4 63 49 111 69 87 84 14 48 36 55 6 29 42 53 75 73 62 76 26 74 65 86 11 43 81 51 83 22 -CX 99 30 66 106 62 110 9 102 81 53 86 50 27 32 117 0 36 4 33 10 23 79 104 57 88 55 39 12 38 6 61 22 -CX 104 32 70 26 16 5 86 51 115 1 72 64 38 0 93 48 43 12 116 20 76 89 25 9 22 96 87 73 82 102 67 107 -CX 92 2 28 51 87 116 85 0 67 53 97 78 102 98 22 6 94 99 50 83 108 24 20 115 11 106 57 69 90 42 111 113 -CX 32 24 64 76 117 105 49 31 85 37 79 113 91 18 112 80 34 46 74 92 35 115 82 28 116 68 84 2 13 27 97 21 -CX 94 41 28 20 83 82 4 77 115 49 33 32 27 81 107 48 5 98 111 19 92 63 55 38 46 51 45 79 25 36 35 103 -CX 61 79 19 100 74 45 18 49 7 9 33 108 99 63 26 58 39 5 34 43 0 88 86 78 104 55 92 54 46 77 62 24 -CX 94 55 110 50 37 12 10 93 20 90 43 46 72 54 108 48 15 102 6 116 77 25 14 29 88 61 49 22 87 17 28 79 -CX 12 88 44 41 64 56 103 98 21 48 80 61 72 23 4 113 25 87 31 97 16 15 35 71 1 0 47 36 27 6 39 84 -CX 86 18 16 8 91 20 75 53 34 97 103 115 9 24 108 112 77 110 27 50 69 62 22 101 114 63 31 26 105 87 19 113 -CX 115 24 78 117 89 18 70 33 8 98 96 73 47 10 45 67 35 22 84 72 60 55 71 106 90 95 28 16 111 75 15 107 -CX 94 54 47 28 56 86 71 50 42 85 72 23 6 98 105 115 79 59 20 84 92 100 73 102 45 19 58 25 69 61 68 36 -CX 76 26 16 77 28 36 13 106 85 11 83 29 54 65 27 109 38 63 91 7 48 116 81 89 6 39 101 79 96 105 1 58 -CX 41 53 31 82 13 21 64 6 94 48 20 106 2 65 63 62 47 70 54 3 114 76 85 55 49 30 66 1 71 80 5 83 -CX 113 25 82 107 40 4 108 17 56 114 89 22 57 18 104 32 64 48 88 11 65 15 9 111 49 61 46 26 112 67 52 28 -CX 61 25 21 30 116 67 85 43 82 89 38 117 105 102 98 78 73 64 114 97 27 36 15 72 1 4 79 40 11 112 83 22 -CX 58 69 10 52 92 17 104 116 5 101 91 112 40 85 95 45 57 103 24 51 59 115 50 46 1 36 27 74 102 47 80 0 -CX 12 57 101 107 97 51 39 89 22 38 94 83 28 95 42 103 27 5 6 2 90 23 76 59 41 91 4 34 75 67 113 8 -CX 91 81 31 112 110 1 16 96 52 44 34 103 63 4 49 14 39 88 53 113 115 29 66 74 55 72 33 114 0 19 60 100 -CX 113 117 44 9 76 108 31 71 82 70 19 16 51 17 78 88 93 42 24 106 104 77 102 14 98 4 116 35 34 45 0 18 -CX 89 106 1 8 102 58 91 55 111 50 39 87 17 108 54 96 46 100 57 44 37 60 23 34 115 107 29 88 6 63 7 0 -CX 10 76 59 0 100 81 31 16 91 50 103 94 78 49 28 75 79 35 23 92 27 20 11 42 44 95 15 71 29 26 106 90 -CX 68 57 101 99 90 10 55 73 45 21 23 79 13 109 107 87 63 78 112 58 111 27 8 95 85 81 32 42 50 47 74 41 -CX 55 66 79 76 83 9 24 52 44 67 94 63 109 88 15 57 42 1 28 38 110 85 86 17 111 35 90 65 78 5 20 74 -CX 39 7 84 14 34 81 75 114 23 90 112 71 57 72 31 59 55 6 18 63 46 64 37 48 11 104 101 16 87 65 29 54 -CX 62 8 75 92 108 46 68 66 23 7 90 26 24 2 44 30 102 107 67 99 83 52 71 53 21 101 0 28 65 111 113 97 -CX 19 110 70 11 102 3 18 69 34 29 46 42 93 17 12 32 52 45 76 5 115 7 86 58 4 81 40 39 38 95 49 90 -CX 101 49 61 37 86 15 74 87 83 1 91 13 54 9 26 112 80 108 31 60 104 27 43 96 114 36 59 107 89 71 73 65 -CX 26 89 59 48 10 93 3 9 38 92 79 57 117 108 37 53 23 82 77 85 50 114 112 115 28 31 63 1 103 34 60 91 -CX 114 109 45 96 14 75 92 88 85 117 15 27 110 57 49 106 53 7 93 22 97 89 87 103 54 47 67 18 8 65 20 5 -CX 74 99 27 97 64 69 92 59 83 39 38 60 17 2 90 57 55 85 86 117 44 101 91 46 115 34 26 100 110 73 106 111 -CX 87 79 53 36 35 111 56 8 14 41 57 117 86 114 66 29 65 40 31 19 20 32 90 99 52 3 105 54 50 28 17 9 -CX 10 21 59 77 93 48 101 111 28 37 114 50 34 1 36 19 15 94 55 103 100 85 38 88 53 70 7 84 87 102 12 116 -CX 66 62 104 55 14 7 45 82 40 39 4 108 102 57 107 44 36 71 80 27 33 95 35 21 101 65 43 100 11 103 16 87 -CX 49 41 43 106 102 60 21 37 3 96 33 2 69 81 78 72 114 53 105 59 109 79 65 117 13 15 73 113 101 94 11 63 -CX 110 27 45 74 4 105 52 60 69 40 24 1 16 117 104 32 55 80 65 12 53 57 70 66 37 11 6 54 19 44 25 10 -CX 58 46 14 75 77 42 102 84 12 26 113 40 20 105 112 98 10 108 95 37 71 74 8 62 72 59 64 56 49 116 66 81 -CX 67 60 103 22 107 86 106 18 0 114 39 108 83 81 19 26 99 17 88 31 58 100 10 62 64 70 49 50 78 104 54 69 -CX 82 64 87 115 55 60 35 61 16 85 84 25 48 4 111 78 100 17 56 27 19 49 99 80 86 7 44 90 28 108 97 36 -CX 75 99 87 72 116 62 42 19 102 76 81 92 8 84 79 50 85 9 100 0 3 101 113 11 16 70 32 7 26 55 43 35 -CX 84 115 45 25 105 87 20 52 10 116 14 54 57 42 64 13 1 117 7 18 110 76 91 27 114 8 85 111 58 65 3 41 -CX 89 102 79 37 84 39 100 19 57 6 5 36 20 97 3 115 40 103 18 32 13 29 98 83 73 63 92 24 9 16 106 111 -CX 82 107 30 20 98 86 22 31 75 78 58 13 0 104 25 72 46 83 81 21 34 12 10 38 29 48 94 69 17 91 89 88 -CX 105 68 15 38 66 12 98 97 26 56 51 81 96 112 3 50 61 82 0 37 88 60 117 47 23 109 101 70 25 113 116 100 -CX 28 17 55 89 95 25 91 46 62 29 6 31 14 110 8 114 5 27 115 42 94 52 57 56 58 84 76 78 83 82 113 47 -CX 5 26 87 33 16 105 66 97 94 13 100 110 53 25 41 108 0 109 82 28 104 91 49 24 38 39 84 47 29 3 86 31 -CX 76 35 39 23 102 85 89 14 1 44 93 19 88 72 96 49 62 58 99 87 110 28 117 108 7 10 30 21 25 54 17 48 -CX 51 75 45 10 4 109 70 58 102 108 105 73 115 36 89 47 95 100 42 112 1 14 49 38 34 7 85 65 61 35 6 62 -CX 40 106 90 54 94 58 74 71 107 65 28 21 108 5 51 32 24 117 25 113 14 78 116 53 98 72 102 77 16 11 33 30 -CX 89 35 73 111 23 58 105 54 2 18 39 65 17 108 52 4 59 64 95 63 66 92 5 49 13 88 37 103 101 93 83 68 -CX 54 103 110 52 3 115 33 76 7 37 111 41 64 113 17 69 109 27 40 94 12 20 38 114 66 18 10 67 63 93 8 117 -CX 46 80 106 19 100 58 51 78 41 87 36 63 71 9 67 17 2 116 11 26 77 76 89 117 61 65 18 22 95 16 54 5 -CX 9 45 81 94 89 102 39 87 32 92 46 47 111 52 51 60 56 106 41 79 113 104 19 16 97 61 109 115 88 91 54 73 -CX 60 77 6 37 67 103 63 44 111 16 57 18 82 89 117 19 29 42 99 8 87 70 72 75 46 21 53 80 52 38 34 28 -CX 78 1 109 83 61 45 10 106 33 62 92 50 58 104 5 54 32 108 67 19 41 18 26 94 48 15 14 97 96 60 80 66 -CX 102 57 110 67 19 63 107 113 108 7 24 90 51 42 82 94 33 62 37 4 55 10 27 3 73 40 105 99 31 38 97 44 -CX 110 56 114 41 105 87 95 35 15 86 47 4 113 36 26 92 75 13 77 93 33 2 97 42 14 51 58 37 69 117 107 18 -CX 63 7 13 48 113 56 40 0 107 59 80 30 17 111 19 26 62 21 36 44 28 82 89 85 102 78 116 77 58 35 94 86 -CX 59 117 60 46 54 113 8 28 44 7 41 13 85 49 69 91 88 63 42 18 25 111 34 100 62 116 40 92 103 10 94 76 -CX 35 93 69 107 82 14 90 73 103 104 16 53 11 95 18 48 12 54 116 71 27 109 0 98 86 79 112 101 10 113 42 15 -CX 86 74 71 82 47 53 87 50 9 52 105 114 57 38 95 99 55 56 110 79 69 77 17 58 96 61 49 13 42 35 80 18 -CX 33 84 5 12 99 21 93 116 4 0 41 43 115 20 53 56 79 2 55 70 72 51 15 86 50 7 3 17 30 64 62 83 -CX 49 40 16 36 78 17 72 111 109 112 70 18 97 106 75 30 92 25 3 27 62 71 58 46 60 81 54 67 0 53 102 48 -CX 106 109 34 79 3 98 67 116 41 13 103 65 105 99 31 54 32 16 66 59 72 70 47 101 15 11 91 44 51 56 46 53 -CX 117 86 64 56 63 48 68 3 107 6 105 62 41 97 29 13 2 44 9 91 22 88 60 15 103 18 58 99 21 80 35 95 -CX 115 88 46 23 29 5 37 83 56 69 70 58 76 36 31 1 38 60 19 25 98 114 39 9 32 21 50 80 75 101 96 106 -CX 113 50 87 11 104 100 45 77 111 29 0 101 69 79 21 58 117 16 36 49 19 43 96 32 107 18 24 94 27 80 93 7 -CX 82 4 109 58 36 28 67 96 117 14 95 9 77 19 25 22 3 55 20 21 56 41 6 57 79 64 45 90 113 74 16 69 -CX 112 115 78 66 6 31 14 57 62 29 65 40 111 85 39 19 34 81 53 33 105 2 0 47 64 23 7 41 20 5 4 3 -CX 33 31 3 92 37 61 114 111 76 82 63 43 75 11 25 22 44 91 95 104 47 18 46 24 59 48 57 101 78 105 10 116 -CX 29 12 30 11 93 39 66 47 10 15 75 106 76 46 31 71 36 99 80 112 108 21 91 52 27 59 17 25 109 55 9 48 -CX 19 73 31 38 32 77 65 92 116 61 49 15 13 84 46 58 108 104 50 27 75 42 117 80 34 41 81 25 76 5 100 51 -CX 34 41 103 13 80 16 26 47 60 113 116 72 5 7 90 58 17 102 63 65 96 27 35 32 78 19 36 15 40 28 43 89 -CX 25 97 15 109 53 27 113 99 17 65 104 68 11 49 3 107 114 50 44 79 116 66 100 77 18 84 105 87 96 29 117 62 -CX 60 48 98 91 40 32 71 1 67 96 109 89 94 93 33 3 116 105 66 50 18 21 78 103 12 52 14 90 61 51 6 62 -CX 10 72 117 28 43 49 78 86 84 106 113 108 45 91 85 96 32 30 52 17 102 114 87 51 79 5 31 24 116 18 89 107 -CX 23 13 115 98 26 53 6 65 42 68 96 91 46 44 84 90 55 29 104 48 9 28 59 16 45 80 47 11 95 110 112 114 -CX 99 33 9 58 59 85 19 116 90 88 1 100 67 46 54 30 44 109 113 42 56 66 107 18 91 102 108 12 28 34 104 89 -CX 108 72 23 56 45 47 106 89 4 110 85 115 49 68 113 44 84 70 73 52 38 65 33 58 10 6 13 71 34 39 27 54 -CX 71 74 101 111 25 31 35 11 53 50 87 64 36 37 77 62 110 52 28 89 113 61 15 65 45 72 69 10 4 90 41 112 -CX 110 56 11 24 113 4 116 16 10 87 60 8 101 6 18 63 117 88 102 55 61 37 1 77 32 69 81 15 76 115 80 68 -CX 71 106 79 46 4 29 69 85 117 19 2 96 48 5 16 42 110 49 109 9 77 57 39 41 26 54 61 8 105 32 0 83 -CX 84 80 111 13 38 93 104 28 72 73 48 1 50 9 83 75 11 31 8 53 67 57 70 39 64 4 18 63 62 79 47 19 -CX 70 14 26 34 69 100 1 63 80 84 75 57 18 105 40 51 56 114 77 58 107 47 90 50 86 79 73 10 2 27 117 28 -CX 31 78 86 44 55 71 97 12 10 3 99 29 82 42 30 92 25 67 20 70 109 65 107 7 38 81 32 75 87 104 114 54 -CX 47 98 26 0 78 74 63 16 22 48 114 23 2 33 46 116 27 52 84 24 31 53 99 75 25 96 73 108 5 21 112 107 -CX 104 80 26 12 7 37 18 70 50 78 46 23 75 97 85 21 20 65 111 63 82 11 57 106 36 34 44 56 31 73 5 29 -CX 83 27 109 110 103 100 34 30 113 95 90 105 55 94 91 35 115 114 46 62 52 25 58 65 86 67 40 5 29 28 69 19 -CX 54 15 69 9 97 53 117 65 48 76 58 12 101 25 7 50 26 115 89 27 55 67 95 110 91 78 94 77 60 47 81 13 -CX 48 81 86 97 37 65 101 47 40 22 53 84 111 69 31 24 9 29 10 32 87 107 94 28 63 82 98 4 50 44 51 59 -CX 53 26 6 36 29 19 18 43 84 109 95 69 86 37 78 74 57 54 105 38 47 94 40 33 113 58 50 42 4 102 116 12 -CX 84 80 17 31 45 112 18 91 64 57 96 51 32 1 53 40 111 102 8 26 49 98 106 78 65 70 11 86 81 59 27 14 -CX 17 103 48 14 49 98 66 75 92 106 102 97 104 31 59 112 61 69 10 20 96 0 45 7 72 58 117 63 113 25 24 77 -CX 7 43 25 61 35 95 22 70 63 64 112 67 53 6 19 24 1 39 105 88 9 23 82 3 48 37 65 18 36 17 21 38 -CX 65 29 101 103 85 39 110 114 57 79 27 117 24 89 6 26 77 48 41 45 84 49 5 86 50 11 20 63 19 98 112 23 -CX 5 88 61 59 45 44 112 79 65 95 69 27 99 107 17 30 53 85 8 102 32 38 68 114 42 80 56 2 62 89 16 3 -CX 54 49 23 36 71 88 48 24 75 86 51 102 94 89 3 31 82 61 43 46 56 39 10 7 66 40 20 17 6 98 60 30 -CX 109 113 78 82 90 5 105 4 1 61 58 104 59 98 54 31 51 22 13 102 36 38 29 3 99 26 103 85 97 63 35 39 -CX 14 80 59 17 35 40 100 39 106 75 69 43 4 38 25 32 115 64 92 67 28 96 0 112 58 117 8 54 45 102 5 114 -CX 88 89 83 64 39 47 100 43 51 22 99 6 59 41 20 76 29 61 103 79 54 69 50 14 113 11 78 58 107 75 55 62 -CX 81 17 94 8 3 71 89 68 61 73 76 110 16 30 5 106 101 38 52 87 108 59 32 72 44 70 42 66 46 115 4 23 diff --git a/crates/ppvm-tableau-2/benches/circuits/gates.stim b/crates/ppvm-tableau-2/benches/circuits/gates.stim deleted file mode 100644 index d5040f25c..000000000 --- a/crates/ppvm-tableau-2/benches/circuits/gates.stim +++ /dev/null @@ -1,4097 +0,0 @@ -H 0 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 -X 11 -Y 46 -Z 94 -CY 85 109 -S 32 -SQRT_Y_DAG 27 -SQRT_Y_DAG 4 -SQRT_Y_DAG 87 -Z 55 -CX 50 102 -CZ 110 65 -S_DAG 69 -SQRT_X_DAG 64 -S 115 -X 111 -X 46 -SQRT_X_DAG 40 -SQRT_X 54 -SQRT_Y 21 -SQRT_Y 22 -H 29 -X 22 -S_DAG 22 -Z 65 -SQRT_Y 46 -SQRT_Y 86 -SQRT_Y 23 -SQRT_X_DAG 101 -SQRT_X 94 -SQRT_Y 116 -CY 46 101 -SQRT_Y_DAG 45 -S_DAG 109 -SQRT_X_DAG 20 -CY 51 91 -CZ 59 83 -SQRT_Y 31 -SQRT_X_DAG 35 -SQRT_X_DAG 64 -SQRT_Y 106 -CY 45 84 -SQRT_X_DAG 115 -SQRT_X_DAG 44 -SQRT_Y_DAG 92 -SQRT_Y 92 -SQRT_X_DAG 62 -CX 28 41 -CZ 106 21 -SQRT_Y_DAG 34 -CY 116 61 -S 38 -CY 90 106 -SQRT_Y 71 -SQRT_Y 64 -CX 78 75 -SQRT_X 39 -CZ 26 62 -SQRT_Y 46 -CX 79 112 -Y 100 -S_DAG 92 -X 116 -H 95 -Y 7 -SQRT_Y_DAG 83 -X 34 -SQRT_Y_DAG 29 -CX 112 117 -Y 96 -SQRT_Y 17 -S 31 -H 112 -X 54 -CZ 97 4 -X 46 -S_DAG 22 -H 86 -X 10 -Y 8 -X 5 -CZ 117 2 -S_DAG 32 -Z 104 -Z 94 -Z 66 -CZ 0 49 -SQRT_Y_DAG 5 -CY 31 19 -X 0 -S_DAG 78 -CX 95 14 -S 43 -SQRT_X_DAG 3 -S 57 -SQRT_Y 98 -SQRT_Y_DAG 94 -X 115 -S 96 -SQRT_X 110 -SQRT_Y_DAG 90 -Z 60 -H 11 -CX 87 40 -Y 3 -SQRT_X_DAG 100 -Z 66 -SQRT_Y_DAG 99 -SQRT_X 62 -SQRT_Y 41 -Z 111 -S_DAG 33 -S 77 -SQRT_X 83 -X 89 -SQRT_Y 17 -CX 7 32 -X 16 -Z 21 -Y 58 -CX 29 65 -CZ 4 31 -H 91 -SQRT_X_DAG 9 -S 10 -SQRT_Y_DAG 29 -SQRT_Y_DAG 101 -CY 79 90 -S_DAG 32 -CX 54 35 -SQRT_Y 96 -X 19 -X 49 -SQRT_X 20 -Y 65 -CZ 11 30 -Y 12 -X 23 -CY 29 13 -H 3 -SQRT_Y 85 -SQRT_X_DAG 58 -S 68 -CX 48 27 -CX 116 97 -H 93 -CY 55 54 -SQRT_Y 2 -SQRT_Y_DAG 75 -X 112 -SQRT_X 67 -SQRT_Y_DAG 23 -Y 84 -CY 61 46 -X 66 -Y 78 -S_DAG 37 -CZ 47 39 -X 111 -CX 52 12 -Y 39 -H 107 -CY 86 105 -X 103 -SQRT_X_DAG 7 -SQRT_X 81 -SQRT_X_DAG 59 -H 113 -SQRT_Y_DAG 78 -Y 0 -S 3 -S_DAG 39 -CZ 9 28 -CY 62 24 -Y 73 -S_DAG 50 -CZ 59 17 -CY 44 50 -Y 32 -Y 15 -Y 78 -S_DAG 82 -SQRT_X 27 -CZ 13 3 -SQRT_Y_DAG 84 -SQRT_X_DAG 99 -X 92 -CZ 63 37 -S_DAG 58 -Z 102 -S_DAG 34 -SQRT_X_DAG 67 -SQRT_X_DAG 92 -CZ 102 53 -SQRT_X_DAG 107 -CX 37 50 -H 20 -SQRT_X_DAG 76 -S 70 -SQRT_X 89 -CX 89 117 -Y 74 -CZ 104 73 -Y 9 -S_DAG 22 -SQRT_Y 18 -CY 53 114 -Y 102 -Y 116 -CX 103 82 -X 16 -S 49 -H 90 -CX 113 87 -S_DAG 56 -Z 67 -S 14 -Z 69 -CY 54 12 -S_DAG 66 -H 91 -SQRT_Y 32 -Z 114 -Z 59 -CZ 30 51 -S_DAG 100 -CY 73 93 -Z 59 -SQRT_X_DAG 92 -X 103 -SQRT_Y_DAG 49 -CZ 23 50 -SQRT_Y 6 -SQRT_X_DAG 35 -SQRT_X 32 -CZ 93 52 -CZ 82 60 -S_DAG 70 -S_DAG 91 -CZ 116 84 -Y 97 -CZ 28 68 -SQRT_Y_DAG 24 -SQRT_X 104 -CX 48 112 -CX 117 1 -S_DAG 59 -SQRT_Y 91 -SQRT_X_DAG 83 -Z 104 -Y 2 -SQRT_X 27 -CZ 72 77 -SQRT_X 115 -H 113 -Y 49 -SQRT_Y 98 -CY 25 35 -CZ 117 75 -SQRT_Y_DAG 24 -SQRT_X_DAG 102 -SQRT_Y_DAG 17 -X 78 -CX 55 61 -S 65 -SQRT_Y_DAG 22 -SQRT_X_DAG 91 -H 97 -Y 44 -X 115 -SQRT_X_DAG 68 -CX 84 8 -CY 75 62 -CX 42 58 -S 112 -SQRT_Y 58 -X 10 -SQRT_Y_DAG 96 -S_DAG 22 -CY 96 100 -SQRT_X 32 -CX 80 100 -CZ 17 6 -Z 63 -SQRT_X 59 -CX 37 19 -X 36 -SQRT_Y 59 -X 46 -X 68 -SQRT_X 72 -SQRT_X_DAG 26 -CX 39 63 -CX 17 61 -CZ 68 91 -S 9 -S 105 -S_DAG 38 -S_DAG 82 -CY 39 83 -CX 50 66 -Y 65 -CX 26 50 -SQRT_Y_DAG 67 -Z 102 -SQRT_Y 80 -Y 39 -X 29 -SQRT_X_DAG 71 -H 66 -S 7 -Y 14 -CX 104 100 -SQRT_X 109 -S_DAG 27 -S_DAG 45 -Y 42 -SQRT_X_DAG 46 -Z 63 -SQRT_X_DAG 111 -S 59 -Z 117 -CZ 56 81 -H 34 -S_DAG 20 -Y 113 -H 60 -H 96 -CX 108 47 -Z 45 -Z 101 -Z 29 -S 103 -SQRT_Y 81 -SQRT_X 51 -CY 105 95 -S_DAG 35 -CZ 76 64 -SQRT_Y_DAG 88 -CZ 41 95 -SQRT_X 96 -CZ 111 90 -CX 96 91 -S 68 -SQRT_Y_DAG 81 -CX 9 47 -S 50 -SQRT_X_DAG 22 -S 115 -S_DAG 56 -SQRT_X_DAG 11 -Z 40 -SQRT_X 16 -X 13 -S_DAG 21 -S_DAG 9 -CZ 97 83 -SQRT_X 1 -SQRT_Y 41 -H 105 -SQRT_Y_DAG 49 -SQRT_Y 36 -SQRT_X_DAG 81 -Z 46 -S_DAG 25 -SQRT_X_DAG 12 -Z 100 -H 42 -S 18 -SQRT_X 46 -S 11 -S_DAG 24 -H 90 -H 93 -SQRT_Y_DAG 5 -S_DAG 47 -CX 98 78 -X 110 -Z 22 -Y 55 -SQRT_X_DAG 99 -S 16 -S_DAG 66 -SQRT_Y_DAG 108 -Y 43 -CX 98 90 -SQRT_Y_DAG 50 -H 6 -SQRT_X 98 -SQRT_X_DAG 62 -SQRT_Y_DAG 111 -S_DAG 69 -SQRT_Y_DAG 76 -Y 75 -SQRT_Y 68 -CX 63 51 -CZ 58 21 -SQRT_X 49 -SQRT_Y 57 -X 112 -Y 57 -SQRT_Y_DAG 16 -Y 117 -CX 64 117 -Z 9 -SQRT_X 39 -SQRT_X_DAG 102 -CZ 1 32 -Y 85 -S_DAG 28 -Z 3 -Z 54 -CX 11 43 -CX 59 6 -SQRT_X_DAG 30 -Y 61 -Z 71 -X 17 -CZ 64 69 -X 6 -H 69 -X 105 -SQRT_Y 43 -CX 67 112 -H 17 -S_DAG 62 -X 16 -SQRT_Y 14 -H 13 -SQRT_X_DAG 27 -CY 6 78 -H 80 -SQRT_X 43 -SQRT_Y_DAG 82 -SQRT_X 115 -CZ 67 64 -CY 117 86 -Z 65 -Y 106 -Z 80 -H 22 -SQRT_X 25 -S 43 -SQRT_X 18 -SQRT_X 16 -SQRT_X 40 -CY 38 103 -Y 71 -Y 60 -S 36 -SQRT_Y 97 -SQRT_X_DAG 35 -H 53 -CZ 17 89 -SQRT_Y 84 -Y 3 -SQRT_Y_DAG 70 -CY 25 27 -H 50 -SQRT_Y_DAG 5 -CX 17 80 -X 95 -S 89 -CZ 60 69 -X 94 -CY 110 105 -H 106 -Z 76 -S_DAG 4 -CZ 25 13 -Z 81 -CZ 69 117 -Z 97 -Y 87 -SQRT_X_DAG 80 -S 26 -Z 105 -S_DAG 89 -S 108 -SQRT_Y 72 -Y 52 -SQRT_X 85 -CZ 4 58 -S 84 -Y 81 -CZ 93 34 -X 27 -SQRT_X 42 -S 68 -CZ 50 75 -SQRT_Y 95 -H 55 -CY 16 88 -Z 100 -SQRT_X_DAG 111 -SQRT_X_DAG 44 -SQRT_X 60 -SQRT_Y_DAG 32 -SQRT_Y_DAG 24 -SQRT_Y_DAG 60 -SQRT_X_DAG 24 -CY 60 108 -SQRT_Y_DAG 43 -S 9 -Z 47 -SQRT_Y_DAG 80 -SQRT_X_DAG 28 -CY 78 83 -CX 73 113 -Z 87 -S 111 -H 68 -S 12 -X 100 -X 25 -S_DAG 7 -S_DAG 68 -S 101 -CZ 85 43 -SQRT_X_DAG 9 -SQRT_X 60 -CZ 2 36 -SQRT_Y_DAG 73 -Z 27 -Z 20 -SQRT_Y_DAG 98 -SQRT_X 92 -Y 81 -SQRT_Y_DAG 57 -S 82 -Y 63 -SQRT_X_DAG 102 -CY 117 30 -Z 72 -S 108 -H 25 -SQRT_Y_DAG 90 -S_DAG 75 -SQRT_Y_DAG 89 -SQRT_X 67 -SQRT_X 30 -CX 27 71 -X 33 -CX 31 17 -SQRT_Y_DAG 93 -SQRT_X 105 -SQRT_X 15 -SQRT_X_DAG 50 -SQRT_X 60 -SQRT_X 36 -H 30 -H 7 -SQRT_Y 67 -Y 77 -SQRT_Y 86 -X 6 -SQRT_X 90 -SQRT_X 51 -H 65 -S 12 -S_DAG 65 -S_DAG 66 -CY 63 74 -Y 90 -SQRT_X_DAG 94 -CZ 28 35 -X 3 -SQRT_X_DAG 5 -Z 83 -Z 26 -S_DAG 30 -SQRT_Y 6 -SQRT_Y_DAG 18 -CX 37 97 -Y 82 -SQRT_Y 69 -Y 86 -CX 17 55 -CZ 17 4 -S 65 -CX 34 60 -X 71 -S_DAG 97 -S_DAG 87 -Y 77 -S_DAG 13 -SQRT_Y_DAG 100 -S_DAG 46 -CY 81 35 -CY 61 114 -S 65 -SQRT_Y_DAG 19 -X 5 -S_DAG 55 -CX 1 44 -CX 68 91 -X 110 -CX 9 88 -SQRT_Y 64 -SQRT_Y_DAG 98 -SQRT_X 54 -SQRT_X 30 -CY 23 20 -SQRT_Y_DAG 5 -X 75 -CY 92 105 -S_DAG 86 -Z 37 -X 115 -X 31 -SQRT_Y_DAG 100 -H 51 -Y 116 -S_DAG 14 -SQRT_Y_DAG 116 -Y 31 -H 70 -H 13 -X 88 -SQRT_X 10 -SQRT_Y 109 -S 74 -CX 28 6 -SQRT_Y 66 -SQRT_Y 117 -SQRT_X 54 -CY 16 19 -SQRT_X 16 -SQRT_X_DAG 94 -S_DAG 6 -SQRT_Y_DAG 23 -SQRT_Y 104 -SQRT_X_DAG 55 -SQRT_Y_DAG 105 -CY 111 82 -SQRT_X_DAG 20 -SQRT_X_DAG 76 -Z 109 -S_DAG 18 -X 32 -CZ 23 19 -CX 52 72 -CX 32 56 -SQRT_X_DAG 59 -H 54 -SQRT_X 34 -CY 28 45 -CY 80 4 -SQRT_X 79 -X 54 -S 107 -X 116 -SQRT_Y 61 -SQRT_Y_DAG 33 -CX 34 31 -SQRT_X_DAG 91 -SQRT_X_DAG 46 -SQRT_Y 108 -SQRT_Y_DAG 59 -CX 31 71 -SQRT_Y 20 -SQRT_X_DAG 36 -CY 46 53 -Y 64 -CX 114 31 -CZ 83 84 -SQRT_X 14 -SQRT_X 76 -SQRT_X_DAG 79 -SQRT_Y 58 -Y 94 -SQRT_X 57 -SQRT_Y_DAG 98 -CZ 89 94 -S_DAG 116 -CY 71 44 -Z 18 -H 85 -CX 22 52 -SQRT_X_DAG 63 -CZ 92 103 -Z 52 -S 116 -S_DAG 73 -SQRT_X 38 -CX 91 100 -S 85 -S_DAG 85 -X 51 -SQRT_Y_DAG 5 -H 58 -Y 14 -X 107 -S_DAG 41 -SQRT_Y_DAG 40 -CZ 51 22 -CZ 106 41 -CY 10 67 -SQRT_Y_DAG 61 -SQRT_X 79 -H 56 -Y 77 -X 44 -X 38 -SQRT_X_DAG 17 -CZ 90 6 -X 42 -SQRT_X 61 -CX 114 78 -X 112 -SQRT_X_DAG 81 -SQRT_Y_DAG 26 -H 94 -SQRT_Y_DAG 41 -Z 42 -S 99 -SQRT_X 72 -SQRT_Y_DAG 92 -SQRT_X_DAG 59 -CY 35 10 -SQRT_Y 27 -SQRT_Y_DAG 46 -H 46 -S_DAG 102 -Z 114 -H 69 -CY 83 92 -SQRT_Y_DAG 28 -H 75 -SQRT_X_DAG 30 -SQRT_X 110 -S 75 -H 65 -Z 0 -SQRT_X 98 -SQRT_X_DAG 94 -S_DAG 83 -Z 86 -H 93 -CZ 77 23 -SQRT_X_DAG 78 -X 101 -CZ 17 117 -H 90 -H 0 -SQRT_Y_DAG 10 -SQRT_X_DAG 99 -CX 25 94 -Z 34 -SQRT_X 78 -X 1 -S_DAG 112 -Y 39 -X 73 -S_DAG 60 -SQRT_X 15 -Y 59 -Z 18 -CY 112 98 -SQRT_X_DAG 116 -CX 8 34 -Z 111 -SQRT_X_DAG 68 -Y 116 -CY 114 67 -CZ 102 37 -S 117 -X 70 -SQRT_Y 26 -Y 53 -Z 23 -SQRT_Y_DAG 39 -SQRT_X_DAG 25 -X 89 -S_DAG 58 -CY 7 19 -H 109 -SQRT_Y_DAG 44 -CX 39 102 -CZ 82 13 -H 19 -H 3 -S_DAG 15 -S 13 -S_DAG 99 -Y 64 -CY 117 109 -CZ 78 17 -S_DAG 16 -SQRT_X 93 -CZ 25 39 -SQRT_Y 97 -X 96 -X 43 -CX 19 92 -Y 110 -SQRT_Y 96 -X 17 -CZ 63 53 -CX 45 61 -CX 37 38 -Y 113 -CX 19 94 -Y 0 -Y 114 -Z 45 -CZ 19 38 -CY 72 86 -SQRT_Y 16 -Y 29 -SQRT_Y_DAG 92 -CY 95 29 -CY 42 11 -Z 85 -SQRT_X 26 -SQRT_X 83 -CX 4 106 -S_DAG 54 -SQRT_X_DAG 83 -SQRT_Y 44 -Z 67 -Y 84 -X 16 -CX 48 18 -SQRT_X 98 -CY 94 28 -CX 34 78 -SQRT_Y 89 -Y 11 -SQRT_Y 106 -SQRT_Y_DAG 92 -SQRT_Y_DAG 22 -H 84 -SQRT_Y_DAG 86 -CZ 9 68 -S_DAG 16 -SQRT_Y 30 -H 86 -SQRT_X_DAG 86 -SQRT_X 58 -Y 40 -H 6 -S_DAG 13 -SQRT_X 2 -SQRT_Y_DAG 14 -CZ 103 33 -S_DAG 70 -H 43 -SQRT_X 29 -X 17 -Z 16 -CZ 106 21 -SQRT_X 19 -Y 82 -S 97 -SQRT_X 59 -X 14 -SQRT_X 0 -CZ 12 106 -X 24 -SQRT_X 0 -H 101 -SQRT_X 104 -S_DAG 108 -Z 22 -S 109 -S 103 -S 72 -H 66 -S 39 -SQRT_Y_DAG 53 -SQRT_X_DAG 61 -SQRT_X 98 -S 50 -Z 83 -CY 53 37 -CY 73 58 -SQRT_Y 54 -SQRT_Y_DAG 88 -S_DAG 69 -Y 72 -X 32 -SQRT_X 113 -SQRT_Y_DAG 58 -CZ 11 114 -SQRT_Y 69 -SQRT_X_DAG 67 -CX 35 20 -H 47 -Y 94 -H 27 -SQRT_X 16 -SQRT_Y 113 -H 64 -SQRT_X 114 -Z 110 -Y 39 -SQRT_X_DAG 18 -CY 65 11 -CZ 47 86 -Z 28 -CY 70 5 -SQRT_X 71 -X 114 -SQRT_X 69 -CY 38 29 -Z 93 -SQRT_X 101 -H 114 -H 73 -H 64 -S 70 -S 77 -S 20 -X 82 -SQRT_Y_DAG 51 -SQRT_Y 116 -CZ 6 7 -CY 62 24 -SQRT_Y 41 -S 93 -CX 5 59 -S_DAG 23 -SQRT_X_DAG 6 -H 47 -S 95 -SQRT_X_DAG 33 -CX 64 112 -S 6 -CZ 41 1 -SQRT_X 83 -H 23 -SQRT_X 88 -CX 61 17 -SQRT_X 88 -S 9 -CX 22 97 -CY 6 53 -Y 68 -SQRT_Y_DAG 19 -X 3 -S_DAG 27 -CX 83 72 -CY 68 60 -SQRT_X 85 -CY 78 88 -SQRT_Y_DAG 42 -Y 107 -X 9 -H 2 -CZ 67 108 -CY 106 113 -SQRT_Y 3 -Z 3 -S 32 -Y 2 -CX 75 2 -S_DAG 115 -S 13 -SQRT_X 85 -CX 81 84 -Z 52 -X 30 -H 113 -Z 115 -SQRT_Y_DAG 91 -SQRT_X 69 -X 73 -S 13 -H 74 -H 6 -H 104 -CX 30 87 -Z 79 -X 21 -Y 74 -H 82 -H 36 -S_DAG 88 -Y 112 -CX 26 81 -Z 72 -CY 31 13 -S_DAG 62 -CZ 23 26 -H 49 -S 0 -Y 29 -SQRT_Y_DAG 39 -CX 47 37 -SQRT_Y 99 -S 84 -X 97 -S 11 -SQRT_X_DAG 64 -CX 17 77 -SQRT_Y_DAG 45 -S 116 -SQRT_Y_DAG 54 -SQRT_Y_DAG 54 -CX 104 12 -SQRT_Y 23 -CX 77 29 -SQRT_Y_DAG 81 -Z 8 -SQRT_X_DAG 10 -SQRT_Y_DAG 18 -SQRT_X_DAG 3 -Z 48 -H 61 -S_DAG 66 -CX 17 55 -H 13 -S_DAG 43 -S 55 -Z 31 -SQRT_Y_DAG 33 -SQRT_X_DAG 103 -S_DAG 0 -CZ 116 106 -X 0 -Y 26 -S 7 -CY 56 64 -CY 28 26 -H 107 -Y 4 -X 14 -SQRT_X 14 -SQRT_X 72 -SQRT_Y_DAG 54 -CX 65 3 -CY 112 95 -X 22 -Y 116 -H 106 -H 10 -CZ 35 66 -SQRT_Y 116 -S 14 -CY 103 23 -CY 76 38 -H 94 -SQRT_Y_DAG 11 -Z 87 -X 116 -X 85 -Y 115 -CX 102 58 -SQRT_X_DAG 2 -CX 114 75 -X 107 -H 104 -CZ 42 86 -Y 30 -CZ 35 100 -CY 110 14 -Z 24 -Y 54 -S 109 -SQRT_X_DAG 14 -S 76 -SQRT_Y_DAG 18 -S_DAG 48 -H 75 -H 25 -SQRT_X_DAG 56 -Y 62 -SQRT_X 83 -S_DAG 91 -X 58 -X 54 -SQRT_X_DAG 92 -CX 90 88 -H 103 -SQRT_Y 52 -SQRT_Y 82 -CY 110 113 -X 100 -CX 103 67 -SQRT_X 34 -Y 57 -S 38 -CZ 113 53 -H 96 -CX 112 104 -CY 21 112 -CZ 31 111 -Y 96 -X 35 -SQRT_Y 89 -Z 49 -SQRT_X 74 -CZ 4 86 -CX 17 90 -CX 5 7 -H 4 -CZ 32 68 -CY 106 23 -Z 63 -S_DAG 86 -Y 90 -SQRT_X 70 -SQRT_Y_DAG 108 -Z 13 -SQRT_X 36 -H 0 -CX 26 98 -S 7 -H 104 -H 87 -SQRT_X_DAG 112 -SQRT_Y 95 -SQRT_X_DAG 57 -CY 2 4 -S 52 -SQRT_Y 79 -CX 67 27 -SQRT_X 12 -SQRT_X_DAG 59 -SQRT_Y 106 -CX 21 71 -Z 38 -SQRT_Y_DAG 18 -CZ 69 108 -Y 66 -Z 3 -S_DAG 92 -CY 6 78 -CZ 77 34 -CX 103 50 -S_DAG 76 -SQRT_Y_DAG 73 -CX 16 8 -SQRT_Y_DAG 31 -Z 74 -S_DAG 90 -S_DAG 6 -CY 108 27 -SQRT_Y_DAG 6 -H 46 -X 1 -CY 87 62 -SQRT_X 77 -Z 90 -X 76 -CX 53 8 -S_DAG 20 -SQRT_Y 66 -H 13 -S 107 -CY 32 43 -SQRT_X_DAG 92 -CX 35 59 -CZ 100 27 -S_DAG 70 -X 78 -X 95 -SQRT_Y_DAG 94 -SQRT_Y_DAG 117 -Y 52 -SQRT_X 53 -SQRT_Y_DAG 58 -SQRT_Y_DAG 35 -SQRT_X 97 -X 32 -X 2 -Z 77 -CX 44 27 -H 105 -Y 113 -SQRT_Y 45 -S 117 -S_DAG 6 -Y 43 -CX 92 38 -Z 84 -Z 32 -S 62 -SQRT_Y 70 -CY 68 33 -S 72 -S_DAG 66 -CY 75 116 -SQRT_Y 59 -SQRT_X 103 -SQRT_X 10 -SQRT_X 10 -SQRT_Y 19 -Y 41 -SQRT_X 61 -CY 111 87 -SQRT_Y_DAG 10 -CZ 52 51 -SQRT_X_DAG 79 -S 117 -SQRT_X_DAG 2 -SQRT_Y 63 -SQRT_Y 46 -CX 53 113 -CZ 25 77 -Y 1 -Z 106 -CX 33 91 -X 37 -X 84 -SQRT_X 99 -S 50 -CZ 31 105 -CX 55 103 -SQRT_Y 34 -SQRT_Y_DAG 48 -SQRT_X 34 -X 33 -Z 110 -S 0 -H 2 -H 23 -S_DAG 100 -S 72 -H 96 -CX 89 43 -SQRT_X 75 -Z 2 -X 13 -H 52 -CX 15 48 -S 17 -S 25 -SQRT_X 4 -CX 110 53 -SQRT_X 43 -Y 26 -SQRT_X_DAG 18 -CZ 68 81 -SQRT_X_DAG 51 -SQRT_X_DAG 90 -S 104 -SQRT_Y_DAG 117 -H 54 -SQRT_X_DAG 30 -CX 113 105 -SQRT_X_DAG 81 -S 26 -CX 60 110 -SQRT_X 3 -S 67 -S 116 -S 40 -H 73 -CX 53 6 -Y 12 -Z 21 -S_DAG 36 -CY 68 28 -CX 25 97 -CX 9 13 -SQRT_Y 32 -SQRT_Y 57 -S_DAG 64 -CY 18 109 -H 29 -Y 38 -SQRT_Y 62 -CY 61 58 -CX 88 99 -SQRT_Y 43 -S 19 -SQRT_X_DAG 56 -X 113 -SQRT_X 100 -SQRT_Y 51 -Z 117 -H 76 -S_DAG 66 -SQRT_X 36 -SQRT_X_DAG 17 -S 46 -CX 53 87 -CX 85 4 -Z 99 -S_DAG 88 -CX 106 66 -SQRT_Y_DAG 27 -SQRT_Y_DAG 74 -SQRT_X 43 -SQRT_Y 27 -CY 34 86 -Z 99 -CX 39 74 -SQRT_Y_DAG 30 -CX 73 9 -X 87 -SQRT_Y 88 -CZ 104 116 -CY 62 0 -SQRT_Y 25 -X 8 -SQRT_Y 82 -S_DAG 21 -SQRT_X 23 -SQRT_Y_DAG 75 -CY 38 110 -SQRT_X 41 -Z 74 -CX 1 87 -SQRT_Y 74 -CY 59 117 -CY 115 2 -SQRT_X 100 -CZ 21 115 -Z 31 -CX 15 58 -H 43 -H 10 -S 6 -Y 34 -SQRT_X 12 -CZ 12 25 -CZ 48 24 -SQRT_X 72 -CZ 31 104 -SQRT_X_DAG 80 -CX 27 9 -Z 108 -SQRT_Y 10 -S_DAG 113 -CZ 24 27 -S_DAG 88 -SQRT_X_DAG 45 -CY 35 116 -Z 57 -CY 81 64 -S 69 -SQRT_Y_DAG 106 -Y 3 -SQRT_Y 69 -Z 42 -SQRT_Y 31 -H 106 -H 31 -CY 52 90 -SQRT_X 36 -Z 87 -S 84 -X 55 -SQRT_Y 84 -CX 58 17 -S 42 -SQRT_Y 74 -Y 19 -SQRT_Y 89 -CX 51 76 -S_DAG 4 -CZ 52 63 -SQRT_X 97 -CZ 11 63 -SQRT_X_DAG 60 -CZ 5 100 -CZ 84 22 -H 94 -S_DAG 50 -S_DAG 106 -CX 78 41 -SQRT_Y_DAG 32 -SQRT_Y_DAG 84 -CY 92 90 -SQRT_Y 58 -SQRT_Y_DAG 100 -SQRT_X 53 -CZ 94 16 -CY 67 79 -Z 24 -CZ 27 81 -H 13 -X 6 -SQRT_X 9 -SQRT_X 69 -SQRT_X_DAG 22 -S_DAG 48 -Z 98 -X 3 -SQRT_X 52 -SQRT_Y_DAG 19 -Z 98 -SQRT_Y 38 -CY 18 13 -Y 23 -H 27 -CZ 25 14 -S_DAG 5 -CY 30 43 -X 64 -CZ 115 97 -CX 68 19 -S_DAG 4 -SQRT_Y 91 -S 31 -Z 70 -H 94 -H 109 -CZ 56 6 -SQRT_Y 64 -SQRT_X 38 -CZ 62 66 -X 23 -SQRT_Y_DAG 30 -Y 44 -X 61 -CY 76 2 -CY 55 15 -H 49 -S_DAG 79 -CZ 60 13 -H 59 -CY 9 77 -CX 65 68 -H 19 -X 61 -SQRT_X_DAG 100 -S_DAG 78 -SQRT_X_DAG 61 -SQRT_X_DAG 98 -S 16 -SQRT_Y_DAG 78 -S 74 -SQRT_X_DAG 39 -Y 12 -X 108 -SQRT_X 95 -Y 40 -X 39 -CY 2 26 -SQRT_Y_DAG 117 -Z 45 -CX 80 107 -S_DAG 83 -CY 18 11 -S 78 -Y 105 -Y 19 -SQRT_Y_DAG 101 -H 0 -S 75 -Z 91 -X 49 -S_DAG 104 -Y 39 -SQRT_X_DAG 83 -SQRT_X_DAG 101 -Z 97 -SQRT_Y_DAG 7 -CY 106 30 -SQRT_X 92 -S 88 -CY 99 43 -Z 72 -SQRT_Y_DAG 58 -S_DAG 10 -S 39 -H 9 -Y 43 -SQRT_Y 61 -SQRT_Y 1 -CX 16 82 -S_DAG 46 -S 97 -SQRT_X_DAG 65 -X 18 -CX 74 45 -CZ 69 75 -Z 34 -S 116 -S 18 -CX 99 3 -SQRT_X 16 -SQRT_X 49 -CZ 15 65 -CX 103 44 -SQRT_X 40 -S 40 -SQRT_X_DAG 4 -S 50 -S 8 -Y 103 -CY 115 83 -CZ 61 17 -Z 105 -CX 15 52 -SQRT_Y 111 -S 66 -SQRT_X 104 -X 80 -SQRT_X_DAG 94 -CY 4 114 -Z 25 -X 16 -SQRT_X_DAG 92 -SQRT_X_DAG 108 -X 31 -CZ 70 93 -CZ 92 108 -CY 82 95 -H 81 -SQRT_Y 80 -CY 8 100 -CZ 38 75 -CY 106 19 -H 68 -SQRT_X_DAG 27 -S 61 -CZ 9 19 -SQRT_X_DAG 6 -H 32 -S 76 -X 114 -X 72 -S_DAG 66 -S 48 -SQRT_X_DAG 64 -CY 91 80 -X 44 -SQRT_Y_DAG 86 -S_DAG 28 -S 1 -X 44 -SQRT_X 2 -SQRT_Y_DAG 65 -H 57 -Y 98 -CX 58 2 -CZ 112 71 -SQRT_X_DAG 42 -Z 56 -S_DAG 98 -SQRT_Y_DAG 37 -CZ 101 76 -S_DAG 34 -Z 91 -SQRT_Y 107 -SQRT_Y 51 -S 6 -S 82 -S_DAG 64 -CZ 71 53 -SQRT_Y 102 -S 84 -Z 10 -CX 17 38 -SQRT_Y_DAG 51 -S_DAG 71 -SQRT_X_DAG 8 -CY 78 33 -CX 111 59 -S 15 -CX 68 102 -SQRT_Y_DAG 99 -CZ 112 107 -S 38 -Y 111 -SQRT_Y_DAG 68 -CZ 77 13 -CY 77 52 -CZ 81 80 -X 96 -X 75 -SQRT_X 80 -H 41 -H 58 -SQRT_Y 36 -CZ 109 59 -S_DAG 47 -SQRT_X 42 -X 71 -SQRT_Y 80 -CX 60 30 -SQRT_X_DAG 94 -H 83 -SQRT_Y 57 -CX 106 78 -SQRT_Y_DAG 92 -SQRT_Y_DAG 26 -Y 112 -SQRT_X 68 -Z 50 -SQRT_Y_DAG 48 -Z 75 -CX 110 50 -S_DAG 98 -X 49 -SQRT_X 80 -S 7 -S 13 -H 71 -CZ 33 37 -CY 1 93 -X 116 -S_DAG 3 -S_DAG 107 -S 88 -CZ 112 96 -SQRT_X 89 -CY 54 28 -Z 97 -SQRT_Y_DAG 4 -CX 67 63 -SQRT_Y_DAG 57 -CY 102 22 -H 55 -SQRT_X_DAG 69 -SQRT_Y 7 -CY 12 112 -Y 55 -S 54 -S_DAG 34 -SQRT_Y 24 -CX 68 94 -SQRT_X 10 -CX 15 84 -CY 24 1 -CY 88 57 -Y 52 -SQRT_Y_DAG 23 -CX 78 74 -CX 9 100 -CZ 44 53 -SQRT_Y 61 -SQRT_Y_DAG 72 -SQRT_X 35 -X 93 -H 115 -X 60 -SQRT_Y 6 -Z 74 -SQRT_Y 50 -SQRT_X_DAG 81 -SQRT_X 46 -Z 92 -H 102 -S 50 -SQRT_X 7 -SQRT_X 104 -SQRT_X 25 -SQRT_X 65 -S_DAG 71 -Z 62 -SQRT_Y 66 -CY 24 89 -S_DAG 102 -SQRT_Y_DAG 10 -S_DAG 75 -X 113 -Y 16 -CX 74 20 -SQRT_X_DAG 34 -SQRT_X_DAG 99 -Y 111 -Y 75 -S_DAG 0 -SQRT_X_DAG 72 -S 94 -S 67 -H 24 -S 57 -Z 108 -H 95 -SQRT_X 92 -CY 32 101 -SQRT_Y 90 -SQRT_Y_DAG 89 -SQRT_Y_DAG 7 -S_DAG 3 -S_DAG 39 -SQRT_X 39 -CX 59 106 -CX 44 99 -CZ 108 34 -H 116 -CX 75 48 -Z 37 -SQRT_X_DAG 80 -S_DAG 15 -SQRT_X_DAG 51 -Z 92 -S_DAG 93 -CX 112 69 -SQRT_X_DAG 3 -H 104 -CZ 36 72 -X 114 -CX 0 85 -Y 77 -Z 11 -X 2 -H 92 -Z 65 -X 45 -SQRT_Y 45 -Y 10 -SQRT_X_DAG 87 -CZ 86 28 -SQRT_X 92 -S 30 -SQRT_Y_DAG 52 -X 20 -CZ 44 30 -S 55 -SQRT_X 55 -CY 24 23 -CY 102 50 -CY 63 35 -CY 22 50 -CZ 49 28 -H 103 -CY 110 13 -SQRT_Y 52 -SQRT_X_DAG 14 -X 12 -H 62 -Z 83 -H 18 -S_DAG 81 -X 44 -X 69 -S 31 -H 106 -S 110 -Z 114 -X 32 -SQRT_X_DAG 76 -SQRT_Y 113 -SQRT_Y_DAG 61 -CX 22 80 -SQRT_X 95 -Z 11 -X 27 -SQRT_X 56 -CY 10 43 -Z 107 -CY 8 76 -CX 39 51 -CY 34 19 -SQRT_Y 66 -Y 106 -SQRT_Y 49 -CY 22 110 -S_DAG 19 -SQRT_Y 42 -SQRT_X 75 -Y 117 -Z 15 -CZ 35 56 -CY 74 31 -H 50 -S 23 -H 105 -CY 99 48 -CX 66 113 -CX 41 9 -S 91 -SQRT_X_DAG 81 -CY 33 97 -X 54 -S 36 -S 87 -X 85 -CZ 32 34 -CY 112 72 -SQRT_X_DAG 6 -SQRT_Y_DAG 85 -CX 111 50 -H 91 -Y 76 -SQRT_X 103 -S_DAG 15 -S 66 -S_DAG 66 -Y 41 -S 31 -Z 11 -SQRT_Y_DAG 92 -SQRT_Y 11 -SQRT_X 19 -Z 97 -Z 52 -SQRT_X_DAG 73 -S_DAG 114 -SQRT_Y 9 -CX 100 55 -S_DAG 8 -SQRT_Y_DAG 9 -SQRT_X_DAG 20 -SQRT_X 30 -CZ 70 43 -SQRT_X 106 -CX 67 63 -CX 101 99 -CX 31 116 -S_DAG 102 -CX 62 96 -SQRT_Y_DAG 1 -S 67 -H 6 -S_DAG 89 -SQRT_Y_DAG 38 -SQRT_Y 31 -S_DAG 6 -SQRT_X_DAG 36 -Y 46 -SQRT_X 83 -CZ 76 22 -Z 13 -SQRT_Y 27 -CY 30 65 -CZ 75 28 -SQRT_X_DAG 38 -CX 69 94 -SQRT_Y 44 -H 101 -SQRT_Y_DAG 108 -X 22 -CZ 41 71 -S_DAG 42 -CX 52 45 -SQRT_Y_DAG 62 -Z 75 -CZ 102 99 -H 38 -SQRT_X 108 -S_DAG 24 -CZ 116 74 -S 116 -SQRT_Y_DAG 27 -CY 110 67 -SQRT_X_DAG 71 -SQRT_X_DAG 86 -H 96 -SQRT_X_DAG 81 -SQRT_Y_DAG 42 -SQRT_Y_DAG 98 -S 115 -S_DAG 68 -SQRT_Y_DAG 110 -S_DAG 0 -SQRT_X_DAG 101 -CX 54 40 -CY 100 3 -CY 33 26 -SQRT_X 102 -X 66 -H 4 -SQRT_X_DAG 103 -CZ 23 47 -SQRT_X_DAG 49 -SQRT_Y_DAG 97 -SQRT_X_DAG 95 -SQRT_Y 2 -H 31 -S_DAG 14 -H 117 -Z 29 -CZ 31 99 -S 91 -Y 68 -Z 113 -SQRT_Y_DAG 78 -X 19 -CY 0 95 -SQRT_X_DAG 74 -X 80 -SQRT_Y_DAG 30 -S_DAG 110 -S_DAG 78 -SQRT_Y_DAG 112 -X 86 -S 78 -H 103 -CX 72 107 -SQRT_Y 61 -H 23 -S_DAG 50 -SQRT_Y 102 -H 94 -CX 116 109 -CZ 18 85 -Z 81 -S 28 -SQRT_X_DAG 99 -S 104 -S 29 -SQRT_X 47 -H 55 -CZ 9 15 -Z 45 -CY 50 52 -SQRT_X_DAG 21 -X 58 -Z 63 -SQRT_X 10 -SQRT_X 108 -SQRT_Y 22 -CY 37 29 -CZ 90 13 -CY 11 66 -CY 30 73 -SQRT_X_DAG 34 -H 106 -SQRT_X_DAG 73 -S_DAG 41 -SQRT_X 55 -H 115 -Z 106 -CY 13 58 -X 43 -S 51 -Y 63 -SQRT_X_DAG 18 -Z 101 -S 24 -SQRT_X_DAG 25 -SQRT_X 41 -H 31 -CZ 24 106 -CY 67 33 -SQRT_Y_DAG 55 -SQRT_X_DAG 67 -CZ 103 63 -CX 29 101 -Z 6 -H 80 -S 5 -SQRT_Y_DAG 49 -SQRT_X_DAG 46 -CZ 41 29 -S_DAG 93 -SQRT_X_DAG 100 -SQRT_Y_DAG 73 -CX 70 64 -S_DAG 43 -Z 57 -CZ 103 83 -CY 104 50 -H 11 -SQRT_X_DAG 38 -S_DAG 23 -SQRT_X_DAG 112 -Y 66 -SQRT_Y_DAG 0 -Y 47 -S_DAG 52 -S_DAG 32 -Z 69 -Y 22 -S_DAG 76 -CY 31 38 -CZ 115 36 -SQRT_Y 96 -SQRT_X 92 -SQRT_Y_DAG 73 -S_DAG 59 -CY 28 80 -Y 67 -SQRT_Y 107 -S_DAG 40 -SQRT_Y_DAG 14 -CZ 11 27 -S_DAG 115 -SQRT_Y 35 -CZ 10 80 -SQRT_X_DAG 32 -S 10 -SQRT_Y_DAG 85 -CX 14 23 -CY 2 79 -Y 115 -CX 56 31 -SQRT_X_DAG 95 -CX 86 13 -S_DAG 108 -SQRT_X_DAG 76 -SQRT_Y 88 -S 8 -S 111 -Z 85 -S_DAG 52 -SQRT_X 110 -H 60 -S 72 -SQRT_X 12 -SQRT_X 51 -Y 3 -CX 111 62 -S 26 -X 96 -X 38 -Z 69 -SQRT_Y 42 -Z 61 -S_DAG 27 -SQRT_Y 32 -X 88 -SQRT_Y_DAG 101 -CX 71 64 -S_DAG 42 -CX 54 50 -SQRT_X 115 -S 55 -X 103 -H 9 -S 85 -SQRT_Y_DAG 0 -S 82 -SQRT_X_DAG 48 -SQRT_X_DAG 82 -SQRT_X_DAG 14 -S_DAG 108 -S_DAG 73 -CZ 65 28 -CX 45 14 -Z 82 -SQRT_X 79 -S_DAG 81 -SQRT_X_DAG 5 -SQRT_Y 36 -Y 20 -S 64 -CZ 23 80 -Z 6 -Z 84 -CX 11 26 -X 32 -SQRT_Y 83 -S_DAG 5 -S 9 -Y 33 -SQRT_X 102 -CY 43 27 -SQRT_X 14 -CY 20 108 -Y 5 -SQRT_X_DAG 108 -Z 60 -SQRT_Y_DAG 37 -SQRT_X 62 -CZ 13 28 -SQRT_X 44 -X 92 -CZ 44 36 -CX 59 91 -X 3 -SQRT_X_DAG 12 -CZ 22 62 -SQRT_X_DAG 103 -SQRT_Y 55 -X 31 -CZ 7 10 -CX 90 63 -CY 81 94 -X 114 -S 79 -SQRT_X_DAG 34 -H 55 -Y 31 -CX 95 31 -CZ 68 79 -SQRT_Y_DAG 98 -CY 112 14 -H 8 -X 67 -SQRT_Y_DAG 79 -CX 111 36 -Y 69 -S 92 -SQRT_Y_DAG 71 -SQRT_Y_DAG 100 -X 116 -X 98 -S 76 -Y 64 -H 83 -SQRT_X 83 -S 27 -Z 62 -SQRT_X 98 -X 86 -CZ 52 79 -Y 30 -S 111 -Y 36 -CZ 37 18 -Z 20 -X 15 -Z 58 -SQRT_Y_DAG 96 -SQRT_X_DAG 16 -SQRT_Y_DAG 94 -CZ 1 24 -SQRT_Y_DAG 76 -SQRT_Y_DAG 26 -CZ 53 69 -H 19 -H 40 -CZ 41 103 -S_DAG 38 -Y 105 -SQRT_X 94 -Y 87 -Z 32 -Y 41 -SQRT_X 117 -SQRT_X_DAG 57 -S 9 -S_DAG 45 -Y 108 -SQRT_X_DAG 97 -Y 46 -SQRT_Y_DAG 101 -SQRT_Y 63 -CX 104 50 -SQRT_Y_DAG 105 -CY 80 50 -Y 103 -SQRT_Y_DAG 79 -SQRT_X 111 -H 85 -Y 115 -S 1 -H 93 -SQRT_X_DAG 104 -SQRT_Y 75 -H 16 -S_DAG 46 -SQRT_Y 70 -S_DAG 74 -X 114 -S_DAG 103 -Z 114 -Z 11 -S_DAG 92 -H 97 -CZ 1 93 -CZ 53 79 -CZ 114 8 -SQRT_Y 101 -S 73 -SQRT_X 52 -S 91 -SQRT_Y_DAG 45 -SQRT_Y 43 -S 111 -SQRT_X_DAG 53 -SQRT_X 58 -Y 15 -S_DAG 9 -CX 64 7 -SQRT_Y 82 -X 57 -Z 16 -S_DAG 24 -Y 99 -Y 2 -Y 26 -Z 62 -CY 105 100 -X 65 -Z 65 -SQRT_Y 103 -SQRT_Y 69 -SQRT_X 100 -SQRT_X_DAG 65 -Y 49 -SQRT_Y 27 -SQRT_X_DAG 73 -SQRT_Y_DAG 86 -SQRT_X_DAG 8 -S_DAG 69 -X 30 -SQRT_X_DAG 53 -Y 2 -SQRT_Y 29 -SQRT_X_DAG 74 -Y 46 -SQRT_Y 56 -CX 34 93 -S 38 -CX 9 17 -X 22 -Y 100 -SQRT_X 24 -X 32 -SQRT_X_DAG 81 -CY 33 3 -CZ 80 68 -Z 22 -Y 55 -Y 2 -X 56 -S 13 -S_DAG 14 -S 58 -S_DAG 66 -Z 33 -Z 87 -S 1 -H 88 -S 55 -X 88 -SQRT_X_DAG 13 -Y 30 -X 55 -SQRT_X 57 -S 31 -Y 14 -S 84 -SQRT_Y 60 -S 92 -SQRT_X 109 -CZ 56 63 -H 10 -CZ 11 98 -Z 50 -S_DAG 88 -X 27 -SQRT_Y 53 -CZ 74 78 -SQRT_X_DAG 102 -SQRT_X_DAG 2 -H 10 -SQRT_Y 88 -SQRT_Y_DAG 109 -Z 40 -SQRT_Y_DAG 45 -SQRT_X_DAG 23 -SQRT_X 117 -SQRT_X_DAG 60 -S_DAG 96 -S_DAG 75 -CX 41 21 -SQRT_X_DAG 114 -X 89 -S 103 -CX 64 1 -CX 83 117 -CY 88 111 -S 26 -X 80 -X 100 -CX 38 115 -SQRT_X 92 -Y 73 -CY 28 29 -CY 36 6 -Y 48 -Z 19 -CY 70 13 -S 98 -SQRT_Y 34 -CX 108 93 -X 116 -SQRT_Y_DAG 85 -H 59 -CY 3 95 -SQRT_X_DAG 103 -CZ 66 59 -Z 87 -SQRT_Y 16 -X 48 -Z 39 -S_DAG 17 -SQRT_X_DAG 117 -S_DAG 81 -S 68 -S 12 -SQRT_Y_DAG 20 -CZ 95 109 -S_DAG 111 -SQRT_Y 44 -CX 82 48 -Y 95 -CX 102 47 -S 0 -H 96 -Y 104 -H 1 -CX 24 13 -S 50 -S 11 -CY 24 12 -Z 19 -H 28 -S 0 -SQRT_Y_DAG 74 -SQRT_Y 64 -CY 91 84 -SQRT_Y_DAG 52 -S_DAG 106 -SQRT_Y 64 -Z 39 -S 117 -SQRT_X_DAG 48 -SQRT_X 58 -SQRT_Y_DAG 51 -CY 15 59 -S_DAG 54 -CZ 8 108 -SQRT_X_DAG 71 -X 23 -SQRT_Y_DAG 29 -CX 64 79 -SQRT_Y 33 -SQRT_Y 7 -Z 67 -CY 32 6 -CX 109 103 -Y 110 -CY 57 38 -SQRT_X 58 -S 110 -SQRT_Y 11 -S 16 -SQRT_X 49 -SQRT_X_DAG 102 -CZ 87 30 -SQRT_Y 20 -CZ 78 73 -CZ 101 2 -H 52 -SQRT_X 91 -S 38 -X 36 -CZ 64 77 -SQRT_X_DAG 30 -Z 104 -Z 93 -CY 24 74 -S 89 -CX 32 83 -SQRT_Y 42 -S_DAG 65 -S_DAG 110 -CY 105 8 -SQRT_X_DAG 43 -SQRT_X 75 -S 104 -SQRT_X_DAG 9 -Y 78 -Z 31 -CX 9 60 -Z 22 -S_DAG 27 -S_DAG 92 -SQRT_Y_DAG 6 -S_DAG 14 -H 17 -SQRT_X_DAG 77 -SQRT_Y 51 -SQRT_Y 6 -SQRT_X 59 -SQRT_Y 38 -CZ 45 29 -CZ 39 55 -X 91 -X 50 -Y 102 -CZ 103 90 -CZ 95 64 -X 15 -CX 82 88 -X 20 -CY 93 105 -CX 117 44 -SQRT_X 17 -Z 76 -H 21 -X 19 -SQRT_Y 66 -S_DAG 56 -SQRT_Y 47 -Z 68 -CY 66 45 -S 17 -S_DAG 89 -X 73 -CZ 12 17 -Y 90 -H 73 -S 79 -CY 41 50 -SQRT_X_DAG 7 -Y 78 -SQRT_X_DAG 45 -SQRT_X_DAG 9 -SQRT_Y_DAG 61 -SQRT_X_DAG 35 -SQRT_Y_DAG 55 -SQRT_Y 86 -Z 20 -Z 82 -CZ 81 100 -CY 22 81 -S 103 -CZ 11 80 -S 41 -S_DAG 104 -H 56 -S 33 -SQRT_X_DAG 27 -CZ 95 50 -S 92 -X 73 -Z 12 -CY 60 111 -SQRT_Y_DAG 106 -CZ 92 98 -Y 50 -S_DAG 94 -SQRT_X_DAG 11 -SQRT_Y_DAG 116 -H 39 -SQRT_Y 63 -SQRT_Y 92 -Y 113 -X 32 -CY 24 79 -SQRT_Y_DAG 106 -Y 5 -X 97 -X 87 -CY 60 107 -SQRT_X 81 -SQRT_X_DAG 73 -Y 39 -SQRT_X 43 -X 49 -CX 33 56 -CZ 71 28 -Y 82 -Y 6 -CZ 80 61 -CZ 109 28 -CZ 105 74 -SQRT_Y_DAG 32 -SQRT_X 78 -S_DAG 59 -CZ 36 32 -SQRT_X 79 -Y 59 -SQRT_X 116 -H 80 -SQRT_X_DAG 117 -CY 71 83 -S_DAG 1 -SQRT_X 28 -CZ 104 68 -Y 8 -SQRT_X_DAG 73 -CY 46 116 -X 14 -CZ 26 14 -X 81 -CX 69 62 -X 60 -Z 107 -SQRT_X 105 -CZ 14 115 -S 79 -S 70 -CZ 25 58 -S 76 -S_DAG 108 -H 96 -CZ 55 41 -S_DAG 87 -CZ 12 76 -H 85 -X 62 -H 93 -H 34 -CY 100 51 -X 53 -SQRT_X_DAG 6 -SQRT_X 106 -H 72 -SQRT_X_DAG 81 -S_DAG 76 -X 114 -SQRT_Y 85 -SQRT_Y 115 -H 115 -Y 4 -X 109 -SQRT_X_DAG 116 -H 13 -S_DAG 80 -CY 45 20 -SQRT_X 38 -H 7 -H 104 -SQRT_X_DAG 1 -SQRT_Y_DAG 80 -SQRT_X_DAG 5 -SQRT_X_DAG 89 -CZ 83 68 -SQRT_X 46 -SQRT_X_DAG 33 -CZ 11 72 -S 8 -H 45 -H 41 -Y 2 -H 67 -SQRT_X 28 -Y 86 -CZ 62 37 -X 99 -CZ 71 91 -CY 94 72 -S_DAG 42 -CX 23 4 -Z 108 -SQRT_Y 92 -CX 44 20 -Y 93 -X 23 -CY 112 60 -S_DAG 36 -CZ 9 110 -S 14 -CY 38 9 -X 93 -CZ 50 27 -SQRT_Y_DAG 58 -Z 18 -H 102 -SQRT_X_DAG 92 -SQRT_Y 14 -S_DAG 10 -CZ 13 109 -H 73 -S 58 -SQRT_Y 39 -CZ 10 88 -SQRT_X_DAG 76 -CX 65 101 -CX 61 96 -CX 54 19 -CY 36 6 -X 21 -SQRT_Y 107 -SQRT_Y_DAG 3 -H 50 -CY 59 45 -Z 104 -SQRT_X 68 -S 96 -Y 116 -Z 40 -S_DAG 4 -X 20 -S 99 -S 116 -CZ 31 84 -H 54 -X 39 -CY 48 91 -Y 108 -Z 117 -H 112 -S 116 -CZ 35 54 -CZ 18 2 -CX 46 14 -X 73 -S 25 -CZ 51 87 -CX 77 106 -CX 63 26 -CX 92 35 -CZ 97 30 -SQRT_X 101 -CY 40 7 -SQRT_Y_DAG 56 -SQRT_X_DAG 88 -CY 68 103 -SQRT_X_DAG 40 -CX 60 90 -CZ 51 37 -Y 49 -X 112 -Y 4 -X 10 -CZ 46 1 -SQRT_Y_DAG 117 -SQRT_X_DAG 3 -Y 38 -SQRT_X_DAG 12 -SQRT_X 96 -CY 62 110 -X 89 -X 8 -S_DAG 3 -H 84 -Z 46 -Y 21 -CY 14 41 -SQRT_Y 43 -Y 114 -SQRT_X 85 -Z 39 -Z 113 -S_DAG 89 -X 71 -CX 95 47 -SQRT_Y_DAG 86 -SQRT_Y 56 -X 94 -X 95 -Z 74 -S_DAG 17 -CX 82 18 -SQRT_Y 98 -CZ 104 7 -S_DAG 85 -SQRT_Y 95 -CY 52 84 -S 24 -CX 65 87 -SQRT_X_DAG 31 -S 97 -Y 85 -SQRT_X_DAG 57 -CZ 109 33 -CX 51 17 -SQRT_Y 57 -Z 62 -S 46 -CX 27 113 -CZ 43 44 -CY 63 106 -CZ 93 54 -CX 101 22 -H 83 -X 37 -H 14 -CX 52 58 -SQRT_X_DAG 3 -Z 93 -Y 11 -X 61 -S 22 -S_DAG 26 -SQRT_Y 38 -S 34 -S 41 -CZ 14 0 -SQRT_X_DAG 71 -S_DAG 113 -SQRT_X_DAG 49 -CX 15 37 -SQRT_X_DAG 24 -CX 108 15 -X 55 -H 36 -SQRT_X_DAG 85 -Y 52 -X 117 -Y 102 -CY 67 91 -Y 105 -CY 94 43 -S_DAG 20 -CZ 76 44 -S 44 -S 24 -Z 0 -H 101 -X 99 -Y 107 -SQRT_Y 53 -SQRT_X_DAG 89 -S_DAG 76 -S 28 -SQRT_X_DAG 73 -Y 98 -Y 90 -CX 5 37 -CZ 23 90 -SQRT_X_DAG 7 -SQRT_X 89 -SQRT_X_DAG 15 -S_DAG 41 -X 53 -CY 56 93 -SQRT_X 27 -SQRT_X 8 -SQRT_X 116 -CY 16 41 -H 76 -SQRT_X 87 -CY 23 92 -SQRT_Y_DAG 63 -Z 115 -CY 61 75 -SQRT_X 61 -SQRT_X 102 -SQRT_X_DAG 56 -CZ 100 81 -S 63 -X 37 -CX 16 19 -Z 19 -CX 43 39 -SQRT_X_DAG 100 -H 1 -Y 39 -S 61 -X 100 -SQRT_X 91 -X 36 -SQRT_Y_DAG 90 -CY 100 51 -CX 87 69 -H 35 -SQRT_Y 79 -Z 51 -S 76 -SQRT_X 12 -CZ 86 99 -SQRT_Y 108 -CZ 93 66 -SQRT_X 90 -SQRT_Y_DAG 13 -S_DAG 110 -Y 67 -CZ 64 97 -CY 64 14 -SQRT_Y 92 -SQRT_X 32 -CY 11 8 -CZ 54 115 -Y 42 -SQRT_X 41 -SQRT_X_DAG 86 -SQRT_X_DAG 1 -SQRT_Y_DAG 0 -Y 102 -SQRT_X_DAG 30 -SQRT_X_DAG 55 -H 71 -CX 101 72 -SQRT_Y 53 -SQRT_Y_DAG 96 -Y 48 -S_DAG 44 -CX 43 79 -SQRT_X_DAG 74 -CX 31 100 -CZ 108 105 -CZ 35 7 -Z 71 -SQRT_X_DAG 61 -CY 68 46 -H 92 -Y 105 -S 0 -Y 57 -H 59 -Z 30 -SQRT_X 3 -X 107 -S 104 -S_DAG 37 -X 45 -SQRT_X 109 -Y 83 -SQRT_X 66 -CY 24 35 -CZ 65 1 -SQRT_Y 44 -H 97 -Y 113 -Z 105 -Y 16 -H 99 -S_DAG 51 -CZ 26 19 -CZ 0 65 -S 32 -S_DAG 66 -SQRT_X 51 -SQRT_X_DAG 74 -Z 97 -Z 98 -Y 36 -S_DAG 24 -H 87 -S 29 -SQRT_X_DAG 47 -Y 66 -Y 79 -CX 67 83 -S_DAG 27 -S 86 -S 89 -Y 112 -CZ 27 111 -CZ 94 19 -CZ 32 12 -H 102 -SQRT_Y_DAG 58 -SQRT_Y 87 -SQRT_Y 70 -SQRT_Y_DAG 110 -X 18 -Y 42 -Y 61 -S_DAG 9 -Z 47 -CY 16 117 -CX 26 85 -CY 102 62 -CZ 69 98 -CY 24 57 -CZ 79 55 -CX 105 36 -SQRT_Y 99 -S_DAG 18 -SQRT_X 7 -H 82 -Y 97 -X 35 -SQRT_Y_DAG 92 -S 6 -CX 37 6 -SQRT_X_DAG 63 -SQRT_X_DAG 82 -S 37 -S 33 -CZ 80 73 -SQRT_X 49 -SQRT_Y_DAG 58 -CZ 79 25 -SQRT_Y_DAG 44 -CX 108 5 -CY 17 6 -SQRT_X_DAG 55 -SQRT_Y_DAG 16 -S 64 -Z 28 -SQRT_X 95 -S_DAG 18 -Y 89 -SQRT_Y_DAG 96 -Y 29 -SQRT_X_DAG 20 -SQRT_Y_DAG 41 -H 93 -SQRT_Y 14 -CZ 97 33 -CY 29 117 -SQRT_Y_DAG 86 -H 69 -X 60 -SQRT_X 52 -SQRT_X 63 -Z 102 -CY 8 12 -CX 82 34 -Y 14 -SQRT_X 48 -Z 50 -X 45 -CX 94 59 -S 40 -Y 25 -H 76 -Y 108 -X 31 -Z 86 -SQRT_X 44 -CX 23 8 -H 97 -SQRT_X 114 -CZ 86 113 -SQRT_Y_DAG 16 -SQRT_Y 107 -H 91 -CX 94 111 -X 5 -Z 84 -CZ 39 51 -SQRT_X_DAG 1 -Z 36 -SQRT_X 39 -S 35 -Z 52 -S 17 -Z 47 -SQRT_Y 25 -S_DAG 51 -SQRT_X 67 -SQRT_Y_DAG 75 -SQRT_Y_DAG 69 -X 76 -SQRT_X_DAG 31 -X 110 -SQRT_Y 82 -CY 88 29 -S_DAG 92 -X 112 -H 78 -CY 50 53 -SQRT_X_DAG 26 -S 98 -X 56 -SQRT_Y 55 -Y 33 -CY 111 73 -Y 53 -H 42 -X 97 -Z 112 -S 93 -SQRT_X 18 -SQRT_X 49 -X 89 -CX 51 9 -Y 23 -CX 42 110 -SQRT_X 50 -SQRT_Y 95 -X 33 -S 90 -S_DAG 22 -S 108 -S_DAG 77 -S 19 -S 84 -CZ 0 42 -SQRT_Y_DAG 47 -SQRT_X 22 -S 79 -SQRT_X 111 -S_DAG 67 -Y 73 -S 103 -CZ 109 95 -S 55 -CX 101 94 -S 25 -CZ 15 96 -SQRT_Y_DAG 68 -S 42 -CZ 27 1 -S 87 -X 5 -X 89 -Z 63 -S_DAG 23 -Z 19 -SQRT_Y_DAG 110 -SQRT_Y 78 -H 10 -CY 65 51 -SQRT_Y 49 -SQRT_Y_DAG 115 -X 62 -X 72 -Z 56 -CY 24 104 -SQRT_X 93 -X 102 -CX 81 31 -X 100 -SQRT_X 3 -S 43 -CX 44 48 -CY 53 96 -Z 59 -Z 7 -H 90 -SQRT_X 16 -SQRT_Y 24 -X 64 -CZ 76 15 -S 54 -SQRT_Y_DAG 2 -SQRT_X_DAG 18 -Z 9 -S_DAG 32 -X 74 -S_DAG 116 -SQRT_Y 69 -H 13 -CZ 93 36 -CX 5 25 -Y 75 -X 78 -S 82 -SQRT_Y_DAG 40 -CX 70 78 -SQRT_X 71 -CZ 10 18 -H 90 -SQRT_X_DAG 0 -SQRT_X 77 -S 101 -S 101 -S_DAG 111 -SQRT_Y_DAG 29 -SQRT_Y 54 -Z 92 -SQRT_X_DAG 29 -H 9 -H 63 -S_DAG 38 -CZ 56 53 -H 4 -X 106 -H 17 -SQRT_Y_DAG 117 -CX 93 29 -CX 39 85 -CY 12 78 -SQRT_X_DAG 66 -SQRT_X 36 -S_DAG 72 -S_DAG 16 -Z 60 -S_DAG 46 -CZ 23 8 -SQRT_X 70 -S 65 -S 103 -CX 91 53 -SQRT_X_DAG 90 -H 68 -S 112 -SQRT_X 3 -Y 64 -SQRT_Y_DAG 64 -SQRT_X_DAG 8 -CX 32 4 -Z 116 -SQRT_Y_DAG 84 -SQRT_Y 38 -Y 91 -H 26 -CZ 24 63 -SQRT_X_DAG 111 -S 45 -CZ 30 24 -SQRT_Y_DAG 68 -Y 28 -S 11 -CZ 30 11 -S 35 -SQRT_Y_DAG 17 -SQRT_X 67 -SQRT_X_DAG 8 -CZ 12 61 -S_DAG 71 -SQRT_X_DAG 27 -Z 57 -CY 45 59 -CZ 50 87 -CZ 38 88 -SQRT_X_DAG 93 -CY 96 76 -CZ 57 72 -CX 23 94 -SQRT_X_DAG 95 -CZ 78 67 -SQRT_Y_DAG 85 -SQRT_Y 89 -SQRT_X_DAG 32 -SQRT_X_DAG 59 -CZ 117 116 -SQRT_X 86 -SQRT_X_DAG 1 -CY 57 101 -X 2 -S 83 -X 95 -S_DAG 76 -SQRT_X_DAG 16 -SQRT_Y 82 -CY 25 31 -CY 41 16 -SQRT_Y_DAG 34 -Z 58 -CY 7 103 -S_DAG 67 -SQRT_Y 67 -Y 100 -CX 31 117 -SQRT_Y 72 -SQRT_Y_DAG 102 -CZ 12 60 -SQRT_X 56 -S_DAG 67 -SQRT_Y 110 -S_DAG 8 -H 37 -Z 14 -S_DAG 96 -SQRT_Y 59 -SQRT_Y 38 -SQRT_Y_DAG 55 -H 112 -SQRT_Y_DAG 47 -SQRT_Y_DAG 70 -SQRT_X_DAG 28 -CY 62 12 -SQRT_X_DAG 105 -Y 44 -CY 44 25 -CZ 108 32 -SQRT_Y 97 -S_DAG 7 -Y 100 -S 16 -CZ 36 32 -CZ 31 50 -CZ 89 97 -Y 81 -SQRT_X_DAG 23 -SQRT_X_DAG 61 -X 6 -X 83 -S 94 -CZ 24 25 -X 39 -CX 101 30 -S_DAG 2 -H 95 -SQRT_X_DAG 25 -X 37 -X 5 -S_DAG 55 -SQRT_Y_DAG 45 -SQRT_Y_DAG 72 -Z 114 -SQRT_Y 93 -S_DAG 50 -S_DAG 8 -SQRT_Y_DAG 14 -S_DAG 81 -S 38 -Z 22 -SQRT_Y 54 -SQRT_X 40 -Y 77 -SQRT_Y_DAG 71 -S 113 -CZ 17 49 -Y 108 -SQRT_Y 101 -CZ 51 2 -CZ 99 116 -SQRT_Y_DAG 63 -Z 97 -SQRT_X 116 -SQRT_X_DAG 71 -CY 117 4 -CX 90 108 -SQRT_X_DAG 37 -CY 34 60 -SQRT_X_DAG 3 -CX 87 83 -X 105 -SQRT_Y_DAG 3 -SQRT_X 68 -Z 50 -Y 93 -SQRT_Y 26 -CX 3 12 -SQRT_Y_DAG 4 -SQRT_Y 113 -SQRT_X 97 -SQRT_Y 2 -S 35 -CZ 41 70 -Z 55 -SQRT_Y 9 -Z 92 -CY 45 7 -CZ 42 68 -H 15 -CZ 70 26 -CX 113 99 -CY 103 14 -Y 112 -SQRT_X_DAG 94 -SQRT_Y_DAG 92 -S 38 -S 30 -Y 40 -SQRT_Y_DAG 102 -CX 21 87 -H 91 -CX 62 49 -S 57 -Z 22 -S 36 -S_DAG 45 -S 65 -S 54 -SQRT_Y_DAG 105 -CY 46 73 -S_DAG 41 -S 21 -CY 9 91 -H 80 -CY 44 8 -Y 86 -SQRT_X_DAG 74 -Y 5 -CY 83 51 -S_DAG 112 -SQRT_X_DAG 60 -CZ 31 110 -SQRT_Y_DAG 59 -X 101 -CZ 41 14 -Z 105 -S 38 -S 3 -S 24 -Z 10 -SQRT_X_DAG 96 -SQRT_Y 37 -S 51 -S_DAG 117 -SQRT_Y_DAG 32 -CX 105 112 -Y 82 -S_DAG 116 -Y 54 -Z 86 -S 108 -S_DAG 72 -S 8 -Z 61 -S_DAG 108 -X 50 -X 63 -SQRT_Y_DAG 63 -SQRT_X 82 -SQRT_X 11 -SQRT_X 97 -CZ 106 71 -S 106 -CY 48 21 -S_DAG 116 -S 31 -Z 94 -CY 84 73 -SQRT_Y 83 -CZ 101 55 -SQRT_Y_DAG 60 -SQRT_Y 82 -S 106 -CY 112 35 -Y 30 -S 109 -X 33 -CX 55 62 -H 69 -SQRT_X_DAG 72 -Z 113 -S_DAG 56 -Y 50 -CX 75 79 -X 34 -X 113 -Y 90 -SQRT_Y 34 -X 97 -SQRT_X_DAG 92 -SQRT_Y 15 -CY 51 65 -SQRT_X 78 -CX 6 90 -Y 59 -SQRT_X_DAG 57 -X 102 -SQRT_X 36 -SQRT_X_DAG 27 -S_DAG 100 -SQRT_X_DAG 57 -CZ 117 29 -CX 116 39 -Y 46 -X 106 -Y 94 -SQRT_Y_DAG 83 -Z 37 -H 65 -X 53 -SQRT_Y 9 -Y 114 -S 65 -Z 42 -Y 60 -CZ 66 60 -X 82 -Y 100 -SQRT_Y_DAG 14 -H 95 -CY 31 64 -SQRT_Y 41 -CZ 5 20 -H 87 -X 36 -X 15 -SQRT_Y_DAG 46 -SQRT_X 115 -CX 115 17 -X 113 -SQRT_X 20 -CY 112 35 -X 25 -SQRT_Y_DAG 50 -CZ 67 79 -SQRT_X_DAG 20 -Z 116 -CY 112 83 -S 65 -SQRT_X 112 -S 72 -CY 53 111 -CY 46 3 -SQRT_Y_DAG 88 -X 19 -X 89 -SQRT_Y_DAG 76 -S 82 -S_DAG 17 -Z 19 -SQRT_Y 102 -X 109 -CX 43 16 -SQRT_Y 17 -SQRT_X_DAG 117 -CX 61 27 -SQRT_X 116 -Z 113 -CZ 25 3 -CY 45 22 -S 13 -CX 26 39 -CX 60 58 -SQRT_X 114 -CY 102 25 -S 98 -CZ 104 38 -X 18 -S 102 -S_DAG 15 -X 92 -X 98 -CZ 0 107 -S 2 -SQRT_X 41 -X 73 -Y 113 -SQRT_Y 25 -H 116 -X 87 -SQRT_X 15 -CY 45 16 -X 56 -H 88 -CZ 37 49 -SQRT_X_DAG 4 -CY 17 83 -SQRT_X 95 -CX 60 82 -CY 85 39 -SQRT_X 48 -S_DAG 86 -CZ 11 113 -CY 63 6 -S_DAG 5 -H 111 -CZ 20 102 -CY 42 13 -S_DAG 61 -S 19 -CY 117 98 -X 104 -CZ 9 106 -CX 98 90 -Y 32 -H 35 -X 101 -SQRT_X_DAG 83 -CY 29 13 -SQRT_Y 104 -H 44 -SQRT_Y_DAG 84 -X 33 -CZ 90 115 -Y 70 -X 22 -SQRT_X 16 -Z 90 -CY 88 71 -Y 30 -S_DAG 14 -H 59 -CX 30 109 -SQRT_Y_DAG 58 -SQRT_Y_DAG 39 -SQRT_Y_DAG 48 -SQRT_X 112 -S 103 -SQRT_X 76 -SQRT_X 46 -CX 30 14 -SQRT_X 70 -H 84 -CX 31 48 -CY 40 74 -CZ 79 18 -SQRT_Y 12 -SQRT_X 70 -CX 40 0 -Z 67 -SQRT_Y_DAG 73 -S_DAG 62 -CY 116 70 -CY 72 18 -X 10 -S_DAG 24 -S 91 -SQRT_X_DAG 95 -H 42 -X 46 -S_DAG 58 -S 51 -H 3 -Y 19 -H 68 -CY 29 112 -Y 99 -H 113 -SQRT_Y 47 -SQRT_Y 58 -S_DAG 4 -Z 10 -Y 50 -CX 27 98 -SQRT_X 4 -X 80 -SQRT_X 49 -Y 27 -SQRT_X_DAG 68 -CY 86 81 -S_DAG 107 -Y 0 -Y 56 -CX 114 1 -CX 82 34 -SQRT_X_DAG 80 -H 103 -CZ 92 79 -CY 91 66 -S_DAG 103 -Z 48 -Z 11 -Y 9 -X 15 -SQRT_X 50 -SQRT_X_DAG 45 -SQRT_Y 101 -Y 74 -Z 61 -X 35 -SQRT_Y 95 -SQRT_X 19 -CX 38 110 -SQRT_Y 92 -SQRT_X 85 -S 4 -CZ 96 82 -X 6 -CX 40 5 -S_DAG 55 -SQRT_X 46 -CX 38 25 -CY 12 96 -Y 22 -SQRT_Y 71 -Y 74 -Y 84 -S 29 -SQRT_X 101 -S 0 -Z 117 -SQRT_X 68 -Z 1 -CX 78 114 -S_DAG 13 -SQRT_Y_DAG 0 -Z 19 -S 45 -SQRT_Y 117 -CZ 66 24 -CZ 110 92 -SQRT_X 12 -X 6 -SQRT_X_DAG 45 -Z 76 -CY 97 38 -S_DAG 65 -S 71 -SQRT_Y_DAG 81 -CZ 25 70 -CX 36 98 -SQRT_Y 21 -S 98 -SQRT_Y 66 -CY 9 27 -S_DAG 54 -SQRT_Y_DAG 17 -SQRT_Y_DAG 105 -H 110 -CY 41 5 -Y 117 -X 31 -CZ 116 51 -SQRT_Y_DAG 37 -Z 34 -H 113 -S 93 -X 7 -SQRT_X 10 -SQRT_Y 11 -H 44 -CZ 84 48 -CY 86 98 -X 44 -S_DAG 41 -S_DAG 4 -S 75 -Z 73 -SQRT_Y_DAG 82 -X 33 -CX 45 70 -X 86 -CX 73 43 -X 63 -CY 19 76 -H 85 -SQRT_X 35 -CX 30 76 -Z 64 -CZ 37 9 -H 106 -S 70 -CY 115 64 -SQRT_X_DAG 28 -S_DAG 91 -S_DAG 68 -CY 89 21 -Y 2 -CZ 99 17 -X 109 -CX 76 92 -SQRT_X_DAG 53 -X 91 -CY 69 2 -SQRT_Y_DAG 79 -S_DAG 44 -SQRT_Y 80 -Y 10 -X 66 -SQRT_Y 54 -SQRT_Y_DAG 5 -SQRT_X_DAG 1 -SQRT_X_DAG 28 -S 70 -CZ 4 84 -Y 35 -Z 74 -S 23 -Z 62 -CX 16 107 -H 29 -Z 49 -CX 77 68 -H 117 -CX 85 55 -Y 24 -CZ 48 79 -CY 61 38 -CY 18 99 -X 5 -CX 4 101 -X 107 -H 3 -SQRT_X 68 -SQRT_X 55 -SQRT_X_DAG 69 -Y 12 -S_DAG 4 -SQRT_X_DAG 69 -CY 32 62 -H 91 -S 99 -S_DAG 89 -SQRT_Y_DAG 66 -SQRT_X 58 -H 23 -S_DAG 84 -S_DAG 89 -CX 63 90 -CZ 12 79 -Y 6 -S_DAG 66 -Y 52 -X 5 -Z 49 -SQRT_X_DAG 76 -CY 7 22 -X 29 -S 24 -Z 57 -SQRT_X 49 -SQRT_X_DAG 59 -S 4 -SQRT_X_DAG 5 -Z 36 -CY 57 19 -CX 84 71 -X 89 -SQRT_Y 23 -CY 63 34 -CX 45 11 -CZ 62 94 -CZ 99 55 -SQRT_X_DAG 57 -SQRT_Y 72 -SQRT_X 33 -Y 58 -CZ 11 57 -CY 116 49 -S_DAG 99 -SQRT_X 41 -CZ 73 113 -S 18 -S_DAG 77 -H 81 -S_DAG 104 -CX 110 80 -S 59 -SQRT_X 35 -SQRT_Y 76 -H 8 -H 67 -S 53 -SQRT_Y_DAG 46 -S 2 -SQRT_Y 101 -SQRT_X_DAG 13 -S 17 -X 43 -CX 97 109 -CX 108 94 -SQRT_Y 25 -CX 16 48 -CX 15 94 -SQRT_Y_DAG 111 -CX 85 23 -SQRT_X 116 -SQRT_Y 0 -CZ 39 108 -SQRT_Y 41 -Z 83 -S_DAG 83 -SQRT_X_DAG 53 -Y 90 -S_DAG 78 -S 90 -SQRT_Y 33 -S_DAG 58 -SQRT_Y 90 -CX 22 32 -Z 58 -Z 70 -SQRT_X 87 -SQRT_Y_DAG 30 -CZ 13 71 -SQRT_X_DAG 98 -SQRT_X_DAG 108 -CZ 81 23 -H 73 -S 38 -Z 20 -H 33 -S 23 -CX 26 52 -SQRT_Y 30 -CY 84 55 -Z 46 -SQRT_Y_DAG 41 -CY 109 44 -CX 6 55 -X 105 -SQRT_Y_DAG 28 -CY 26 104 -SQRT_Y_DAG 102 -CY 51 38 -X 39 -SQRT_Y_DAG 56 -SQRT_Y 77 -H 2 -CY 42 47 -SQRT_Y 37 -Z 88 -S_DAG 20 -CZ 82 64 -SQRT_X_DAG 6 -CX 103 26 -S_DAG 62 -Z 33 -SQRT_Y_DAG 87 -SQRT_Y_DAG 27 -SQRT_Y 13 -SQRT_Y 109 -CY 36 26 -CZ 114 49 -Y 49 -Y 83 -Z 68 -CZ 49 70 -SQRT_X_DAG 50 -CY 113 38 -CY 72 26 -SQRT_X 8 -CY 80 25 -Z 54 -S 99 -S 96 -X 58 -CX 41 71 -S_DAG 79 -Y 27 -H 27 -CZ 55 5 -Y 12 -CZ 39 57 -S 9 -SQRT_Y_DAG 14 -S_DAG 58 -SQRT_Y 10 -H 115 -X 82 -Y 38 -CZ 53 87 -SQRT_X_DAG 51 -S 97 -SQRT_X_DAG 93 -SQRT_X_DAG 112 -SQRT_Y_DAG 25 -SQRT_X 1 -Z 57 -SQRT_Y_DAG 29 -X 107 -Y 55 -S_DAG 17 -SQRT_Y 108 -Y 70 -CZ 50 61 -SQRT_X 72 -SQRT_X 43 -SQRT_Y 3 -X 115 -CX 64 115 -CX 48 96 -H 84 -SQRT_X 101 -Z 29 -SQRT_Y 58 -X 104 -SQRT_Y_DAG 7 -SQRT_X_DAG 23 -S_DAG 1 -SQRT_X_DAG 114 -SQRT_X 59 -CY 101 111 -Z 63 -Z 48 -H 89 -Z 98 -CZ 28 76 -SQRT_Y_DAG 80 -H 38 -CZ 75 49 -SQRT_Y_DAG 82 -SQRT_X 59 -Y 59 -CY 70 106 -CZ 22 53 -CZ 102 2 -SQRT_X 10 -CX 81 9 -S 72 -CX 55 104 -CZ 20 33 -CZ 96 84 -X 64 -S_DAG 101 -SQRT_Y 25 -SQRT_Y_DAG 62 -CY 33 37 -CZ 85 102 -Y 65 -X 23 -H 80 -Z 107 -H 109 -S 101 -SQRT_Y 12 -S_DAG 29 -Y 78 -CY 99 37 -CZ 105 76 -X 117 -S 62 -SQRT_X_DAG 9 -SQRT_Y 39 -CY 30 61 -CZ 106 11 -CX 102 36 -SQRT_X_DAG 105 -X 23 -CY 112 1 -S_DAG 83 -S 53 -SQRT_X 105 -S 91 -CX 112 107 -X 71 -SQRT_Y_DAG 50 -S 67 -CZ 109 64 -CX 59 112 -CZ 112 49 -Y 23 -S 84 -X 20 -SQRT_Y 46 -SQRT_X 61 -SQRT_Y_DAG 0 -X 75 -S 109 -CX 38 88 -X 108 -Z 81 -SQRT_Y 59 -SQRT_X_DAG 109 -SQRT_Y 13 -S 10 -S_DAG 19 -SQRT_Y_DAG 75 -SQRT_Y_DAG 4 -Z 117 -X 34 -SQRT_Y 25 -SQRT_X_DAG 100 -CZ 13 5 -S_DAG 93 -X 103 -S_DAG 109 -Y 82 -H 84 -S_DAG 39 -Z 9 -CZ 83 3 -CZ 112 55 -Z 1 -S_DAG 42 -CY 90 107 -CZ 4 51 -H 25 -SQRT_X_DAG 102 -SQRT_Y_DAG 46 -X 8 -SQRT_X_DAG 100 -CX 6 59 -H 9 -H 15 -CY 82 1 -CZ 86 117 -Y 38 -Z 94 -SQRT_X 53 -SQRT_Y_DAG 86 -SQRT_Y 29 -H 106 -CZ 62 41 -CX 26 80 -SQRT_X_DAG 12 -CZ 93 112 -CX 39 103 -CY 32 20 -H 88 -CX 37 24 -CY 5 47 -SQRT_X 32 -CY 89 99 -SQRT_Y 104 -H 0 -Y 112 -SQRT_Y 41 -H 104 -CX 88 14 -SQRT_X 57 -X 25 -SQRT_X 45 -SQRT_Y_DAG 63 -CX 20 107 -CX 79 68 -CX 68 70 -SQRT_Y 32 -S 108 -S 57 -Y 95 -S 50 -H 54 -S 93 -SQRT_Y_DAG 27 -Z 59 -H 50 -Y 64 -X 90 -S 108 -S_DAG 59 -CY 3 24 -CY 26 52 -CX 112 14 -S_DAG 51 -S_DAG 78 -X 23 -SQRT_X 27 -S 58 -CZ 39 72 -Y 29 -X 2 -CZ 12 43 -H 12 -SQRT_Y 50 -X 92 -H 34 -SQRT_X_DAG 64 -S_DAG 6 -H 114 -CX 107 72 -SQRT_Y 29 -SQRT_Y 57 -Z 57 -S_DAG 3 -CZ 11 82 -Y 87 -CZ 110 5 -SQRT_X 25 -S_DAG 11 -SQRT_X_DAG 87 -CX 33 15 -X 29 -S 57 -SQRT_Y_DAG 84 -SQRT_X_DAG 52 -SQRT_X 23 -S_DAG 57 -X 117 -SQRT_Y_DAG 11 -S_DAG 56 -CX 75 69 -CZ 63 67 -CX 44 0 -SQRT_X 16 -CZ 71 7 -CX 117 19 -SQRT_Y 85 -S 67 -X 83 -CZ 106 0 -CY 16 102 -Y 117 -Z 29 -Y 107 -SQRT_X_DAG 6 -S 38 -S 6 -Y 11 -CY 21 108 -SQRT_Y_DAG 94 -Y 87 -X 103 -S_DAG 84 -CX 109 16 -SQRT_X_DAG 32 -CY 113 74 -H 60 -S 58 -Y 67 -CY 74 26 -S 1 -CZ 24 104 -SQRT_Y 99 -CY 14 77 -CY 24 89 -S_DAG 88 -SQRT_Y_DAG 76 -S 6 -S_DAG 1 -CX 16 101 -SQRT_X_DAG 83 -SQRT_Y 112 -SQRT_Y 92 -CY 58 70 -S 54 -H 12 -SQRT_X 45 -SQRT_X_DAG 38 -Z 8 -S 72 -SQRT_X_DAG 59 -X 89 -S 87 -SQRT_X_DAG 4 -CY 61 44 -CX 113 27 -SQRT_X_DAG 35 -SQRT_X_DAG 61 -Y 90 -Y 63 -X 25 -Y 9 -SQRT_X 111 -S 87 -SQRT_Y 43 -CX 81 103 -S 116 -S_DAG 3 -SQRT_Y_DAG 94 -S_DAG 15 -S_DAG 102 -SQRT_Y_DAG 30 -SQRT_X 82 -X 97 -CY 77 113 -CX 87 111 -CZ 8 43 -X 30 -X 52 -S 25 -SQRT_Y 13 -Z 72 -Z 27 -S 100 -SQRT_Y_DAG 16 -Y 47 -CX 39 9 -CY 106 64 -S 84 -S_DAG 106 -S 12 -SQRT_Y 5 -S 97 -Z 69 -CX 11 50 -CZ 22 75 -Y 80 -SQRT_X 93 -SQRT_X_DAG 58 -Y 84 -SQRT_Y 88 -CZ 102 1 -Y 39 -CZ 21 109 -H 87 -S_DAG 57 -SQRT_Y_DAG 60 -SQRT_X 18 -S 68 -Z 78 -CY 21 81 -H 31 -Z 66 -S 51 -X 88 -SQRT_Y_DAG 95 diff --git a/crates/ppvm-tableau-2/benches/circuits/gen.py b/crates/ppvm-tableau-2/benches/circuits/gen.py index 79b9a408e..55a8e75a7 100644 --- a/crates/ppvm-tableau-2/benches/circuits/gen.py +++ b/crates/ppvm-tableau-2/benches/circuits/gen.py @@ -14,7 +14,8 @@ - mr_unfused: mr_split with a TICK after each MR, which Stim cannot merge: really one target per instruction. -Regenerate with `python3 gen.py` in this directory (seeded, deterministic). +Generate with `python3 gen.py` in this directory (seeded, deterministic); the +output is not tracked. """ import random n = 118 diff --git a/crates/ppvm-tableau-2/benches/circuits/mr_batched.stim b/crates/ppvm-tableau-2/benches/circuits/mr_batched.stim deleted file mode 100644 index 952f78539..000000000 --- a/crates/ppvm-tableau-2/benches/circuits/mr_batched.stim +++ /dev/null @@ -1,100 +0,0 @@ -H 0 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 -CX 54 28 63 104 80 47 84 32 92 95 96 31 8 78 33 37 65 98 4 102 61 97 90 20 11 86 75 67 70 25 6 73 -CX 74 66 43 36 8 71 77 28 75 83 21 18 87 92 55 80 78 40 81 90 25 111 59 85 23 0 97 6 2 9 49 46 -CX 115 55 24 107 20 0 104 54 4 31 7 50 14 47 111 70 76 17 48 66 81 12 95 40 62 80 112 59 30 51 71 10 -CX 55 108 76 75 48 2 44 65 37 29 50 63 51 47 6 7 52 77 54 114 20 88 18 42 24 95 62 101 40 0 46 104 -CX 46 93 10 100 96 49 16 91 82 4 18 6 116 39 33 42 5 115 111 17 30 40 26 20 56 25 52 47 99 104 75 23 -CX 58 3 73 22 93 80 15 4 95 18 53 84 37 65 96 27 7 9 77 67 104 69 90 54 113 34 88 60 71 56 59 82 -CX 74 79 115 10 72 43 13 38 29 99 5 30 14 26 22 83 96 50 101 51 81 16 113 109 71 82 104 62 88 75 92 19 -CX 87 94 80 96 14 49 56 45 85 2 77 103 61 81 62 98 7 31 78 36 22 64 107 60 8 40 11 33 89 66 38 86 -MR 0 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 -H 0 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 -CX 16 69 81 7 40 10 18 64 89 117 22 85 99 3 78 68 0 114 8 96 116 46 61 6 87 67 82 86 102 9 58 93 -CX 72 86 2 35 45 57 10 44 8 22 108 96 73 0 105 50 24 117 85 28 19 62 15 110 67 116 106 76 13 79 69 107 -CX 64 116 39 12 38 11 79 14 73 41 68 56 109 94 74 36 106 102 96 117 101 107 59 75 19 113 103 34 9 85 100 4 -CX 72 111 50 91 16 62 74 83 105 98 93 114 41 85 33 15 73 37 55 28 31 49 69 116 9 51 86 113 32 5 87 89 -CX 98 30 43 10 33 48 13 16 116 62 41 59 52 99 114 65 74 106 4 55 25 50 72 73 87 113 78 79 110 17 112 7 -CX 2 82 107 6 56 0 44 65 104 57 41 102 23 89 85 26 92 113 79 27 84 25 49 42 90 100 35 91 20 38 103 11 -CX 98 79 77 70 9 67 116 61 73 75 69 31 12 63 15 57 64 107 21 14 89 82 41 66 91 48 74 28 94 105 102 29 -CX 14 107 97 11 60 32 73 65 38 105 2 42 77 103 87 101 49 31 55 19 64 72 57 8 113 112 52 69 111 71 94 15 -MR 0 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 -H 0 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 -CX 116 49 15 107 34 48 109 4 11 69 44 9 50 86 60 61 95 85 82 99 20 51 5 33 105 104 93 117 57 1 92 31 -CX 68 16 9 63 51 26 13 107 98 88 91 59 25 113 81 104 55 3 35 2 5 62 94 53 116 76 22 24 64 48 36 10 -CX 70 5 52 90 18 59 20 38 116 83 110 74 37 4 78 67 41 105 104 62 11 111 94 81 86 76 49 89 80 63 33 57 -CX 100 2 53 4 37 98 6 44 25 43 23 31 17 11 5 12 19 39 40 63 75 73 76 60 116 49 96 69 48 30 67 18 -CX 70 98 13 53 117 49 28 71 1 76 105 73 110 40 52 101 112 107 80 85 27 16 55 22 14 72 93 38 94 45 60 63 -CX 98 111 67 20 113 75 110 34 81 89 30 33 9 91 65 41 31 83 78 71 28 106 114 109 16 7 59 45 93 79 101 38 -CX 94 5 54 57 63 111 95 6 22 71 48 103 85 87 113 83 92 40 39 35 20 45 105 67 93 47 11 115 31 46 12 96 -CX 31 91 8 46 86 68 84 111 87 39 5 72 45 66 1 49 110 12 0 99 73 22 59 13 27 47 115 65 17 51 23 56 -MR 0 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 -H 0 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 -CX 108 77 12 65 34 90 61 103 23 47 84 19 15 82 17 9 81 50 20 109 28 36 13 6 116 27 0 76 89 92 1 101 -CX 56 88 24 78 48 38 19 108 43 47 79 113 13 103 114 104 115 109 1 54 57 92 101 21 2 64 33 59 29 10 8 27 -CX 76 92 17 31 101 30 91 109 69 46 66 107 2 73 65 25 57 4 12 22 52 16 86 62 64 29 112 7 47 21 70 85 -CX 20 55 104 68 31 34 44 96 97 81 100 76 114 25 37 75 30 13 69 5 42 39 71 22 16 52 40 99 36 106 2 82 -CX 68 31 3 40 61 6 26 51 12 105 56 90 80 102 110 63 117 70 25 96 57 13 86 81 24 83 22 94 32 77 33 0 -CX 8 103 109 85 114 49 2 116 24 64 42 35 81 72 26 98 61 71 18 57 5 48 65 50 51 7 105 92 37 45 29 25 -CX 58 71 60 115 4 98 70 24 15 41 104 91 38 54 93 31 89 94 20 35 75 34 87 111 74 47 13 84 88 46 107 64 -CX 54 57 104 61 28 12 22 66 76 116 79 59 7 13 10 51 102 96 34 24 83 87 62 73 85 86 45 109 48 69 64 14 -MR 0 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 -H 0 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 -CX 75 1 30 62 49 33 78 107 34 10 20 50 12 100 6 27 44 23 22 37 72 14 29 82 116 53 36 110 18 74 17 77 -CX 41 70 24 15 42 84 63 25 39 79 89 101 5 91 77 115 22 113 112 64 102 97 83 54 14 117 74 72 34 106 0 81 -CX 88 91 106 112 64 38 48 69 49 51 8 40 43 17 33 34 77 56 20 59 24 109 60 11 100 90 15 71 23 32 39 52 -CX 104 37 95 100 38 25 45 83 89 16 85 76 96 27 69 82 13 55 117 33 105 68 97 107 80 30 108 94 72 84 8 43 -CX 6 94 97 57 32 110 81 90 7 84 109 77 31 4 95 45 11 21 85 23 24 115 82 78 5 64 116 91 80 66 1 86 -CX 52 63 99 80 7 69 30 66 111 84 13 104 41 54 22 4 65 79 49 39 45 3 58 43 115 56 116 60 97 83 20 55 -CX 7 86 89 67 42 33 72 6 3 49 19 60 25 108 69 78 93 59 22 28 65 117 102 21 52 58 74 82 0 105 5 111 -CX 79 50 9 90 67 51 0 18 101 28 88 105 112 43 26 13 83 49 40 4 76 115 25 44 45 89 5 82 54 75 39 55 -MR 0 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 -H 0 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 -CX 37 83 64 20 95 21 101 65 72 17 84 105 88 1 109 0 55 27 28 56 12 102 111 114 70 36 66 59 85 23 79 73 -CX 50 24 102 115 90 110 37 40 14 82 15 71 107 61 6 100 106 49 45 101 7 13 59 34 97 74 41 56 16 39 58 103 -CX 16 36 68 108 14 100 75 15 82 10 107 54 55 65 114 106 45 71 111 21 89 73 63 95 91 83 90 77 64 53 37 59 -CX 59 30 26 75 100 68 24 16 7 35 94 1 97 56 57 85 22 115 37 69 38 18 10 23 67 11 84 21 90 52 89 27 -CX 97 53 103 32 43 99 84 19 107 71 29 116 42 95 117 1 112 96 104 92 109 21 80 37 7 14 28 79 86 3 54 75 -CX 0 66 4 22 87 101 76 2 102 68 56 92 94 54 80 18 46 51 28 96 86 72 63 91 71 81 35 15 64 36 3 37 -CX 81 67 97 7 36 114 11 47 45 14 85 64 57 35 90 4 112 70 34 43 44 31 104 62 33 8 29 52 48 60 20 78 -CX 52 58 55 28 116 99 65 80 10 94 64 50 48 24 25 79 17 4 110 18 113 44 45 87 12 32 30 40 39 2 86 92 -MR 0 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 -H 0 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 -CX 55 58 33 83 43 100 91 90 10 110 95 1 34 54 89 19 29 76 47 79 49 0 30 69 108 92 22 28 85 15 24 12 -CX 24 93 80 29 5 52 116 43 18 92 86 1 59 81 76 27 7 79 4 91 97 12 99 117 45 8 48 73 103 102 87 40 -CX 50 37 21 76 116 39 35 74 46 29 28 15 94 48 18 109 36 45 114 67 75 100 6 98 7 11 55 43 105 96 69 30 -CX 39 66 110 36 49 116 69 41 21 19 68 12 84 78 108 105 28 89 38 55 54 33 64 80 79 85 82 46 117 56 57 7 -CX 58 71 52 0 73 15 38 78 64 23 82 9 14 30 46 57 103 84 37 19 35 85 13 93 16 87 62 42 70 29 45 4 -CX 7 15 23 63 58 87 65 11 51 117 73 93 19 9 38 1 80 76 113 31 78 94 29 27 91 16 70 74 109 92 90 105 -CX 20 45 86 108 107 114 7 47 3 94 87 85 63 57 11 4 88 90 104 17 48 82 34 111 102 67 93 23 28 78 36 92 -CX 15 97 73 39 23 4 75 13 61 19 116 32 14 71 100 10 86 42 16 29 98 43 111 56 44 72 91 85 1 92 8 34 -MR 0 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 -H 0 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 -CX 73 70 107 23 1 95 69 83 96 47 76 37 71 21 80 113 62 116 104 46 117 64 98 88 100 54 63 60 44 52 12 86 -CX 9 12 101 105 29 103 41 26 4 89 115 77 75 90 7 114 45 28 98 96 78 53 48 6 85 94 55 102 35 84 74 47 -CX 89 9 13 42 108 22 44 49 59 20 93 112 53 90 60 24 3 64 95 113 25 85 82 23 96 10 76 78 79 101 11 102 -CX 57 109 32 7 100 51 34 105 53 95 30 117 15 8 27 26 103 3 22 52 80 62 56 42 99 86 24 116 11 48 46 87 -CX 11 66 87 58 67 86 71 88 0 25 35 45 106 3 24 31 54 41 47 112 77 56 59 91 92 14 70 4 81 26 43 9 -CX 55 32 90 85 99 31 7 39 12 110 72 18 103 117 46 66 10 100 108 9 56 61 20 30 73 0 68 3 25 26 41 76 -CX 11 74 49 52 65 102 83 48 85 72 43 110 56 36 113 45 24 17 68 80 38 69 12 93 62 44 50 23 84 4 10 63 -CX 87 7 50 19 74 94 17 23 65 8 112 115 71 24 66 106 98 45 89 75 53 22 52 101 35 29 107 47 78 103 1 37 -MR 0 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 -H 0 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 -CX 36 64 97 104 34 106 18 26 115 59 4 101 98 113 14 92 86 11 6 81 83 8 63 17 62 108 78 49 28 87 99 53 -CX 21 6 37 58 5 19 64 83 110 24 26 80 89 20 27 16 2 48 106 101 96 51 78 47 46 42 112 41 90 68 43 99 -CX 7 41 58 98 110 97 20 11 1 92 3 62 87 104 13 24 49 79 67 52 77 14 33 66 55 5 115 27 108 70 0 37 -CX 53 8 42 44 112 74 51 7 77 84 34 16 26 1 20 57 6 78 72 52 88 13 70 4 67 21 90 83 108 82 58 116 -CX 1 109 70 117 107 18 28 0 68 4 65 82 75 87 30 76 90 55 91 13 62 47 42 20 92 84 27 31 10 83 99 21 -CX 88 38 11 10 117 26 16 95 9 57 96 99 33 46 31 18 22 74 98 30 54 109 47 67 68 101 7 69 86 5 35 0 -CX 71 95 74 61 53 24 75 100 14 45 90 42 64 105 111 116 77 96 84 97 33 21 94 115 36 37 62 65 41 104 82 78 -CX 54 14 6 8 111 114 39 78 53 82 49 47 42 73 11 90 18 71 72 95 56 64 109 28 70 32 98 43 83 113 22 5 -MR 0 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 -H 0 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 -CX 14 92 12 56 15 116 60 97 89 44 35 113 40 48 106 21 66 94 68 57 93 64 11 63 43 72 103 41 115 109 70 31 -CX 63 31 62 43 5 21 107 110 7 1 54 87 42 39 70 79 94 116 45 18 106 95 9 80 24 4 22 90 99 55 97 33 -CX 26 20 117 51 96 109 67 23 22 30 105 59 18 78 79 97 104 98 36 72 41 116 86 40 69 73 103 99 108 24 21 90 -CX 4 18 16 3 55 6 44 108 91 57 116 54 27 20 69 45 94 22 101 8 109 33 47 42 112 66 39 87 19 76 61 82 -CX 6 103 59 80 99 53 78 104 11 2 1 98 5 108 3 106 27 47 26 57 105 90 117 28 38 25 71 22 24 73 92 100 -CX 68 27 93 17 72 90 110 12 40 20 69 102 80 59 84 63 2 107 61 8 1 58 109 73 45 44 52 18 15 13 104 103 -CX 103 112 41 105 108 21 82 8 49 2 64 56 0 85 96 77 17 37 35 38 7 60 117 97 72 19 114 63 76 52 4 31 -CX 14 93 49 86 68 67 3 74 39 0 105 2 30 112 91 81 115 35 80 46 92 108 27 78 97 54 95 57 64 6 16 69 -MR 0 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 diff --git a/crates/ppvm-tableau-2/benches/circuits/mr_split.stim b/crates/ppvm-tableau-2/benches/circuits/mr_split.stim deleted file mode 100644 index 0c2ea5bb0..000000000 --- a/crates/ppvm-tableau-2/benches/circuits/mr_split.stim +++ /dev/null @@ -1,1270 +0,0 @@ -H 0 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 -CX 54 28 63 104 80 47 84 32 92 95 96 31 8 78 33 37 65 98 4 102 61 97 90 20 11 86 75 67 70 25 6 73 -CX 74 66 43 36 8 71 77 28 75 83 21 18 87 92 55 80 78 40 81 90 25 111 59 85 23 0 97 6 2 9 49 46 -CX 115 55 24 107 20 0 104 54 4 31 7 50 14 47 111 70 76 17 48 66 81 12 95 40 62 80 112 59 30 51 71 10 -CX 55 108 76 75 48 2 44 65 37 29 50 63 51 47 6 7 52 77 54 114 20 88 18 42 24 95 62 101 40 0 46 104 -CX 46 93 10 100 96 49 16 91 82 4 18 6 116 39 33 42 5 115 111 17 30 40 26 20 56 25 52 47 99 104 75 23 -CX 58 3 73 22 93 80 15 4 95 18 53 84 37 65 96 27 7 9 77 67 104 69 90 54 113 34 88 60 71 56 59 82 -CX 74 79 115 10 72 43 13 38 29 99 5 30 14 26 22 83 96 50 101 51 81 16 113 109 71 82 104 62 88 75 92 19 -CX 87 94 80 96 14 49 56 45 85 2 77 103 61 81 62 98 7 31 78 36 22 64 107 60 8 40 11 33 89 66 38 86 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 16 69 81 7 40 10 18 64 89 117 22 85 99 3 78 68 0 114 8 96 116 46 61 6 87 67 82 86 102 9 58 93 -CX 72 86 2 35 45 57 10 44 8 22 108 96 73 0 105 50 24 117 85 28 19 62 15 110 67 116 106 76 13 79 69 107 -CX 64 116 39 12 38 11 79 14 73 41 68 56 109 94 74 36 106 102 96 117 101 107 59 75 19 113 103 34 9 85 100 4 -CX 72 111 50 91 16 62 74 83 105 98 93 114 41 85 33 15 73 37 55 28 31 49 69 116 9 51 86 113 32 5 87 89 -CX 98 30 43 10 33 48 13 16 116 62 41 59 52 99 114 65 74 106 4 55 25 50 72 73 87 113 78 79 110 17 112 7 -CX 2 82 107 6 56 0 44 65 104 57 41 102 23 89 85 26 92 113 79 27 84 25 49 42 90 100 35 91 20 38 103 11 -CX 98 79 77 70 9 67 116 61 73 75 69 31 12 63 15 57 64 107 21 14 89 82 41 66 91 48 74 28 94 105 102 29 -CX 14 107 97 11 60 32 73 65 38 105 2 42 77 103 87 101 49 31 55 19 64 72 57 8 113 112 52 69 111 71 94 15 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 116 49 15 107 34 48 109 4 11 69 44 9 50 86 60 61 95 85 82 99 20 51 5 33 105 104 93 117 57 1 92 31 -CX 68 16 9 63 51 26 13 107 98 88 91 59 25 113 81 104 55 3 35 2 5 62 94 53 116 76 22 24 64 48 36 10 -CX 70 5 52 90 18 59 20 38 116 83 110 74 37 4 78 67 41 105 104 62 11 111 94 81 86 76 49 89 80 63 33 57 -CX 100 2 53 4 37 98 6 44 25 43 23 31 17 11 5 12 19 39 40 63 75 73 76 60 116 49 96 69 48 30 67 18 -CX 70 98 13 53 117 49 28 71 1 76 105 73 110 40 52 101 112 107 80 85 27 16 55 22 14 72 93 38 94 45 60 63 -CX 98 111 67 20 113 75 110 34 81 89 30 33 9 91 65 41 31 83 78 71 28 106 114 109 16 7 59 45 93 79 101 38 -CX 94 5 54 57 63 111 95 6 22 71 48 103 85 87 113 83 92 40 39 35 20 45 105 67 93 47 11 115 31 46 12 96 -CX 31 91 8 46 86 68 84 111 87 39 5 72 45 66 1 49 110 12 0 99 73 22 59 13 27 47 115 65 17 51 23 56 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 108 77 12 65 34 90 61 103 23 47 84 19 15 82 17 9 81 50 20 109 28 36 13 6 116 27 0 76 89 92 1 101 -CX 56 88 24 78 48 38 19 108 43 47 79 113 13 103 114 104 115 109 1 54 57 92 101 21 2 64 33 59 29 10 8 27 -CX 76 92 17 31 101 30 91 109 69 46 66 107 2 73 65 25 57 4 12 22 52 16 86 62 64 29 112 7 47 21 70 85 -CX 20 55 104 68 31 34 44 96 97 81 100 76 114 25 37 75 30 13 69 5 42 39 71 22 16 52 40 99 36 106 2 82 -CX 68 31 3 40 61 6 26 51 12 105 56 90 80 102 110 63 117 70 25 96 57 13 86 81 24 83 22 94 32 77 33 0 -CX 8 103 109 85 114 49 2 116 24 64 42 35 81 72 26 98 61 71 18 57 5 48 65 50 51 7 105 92 37 45 29 25 -CX 58 71 60 115 4 98 70 24 15 41 104 91 38 54 93 31 89 94 20 35 75 34 87 111 74 47 13 84 88 46 107 64 -CX 54 57 104 61 28 12 22 66 76 116 79 59 7 13 10 51 102 96 34 24 83 87 62 73 85 86 45 109 48 69 64 14 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 75 1 30 62 49 33 78 107 34 10 20 50 12 100 6 27 44 23 22 37 72 14 29 82 116 53 36 110 18 74 17 77 -CX 41 70 24 15 42 84 63 25 39 79 89 101 5 91 77 115 22 113 112 64 102 97 83 54 14 117 74 72 34 106 0 81 -CX 88 91 106 112 64 38 48 69 49 51 8 40 43 17 33 34 77 56 20 59 24 109 60 11 100 90 15 71 23 32 39 52 -CX 104 37 95 100 38 25 45 83 89 16 85 76 96 27 69 82 13 55 117 33 105 68 97 107 80 30 108 94 72 84 8 43 -CX 6 94 97 57 32 110 81 90 7 84 109 77 31 4 95 45 11 21 85 23 24 115 82 78 5 64 116 91 80 66 1 86 -CX 52 63 99 80 7 69 30 66 111 84 13 104 41 54 22 4 65 79 49 39 45 3 58 43 115 56 116 60 97 83 20 55 -CX 7 86 89 67 42 33 72 6 3 49 19 60 25 108 69 78 93 59 22 28 65 117 102 21 52 58 74 82 0 105 5 111 -CX 79 50 9 90 67 51 0 18 101 28 88 105 112 43 26 13 83 49 40 4 76 115 25 44 45 89 5 82 54 75 39 55 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 37 83 64 20 95 21 101 65 72 17 84 105 88 1 109 0 55 27 28 56 12 102 111 114 70 36 66 59 85 23 79 73 -CX 50 24 102 115 90 110 37 40 14 82 15 71 107 61 6 100 106 49 45 101 7 13 59 34 97 74 41 56 16 39 58 103 -CX 16 36 68 108 14 100 75 15 82 10 107 54 55 65 114 106 45 71 111 21 89 73 63 95 91 83 90 77 64 53 37 59 -CX 59 30 26 75 100 68 24 16 7 35 94 1 97 56 57 85 22 115 37 69 38 18 10 23 67 11 84 21 90 52 89 27 -CX 97 53 103 32 43 99 84 19 107 71 29 116 42 95 117 1 112 96 104 92 109 21 80 37 7 14 28 79 86 3 54 75 -CX 0 66 4 22 87 101 76 2 102 68 56 92 94 54 80 18 46 51 28 96 86 72 63 91 71 81 35 15 64 36 3 37 -CX 81 67 97 7 36 114 11 47 45 14 85 64 57 35 90 4 112 70 34 43 44 31 104 62 33 8 29 52 48 60 20 78 -CX 52 58 55 28 116 99 65 80 10 94 64 50 48 24 25 79 17 4 110 18 113 44 45 87 12 32 30 40 39 2 86 92 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 55 58 33 83 43 100 91 90 10 110 95 1 34 54 89 19 29 76 47 79 49 0 30 69 108 92 22 28 85 15 24 12 -CX 24 93 80 29 5 52 116 43 18 92 86 1 59 81 76 27 7 79 4 91 97 12 99 117 45 8 48 73 103 102 87 40 -CX 50 37 21 76 116 39 35 74 46 29 28 15 94 48 18 109 36 45 114 67 75 100 6 98 7 11 55 43 105 96 69 30 -CX 39 66 110 36 49 116 69 41 21 19 68 12 84 78 108 105 28 89 38 55 54 33 64 80 79 85 82 46 117 56 57 7 -CX 58 71 52 0 73 15 38 78 64 23 82 9 14 30 46 57 103 84 37 19 35 85 13 93 16 87 62 42 70 29 45 4 -CX 7 15 23 63 58 87 65 11 51 117 73 93 19 9 38 1 80 76 113 31 78 94 29 27 91 16 70 74 109 92 90 105 -CX 20 45 86 108 107 114 7 47 3 94 87 85 63 57 11 4 88 90 104 17 48 82 34 111 102 67 93 23 28 78 36 92 -CX 15 97 73 39 23 4 75 13 61 19 116 32 14 71 100 10 86 42 16 29 98 43 111 56 44 72 91 85 1 92 8 34 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 73 70 107 23 1 95 69 83 96 47 76 37 71 21 80 113 62 116 104 46 117 64 98 88 100 54 63 60 44 52 12 86 -CX 9 12 101 105 29 103 41 26 4 89 115 77 75 90 7 114 45 28 98 96 78 53 48 6 85 94 55 102 35 84 74 47 -CX 89 9 13 42 108 22 44 49 59 20 93 112 53 90 60 24 3 64 95 113 25 85 82 23 96 10 76 78 79 101 11 102 -CX 57 109 32 7 100 51 34 105 53 95 30 117 15 8 27 26 103 3 22 52 80 62 56 42 99 86 24 116 11 48 46 87 -CX 11 66 87 58 67 86 71 88 0 25 35 45 106 3 24 31 54 41 47 112 77 56 59 91 92 14 70 4 81 26 43 9 -CX 55 32 90 85 99 31 7 39 12 110 72 18 103 117 46 66 10 100 108 9 56 61 20 30 73 0 68 3 25 26 41 76 -CX 11 74 49 52 65 102 83 48 85 72 43 110 56 36 113 45 24 17 68 80 38 69 12 93 62 44 50 23 84 4 10 63 -CX 87 7 50 19 74 94 17 23 65 8 112 115 71 24 66 106 98 45 89 75 53 22 52 101 35 29 107 47 78 103 1 37 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 36 64 97 104 34 106 18 26 115 59 4 101 98 113 14 92 86 11 6 81 83 8 63 17 62 108 78 49 28 87 99 53 -CX 21 6 37 58 5 19 64 83 110 24 26 80 89 20 27 16 2 48 106 101 96 51 78 47 46 42 112 41 90 68 43 99 -CX 7 41 58 98 110 97 20 11 1 92 3 62 87 104 13 24 49 79 67 52 77 14 33 66 55 5 115 27 108 70 0 37 -CX 53 8 42 44 112 74 51 7 77 84 34 16 26 1 20 57 6 78 72 52 88 13 70 4 67 21 90 83 108 82 58 116 -CX 1 109 70 117 107 18 28 0 68 4 65 82 75 87 30 76 90 55 91 13 62 47 42 20 92 84 27 31 10 83 99 21 -CX 88 38 11 10 117 26 16 95 9 57 96 99 33 46 31 18 22 74 98 30 54 109 47 67 68 101 7 69 86 5 35 0 -CX 71 95 74 61 53 24 75 100 14 45 90 42 64 105 111 116 77 96 84 97 33 21 94 115 36 37 62 65 41 104 82 78 -CX 54 14 6 8 111 114 39 78 53 82 49 47 42 73 11 90 18 71 72 95 56 64 109 28 70 32 98 43 83 113 22 5 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 -H 0 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 -CX 14 92 12 56 15 116 60 97 89 44 35 113 40 48 106 21 66 94 68 57 93 64 11 63 43 72 103 41 115 109 70 31 -CX 63 31 62 43 5 21 107 110 7 1 54 87 42 39 70 79 94 116 45 18 106 95 9 80 24 4 22 90 99 55 97 33 -CX 26 20 117 51 96 109 67 23 22 30 105 59 18 78 79 97 104 98 36 72 41 116 86 40 69 73 103 99 108 24 21 90 -CX 4 18 16 3 55 6 44 108 91 57 116 54 27 20 69 45 94 22 101 8 109 33 47 42 112 66 39 87 19 76 61 82 -CX 6 103 59 80 99 53 78 104 11 2 1 98 5 108 3 106 27 47 26 57 105 90 117 28 38 25 71 22 24 73 92 100 -CX 68 27 93 17 72 90 110 12 40 20 69 102 80 59 84 63 2 107 61 8 1 58 109 73 45 44 52 18 15 13 104 103 -CX 103 112 41 105 108 21 82 8 49 2 64 56 0 85 96 77 17 37 35 38 7 60 117 97 72 19 114 63 76 52 4 31 -CX 14 93 49 86 68 67 3 74 39 0 105 2 30 112 91 81 115 35 80 46 92 108 27 78 97 54 95 57 64 6 16 69 -MR 0 -MR 1 -MR 2 -MR 3 -MR 4 -MR 5 -MR 6 -MR 7 -MR 8 -MR 9 -MR 10 -MR 11 -MR 12 -MR 13 -MR 14 -MR 15 -MR 16 -MR 17 -MR 18 -MR 19 -MR 20 -MR 21 -MR 22 -MR 23 -MR 24 -MR 25 -MR 26 -MR 27 -MR 28 -MR 29 -MR 30 -MR 31 -MR 32 -MR 33 -MR 34 -MR 35 -MR 36 -MR 37 -MR 38 -MR 39 -MR 40 -MR 41 -MR 42 -MR 43 -MR 44 -MR 45 -MR 46 -MR 47 -MR 48 -MR 49 -MR 50 -MR 51 -MR 52 -MR 53 -MR 54 -MR 55 -MR 56 -MR 57 -MR 58 -MR 59 -MR 60 -MR 61 -MR 62 -MR 63 -MR 64 -MR 65 -MR 66 -MR 67 -MR 68 -MR 69 -MR 70 -MR 71 -MR 72 -MR 73 -MR 74 -MR 75 -MR 76 -MR 77 -MR 78 -MR 79 -MR 80 -MR 81 -MR 82 -MR 83 -MR 84 -MR 85 -MR 86 -MR 87 -MR 88 -MR 89 -MR 90 -MR 91 -MR 92 -MR 93 -MR 94 -MR 95 -MR 96 -MR 97 -MR 98 -MR 99 -MR 100 -MR 101 -MR 102 -MR 103 -MR 104 -MR 105 -MR 106 -MR 107 -MR 108 -MR 109 -MR 110 -MR 111 -MR 112 -MR 113 -MR 114 -MR 115 -MR 116 -MR 117 diff --git a/crates/ppvm-tableau-2/benches/circuits/mr_unfused.stim b/crates/ppvm-tableau-2/benches/circuits/mr_unfused.stim deleted file mode 100644 index c6d3ff195..000000000 --- a/crates/ppvm-tableau-2/benches/circuits/mr_unfused.stim +++ /dev/null @@ -1,2450 +0,0 @@ -H 0 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 -CX 54 28 63 104 80 47 84 32 92 95 96 31 8 78 33 37 65 98 4 102 61 97 90 20 11 86 75 67 70 25 6 73 -CX 74 66 43 36 8 71 77 28 75 83 21 18 87 92 55 80 78 40 81 90 25 111 59 85 23 0 97 6 2 9 49 46 -CX 115 55 24 107 20 0 104 54 4 31 7 50 14 47 111 70 76 17 48 66 81 12 95 40 62 80 112 59 30 51 71 10 -CX 55 108 76 75 48 2 44 65 37 29 50 63 51 47 6 7 52 77 54 114 20 88 18 42 24 95 62 101 40 0 46 104 -CX 46 93 10 100 96 49 16 91 82 4 18 6 116 39 33 42 5 115 111 17 30 40 26 20 56 25 52 47 99 104 75 23 -CX 58 3 73 22 93 80 15 4 95 18 53 84 37 65 96 27 7 9 77 67 104 69 90 54 113 34 88 60 71 56 59 82 -CX 74 79 115 10 72 43 13 38 29 99 5 30 14 26 22 83 96 50 101 51 81 16 113 109 71 82 104 62 88 75 92 19 -CX 87 94 80 96 14 49 56 45 85 2 77 103 61 81 62 98 7 31 78 36 22 64 107 60 8 40 11 33 89 66 38 86 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 16 69 81 7 40 10 18 64 89 117 22 85 99 3 78 68 0 114 8 96 116 46 61 6 87 67 82 86 102 9 58 93 -CX 72 86 2 35 45 57 10 44 8 22 108 96 73 0 105 50 24 117 85 28 19 62 15 110 67 116 106 76 13 79 69 107 -CX 64 116 39 12 38 11 79 14 73 41 68 56 109 94 74 36 106 102 96 117 101 107 59 75 19 113 103 34 9 85 100 4 -CX 72 111 50 91 16 62 74 83 105 98 93 114 41 85 33 15 73 37 55 28 31 49 69 116 9 51 86 113 32 5 87 89 -CX 98 30 43 10 33 48 13 16 116 62 41 59 52 99 114 65 74 106 4 55 25 50 72 73 87 113 78 79 110 17 112 7 -CX 2 82 107 6 56 0 44 65 104 57 41 102 23 89 85 26 92 113 79 27 84 25 49 42 90 100 35 91 20 38 103 11 -CX 98 79 77 70 9 67 116 61 73 75 69 31 12 63 15 57 64 107 21 14 89 82 41 66 91 48 74 28 94 105 102 29 -CX 14 107 97 11 60 32 73 65 38 105 2 42 77 103 87 101 49 31 55 19 64 72 57 8 113 112 52 69 111 71 94 15 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 116 49 15 107 34 48 109 4 11 69 44 9 50 86 60 61 95 85 82 99 20 51 5 33 105 104 93 117 57 1 92 31 -CX 68 16 9 63 51 26 13 107 98 88 91 59 25 113 81 104 55 3 35 2 5 62 94 53 116 76 22 24 64 48 36 10 -CX 70 5 52 90 18 59 20 38 116 83 110 74 37 4 78 67 41 105 104 62 11 111 94 81 86 76 49 89 80 63 33 57 -CX 100 2 53 4 37 98 6 44 25 43 23 31 17 11 5 12 19 39 40 63 75 73 76 60 116 49 96 69 48 30 67 18 -CX 70 98 13 53 117 49 28 71 1 76 105 73 110 40 52 101 112 107 80 85 27 16 55 22 14 72 93 38 94 45 60 63 -CX 98 111 67 20 113 75 110 34 81 89 30 33 9 91 65 41 31 83 78 71 28 106 114 109 16 7 59 45 93 79 101 38 -CX 94 5 54 57 63 111 95 6 22 71 48 103 85 87 113 83 92 40 39 35 20 45 105 67 93 47 11 115 31 46 12 96 -CX 31 91 8 46 86 68 84 111 87 39 5 72 45 66 1 49 110 12 0 99 73 22 59 13 27 47 115 65 17 51 23 56 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 108 77 12 65 34 90 61 103 23 47 84 19 15 82 17 9 81 50 20 109 28 36 13 6 116 27 0 76 89 92 1 101 -CX 56 88 24 78 48 38 19 108 43 47 79 113 13 103 114 104 115 109 1 54 57 92 101 21 2 64 33 59 29 10 8 27 -CX 76 92 17 31 101 30 91 109 69 46 66 107 2 73 65 25 57 4 12 22 52 16 86 62 64 29 112 7 47 21 70 85 -CX 20 55 104 68 31 34 44 96 97 81 100 76 114 25 37 75 30 13 69 5 42 39 71 22 16 52 40 99 36 106 2 82 -CX 68 31 3 40 61 6 26 51 12 105 56 90 80 102 110 63 117 70 25 96 57 13 86 81 24 83 22 94 32 77 33 0 -CX 8 103 109 85 114 49 2 116 24 64 42 35 81 72 26 98 61 71 18 57 5 48 65 50 51 7 105 92 37 45 29 25 -CX 58 71 60 115 4 98 70 24 15 41 104 91 38 54 93 31 89 94 20 35 75 34 87 111 74 47 13 84 88 46 107 64 -CX 54 57 104 61 28 12 22 66 76 116 79 59 7 13 10 51 102 96 34 24 83 87 62 73 85 86 45 109 48 69 64 14 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 75 1 30 62 49 33 78 107 34 10 20 50 12 100 6 27 44 23 22 37 72 14 29 82 116 53 36 110 18 74 17 77 -CX 41 70 24 15 42 84 63 25 39 79 89 101 5 91 77 115 22 113 112 64 102 97 83 54 14 117 74 72 34 106 0 81 -CX 88 91 106 112 64 38 48 69 49 51 8 40 43 17 33 34 77 56 20 59 24 109 60 11 100 90 15 71 23 32 39 52 -CX 104 37 95 100 38 25 45 83 89 16 85 76 96 27 69 82 13 55 117 33 105 68 97 107 80 30 108 94 72 84 8 43 -CX 6 94 97 57 32 110 81 90 7 84 109 77 31 4 95 45 11 21 85 23 24 115 82 78 5 64 116 91 80 66 1 86 -CX 52 63 99 80 7 69 30 66 111 84 13 104 41 54 22 4 65 79 49 39 45 3 58 43 115 56 116 60 97 83 20 55 -CX 7 86 89 67 42 33 72 6 3 49 19 60 25 108 69 78 93 59 22 28 65 117 102 21 52 58 74 82 0 105 5 111 -CX 79 50 9 90 67 51 0 18 101 28 88 105 112 43 26 13 83 49 40 4 76 115 25 44 45 89 5 82 54 75 39 55 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 37 83 64 20 95 21 101 65 72 17 84 105 88 1 109 0 55 27 28 56 12 102 111 114 70 36 66 59 85 23 79 73 -CX 50 24 102 115 90 110 37 40 14 82 15 71 107 61 6 100 106 49 45 101 7 13 59 34 97 74 41 56 16 39 58 103 -CX 16 36 68 108 14 100 75 15 82 10 107 54 55 65 114 106 45 71 111 21 89 73 63 95 91 83 90 77 64 53 37 59 -CX 59 30 26 75 100 68 24 16 7 35 94 1 97 56 57 85 22 115 37 69 38 18 10 23 67 11 84 21 90 52 89 27 -CX 97 53 103 32 43 99 84 19 107 71 29 116 42 95 117 1 112 96 104 92 109 21 80 37 7 14 28 79 86 3 54 75 -CX 0 66 4 22 87 101 76 2 102 68 56 92 94 54 80 18 46 51 28 96 86 72 63 91 71 81 35 15 64 36 3 37 -CX 81 67 97 7 36 114 11 47 45 14 85 64 57 35 90 4 112 70 34 43 44 31 104 62 33 8 29 52 48 60 20 78 -CX 52 58 55 28 116 99 65 80 10 94 64 50 48 24 25 79 17 4 110 18 113 44 45 87 12 32 30 40 39 2 86 92 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 55 58 33 83 43 100 91 90 10 110 95 1 34 54 89 19 29 76 47 79 49 0 30 69 108 92 22 28 85 15 24 12 -CX 24 93 80 29 5 52 116 43 18 92 86 1 59 81 76 27 7 79 4 91 97 12 99 117 45 8 48 73 103 102 87 40 -CX 50 37 21 76 116 39 35 74 46 29 28 15 94 48 18 109 36 45 114 67 75 100 6 98 7 11 55 43 105 96 69 30 -CX 39 66 110 36 49 116 69 41 21 19 68 12 84 78 108 105 28 89 38 55 54 33 64 80 79 85 82 46 117 56 57 7 -CX 58 71 52 0 73 15 38 78 64 23 82 9 14 30 46 57 103 84 37 19 35 85 13 93 16 87 62 42 70 29 45 4 -CX 7 15 23 63 58 87 65 11 51 117 73 93 19 9 38 1 80 76 113 31 78 94 29 27 91 16 70 74 109 92 90 105 -CX 20 45 86 108 107 114 7 47 3 94 87 85 63 57 11 4 88 90 104 17 48 82 34 111 102 67 93 23 28 78 36 92 -CX 15 97 73 39 23 4 75 13 61 19 116 32 14 71 100 10 86 42 16 29 98 43 111 56 44 72 91 85 1 92 8 34 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 73 70 107 23 1 95 69 83 96 47 76 37 71 21 80 113 62 116 104 46 117 64 98 88 100 54 63 60 44 52 12 86 -CX 9 12 101 105 29 103 41 26 4 89 115 77 75 90 7 114 45 28 98 96 78 53 48 6 85 94 55 102 35 84 74 47 -CX 89 9 13 42 108 22 44 49 59 20 93 112 53 90 60 24 3 64 95 113 25 85 82 23 96 10 76 78 79 101 11 102 -CX 57 109 32 7 100 51 34 105 53 95 30 117 15 8 27 26 103 3 22 52 80 62 56 42 99 86 24 116 11 48 46 87 -CX 11 66 87 58 67 86 71 88 0 25 35 45 106 3 24 31 54 41 47 112 77 56 59 91 92 14 70 4 81 26 43 9 -CX 55 32 90 85 99 31 7 39 12 110 72 18 103 117 46 66 10 100 108 9 56 61 20 30 73 0 68 3 25 26 41 76 -CX 11 74 49 52 65 102 83 48 85 72 43 110 56 36 113 45 24 17 68 80 38 69 12 93 62 44 50 23 84 4 10 63 -CX 87 7 50 19 74 94 17 23 65 8 112 115 71 24 66 106 98 45 89 75 53 22 52 101 35 29 107 47 78 103 1 37 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 36 64 97 104 34 106 18 26 115 59 4 101 98 113 14 92 86 11 6 81 83 8 63 17 62 108 78 49 28 87 99 53 -CX 21 6 37 58 5 19 64 83 110 24 26 80 89 20 27 16 2 48 106 101 96 51 78 47 46 42 112 41 90 68 43 99 -CX 7 41 58 98 110 97 20 11 1 92 3 62 87 104 13 24 49 79 67 52 77 14 33 66 55 5 115 27 108 70 0 37 -CX 53 8 42 44 112 74 51 7 77 84 34 16 26 1 20 57 6 78 72 52 88 13 70 4 67 21 90 83 108 82 58 116 -CX 1 109 70 117 107 18 28 0 68 4 65 82 75 87 30 76 90 55 91 13 62 47 42 20 92 84 27 31 10 83 99 21 -CX 88 38 11 10 117 26 16 95 9 57 96 99 33 46 31 18 22 74 98 30 54 109 47 67 68 101 7 69 86 5 35 0 -CX 71 95 74 61 53 24 75 100 14 45 90 42 64 105 111 116 77 96 84 97 33 21 94 115 36 37 62 65 41 104 82 78 -CX 54 14 6 8 111 114 39 78 53 82 49 47 42 73 11 90 18 71 72 95 56 64 109 28 70 32 98 43 83 113 22 5 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK -H 0 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 -CX 14 92 12 56 15 116 60 97 89 44 35 113 40 48 106 21 66 94 68 57 93 64 11 63 43 72 103 41 115 109 70 31 -CX 63 31 62 43 5 21 107 110 7 1 54 87 42 39 70 79 94 116 45 18 106 95 9 80 24 4 22 90 99 55 97 33 -CX 26 20 117 51 96 109 67 23 22 30 105 59 18 78 79 97 104 98 36 72 41 116 86 40 69 73 103 99 108 24 21 90 -CX 4 18 16 3 55 6 44 108 91 57 116 54 27 20 69 45 94 22 101 8 109 33 47 42 112 66 39 87 19 76 61 82 -CX 6 103 59 80 99 53 78 104 11 2 1 98 5 108 3 106 27 47 26 57 105 90 117 28 38 25 71 22 24 73 92 100 -CX 68 27 93 17 72 90 110 12 40 20 69 102 80 59 84 63 2 107 61 8 1 58 109 73 45 44 52 18 15 13 104 103 -CX 103 112 41 105 108 21 82 8 49 2 64 56 0 85 96 77 17 37 35 38 7 60 117 97 72 19 114 63 76 52 4 31 -CX 14 93 49 86 68 67 3 74 39 0 105 2 30 112 91 81 115 35 80 46 92 108 27 78 97 54 95 57 64 6 16 69 -MR 0 -TICK -MR 1 -TICK -MR 2 -TICK -MR 3 -TICK -MR 4 -TICK -MR 5 -TICK -MR 6 -TICK -MR 7 -TICK -MR 8 -TICK -MR 9 -TICK -MR 10 -TICK -MR 11 -TICK -MR 12 -TICK -MR 13 -TICK -MR 14 -TICK -MR 15 -TICK -MR 16 -TICK -MR 17 -TICK -MR 18 -TICK -MR 19 -TICK -MR 20 -TICK -MR 21 -TICK -MR 22 -TICK -MR 23 -TICK -MR 24 -TICK -MR 25 -TICK -MR 26 -TICK -MR 27 -TICK -MR 28 -TICK -MR 29 -TICK -MR 30 -TICK -MR 31 -TICK -MR 32 -TICK -MR 33 -TICK -MR 34 -TICK -MR 35 -TICK -MR 36 -TICK -MR 37 -TICK -MR 38 -TICK -MR 39 -TICK -MR 40 -TICK -MR 41 -TICK -MR 42 -TICK -MR 43 -TICK -MR 44 -TICK -MR 45 -TICK -MR 46 -TICK -MR 47 -TICK -MR 48 -TICK -MR 49 -TICK -MR 50 -TICK -MR 51 -TICK -MR 52 -TICK -MR 53 -TICK -MR 54 -TICK -MR 55 -TICK -MR 56 -TICK -MR 57 -TICK -MR 58 -TICK -MR 59 -TICK -MR 60 -TICK -MR 61 -TICK -MR 62 -TICK -MR 63 -TICK -MR 64 -TICK -MR 65 -TICK -MR 66 -TICK -MR 67 -TICK -MR 68 -TICK -MR 69 -TICK -MR 70 -TICK -MR 71 -TICK -MR 72 -TICK -MR 73 -TICK -MR 74 -TICK -MR 75 -TICK -MR 76 -TICK -MR 77 -TICK -MR 78 -TICK -MR 79 -TICK -MR 80 -TICK -MR 81 -TICK -MR 82 -TICK -MR 83 -TICK -MR 84 -TICK -MR 85 -TICK -MR 86 -TICK -MR 87 -TICK -MR 88 -TICK -MR 89 -TICK -MR 90 -TICK -MR 91 -TICK -MR 92 -TICK -MR 93 -TICK -MR 94 -TICK -MR 95 -TICK -MR 96 -TICK -MR 97 -TICK -MR 98 -TICK -MR 99 -TICK -MR 100 -TICK -MR 101 -TICK -MR 102 -TICK -MR 103 -TICK -MR 104 -TICK -MR 105 -TICK -MR 106 -TICK -MR 107 -TICK -MR 108 -TICK -MR 109 -TICK -MR 110 -TICK -MR 111 -TICK -MR 112 -TICK -MR 113 -TICK -MR 114 -TICK -MR 115 -TICK -MR 116 -TICK -MR 117 -TICK From 56c076feacb41bd44f7e740932795270b2dfa7a4 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:10:36 +0100 Subject: [PATCH 48/60] perf(ppvm-tableau-2): batch live targets in measure_many with lost qubits One lost target sent the whole instruction down the one-qubit-at-a-time path. Now the live targets run as one tableau batch and lost ones give `None`. Surface d17 with 1% loss per gate: 16.2 -> 7.4 ms/shot. Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/measure.rs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs index b66e7abcf..d51334823 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/measure.rs @@ -45,17 +45,27 @@ impl Measure for GeneralizedTableau { Some(self.sample_z(q)) } - /// With no active and no lost qubits, the whole batch runs on the - /// tableau's `measure_many`; otherwise one target at a time. + /// With no active qubits, the live targets run as one batch on the + /// tableau's `measure_many` and lost ones give `None`; otherwise one target + /// at a time. fn measure_many(&mut self, targets: &[usize]) -> Vec> { - if !self.state.is_zero() || targets.iter().any(|&q| self.is_lost(q)) { + if !self.state.is_zero() { return targets.iter().map(|&q| self.measure(q)).collect(); } - let mut outcomes = vec![false; targets.len()]; + let live: Vec = targets + .iter() + .copied() + .filter(|&q| !self.is_lost(q)) + .collect(); + let mut outcomes = vec![false; live.len()]; let rng = &mut self.rng; self.tableau - .measure_many(targets, || rng.random(), &mut outcomes); - outcomes.into_iter().map(Some).collect() + .measure_many(&live, || rng.random(), &mut outcomes); + let mut outcomes = outcomes.into_iter(); + targets + .iter() + .map(|&q| (!self.is_lost(q)).then(|| outcomes.next().unwrap())) + .collect() } } From bd381b13740737a7941c573e3f3e65bcc876ed08 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:10:59 +0100 Subject: [PATCH 49/60] bench(ppvm-tableau-2): add --loss to run_clifft Applies a loss channel with probability p to each target after every gate; a lost qubit's measurement is recorded as 0. Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/examples/run_clifft.rs | 41 ++++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/crates/ppvm-tableau-2/examples/run_clifft.rs b/crates/ppvm-tableau-2/examples/run_clifft.rs index a56b086d7..9d259ac8d 100644 --- a/crates/ppvm-tableau-2/examples/run_clifft.rs +++ b/crates/ppvm-tableau-2/examples/run_clifft.rs @@ -6,9 +6,13 @@ //! simulator per shot. //! //! ```text -//! cargo run --release -p ppvm-tableau-2 --example run_clifft -- [shots] [--records ] +//! cargo run --release -p ppvm-tableau-2 --example run_clifft -- [shots] [--records ] [--loss

] //! ``` //! +//! `--loss p` applies a loss channel with probability `p` to each target after +//! every gate. A lost qubit's measurement is recorded as `0`; a full reset +//! brings it back. +//! //! Clifft's plain `R_X(α)`, `R_Y(α)`, `R_Z(α)` and `U3(θ, φ, λ)` / `U(…)` //! lines (angles in half-turns) are rewritten into `stim-parser`'s `I[…]` tags before parsing. //! `stim-parser` drops `rec[-k]` targets, so detectors are not evaluated here; @@ -24,7 +28,7 @@ use rand::{RngExt, SeedableRng}; use ppvm_tableau_2::{ActiveDormant, DenseState, GeneralizedTableau}; use ppvm_traits_2::{ - Clifford, Depolarizing2, Measure, PauliError, Reset, RotationOne, TGate, U3Gate, + Clifford, Depolarizing2, LossChannel, Measure, PauliError, Reset, RotationOne, TGate, U3Gate, }; use stim_parser::ast::{Axis, ExtendedInstruction, PauliAxis, PauliFactor, Target}; use stim_parser::instructions::{AnnotationKind, GateName, MeasureName, NoiseName}; @@ -346,7 +350,7 @@ fn run( reset, } => { change_basis(s, basis, q, true); - let m = s.measure(q).expect("no loss"); + let m = s.measure(q).unwrap_or(false); if reset && m { s.x(q); } @@ -361,7 +365,7 @@ fn run( } => { let targets = &measured[at..at + len]; for (m, &q) in s.measure_many(targets).into_iter().zip(targets) { - let m = m.expect("no loss"); + let m = m.unwrap_or(false); if reset && m { s.x(q); } @@ -392,7 +396,7 @@ fn run( for f in &product[1..] { s.cnot(f.qubit, first); } - let m = s.measure(first).expect("no loss"); + let m = s.measure(first).unwrap_or(false); for f in product[1..].iter().rev() { s.cnot(f.qubit, first); } @@ -404,6 +408,20 @@ fn run( } } +/// Loss channel with probability `p` on each target of `op`, if it is a gate. +fn lose_after(s: &mut Sim, op: Op, p: f64) { + match op { + Op::One(_, q) | Op::T(q) | Op::TDag(q) | Op::Rotation(_, _, q) | Op::U3(_, q) => { + s.loss_channel(q, p) + } + Op::Two(_, a, b) => { + s.loss_channel(a, p); + s.loss_channel(b, p); + } + _ => {} + } +} + fn qubits(op: &Op, (factors, measured): (&[PauliFactor], &[usize])) -> Vec { match *op { Op::Two(_, a, b) | Op::Depolarize2(_, a, b) => vec![a, b], @@ -425,11 +443,17 @@ fn main() { let mut args = std::env::args().skip(1); let path = args .next() - .expect("usage: run_clifft [shots] [--records ]"); - let (mut shots, mut records_path) = (1000usize, None); + .expect("usage: run_clifft [shots] [--records ] [--loss

]"); + let (mut shots, mut records_path, mut loss) = (1000usize, None, 0.0); while let Some(arg) = args.next() { if arg == "--records" { records_path = Some(args.next().expect("--records needs a path")); + } else if arg == "--loss" { + loss = args + .next() + .expect("--loss needs p") + .parse() + .expect("loss p"); } else { shots = arg.parse().expect("shots"); } @@ -456,6 +480,9 @@ fn main() { record.clear(); for &op in &ops { run(&mut s, op, pools, &mut rng, &mut record); + if loss > 0.0 { + lose_after(&mut s, op, loss); + } let k = s.state().active().len(); max_k = max_k.max(k); let count = match op { From 98cc632422275b5ce9823d80d53e8953bcd3b7be Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:18:59 +0100 Subject: [PATCH 50/60] fix(ppvm-tableau-2): keep dense branch weights non-negative MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `0.5 * (norm ± ⟨P⟩)` could round to about -1e-16 near an eigenstate of P, tripping the `p >= 0` debug assert in `draw`. Sum ‖½(1 ± P)φ‖² directly instead, so an impossible branch stays O(ε²). Co-Authored-By: Claude Opus 5.5 --- .../src/virtual_state/active_dormant/dense.rs | 57 +++++++++++++++---- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs index f6df8cc5a..5550bb725 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs @@ -127,17 +127,15 @@ impl ActiveState for DenseState { fn branch_weights(&self, p: AxisPauli) -> [f64; 2] { let x = p.x as usize; - let norm: f64 = self.amplitudes.iter().map(|a| a.norm_sqr()).sum(); - // ⟨φ|P|φ⟩ = Σ_α conj(φ[α ⊕ x]) · c(α) · φ[α], real for Hermitian P. - let expectation: f64 = self - .amplitudes - .iter() - .enumerate() - .map(|(alpha, amp)| { - (self.amplitudes[alpha ^ x].conj() * pauli_factor(p, alpha) * amp).re - }) - .sum(); - [0.5 * (norm + expectation), 0.5 * (norm - expectation)] + // ‖½(1 ± P)φ‖² summed directly, with (Pφ)[α] = c(α ⊕ x) · φ[α ⊕ x]: never + // negative, and an impossible branch stays O(ε²) instead of ±ε. + let (mut plus, mut minus) = (0.0, 0.0); + for (alpha, &) in self.amplitudes.iter().enumerate() { + let p_amp = pauli_factor(p, alpha ^ x) * self.amplitudes[alpha ^ x]; + plus += (amp + p_amp).norm_sqr(); + minus += (amp - p_amp).norm_sqr(); + } + [0.25 * plus, 0.25 * minus] } fn collapse(&mut self, p: AxisPauli, m: bool, v: &Rotation) { @@ -392,6 +390,43 @@ mod tests { } } + /// On an eigenstate of `P`, up to rounding, the weights are probabilities: + /// never negative, summing to 1, with the impossible branch at rounding + /// level. + #[test] + fn branch_weights_on_eigenstates() { + let mut rng = Rng(13); + for k in 1..=5 { + for _ in 0..200 { + let (psi, p, q) = (rng.state(k), rng.pauli(k), rng.pauli(k)); + if p.x == 0 && p.z == 0 { + continue; + } + let sign = if rng.next() & 1 == 1 { -1.0 } else { 1.0 }; + let pv = apply(&pauli(p, k), &psi.amplitudes); + let amps: Vec = psi + .amplitudes + .iter() + .zip(&pv) + .map(|(a, b)| a + sign * b) + .collect(); + let norm = amps.iter().map(|a| a.norm_sqr()).sum::().sqrt(); + let mut phi = DenseState { + amplitudes: amps.into_iter().map(|a| a / norm).collect(), + }; + // e^{−iθQ} then e^{iθQ}: the same state, with rounding. + let theta = rng.unit(); + let (cos, sin) = (theta.cos(), theta.sin()); + phi.rotate(q, c(cos, 0.0), c(0.0, -sin)); + phi.rotate(q, c(cos, 0.0), c(0.0, sin)); + let [w0, w1] = phi.branch_weights(p); + assert!(w0 >= 0.0 && w1 >= 0.0, "{w0} {w1}"); + assert!((w0 + w1 - 1.0).abs() < 1e-12); + assert!(w0.min(w1) < 1e-12); + } + } + } + /// `V† ½(1 + (−1)^m P) V` is the projector onto "pivot in |0⟩". #[test] fn rotation_maps_the_branch_to_pivot_zero() { From 9275f1409b866bc34938762af80f67e9b3eec7b0 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:19:02 +0100 Subject: [PATCH 51/60] fix(ppvm-tableau-2): range-check qubits in apply_sign1, ix and iz `apply_sign1` did not check `q`, so on 4 qubits `z(5)` flipped `iz(1)`'s sign; `ix`/`iz` only checked in debug builds. Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/src/inverse_tableau/data.rs | 4 ++-- .../ppvm-tableau-2/src/inverse_tableau/physical_gates.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs index 6294f554c..5a463506d 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/data.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/data.rs @@ -81,13 +81,13 @@ impl InverseTableau { /// Row `ix_q = C⁻¹ X_q C`. pub fn ix(&self, q: usize) -> RowRef<'_> { - debug_assert!(q < self.n); + assert!(q < self.n, "qubit {q} out of range"); self.row(q) } /// Row `iz_q = C⁻¹ Z_q C`. pub fn iz(&self, q: usize) -> RowRef<'_> { - debug_assert!(q < self.n); + assert!(q < self.n, "qubit {q} out of range"); self.row(self.n + q) } diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs index cd49a4603..8a7987292 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/physical_gates.rs @@ -239,6 +239,7 @@ impl ApplyGateRule for InverseTableau { } fn apply_sign1(&mut self, q: usize, rule: impl Fn(bool, bool) -> ([bool; 2], bool)) { + assert!(q < self.n, "qubit {q} out of range"); let ((x_bits, x_flip), (z_bits, z_flip)) = (rule(true, false), rule(false, true)); debug_assert!(x_bits == [true, false] && z_bits == [false, true]); if x_flip { @@ -321,4 +322,12 @@ mod tests { } } } + + /// A sign-only gate past the last qubit would flip another row's sign. + #[test] + #[should_panic(expected = "out of range")] + fn sign_gate_out_of_range_panics() { + use ppvm_traits_2::Clifford; + InverseTableau::new(4).z(5); + } } From db75eb3d5251e4fcb7a90bd69cec6191999fb501 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:23:04 +0100 Subject: [PATCH 52/60] docs(ppvm-traits-2): limit the call-rule-once contract to Pauli containers The inverse tableau evaluates `rule` on the gate's generators, several times per gate, which the doc already allows above; the "exactly once" rule only binds containers that `Phased` can wrap. Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-traits-2/src/gates/apply_gate_rule.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/ppvm-traits-2/src/gates/apply_gate_rule.rs b/crates/ppvm-traits-2/src/gates/apply_gate_rule.rs index 3b5bbf888..9c59d2714 100644 --- a/crates/ppvm-traits-2/src/gates/apply_gate_rule.rs +++ b/crates/ppvm-traits-2/src/gates/apply_gate_rule.rs @@ -27,10 +27,12 @@ use super::Clifford; /// tableau evaluates it on the gate's generators to find which rows to /// multiply. /// -/// With `B = bool`, `apply1` and `apply2` must call `rule` exactly once if they -/// apply the gate, and not at all if they skip it (e.g. on a lost site). Check -/// for loss before calling `rule`. Wrappers such as `Phased` take the sign flip -/// from that one call, so an extra or early call gives a wrong sign. +/// A container of Pauli operators with `B = bool` must call `rule` exactly once +/// in `apply1` and `apply2` if it applies the gate, and not at all if it skips +/// it (e.g. on a lost site). Check for loss before calling `rule`. Wrappers such +/// as `Phased` take the sign flip from that one call, so an extra or early call +/// gives a wrong sign. Types that evaluate `rule` on fixed inputs are not +/// bound by this and cannot be wrapped that way. /// /// Implementing this trait gives [`Clifford`] through the blanket impl, so a /// type must not also implement [`Clifford`] by hand. From 0b37e19bbe079dffb98c0b77e5b945c5ac44e244 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:34:03 +0100 Subject: [PATCH 53/60] perf(ppvm-tableau-2): skip-sample batched one-qubit Pauli channels `pauli_error_many`, `x/y/z_error_many` and `depolarize1_many` jump straight to the next error with a geometric skip instead of drawing one random number per target. Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/noise.rs | 114 +++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs b/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs index 8faa6067f..6a4ec8217 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs @@ -25,6 +25,32 @@ impl GeneralizedTableau { }) } + /// Draw event `k` with probability `probabilities[k] / total`, given that + /// one happens. Rounding falls back to the last possible event. + fn draw_given_event(&mut self, probabilities: &[f64], total: f64) -> usize { + let r = self.rng.random::() * total; + let mut cumulative = 0.0; + probabilities + .iter() + .position(|&p| { + cumulative += p; + r < cumulative + }) + .unwrap_or_else(|| probabilities.iter().rposition(|&p| p > 0.0).unwrap()) + } + + /// First index `≥ i` that gets an error when each target does independently + /// with probability `p`: a geometric skip from one random number. + fn next_error(&mut self, i: usize, p: f64) -> usize { + if p <= 0.0 { + return usize::MAX; + } + let u: f64 = self.rng.random(); + // ln(1 − u) / ln(1 − p) ≥ 0; the cast saturates, so huge skips end the loop. + let skip = ((-u).ln_1p() / (-p).ln_1p()).floor() as usize; + i.saturating_add(skip) + } + /// Apply Pauli `k` (0 = I, 1 = X, 2 = Y, 3 = Z) to `q`. fn pauli(&mut self, q: usize, k: usize) { match k { @@ -36,13 +62,36 @@ impl GeneralizedTableau { } } -/// `X`, `Y`, `Z` with probabilities `p[0]`, `p[1]`, `p[2]`. +/// `X`, `Y`, `Z` with probabilities `p[0]`, `p[1]`, `p[2]`. The batched +/// methods skip straight to the next error instead of drawing per target. impl PauliError for GeneralizedTableau { fn pauli_error(&mut self, q: usize, p: [f64; 3]) { if let Some(k) = self.draw(&p) { self.pauli(q, k + 1); } } + + fn pauli_error_many(&mut self, targets: &[usize], p: [f64; 3]) { + let total: f64 = p.iter().sum(); + let mut i = self.next_error(0, total); + while i < targets.len() { + let k = self.draw_given_event(&p, total); + self.pauli(targets[i], k + 1); + i = self.next_error(i + 1, total); + } + } + + fn x_error_many(&mut self, targets: &[usize], p: f64) { + self.pauli_error_many(targets, [p, 0.0, 0.0]); + } + + fn y_error_many(&mut self, targets: &[usize], p: f64) { + self.pauli_error_many(targets, [0.0, p, 0.0]); + } + + fn z_error_many(&mut self, targets: &[usize], p: f64) { + self.pauli_error_many(targets, [0.0, 0.0, p]); + } } /// `p[k]` is the probability of `P_a ⊗ P_b` with `4a + b = k + 1`, in the @@ -61,6 +110,10 @@ impl Depolarizing for GeneralizedTableau { fn depolarize1(&mut self, q: usize, p: f64) { self.pauli_error(q, [p / 3.0; 3]); } + + fn depolarize1_many(&mut self, targets: &[usize], p: f64) { + self.pauli_error_many(targets, [p / 3.0; 3]); + } } /// Stim's `DEPOLARIZE2(p)`: each of the 15 non-identity Paulis with @@ -154,4 +207,63 @@ mod tests { assert!(within(first, 8.0 * p / 15.0), "{first} of {shots}"); assert!(within(both, 4.0 * p / 15.0), "{both} of {shots}"); } + + /// Batched channels act on each target independently with the channel's + /// rates: per Pauli, at either end of the batch, and on neighbouring pairs. + #[test] + fn batched_channels_match_independent_rates() { + let (n, shots, p) = (400, 100, 0.06); + let targets: Vec = (0..n).collect(); + let samples = |count: usize| (count * shots) as f64; + let within = |hits: usize, trials: f64, rate: f64| { + let sigma = (rate * (1.0 - rate) / trials).sqrt(); + ((hits as f64 / trials) - rate).abs() <= 5.0 * sigma + }; + let channels: [(&str, [f64; 3], fn(&mut Sim, &[usize], f64)); 4] = [ + ("X", [p, 0.0, 0.0], |s, t, p| s.x_error_many(t, p)), + ("Y", [0.0, p, 0.0], |s, t, p| s.y_error_many(t, p)), + ("Z", [0.0, 0.0, p], |s, t, p| s.z_error_many(t, p)), + ("DEPOLARIZE1", [p / 3.0; 3], |s, t, p| { + s.depolarize1_many(t, p) + }), + ]; + for (name, rates, channel) in channels { + let (mut per_pauli, mut ends, mut pairs) = ([0; 3], [0; 2], 0); + for seed in 0..shots { + let got = applied(n, seed as u64, |s| channel(s, &targets, p)); + let hit: Vec = got.iter().map(|&b| b != bits('I')).collect(); + for b in &got { + if let Some(k) = "XYZ".chars().position(|c| bits(c) == *b) { + per_pauli[k] += 1; + } + } + ends[0] += hit[..n / 10].iter().filter(|&&h| h).count(); + ends[1] += hit[n - n / 10..].iter().filter(|&&h| h).count(); + pairs += hit.windows(2).filter(|w| w[0] && w[1]).count(); + } + for k in 0..3 { + assert!( + within(per_pauli[k], samples(n), rates[k]), + "{name}: {per_pauli:?}" + ); + } + for end in ends { + assert!(within(end, samples(n / 10), p), "{name}: ends {ends:?}"); + } + assert!( + within(pairs, samples(n - 1), p * p), + "{name}: pairs {pairs}" + ); + } + } + + /// `p = 0` never applies the Pauli, `p = 1` applies it to every target. + #[test] + fn batched_channels_at_zero_and_one() { + let targets: Vec = (0..50).collect(); + let none = applied(50, 0, |s| s.x_error_many(&targets, 0.0)); + assert!(none.iter().all(|&b| b == bits('I'))); + let all = applied(50, 0, |s| s.x_error_many(&targets, 1.0)); + assert!(all.iter().all(|&b| b == bits('X'))); + } } From 2e47c09e64ac3960fb0411a863a8bf54ebf8d2d7 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:35:16 +0100 Subject: [PATCH 54/60] perf(ppvm-tableau-2): skip-sample batched two-qubit Pauli channels `two_qubit_pauli_error_many` and `depolarize2_many` skip straight to the next pair with an error, as the one-qubit channels do. Co-Authored-By: Claude Opus 5.5 --- .../src/generalized_tableau/noise.rs | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs b/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs index 6a4ec8217..3c1634990 100644 --- a/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs +++ b/crates/ppvm-tableau-2/src/generalized_tableau/noise.rs @@ -103,6 +103,19 @@ impl TwoQubitPauliError for GeneralizedTableau { self.pauli(b, (k + 1) % 4); } } + + /// Skip sampling over pairs, as for `pauli_error_many`. + fn two_qubit_pauli_error_many(&mut self, pairs: &[(usize, usize)], p: [f64; 15]) { + let total: f64 = p.iter().sum(); + let mut i = self.next_error(0, total); + while i < pairs.len() { + let k = self.draw_given_event(&p, total); + let (a, b) = pairs[i]; + self.pauli(a, (k + 1) / 4); + self.pauli(b, (k + 1) % 4); + i = self.next_error(i + 1, total); + } + } } /// Stim's `DEPOLARIZE1(p)`: each of `X`, `Y`, `Z` with probability `p/3`. @@ -122,6 +135,10 @@ impl Depolarizing2 for GeneralizedTableau { fn depolarize2(&mut self, a: usize, b: usize, p: f64) { self.two_qubit_pauli_error(a, b, [p / 15.0; 15]); } + + fn depolarize2_many(&mut self, pairs: &[(usize, usize)], p: f64) { + self.two_qubit_pauli_error_many(pairs, [p / 15.0; 15]); + } } #[cfg(test)] @@ -266,4 +283,71 @@ mod tests { let all = applied(50, 0, |s| s.x_error_many(&targets, 1.0)); assert!(all.iter().all(|&b| b == bits('X'))); } + + /// Batched two-qubit channels act on each pair independently: each of the + /// 15 Paulis at its rate (all distinct, so a mixed-up order shows), at + /// either end of the batch, and on neighbouring pairs. + #[test] + fn batched_two_qubit_channels_match_independent_rates() { + let (n, shots) = (400, 100); + let pairs: Vec<(usize, usize)> = (0..n / 2).map(|i| (2 * i, 2 * i + 1)).collect(); + let rates: [f64; 15] = std::array::from_fn(|k| 0.0005 * (k + 1) as f64); + let total: f64 = rates.iter().sum(); + let order: Vec<(bool, bool, bool, bool)> = "IX IY IZ XI XX XY XZ YI YX YY YZ ZI ZX ZY ZZ" + .split(' ') + .map(|pair| { + let c: Vec = pair.chars().collect(); + let ((xa, za), (xb, zb)) = (bits(c[0]), bits(c[1])); + (xa, za, xb, zb) + }) + .collect(); + let samples = |count: usize| (count * shots) as f64; + let within = |hits: usize, trials: f64, rate: f64| { + let sigma = (rate * (1.0 - rate) / trials).sqrt(); + ((hits as f64 / trials) - rate).abs() <= 5.0 * sigma + }; + let channels: [(&str, [f64; 15], f64, fn(&mut Sim, &[(usize, usize)])); 2] = [ + ("PAULI_CHANNEL_2", rates, total, |s, t| { + s.two_qubit_pauli_error_many(t, std::array::from_fn(|k| 0.0005 * (k + 1) as f64)) + }), + ("DEPOLARIZE2", [0.06 / 15.0; 15], 0.06, |s, t| { + s.depolarize2_many(t, 0.06) + }), + ]; + for (name, rates, total, channel) in channels { + let (mut per_pauli, mut ends, mut both) = ([0; 15], [0; 2], 0); + let tenth = pairs.len() / 10; + for seed in 0..shots { + let got = applied(n, seed as u64, |s| channel(s, &pairs)); + let kinds: Vec> = got + .chunks(2) + .map(|q| { + order + .iter() + .position(|&o| o == (q[0].0, q[0].1, q[1].0, q[1].1)) + }) + .collect(); + for k in kinds.iter().flatten() { + per_pauli[*k] += 1; + } + let hit: Vec = kinds.iter().map(Option::is_some).collect(); + ends[0] += hit[..tenth].iter().filter(|&&h| h).count(); + ends[1] += hit[hit.len() - tenth..].iter().filter(|&&h| h).count(); + both += hit.windows(2).filter(|w| w[0] && w[1]).count(); + } + for k in 0..15 { + assert!( + within(per_pauli[k], samples(pairs.len()), rates[k]), + "{name}: {per_pauli:?}" + ); + } + for end in ends { + assert!(within(end, samples(tenth), total), "{name}: ends {ends:?}"); + } + assert!( + within(both, samples(pairs.len() - 1), total * total), + "{name}: both {both}" + ); + } + } } From 754e90b3dcb41362a2d28632895e5404df0fe372 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 09:41:58 +0100 Subject: [PATCH 55/60] bench(ppvm-tableau-2): batch noise instructions in run_clifft Each noise instruction becomes one `pauli_error_many` or `depolarize2_many` call instead of one call per target, so the harness uses skip sampling. Surface d17 at p = 1e-3: 1.36 -> 1.18 ms/shot. Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/examples/run_clifft.rs | 123 +++++++++++-------- 1 file changed, 71 insertions(+), 52 deletions(-) diff --git a/crates/ppvm-tableau-2/examples/run_clifft.rs b/crates/ppvm-tableau-2/examples/run_clifft.rs index 9d259ac8d..fc564f73b 100644 --- a/crates/ppvm-tableau-2/examples/run_clifft.rs +++ b/crates/ppvm-tableau-2/examples/run_clifft.rs @@ -51,9 +51,19 @@ enum Op { TDag(usize), Rotation(Axis, f64, usize), U3([f64; 3], usize), - /// `X`, `Y`, `Z` with these probabilities. - Pauli([f64; 3], usize), - Depolarize2(f64, usize, usize), + /// `X`, `Y`, `Z` with these probabilities on `noise[at..at + len]`, as one + /// `pauli_error_many`. + Pauli { + p: [f64; 3], + at: usize, + len: usize, + }, + /// `DEPOLARIZE2(p)` on `pairs[at..at + len]`, as one `depolarize2_many`. + Depolarize2 { + p: f64, + at: usize, + len: usize, + }, /// `M`/`MR` on `measured[at..at + len]`, as one `measure_many`; flips and /// resets as for `Measure`. MeasureZ { @@ -119,14 +129,21 @@ fn flip_arg(args: &[f64]) -> f64 { args.first().copied().unwrap_or(0.0) } -/// Flatten the program into ops, unrolling `REPEAT` blocks. MPP factors go to -/// `factors`, Z-basis measurement targets to `measured`. -fn lower( - instructions: &[ExtendedInstruction], - ops: &mut Vec, - factors: &mut Vec, - measured: &mut Vec, -) { +/// Target lists that ops index into with `at..at + len`. +#[derive(Default)] +struct Pools { + /// MPP factors. + factors: Vec, + /// Z-basis measurement targets. + measured: Vec, + /// One-qubit noise targets. + noise: Vec, + /// Two-qubit noise pairs. + pairs: Vec<(usize, usize)>, +} + +/// Flatten the program into ops, unrolling `REPEAT` blocks. +fn lower(instructions: &[ExtendedInstruction], ops: &mut Vec, pools: &mut Pools) { for instruction in instructions { match instruction { ExtendedInstruction::Gate(g) if g.targets.iter().any(|t| t.as_qubit().is_none()) => ops @@ -179,22 +196,24 @@ fn lower( } => ops.extend(targets.iter().map(|&q| Op::U3([*theta, *phi, *lambda], q))), ExtendedInstruction::Noise(n) => { let p = flip_arg(&n.args); - let one = |probabilities: [f64; 3]| { - n.targets.iter().map(move |&q| Op::Pauli(probabilities, q)) - }; - match n.name { - NoiseName::XError => ops.extend(one([p, 0.0, 0.0])), - NoiseName::YError => ops.extend(one([0.0, p, 0.0])), - NoiseName::ZError => ops.extend(one([0.0, 0.0, p])), - NoiseName::Depolarize1 => ops.extend(one([p / 3.0; 3])), - NoiseName::PauliChannel1 => ops.extend(one([n.args[0], n.args[1], n.args[2]])), - NoiseName::Depolarize2 => ops.extend( - n.targets - .chunks(2) - .map(|pair| Op::Depolarize2(p, pair[0], pair[1])), - ), + let one = match n.name { + NoiseName::XError => [p, 0.0, 0.0], + NoiseName::YError => [0.0, p, 0.0], + NoiseName::ZError => [0.0, 0.0, p], + NoiseName::Depolarize1 => [p / 3.0; 3], + NoiseName::PauliChannel1 => [n.args[0], n.args[1], n.args[2]], + NoiseName::Depolarize2 => { + let (at, len) = (pools.pairs.len(), n.targets.len() / 2); + ops.push(Op::Depolarize2 { p, at, len }); + let pairs = n.targets.chunks(2).map(|pair| (pair[0], pair[1])); + pools.pairs.extend(pairs); + continue; + } other => panic!("unsupported noise {other:?}"), - } + }; + let (at, len) = (pools.noise.len(), n.targets.len()); + ops.push(Op::Pauli { p: one, at, len }); + pools.noise.extend_from_slice(&n.targets); } ExtendedInstruction::Measure(m) => { let (basis, reset) = match m.name { @@ -209,12 +228,12 @@ fn lower( let flip = flip_arg(&m.args); if let Basis::Z = basis { ops.push(Op::MeasureZ { - at: measured.len(), + at: pools.measured.len(), len: m.targets.len(), flip, reset, }); - measured.extend_from_slice(&m.targets); + pools.measured.extend_from_slice(&m.targets); continue; } ops.extend(m.targets.iter().map(|&q| Op::Measure { @@ -228,16 +247,16 @@ fn lower( let flip = flip_arg(&mpp.args); for product in &mpp.products { ops.push(Op::Mpp { - at: factors.len(), + at: pools.factors.len(), len: product.len(), flip, }); - factors.extend_from_slice(product); + pools.factors.extend_from_slice(product); } } ExtendedInstruction::Repeat { count, body, .. } => { let mut block = Vec::new(); - lower(body, &mut block, factors, measured); + lower(body, &mut block, pools); for _ in 0..*count { ops.extend_from_slice(&block); } @@ -322,13 +341,7 @@ fn pauli_basis(axis: PauliAxis) -> Basis { /// Run `op`, appending measurement outcomes to `record`; `rng` flips recorded /// outcomes. -fn run( - s: &mut Sim, - op: Op, - (factors, measured): (&[PauliFactor], &[usize]), - rng: &mut SmallRng, - record: &mut Vec, -) { +fn run(s: &mut Sim, op: Op, pools: &Pools, rng: &mut SmallRng, record: &mut Vec) { let mut noisy = |m: bool, flip: f64| m ^ (flip > 0.0 && rng.random::() < flip); match op { Op::One(name, q) => apply_one(s, name, q), @@ -341,8 +354,8 @@ fn run( Axis::Z => s.rz(q, theta), }, Op::U3([theta, phi, lambda], q) => s.u3(q, theta, phi, lambda), - Op::Pauli(p, q) => s.pauli_error(q, p), - Op::Depolarize2(p, a, b) => s.depolarize2(a, b, p), + Op::Pauli { p, at, len } => s.pauli_error_many(&pools.noise[at..at + len], p), + Op::Depolarize2 { p, at, len } => s.depolarize2_many(&pools.pairs[at..at + len], p), Op::Measure { basis, q, @@ -363,7 +376,7 @@ fn run( flip, reset, } => { - let targets = &measured[at..at + len]; + let targets = &pools.measured[at..at + len]; for (m, &q) in s.measure_many(targets).into_iter().zip(targets) { let m = m.unwrap_or(false); if reset && m { @@ -388,7 +401,7 @@ fn run( }, // Basis changes to Z, a CX ladder onto the first qubit, measure, undo. Op::Mpp { at, len, flip } => { - let product = &factors[at..at + len]; + let product = &pools.factors[at..at + len]; let first = product[0].qubit; for f in product { change_basis(s, pauli_basis(f.axis), f.qubit, true); @@ -422,20 +435,27 @@ fn lose_after(s: &mut Sim, op: Op, p: f64) { } } -fn qubits(op: &Op, (factors, measured): (&[PauliFactor], &[usize])) -> Vec { +fn qubits(op: &Op, pools: &Pools) -> Vec { match *op { - Op::Two(_, a, b) | Op::Depolarize2(_, a, b) => vec![a, b], + Op::Two(_, a, b) => vec![a, b], Op::One(_, q) | Op::T(q) | Op::TDag(q) | Op::Rotation(_, _, q) | Op::U3(_, q) - | Op::Pauli(_, q) | Op::Measure { q, .. } | Op::Reset(_, q) | Op::Feedback { q, .. } => vec![q], - Op::Mpp { at, len, .. } => factors[at..at + len].iter().map(|f| f.qubit).collect(), - Op::MeasureZ { at, len, .. } => measured[at..at + len].to_vec(), + Op::Mpp { at, len, .. } => pools.factors[at..at + len] + .iter() + .map(|f| f.qubit) + .collect(), + Op::MeasureZ { at, len, .. } => pools.measured[at..at + len].to_vec(), + Op::Pauli { at, len, .. } => pools.noise[at..at + len].to_vec(), + Op::Depolarize2 { at, len, .. } => pools.pairs[at..at + len] + .iter() + .flat_map(|&(a, b)| [a, b]) + .collect(), } } @@ -461,12 +481,11 @@ fn main() { let src = rewrite_dialect(&std::fs::read_to_string(&path).expect("read circuit")); let program = stim_parser::parse_extended(&src).unwrap_or_else(|e| panic!("parse error: {e:?}")); - let (mut ops, mut factors, mut measured) = (Vec::new(), Vec::new(), Vec::new()); - lower(&program.instructions, &mut ops, &mut factors, &mut measured); - let pools = (&factors[..], &measured[..]); + let (mut ops, mut pools) = (Vec::new(), Pools::default()); + lower(&program.instructions, &mut ops, &mut pools); let n = 1 + ops .iter() - .flat_map(|op| qubits(op, pools)) + .flat_map(|op| qubits(op, &pools)) .max() .expect("empty circuit"); @@ -479,7 +498,7 @@ fn main() { let mut s = Sim::new_with_seed(n, rng.random()); record.clear(); for &op in &ops { - run(&mut s, op, pools, &mut rng, &mut record); + run(&mut s, op, &pools, &mut rng, &mut record); if loss > 0.0 { lose_after(&mut s, op, loss); } From d33ccc0c49f5e4967547f5cd96df62f9e9b8de1e Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 10:50:54 +0100 Subject: [PATCH 56/60] perf(ppvm-tableau-2): transpose four 64x64 blocks at once `transpose_64x64` runs each pass on four side-by-side column blocks, so the compiler vectorizes across them, and `from_rows`/`write_rows` gather a row's words for the group together. Noiseless surface d17: 1.19 -> 1.09 ms/shot; with 1% loss 7.8 -> 7.0 ms/shot. Co-Authored-By: Claude Opus 5.5 --- .../src/inverse_tableau/bitword.rs | 21 ++++--- .../src/inverse_tableau/transposed.rs | 60 ++++++++++++------- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs index 1f007c573..4deb980e5 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/bitword.rs @@ -54,10 +54,15 @@ pub(super) fn set_bits_of(words: impl Iterator) -> impl Iterator }) } -/// Transpose a 64 × 64 bit matrix in place: bit `j` of word `i` swaps with bit -/// `i` of word `j`. Width-specific (64-bit words). +/// Column blocks transposed together by [`transpose_64x64`]. +pub(super) const LANES: usize = 4; + +/// Transpose [`LANES`] independent 64 × 64 bit matrices in place: lane `l` of +/// word `i` is word `i` of matrix `l`, and bit `j` of word `i` swaps with bit +/// `i` of word `j` in each. Running every step on all lanes lets the compiler +/// vectorize across matrices. Width-specific (64-bit words). #[inline(always)] -pub(super) fn transpose_64x64(a: &mut [BitWord; 64]) { +pub(super) fn transpose_64x64(a: &mut [[BitWord; LANES]; 64]) { const PASSES: [(BitWord, usize); 6] = [ (0x5555_5555_5555_5555, 1), (0x3333_3333_3333_3333, 2), @@ -67,11 +72,13 @@ pub(super) fn transpose_64x64(a: &mut [BitWord; 64]) { (0x0000_0000_FFFF_FFFF, 32), ]; for (mask, shift) in PASSES { - for k in 0..64 { - if k & shift == 0 { + for base in (0..64).step_by(2 * shift) { + for k in base..base + shift { let (x, y) = (a[k], a[k + shift]); - a[k] = (x & mask) | ((y & mask) << shift); - a[k + shift] = ((x & !mask) >> shift) | (y & !mask); + for l in 0..LANES { + a[k][l] = (x[l] & mask) | ((y[l] & mask) << shift); + a[k + shift][l] = ((x[l] & !mask) >> shift) | (y[l] & !mask); + } } } } diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs b/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs index 79df8be11..f484ea81e 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs @@ -11,7 +11,7 @@ use ppvm_pauli_kernels::heisenberg; use super::InverseTableau; -use super::bitword::{BitWord, ZERO, flip_bit, get_bit, transpose_64x64}; +use super::bitword::{BitWord, LANES, ZERO, flip_bit, get_bit, transpose_64x64}; pub(super) struct TransposedTableau { n: usize, @@ -39,19 +39,27 @@ impl TransposedTableau { for k in 0..2 { for plane in 0..2 { let dst = if plane == 0 { &mut c.x[k] } else { &mut c.z[k] }; - for_each_block(n, stride, |rows, cols| { - let mut block = [ZERO; 64]; - for (r, word) in block.iter_mut().enumerate() { + for_each_block(stride, |rows, cols, lanes| { + let mut block = [[ZERO; LANES]; 64]; + for (r, words) in block.iter_mut().enumerate() { let i = rows + r; if i < n { let (x, z) = InverseTableau::offsets(n, stride, k, i); - *word = t.words[if plane == 0 { x } else { z } + cols / 64]; + let at = if plane == 0 { x } else { z } + cols / 64; + // A loop: `copy_from_slice` (or a zip) of < 4 words calls `memmove`. + #[allow(clippy::manual_memcpy)] + for l in 0..lanes { + words[l] = t.words[at + l]; + } } } transpose_64x64(&mut block); - for (j, &word) in block.iter().enumerate() { - if cols + j < n { - dst[(cols + j) * stride + rows / 64] = word; + for (j, words) in block.iter().enumerate() { + for (l, &word) in words[..lanes].iter().enumerate() { + let c = cols + 64 * l + j; + if c < n { + dst[c * stride + rows / 64] = word; + } } } }); @@ -71,19 +79,26 @@ impl TransposedTableau { for k in 0..2 { for plane in 0..2 { let src = if plane == 0 { &self.x[k] } else { &self.z[k] }; - for_each_block(n, stride, |rows, cols| { - let mut block = [ZERO; 64]; - for (j, word) in block.iter_mut().enumerate() { - if cols + j < n { - *word = src[(cols + j) * stride + rows / 64]; + for_each_block(stride, |rows, cols, lanes| { + let mut block = [[ZERO; LANES]; 64]; + for (j, words) in block.iter_mut().enumerate() { + for (l, word) in words[..lanes].iter_mut().enumerate() { + let c = cols + 64 * l + j; + if c < n { + *word = src[c * stride + rows / 64]; + } } } transpose_64x64(&mut block); - for (r, &word) in block.iter().enumerate() { + for (r, words) in block.iter().enumerate() { let i = rows + r; if i < n { let (x, z) = InverseTableau::offsets(n, stride, k, i); - t.words[if plane == 0 { x } else { z } + cols / 64] = word; + let at = if plane == 0 { x } else { z } + cols / 64; + #[allow(clippy::manual_memcpy)] + for l in 0..lanes { + t.words[at + l] = words[l]; + } } } }); @@ -141,12 +156,12 @@ impl TransposedTableau { } } -/// Visit the 64 × 64 blocks of an `n × n` bit matrix by their first row and -/// first column. -fn for_each_block(n: usize, stride: usize, mut f: impl FnMut(usize, usize)) { - for rows in (0..stride).map(|w| 64 * w).filter(|&r| r < n) { - for cols in (0..stride).map(|w| 64 * w).filter(|&c| c < n) { - f(rows, cols); +/// Visit the 64 × 64 blocks of a `stride`-word square bit matrix in groups of up to +/// [`LANES`] side by side: first row, first column, and blocks in the group. +fn for_each_block(stride: usize, mut f: impl FnMut(usize, usize, usize)) { + for rows in (0..stride).map(|w| 64 * w) { + for w in (0..stride).step_by(LANES) { + f(rows, 64 * w, LANES.min(stride - w)); } } } @@ -160,7 +175,8 @@ mod tests { /// and the copy's bits and signs are the rows' bits and signs. #[test] fn round_trip_and_bits_match() { - for (i, n) in SIZES.into_iter().enumerate() { + // 300 qubits: one full group of `LANES` column blocks and a partial one. + for (i, n) in SIZES.into_iter().chain([300]).enumerate() { let mut t = InverseTableau::new(n); random_circuit(n, 150, 0x7a + i as u64) .into_iter() From d52b3fe8960a8591aa4dbd5d3ad28e3c1fc23818 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Wed, 18 Nov 2026 11:05:48 +0100 Subject: [PATCH 57/60] perf(ppvm-tableau-2): scan only the X words in the transposed iz_x_support The scan built two slices per column and read the Z bit too; now it steps over word `q / 64` of each column's X bits. Noiseless surface d17: 1.09 -> 0.97 ms/shot; 118-qubit mr_batched 0.26 -> 0.20 ms/shot. Co-Authored-By: Claude Opus 5.5 --- .../src/inverse_tableau/measure.rs | 2 +- .../src/inverse_tableau/transposed.rs | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs index 075362a9d..0fba7d91a 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/measure.rs @@ -107,7 +107,7 @@ impl Collapse for InverseTableau { impl Collapse for TransposedTableau { fn iz_x_support(&self, q: usize, out: &mut Vec) { - out.extend((0..self.num_qubits()).filter(|&c| self.bits(1, q, c)[0])); + self.x_support(1, q, out); } fn iz_z_bit(&self, q: usize, c: usize) -> bool { diff --git a/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs b/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs index f484ea81e..f08cd88fc 100644 --- a/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs +++ b/crates/ppvm-tableau-2/src/inverse_tableau/transposed.rs @@ -11,7 +11,7 @@ use ppvm_pauli_kernels::heisenberg; use super::InverseTableau; -use super::bitword::{BitWord, LANES, ZERO, flip_bit, get_bit, transpose_64x64}; +use super::bitword::{BitWord, LANES, ZERO, bit_position, flip_bit, get_bit, transpose_64x64}; pub(super) struct TransposedTableau { n: usize, @@ -120,12 +120,21 @@ impl TransposedTableau { ] } - pub(super) fn negative(&self, k: usize, i: usize) -> bool { - get_bit(&self.signs[k], i) + /// Columns where row `i` of block `k` has an X bit, ascending, into `out`: + /// word `i / 64` of each column, without touching the Z bits. + pub(super) fn x_support(&self, k: usize, i: usize, out: &mut Vec) { + let (w, mask) = bit_position(i); + let words = self.x[k][w..].iter().step_by(self.stride); + out.extend( + words + .enumerate() + .filter(|&(_, &word)| word & mask != ZERO) + .map(|(c, _)| c), + ); } - pub(super) fn num_qubits(&self) -> usize { - self.n + pub(super) fn negative(&self, k: usize, i: usize) -> bool { + get_bit(&self.signs[k], i) } /// [`InverseTableau::fan_out`] without CZ targets, on columns: the rules From 60dcf57ace831b5b942dfad2606eef708bd879c1 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Thu, 19 Nov 2026 10:54:31 +0100 Subject: [PATCH 58/60] docs(ppvm-traits-2): explain gate order across Clifford implementors Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-traits-2/src/gates/clifford.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/ppvm-traits-2/src/gates/clifford.rs b/crates/ppvm-traits-2/src/gates/clifford.rs index 431f21f4d..74f6167f4 100644 --- a/crates/ppvm-traits-2/src/gates/clifford.rs +++ b/crates/ppvm-traits-2/src/gates/clifford.rs @@ -2,7 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 /// The Clifford gate set, applied in the Heisenberg picture. -/// Consumers implement the gate methods directly; aliases delegate to those methods. +/// +/// Every gate acts by its Heisenberg rule `P ↦ G† P G`; where the gate composes +/// sets the call order. Pauli words and sums apply it to their evolved operators, +/// outside the gates seen so far, so a circuit is fed last gate first. An inverse +/// tableau applies it to the fixed generators, inside, so a circuit is fed in order. +/// +/// Types get this trait from [`ApplyGateRule`](super::ApplyGateRule) through its +/// blanket impl; aliases delegate to the gate methods. pub trait Clifford { /// Apply Pauli `X` to one qubit. fn x(&mut self, qubit: usize); From 27be797dc2433a5cec0516ec8dcfec3f97579ad4 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Thu, 19 Nov 2026 10:55:55 +0100 Subject: [PATCH 59/60] refactor(ppvm-tableau-2): rename Rotation to RotationCollapse Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/src/lib.rs | 2 +- .../src/virtual_state/active_dormant/axis.rs | 6 +++--- .../src/virtual_state/active_dormant/dense.rs | 12 ++++++------ .../src/virtual_state/active_dormant/mod.rs | 8 ++++---- .../src/virtual_state/active_dormant/state.rs | 6 +++--- crates/ppvm-tableau-2/src/virtual_state/mod.rs | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/ppvm-tableau-2/src/lib.rs b/crates/ppvm-tableau-2/src/lib.rs index 10cd2ed91..b8d81fb11 100644 --- a/crates/ppvm-tableau-2/src/lib.rs +++ b/crates/ppvm-tableau-2/src/lib.rs @@ -18,5 +18,5 @@ mod virtual_state; pub use generalized_tableau::GeneralizedTableau; pub use inverse_tableau::{InverseTableau, OwnedRow, QubitMask, RowRef, VirtualView}; pub use virtual_state::{ - ActiveDormant, ActiveState, AxisPauli, DenseState, Rotation, VirtualState, + ActiveDormant, ActiveState, AxisPauli, DenseState, RotationCollapse, VirtualState, }; diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/axis.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/axis.rs index 5f6b80bb3..bc53034d2 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/axis.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/axis.rs @@ -60,10 +60,10 @@ impl ApplyGateRule for Signed { /// `fan_in` to `pivot`, CZ between `pivot` and every axis in `cz`, then `S`, /// `H`, `X` on `pivot` where set. /// -/// Built by [`Rotation::collapse`] so that `V† P V = ±Z_pivot` and the +/// Built by [`RotationCollapse::collapse`] so that `V† P V = ±Z_pivot` and the /// measured branch leaves `pivot` in `|0⟩`; one place decides the gates. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Rotation { +pub struct RotationCollapse { pub pivot: usize, pub fan_out: u64, pub fan_in: u64, @@ -73,7 +73,7 @@ pub struct Rotation { pub x: bool, } -impl Rotation { +impl RotationCollapse { /// The rotation for measuring `p` with outcome `m` (eigenvalue `(−1)^m`): /// after `V`, the projection onto that outcome leaves `pivot` in `|0⟩`. /// diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs index 5550bb725..aeacf7cae 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/dense.rs @@ -5,7 +5,7 @@ use num_complex::Complex64; -use super::axis::{AxisPauli, Rotation, bits}; +use super::axis::{AxisPauli, RotationCollapse, bits}; use super::state::ActiveState; #[derive(Clone, Debug, PartialEq)] @@ -45,7 +45,7 @@ impl DenseState { } /// `φ ← G† φ` for each gate of `v` in order. - fn apply_rotation_dagger(&mut self, v: &Rotation) { + fn apply_rotation_dagger(&mut self, v: &RotationCollapse) { let p = 1usize << v.pivot; for t in bits(v.fan_out) { self.cx(p, 1 << t); @@ -138,7 +138,7 @@ impl ActiveState for DenseState { [0.25 * plus, 0.25 * minus] } - fn collapse(&mut self, p: AxisPauli, m: bool, v: &Rotation) { + fn collapse(&mut self, p: AxisPauli, m: bool, v: &RotationCollapse) { // Project onto eigenvalue (−1)^m: φ ← ½(φ ± P φ). let sign = if m { -0.5 } else { 0.5 }; self.rotate(p, Complex64::new(0.5, 0.0), Complex64::new(sign, 0.0)); @@ -235,7 +235,7 @@ mod tests { } /// `V` = the product of `v`'s gate matrices in gate order. - fn rotation_matrix(v: &Rotation, k: usize) -> Mat { + fn rotation_matrix(v: &RotationCollapse, k: usize) -> Mat { let d = 1 << k; let (o, l, i) = (c(0.0, 0.0), c(1.0, 0.0), c(0.0, 1.0)); let r = std::f64::consts::FRAC_1_SQRT_2; @@ -434,7 +434,7 @@ mod tests { for k in 1..=5 { for _ in 0..30 { let (p, m) = (rng.pauli(k), rng.next() & 1 == 1); - let v = Rotation::collapse(p, m); + let v = RotationCollapse::collapse(p, m); let vm = rotation_matrix(&v, k); let (pm, id) = (pauli(p, k), identity(1 << k)); let s = if m { -0.5 } else { 0.5 }; @@ -472,7 +472,7 @@ mod tests { if phi.branch_weights(p)[usize::from(m)] < 1e-6 { continue; } - let v = Rotation::collapse(p, m); + let v = RotationCollapse::collapse(p, m); let mut got = phi.clone(); got.collapse(p, m, &v); // Project, apply V†, keep the pivot's |0⟩ half, renormalize. diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs index fa17c81cd..0fda26415 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/mod.rs @@ -6,7 +6,7 @@ //! (dormant) virtual qubit in `|0⟩`. //! //! - `axis`: Paulis on the axes ([`AxisPauli`]) and the collapse rotation -//! ([`Rotation`]) shared with the tableau. +//! ([`RotationCollapse`]) shared with the tableau. //! - `state`: the [`ActiveState`] trait. //! - `dense`: [`DenseState`], an exact vector of `2^k` amplitudes. @@ -21,7 +21,7 @@ use ppvm_traits_2::ApplyGateRule; use super::VirtualState; use crate::{QubitMask, RowRef, VirtualView}; -pub use axis::{AxisPauli, Rotation}; +pub use axis::{AxisPauli, RotationCollapse}; pub use dense::DenseState; pub use state::ActiveState; @@ -87,7 +87,7 @@ impl ActiveDormant { } } -/// The view with axis `j` as virtual qubit `active[j]`, so a [`Rotation`]'s +/// The view with axis `j` as virtual qubit `active[j]`, so a [`RotationCollapse`]'s /// gates change the tableau's basis. struct OnAxes<'v, 'a> { t: &'v mut VirtualView<'a>, @@ -167,7 +167,7 @@ impl VirtualState for ActiveDormant { Case::DormantPivot => t.collapse_dormant(q, &self.mask, m), Case::Active { p, negative } => { let m = m != negative; - let v = Rotation::collapse(p, m); + let v = RotationCollapse::collapse(p, m); v.apply_gates(&mut OnAxes { t, active: &self.active, diff --git a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs index b0ddbeffb..6f35894ca 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/active_dormant/state.rs @@ -5,7 +5,7 @@ use num_complex::Complex64; -use super::axis::{AxisPauli, Rotation}; +use super::axis::{AxisPauli, RotationCollapse}; /// Storage for `φ` over the active axes `0..k`. Only three things happen to /// it: a Pauli rotation, an activation, and a collapse; every other change of @@ -27,8 +27,8 @@ pub trait ActiveState: Clone { fn branch_weights(&self, p: AxisPauli) -> [f64; 2]; /// Project onto outcome `m` of `p` (eigenvalue `(−1)^m`), apply `V†` for - /// `v = Rotation::collapse(p, m)`, drop axis `v.pivot` (now `|0⟩`), and + /// `v = RotationCollapse::collapse(p, m)`, drop axis `v.pivot` (now `|0⟩`), and /// renormalize. Axes above the pivot shift down by one. The branch must /// have nonzero weight. - fn collapse(&mut self, p: AxisPauli, m: bool, v: &Rotation); + fn collapse(&mut self, p: AxisPauli, m: bool, v: &RotationCollapse); } diff --git a/crates/ppvm-tableau-2/src/virtual_state/mod.rs b/crates/ppvm-tableau-2/src/virtual_state/mod.rs index baa2cebb8..ee1d05cd1 100644 --- a/crates/ppvm-tableau-2/src/virtual_state/mod.rs +++ b/crates/ppvm-tableau-2/src/virtual_state/mod.rs @@ -13,7 +13,7 @@ use num_complex::Complex64; use crate::VirtualView; -pub use active_dormant::{ActiveDormant, ActiveState, AxisPauli, DenseState, Rotation}; +pub use active_dormant::{ActiveDormant, ActiveState, AxisPauli, DenseState, RotationCollapse}; /// The `2ⁿ`-dimensional state `|χ⟩` of the `n` virtual qubits, in a /// representation of the implementor's choice. From 7bb9f38584db322afadbceca25605d6de32848e3 Mon Sep 17 00:00:00 2001 From: David Plankensteiner Date: Thu, 19 Nov 2026 10:59:45 +0100 Subject: [PATCH 60/60] fix(ppvm-tableau-2): revive lost qubits on MR in run_clifft Co-Authored-By: Claude Opus 5.5 --- crates/ppvm-tableau-2/examples/run_clifft.rs | 29 +++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/crates/ppvm-tableau-2/examples/run_clifft.rs b/crates/ppvm-tableau-2/examples/run_clifft.rs index fc564f73b..12e7cd97d 100644 --- a/crates/ppvm-tableau-2/examples/run_clifft.rs +++ b/crates/ppvm-tableau-2/examples/run_clifft.rs @@ -10,8 +10,8 @@ //! ``` //! //! `--loss p` applies a loss channel with probability `p` to each target after -//! every gate. A lost qubit's measurement is recorded as `0`; a full reset -//! brings it back. +//! every gate. A lost qubit's measurement is recorded as `0`; a reset, also the +//! one in `MR`, brings it back. //! //! Clifft's plain `R_X(α)`, `R_Y(α)`, `R_Z(α)` and `U3(θ, φ, λ)` / `U(…)` //! lines (angles in half-turns) are rewritten into `stim-parser`'s `I[…]` tags before parsing. @@ -363,12 +363,12 @@ fn run(s: &mut Sim, op: Op, pools: &Pools, rng: &mut SmallRng, record: &mut Vec< reset, } => { change_basis(s, basis, q, true); - let m = s.measure(q).unwrap_or(false); - if reset && m { - s.x(q); + let m = s.measure(q); + if reset { + reset_measured(s, q, m); } change_basis(s, basis, q, false); - record.push(noisy(m, flip)); + record.push(noisy(m.unwrap_or(false), flip)); } Op::MeasureZ { at, @@ -378,11 +378,10 @@ fn run(s: &mut Sim, op: Op, pools: &Pools, rng: &mut SmallRng, record: &mut Vec< } => { let targets = &pools.measured[at..at + len]; for (m, &q) in s.measure_many(targets).into_iter().zip(targets) { - let m = m.unwrap_or(false); - if reset && m { - s.x(q); + if reset { + reset_measured(s, q, m); } - record.push(noisy(m, flip)); + record.push(noisy(m.unwrap_or(false), flip)); } } Op::Feedback { pauli, back, q } => { @@ -421,6 +420,16 @@ fn run(s: &mut Sim, op: Op, pools: &Pools, rng: &mut SmallRng, record: &mut Vec< } } +/// Reset `q` to `|0⟩` after measuring `m`: flip a `1`, and revive a lost qubit +/// (`None`) with a full reset. +fn reset_measured(s: &mut Sim, q: usize, m: Option) { + match m { + Some(true) => s.x(q), + Some(false) => {} + None => s.reset(q), + } +} + /// Loss channel with probability `p` on each target of `op`, if it is a gate. fn lose_after(s: &mut Sim, op: Op, p: f64) { match op {