MagicState-Lab: magic state lab - M2 entropy, 15-to-1 distillation map, T injection. Sim only!
43
Magic State Lab — Stabilizer Rényi Entropy, the Bravyi–Kitaev 15-to-1 Distillation Map, and T-Gate Injection — by Darion Kellar
Educational magic state lab in ONE pure-Python file: for pure states of one and two qubits it builds the density matrix, measures every Pauli expectation value Tr(ρP) from the state itself, and evaluates the stabilizer Rényi entropy M2 = −log2((1/2ⁿ) Σ_P |⟨P⟩|⁴). Stabilizer states (|0⟩, |+⟩, |+i⟩, |++⟩, the Bell state) compute to M2 = 0; the T state computes to 0.415037499279, matching −log2(3/4) to 1e-12; the H-type state matches the same value and the body-diagonal state matches log2(3/2) = 0.584962500721. M2 is additive on the nose: |T⟩⊗|0⟩ = 0.415037499279 and |T⟩⊗|T⟩ = 0.830074998558. Distillation is the Bravyi–Kitaev 15-to-1 protocol's known leading-order error map p → 35p³, applied exactly as a map — the 15-qubit Reed–Muller circuit is NOT simulated qubit-by-qubit, and the lab says so wherever the map appears. Iterated from p₀ = 0.10 the error falls 0.035 → 0.001500625 → 1.182727e-07 → 5.79e-20, reaching 1e-6 in 3 rounds (3,375 raw states) and 1e-12 in 4 rounds (50,625); the threshold is located by bisection at 0.169030850946 = 1/√35, and above it the same map amplifies (0.25 → 0.546875 → 5.724430). Injection IS simulated in full: the two-qubit gate-teleportation circuit (CNOT, measure, S correction) runs on density matrices with both branches carried exactly — a perfect ancilla returns |T⟩⟨T| itself, depolarizing noise gives ε_out = p/2 exactly, and phase-flip noise gives ε_out = p, both printed. A from-scratch distance-weighted kNN classifier routes supply batches from the lab's own policy.
Pure Python standard library. Zero dependencies. Zero network. Honest scope: no state was distilled on real hardware, no device error rate is certified, no fault-tolerant computer is claimed, and every real-world use is a permanent HOLD gate (14 total).
math, hashlib, itertools, json, sys) — zero dependencies, zero network, deterministic (double-run byte-identical)
python:3.12-slim Docker image
python:3.12-slim), GitHub Actions CI + Docker Publish (delivered in the paste pack)credkellar/magicstate-lab:latestpytest (22 tests) + the built-in CHECKS suite (49 checks) in main.pydocker pull credkellar/magicstate-lab:latest