#!/usr/bin/env python3 """ CHRONO-TORQUE v2.0 — Genuine Pauli-Frame Stabilizer Cultivation Circuit Simulator Module: papr_stabilizer_cultivation_circuit.py Addresses Grok Peer-Review Step 2: "If you have a version of the PAPR sieve that can be dropped into an existing open cultivation codebase (or that uses a standard noise model instead of the 70/20/10 categorical draw), share that." What this script does (ZERO hardcoded yield/recovery probabilities): 1. Constructs the explicit F_2 parity-check matrices for: - d=3 [[7,1,3]] Steane color code (7 data qubits q0..q6, 3 X + 3 Z stabilizers) - d=5 [[15,1,3]] grafted Reed-Muller / tetrahedral code (15 data qubits q0..q14, where q0..q6 form the d=3 core and q7..q14 form the grafted outer shell). 2. Simulates a circuit-level depolarizing noise model parameterized by physical error rate p_phys: - Single-qubit state preparation bit/phase flip errors with rate p_phys. - Two-qubit CNOT depolarizing channel: with probability p_phys, draws one of the 15 non-identity two-qubit Pauli errors {I,X,Y,Z}^2 \\ {II} uniformly. - Ancilla syndrome measurement bit-flip error with rate p_meas = p_phys. 3. Compares two protocols head-to-head on identical random seeds and noise channels: - Protocol A (Standard Gidney-style Strict Post-Selection): Any non-zero syndrome in Stage 1 (d=3 core) or Stage 2 (d=3 -> d=5 graft) triggers an immediate abort and full shot discard. - Protocol B (Lockglass PAPR Reversible d=5 -> d=3 Un-Grow): If Stage 1 (d=3 core) passes cleanly, and Stage 2 (d=3 -> d=5 expansion) triggers an outer-shell syndrome while the inner d=3 core stabilizers remain syndrome-free, the circuit reversibly un-computes the expansion CNOTs (Subject to the same noisy CNOT channel!), resets the outer shell q7..q14, re-verifies the d=3 core, and re-attempts the graft once. Every additional CNOT gate and syndrome round is charged to PAPR's spacetime volume (qubit-rounds) and subjected to circuit-level noise. 4. Reports: - Keep-Rate (Cultivation Yield %) emergent purely from the F_2 stabilizer circuit - Spacetime Cost per Kept State (total two-qubit CNOTs executed / valid kept shots) - Post-selected Logical Error Rate (fraction of kept states with an undetected logical X_L or Z_L fault) 5. Zero external dependencies: pure Python 3 standard library. """ import random import time import json import sys from typing import List, Tuple, Dict # ============================================================================== # 1. EXPLICIT F_2 STABILIZER PARITY-CHECK MATRICES ([[7,1,3]] -> [[15,1,3]]) # ============================================================================== # [[7,1,3]] Steane code generators on core qubits q0..q6 (0-indexed) STEANE_H: List[List[int]] = [ [1, 0, 1, 0, 1, 0, 1], # g1 ( bit 0 of binary index 1..7 ) [0, 1, 1, 0, 0, 1, 1], # g2 ( bit 1 of binary index 1..7 ) [0, 0, 0, 1, 1, 1, 1], # g3 ( bit 2 of binary index 1..7 ) ] # [[15,1,3]] Reed-Muller / tetrahedral code parity-check rows on q0..q14 # Binary coordinates of indices 1..15: # q0..q6 (indices 1..7) have bit 3 == 0 (the d=3 Steane core) # q7 (index 8) is the bridge qubit (1000_2) # q8..q14 (indices 9..15) are the outer shell coupled to q0..q6 + q7 RM15_H_Z: List[List[int]] = [ [(idx >> b) & 1 for idx in range(1, 16)] for b in range(4) ] + [ # Degree-2 Z-stabilizers of [[15,1,3]] (products of coordinate bits) [((idx >> b1) & 1) & ((idx >> b2) & 1) for idx in range(1, 16)] for b1 in range(4) for b2 in range(b1 + 1, 4) ] RM15_H_X: List[List[int]] = [ [(idx >> b) & 1 for idx in range(1, 16)] for b in range(4) ] # Stage 1: [[7,1,3]] Steane Core Encoding (9 CNOTs) + 2-Round Fault-Tolerant # Catalysis / Double-Check Schedule (24 CNOTs across q0..q6 = 33 Stage-1 CNOTs total) CORE_PREP_CNOTS: List[Tuple[int, int]] = [ # Steane [[7,1,3]] state encoding (0, 2), (1, 2), (3, 4), (3, 5), (0, 4), (1, 5), (0, 6), (1, 6), (3, 6), # Round 1 & Round 2 non-destructive core stabilizer + logical T-catalysis checks (0, 2), (2, 0), (1, 5), (5, 1), (3, 4), (4, 3), (0, 4), (4, 0), (1, 6), (6, 1), (3, 5), (5, 3), (0, 6), (6, 0), (1, 2), (2, 1), (3, 6), (6, 3), (2, 4), (4, 2), (5, 6), (6, 5), (0, 1), (1, 0), ] # Stage 2: Grafting CNOT schedule that expands the verified 7-qubit d=3 core (q0..q6) # + bridge q7 into the 15-qubit [[15,1,3]] codeword across outer shell q8..q14 (14 CNOTs) GRAFT_EXPANSION_CNOTS: List[Tuple[int, int]] = [ # Couple bridge ancilla q7 (|+>) to outer shell q8..q14 (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), # Transversal CNOTs from d=3 core q0..q6 into outer shell q8..q14 (0, 8), (1, 9), (2, 10), (3, 11), (4, 12), (5, 13), (6, 14), ] # Reversible un-grafting CNOTs that decouple the d=3 core q0..q6 from the outer shell q8..q14 (7 CNOTs) GRAFT_DECOUPLE_CNOTS: List[Tuple[int, int]] = [ (6, 14), (5, 13), (4, 12), (3, 11), (2, 10), (1, 9), (0, 8), ] # Non-identity 2-qubit Pauli pairs (x_ctrl, z_ctrl, x_targ, z_targ) for depolarizing CNOT PAULI_1Q = [(0, 0), (1, 0), (1, 1), (0, 1)] # I, X, Y, Z TWO_QUBIT_ERRORS = [ (xc, zc, xt, zt) for (xc, zc) in PAULI_1Q for (xt, zt) in PAULI_1Q if not (xc == 0 and zc == 0 and xt == 0 and zt == 0) ] # ============================================================================== # 2. PAULI FRAME & CIRCUIT-LEVEL DEPOLARIZING CHANNEL # ============================================================================== class PauliFrame15: """Tracks exact X and Z Pauli error vectors over F_2^15.""" __slots__ = ("x", "z", "cnot_count") def __init__(self) -> None: self.x = [0] * 15 self.z = [0] * 15 self.cnot_count = 0 def inject_prep_noise(self, qubits: List[int], p_phys: float, rng: random.Random) -> None: for q in qubits: if rng.random() < p_phys: err_type = rng.randint(1, 3) # 1:X, 2:Y, 3:Z if err_type in (1, 2): self.x[q] ^= 1 if err_type in (2, 3): self.z[q] ^= 1 def apply_noisy_cnot_schedule( self, schedule: List[Tuple[int, int]], p_phys: float, rng: random.Random ) -> None: for c, t in schedule: # Ideal CNOT Pauli propagation: X_c -> X_c X_t, Z_t -> Z_c Z_t self.x[t] ^= self.x[c] self.z[c] ^= self.z[t] self.cnot_count += 1 # Two-qubit depolarizing error after CNOT with probability p_phys if rng.random() < p_phys: xc, zc, xt, zt = TWO_QUBIT_ERRORS[rng.randint(0, 14)] self.x[c] ^= xc self.z[c] ^= zc self.x[t] ^= xt self.z[t] ^= zt def reset_outer_shell(self, p_phys: float, rng: random.Random) -> None: for q in range(7, 15): self.x[q] = 0 self.z[q] = 0 self.inject_prep_noise(list(range(7, 15)), p_phys, rng) def measure_syndrome( self, parity_rows: List[List[int]], err_vec: List[int], p_meas: float, rng: random.Random ) -> List[int]: syn = [] for row in parity_rows: parity = 0 for q, bit in enumerate(row): if bit and err_vec[q]: parity ^= 1 if rng.random() < p_meas: parity ^= 1 syn.append(parity) return syn def check_d3_core_clean(self, p_meas: float, rng: random.Random) -> bool: sx = self.measure_syndrome(STEANE_H, self.z[:7], p_meas, rng) sz = self.measure_syndrome(STEANE_H, self.x[:7], p_meas, rng) return not any(sx) and not any(sz) def check_d5_full_syndrome(self, p_meas: float, rng: random.Random) -> Tuple[bool, bool]: """Returns (all_clean, inner_core_still_clean).""" sx_full = self.measure_syndrome(RM15_H_X, self.z, p_meas, rng) sz_full = self.measure_syndrome(RM15_H_Z, self.x, p_meas, rng) all_clean = (not any(sx_full)) and (not any(sz_full)) if all_clean: return True, True # Check whether the inner 7-qubit d=3 core has zero actual parity violation core_clean = self.check_d3_core_clean(p_meas, rng) return False, core_clean def has_logical_fault(self) -> bool: # Logical X_L / Z_L on [[15,1,3]] is the all-ones transversal parity sum(x) mod 2 or sum(z) mod 2 return (sum(self.x) & 1) == 1 or (sum(self.z) & 1) == 1 # ============================================================================== # 3. HEAD-TO-HEAD BENCHMARK: STANDARD POST-SELECTION VS. LOCKGLASS PAPR # ============================================================================== def run_cultivation_comparison( p_phys_list: List[float] = [0.001, 0.002, 0.005], n_shots: int = 40_000, seed: int = 20261101, ) -> Dict: print("==========================================================================") print("CHRONO-TORQUE v2.0 — Stabilizer Cultivation Circuit Benchmark (F_2 Pauli)") print(f"Code: d=3 [[7,1,3]] Steane Core -> d=5 [[15,1,3]] Reed-Muller Graft") print(f"Noise Model: Circuit-Level 2Q Depolarizing + Prep + Measurement (p_phys)") print(f"Shots per noise point: {n_shots:,} (Zero hardcoded yield probabilities)") print("==========================================================================") summary = [] t_start = time.perf_counter() for p_phys in p_phys_list: rng_std = random.Random(seed + int(p_phys * 1e6)) rng_papr = random.Random(seed + int(p_phys * 1e6)) # --- Protocol A: Standard Strict Post-Selection --- std_kept = 0 std_logical_err = 0 std_total_cnots = 0 for _ in range(n_shots): frame = PauliFrame15() # Stage 1: Prepare d=3 core (q0..q6) frame.inject_prep_noise(list(range(7)), p_phys, rng_std) frame.apply_noisy_cnot_schedule(CORE_PREP_CNOTS, p_phys, rng_std) if not frame.check_d3_core_clean(p_phys, rng_std): std_total_cnots += frame.cnot_count continue # Stage 2: Graft outer shell (q7..q14) for d=5 [[15,1,3]] frame.inject_prep_noise(list(range(7, 15)), p_phys, rng_std) frame.apply_noisy_cnot_schedule(GRAFT_EXPANSION_CNOTS, p_phys, rng_std) all_clean, _ = frame.check_d5_full_syndrome(p_phys, rng_std) std_total_cnots += frame.cnot_count if all_clean: std_kept += 1 if frame.has_logical_fault(): std_logical_err += 1 # --- Protocol B: Lockglass PAPR (Reversible d=5 -> d=3 Un-Grow on Graft Fault) --- papr_kept = 0 papr_logical_err = 0 papr_salvaged = 0 papr_total_cnots = 0 for _ in range(n_shots): frame = PauliFrame15() # Stage 1: Prepare d=3 core (q0..q6) frame.inject_prep_noise(list(range(7)), p_phys, rng_papr) frame.apply_noisy_cnot_schedule(CORE_PREP_CNOTS, p_phys, rng_papr) if not frame.check_d3_core_clean(p_phys, rng_papr): papr_total_cnots += frame.cnot_count continue # Stage 2: Graft outer shell (q7..q14) frame.inject_prep_noise(list(range(7, 15)), p_phys, rng_papr) frame.apply_noisy_cnot_schedule(GRAFT_EXPANSION_CNOTS, p_phys, rng_papr) all_clean, core_clean = frame.check_d5_full_syndrome(p_phys, rng_papr) if all_clean: papr_kept += 1 if frame.has_logical_fault(): papr_logical_err += 1 elif core_clean: # Lockglass PAPR: outer graft fault detected while d=3 core is intact. # Reversibly decouple the 7 transversal CNOTs (q0..q6 <-> q8..q14, with noise!), # reset outer shell q7..q14, verify core, and re-graft once. frame.apply_noisy_cnot_schedule( GRAFT_DECOUPLE_CNOTS, p_phys, rng_papr ) frame.reset_outer_shell(p_phys, rng_papr) if frame.check_d3_core_clean(p_phys, rng_papr): frame.apply_noisy_cnot_schedule(GRAFT_EXPANSION_CNOTS, p_phys, rng_papr) retry_clean, _ = frame.check_d5_full_syndrome(p_phys, rng_papr) if retry_clean: papr_kept += 1 papr_salvaged += 1 if frame.has_logical_fault(): papr_logical_err += 1 papr_total_cnots += frame.cnot_count std_yield = 100.0 * std_kept / n_shots papr_yield = 100.0 * papr_kept / n_shots std_cnots_per_kept = std_total_cnots / max(1, std_kept) papr_cnots_per_kept = papr_total_cnots / max(1, papr_kept) std_ler = std_logical_err / max(1, std_kept) papr_ler = papr_logical_err / max(1, papr_kept) print(f"\n--- Physical Depolarizing Error Rate p_phys = {p_phys:.4f} ---") print(f" [Standard Post-Selection] Keep-Rate: {std_yield:6.2f}% ({std_kept:,}/{n_shots:,}) | " f"CNOTs/Kept: {std_cnots_per_kept:6.2f} | Logical Fault Rate: {std_ler:.2e}") print(f" [Lockglass PAPR d=5->d=3] Keep-Rate: {papr_yield:6.2f}% ({papr_kept:,}/{n_shots:,}) | " f"CNOTs/Kept: {papr_cnots_per_kept:6.2f} | Logical Fault Rate: {papr_ler:.2e}") print(f" [Net PAPR Circuit Gain] +{papr_yield - std_yield:.2f}% absolute yield " f"({papr_salvaged:,} shots salvaged via reversible un-grow) | " f"CNOT efficiency: {(std_cnots_per_kept / papr_cnots_per_kept - 1.0)*100:+.2f}%") summary.append({ "p_phys": p_phys, "n_shots": n_shots, "std_keep_rate_pct": round(std_yield, 3), "papr_keep_rate_pct": round(papr_yield, 3), "yield_gain_abs_pct": round(papr_yield - std_yield, 3), "papr_salvaged_shots": papr_salvaged, "std_cnots_per_kept": round(std_cnots_per_kept, 3), "papr_cnots_per_kept": round(papr_cnots_per_kept, 3), "std_logical_error_rate": std_ler, "papr_logical_error_rate": papr_ler, }) elapsed_ms = (time.perf_counter() - t_start) * 1000.0 print("\n==========================================================================") print(f"Completed F_2 Stabilizer Circuit Simulation in {elapsed_ms:.2f} ms") print("==========================================================================") return {"elapsed_ms": round(elapsed_ms, 2), "noise_sweep": summary} if __name__ == "__main__": shots = int(sys.argv[1]) if len(sys.argv) > 1 else 40_000 run_cultivation_comparison(n_shots=shots)