Examples and Workflows
Three short, task-driven Jupyter notebooks plus an optional under-the-hood deep dive in examples/, alongside the reproducible 980-point sweep script in benchmarks/.
1. Check, Measure, Fix (5-Minute Quickstart)
examples/01_check_measure_fix.ipynb walks through the core workflow end-to-end: certify a code with pauli_exact (both fidelity and full_channel=True), measure the exact true-vs-Pauli mismatch with logical_channel, restore exactness by twirling only the minimum subset from minimal_twirl_set, and see PauliTwirlApproximationWarning plus method="exact_channel" and Qiskit SamplerV2 method="aer_circuit" verification in action.
(0, 1, 2), and verify gap == 1.0.import paulicert as pc
steane = pc.get_preset_code("7-1-3-steane")
noise = pc.CHANNEL_FAMILIES["coherent_x"](0.01)
# 1. Check & measure before twirling
cert = pc.pauli_exact(steane, noise)
lc = pc.logical_channel(steane, noise)
print(f"Before twirling : exact={cert.exact} | witnesses={cert.num_witnesses} | gap={lc.gap:.6f}")
# Before twirling : exact=False | witnesses=7 | gap=2.933232
# 2. Twirl only the provably minimal 3 qubits (0, 1, 2)
ts = pc.minimal_twirl_set(steane, noise)
pauli_ch = pc.pauli_channel(*pc.pauli_twirl(noise)[1:])
twirled = [pauli_ch if q in ts.qubits else noise for q in range(steane.n)]
cert_tw = pc.pauli_exact(steane, twirled)
print(f"Qubits to twirl : {ts.qubits} ({len(ts.qubits)}/{steane.n} qubits, provably_minimal={ts.provably_minimal})")
print(f"After twirling : exact={cert_tw.exact} | witnesses={cert_tw.num_witnesses} | gap={pc.logical_channel(steane, twirled).gap:.6f}")
# Qubits to twirl : (0, 1, 2) (3/7 qubits, provably_minimal=True)
# After twirling : exact=True | witnesses=0 | gap=1.000000
2. Bring Your Own Code and Hardware Noise
examples/02_custom_codes_and_hardware_noise.ipynb shows how to plug in your own stabilizers ([[9, 1, 3]] rotated surface code vs. XZZX deformation), per-qubit hardware T1/T2 thermal_relaxation calibration data, custom composite Qiskit Kraus channels, decoder comparisons (min_weight, mpe, ml), and physical-order Qiskit circuits.
import paulicert as pc
surf = pc.from_pauli_list(
["IIIIXXIXX", "XXIXXIIII", "IIIIIIXXI", "IXXIIIIII",
"IIIZZIZZI", "IZZIZZIII", "IIIIIZIIZ", "ZIIZIIIII"],
d=3,
)
xzzx = pc.deform_stabilizer_code(surf, {1: "H", 3: "H", 5: "H", 7: "H"})
# Per-qubit T1/T2 (us) over a 1.0 us idle cycle
t1 = [110, 95, 130, 85, 120, 105, 90, 140, 100]
t2 = [80, 70, 95, 60, 85, 75, 65, 100, 72]
hw_noise = [pc.thermal_relaxation(a, b, time=1.0) for a, b in zip(t1, t2)]
for label, code in [("CSS ", surf), ("XZZX", xzzx)]:
cert = pc.pauli_exact(code, hw_noise)
lc = pc.logical_channel(code, hw_noise)
print(f"{label} -> exact={str(cert.exact):5s} | witnesses={cert.num_witnesses:2d} | gap={lc.gap:.5f}")
# CSS -> exact=False | witnesses=15 | gap=1.03149
# XZZX -> exact=True | witnesses= 0 | gap=1.00000
Note on Qiskit qubit order: Explanations use textbook order (q0 on the left: q0 q1 … q8), whereas Qiskit strings start q0 on the right (q8 … q1 q0, so plaquette {0, 1, 3, 4} is written "IIIIXXIXX"; pass qiskit_order=False to from_pauli_list to supply left-to-right strings directly).
3. Scaling to Large qLDPC Codes (37 to 360+ Qubits)
examples/03_qldpc_and_clifford_deformation.ipynb certifies custom HGP and bivariate bicycle qLDPC codes in milliseconds, demonstrates how XZZX Clifford deformation slashes twirling overhead (e.g. 50/108 → 2/108 and 174/360 → 4/360), and computes the minimal 3/144 twirl set for inhomogeneous noise on the [[144, 12, 12]] gross code.
[[360, 12, ≤24]] BB code from 174 to 4 qubits.import paulicert as pc
bb360 = pc.build_bivariate_bicycle_code(
30, 6, [(9, 0), (0, 1), (0, 2)], [(0, 3), (25, 0), (26, 0)], d=24
)
bb360_xzzx = pc.deform_stabilizer_code(bb360, "xzzx")
noise = pc.CHANNEL_FAMILIES["coherent_z"](0.01)
ts_css = pc.minimal_twirl_set(bb360, noise)
ts_xzzx = pc.minimal_twirl_set(bb360_xzzx, noise)
print(f"CSS qubits to twirl: {ts_css.qubits} ({len(ts_css.qubits)}/360, provably_minimal={ts_css.provably_minimal})")
print(f"XZZX qubits to twirl: {ts_xzzx.qubits} ({len(ts_xzzx.qubits)}/360, provably_minimal={ts_xzzx.provably_minimal})")
# CSS qubits to twirl: (0, 1, ..., 186, 187) (174/360, provably_minimal=True)
# XZZX qubits to twirl: (0, 1, 6, 7) (4/360, provably_minimal=True)
4. Under the Hood: Live Steane Syndrome Collisions & 8-Run Sign Table
examples/04_why_interference_happens.ipynb is an under-the-hood statevector walkthrough that uses compute_syndrome to enumerate all 7 nonzero Steane Z-syndromes and colliding weight-2 X errors (000110 → IIIXIII, XXIIIII, IIXIIIX, IIIIXXI), verifies exact 8-configuration Pauli-twirl sign cancellation on qubits (0, 1, 2) vs. (0, 1, 3), and contrasts directly with [[5, 1, 3]] (zero pure-X syndrome collisions).
All Notebooks & Benchmark Sweep
| Artifact | Path | Best for |
|---|---|---|
| Notebook 01 | examples/01_check_measure_fix.ipynb | 5-minute quickstart: pauli_exact, logical_channel, minimal_twirl_set, and automatic warnings. |
| Notebook 02 | examples/02_custom_codes_and_hardware_noise.ipynb | Custom codes ([[9, 1, 3]] surface vs. XZZX), per-qubit T1/T2 & composite Kraus noise, decoders, Qiskit circuits. |
| Notebook 03 | examples/03_qldpc_and_clifford_deformation.ipynb | Large HGP & BB qLDPC codes ([[37]]–[[360]]), XZZX twirl reduction, inhomogeneous [[144]] noise. |
| Notebook 04 | examples/04_why_interference_happens.ipynb | Optional deep dive: prints all 7 Steane syndromes, colliding X errors, live 8-run sign table, and [[5, 1, 3]] contrast. |
| Benchmark sweep | benchmarks/gap_sweep.py | Generates the 980-row benchmarks/data/gap_sweep.csv (gap, full_channel_exact, choi_distance) and figures. |