Clifford circuits and stabilizers

Import std.stabilizer explicitly for exact simulation of Clifford circuits over thousands of qubits. Tableau.new(n, seed) starts in |0⟩ⁿ. Qubits are numbered from zero. A tableau is an owned value and clone() copies its state and the position in its seeded random stream.

import std.stabilizer as st

fn main() {
    var t = st.Tableau.new(2, 42)
    t.h(0)
    t.cnot(0, 1)
    assert(t.is_valid(), "symplectic tableau")
    assert(t.measure_expectation_z(0) == st.ZExpectation.Random, "fair Bell branch")
    let bit = t.measure(0)
    assert(t.measure(1) == bit, "correlated Bell measurements")
}

Gates and measurement

The direct gates are h, s, sdg, x, y, z, cnot, cz and swap. Apply gates in execution order. The first argument to cnot is the control. Wire indices must be in range, and controlled gates require distinct operands.

measure_expectation_z(q) returns Zero, One, or Random for probability of measuring one equal to 0, 1, or ½. It reads the tableau without sampling. measure(q) returns a boolean and collapses a random branch using SplitMix64. A deterministic measurement leaves the state and the random stream unchanged. Rotate the wire before measuring to read another Pauli basis. Apply h for X, or apply sdg followed by h for Y.

The Aaronson–Gottesman tableau has n destabilizer rows followed by n stabilizer rows. Each row represents an X mask, a Z mask, and a sign bit. Set both masks at a wire to represent Y. The tableau stores these bits by column, with 64 rows per word, so a gate processes O(⌈n/64⌉) words. Row operations gather X and Z into ⌈n/64⌉ words each and track the phases of Pauli products exactly. is_valid() checks every pair of rows against the symplectic form over 𝔽₂. Aaronson and Gottesman (2004)

stabilizer(i) returns the signed Pauli generator at stabilizer index i. Read its masks through x_words() and z_words() and its sign through is_negative(). Wire zero is the low bit of the first word.

Replay a quantum circuit

Pass a std.quantum.Circuit to t.apply_circuit(c) to replay it on the current tableau. The tableau must have at least as many wires as the recording. The bridge recognizes an uncontrolled Clifford by checking its conjugation action on X and Z up to a global phase. It recognizes a controlled Pauli with a branch phase of 1, i, −1, or −i, including controls on zero. Two controls on a −I target matrix give a phase gate on the control pair. The matrix recognition tolerance is 1e-12 per complex entry. Once a gate is recognized, tableau updates use exact integer operations.

The result is Result((), String). For a non-Clifford gate such as T or Toffoli, the error identifies the gate index, target wire, and control mask. Clean-release checks run at their recorded boundaries. An error leaves the original tableau intact.

Compare operators and states

Start two tableaux with the same wire count, apply each gate sequence, and call a.clifford_eq(b) for exact equality up to global phase. The signed rows encode the images of every X and Z generator under conjugation. Comparing the packed rows takes O(n⌈n/64⌉) word comparisons, independent of the number of gates. A random collapse ends unitary tracking and such a tableau yields false in a Clifford comparison.

Use a.canonical_stabilizers() == b.canonical_stabilizers() to compare the simulated states, including after measurements. The result contains the signed stabilizer subgroup in reduced row echelon form with X columns first and Z columns second. Equality includes the signs, so it distinguishes opposite eigenstates.

Enumerate projective Cliffords

enumerate_cliffords(n) performs breadth-first search with packed tableau keys for n = 1 or 2. Its counts include the unsigned symplectic maps and the full signed projective Cliffords. At one qubit, order_histogram counts the elements of orders 1 through 4.

Qubits Symplectic maps Projective Cliffords Order histogram (orders 1–4)
1 6 24 1, 9, 8, 6
2 720 11,520

The one-qubit projective Clifford group is isomorphic to S₄. The two-qubit symplectic group is isomorphic to S₆.

symplectic_order(n) evaluates 2^(n²) ∏ⱼ₌₁ⁿ (4ʲ − 1). clifford_order(n) multiplies this by 4ⁿ to include all generator sign choices. Both return an exact Some(u128) for 0 through 7 qubits and None outside that range.

Run the example to enumerate both groups, verify seven Clifford identities and a Bell replay, time all 1,024 local checks of a linear cluster, and time the collapse and readout of every wire in a 2,048-qubit GHZ state. Runnable example

out/jihoo run examples/stabilizer.jh
Esc