Manuscript En02-C@1 · En02-C-v1.tex · section · 7549c6df
Standalone exact finite checks
Standalone exact finite checks · lines 1853–1984
The following standard-library Python source can be copied to
02_c_checks.py. It uses Gaussian integers and exact rational
normalizations. Hadamards are applied without their
factor, and the omitted factors are tracked.
The checks inspect all message matrix units for the specified third-prefix channel, its early reductions, the corrected streaming branches, and the one-message SWAP recovery and amplification example. The live-register checker distinguishes laboratory occupancy from supplier storage. No execution is asserted by this source file.
python en02_c_checks.py
"""En02-C: exact finite checks.
Standard library only. No execution log is supplied. The manuscript's general proofs do not depend on running this file. """
from fractions import Fraction as F from itertools import product
ZERO = (0, 0) ONE = (1, 0)
class CheckFailure(Exception): pass
class CapacityRefusal(Exception): pass
def need(condition, message): if not condition: raise CheckFailure(message)
def add(a, b): return (a[0] + b[0], a[1] + b[1])
def neg(a): return (-a[0], -a[1])
def mul(a, b): return (a[0] * b[0] - a[1] * b[1], a[0] * b[1] + a[1] * b[0])
def conj(a): return (a[0], -a[1])
def sq(a): return a[0] * a[0] + a[1] * a[1]
def bits(index, n): return tuple((index >> (n - 1 - j)) & 1 for j in range(n))
def index_of(bs): out = 0 for b in bs: out = 2 * out + b return out
def nq(v): size = len(v) need(size > 0 and (size & (size - 1)) == 0, "state dimension is not a power of two") return size.bit_length() - 1
def gate(v, op): """Unnormalized H; caller tracks the number of H gates.""" n = nq(v) kind = op[0] out = [ZERO] * len(v)
if kind == "H": q = op[1] for j, amp in enumerate(v): bs = list(bits(j, n)) old = bs[q] for new in (0, 1): bs[q] = new term = neg(amp) if old and new else amp t = index_of(bs) out[t] = add(out[t], term) return out
for j, amp in enumerate(v): bs = list(bits(j, n)) term = amp
if kind == "X": bs[op[1]] ^= 1 elif kind == "Y": q = op[1] term = mul(amp, (0, 1) if bs[q] == 0 else (0, -1)) bs[q] ^= 1 elif kind == "Z": if bs[op[1]]: term = neg(amp) elif kind == "S": if bs[op[1]]: term = mul(amp, (0, 1)) elif kind == "SD": if bs[op[1]]: term = mul(amp, (0, -1)) elif kind == "CX": if bs[op[1]]: bs[op[2]] ^= 1 elif kind == "CZ": if bs[op[1]] and bs[op[2]]: term = neg(amp) elif kind == "Z0": if all(bs[q] == 0 for q in op[1:]): term = neg(amp) else: raise CheckFailure("unknown gate")