Classical simulation of magic state factories

QR code: these slides online

Yifei Wang · Tsinghua ⁠University

3 December 2026 · Chair for Design Automation · Technical University of ⁠Munich

  1. 1
    Magic ⁠state ⁠factoriesbackground and inspiration: distillation, code switching, ⁠cultivation
  2. 2
    Why they are hard to simulateexisting simulators and their cost on one ⁠protocol
  3. 3
    Magnaextract the code structure of a factory, and use ⁠it
  4. 4
    Results ⁠and ⁠usagebenchmarks from the paper; circuit files and ⁠decoders

Universality: one non-Clifford gate, teleported from a magic state

  • Clifford ⁠gates , , CNOT are the easy part: stabilizer codes protect them (transversal gates, lattice surgery). By Gottesman–Knill they are not ⁠universal.
  • Add one non-Clifford gate, e.g. , and use gate ⁠teleportation: only Clifford operations and a feedforward touch the ⁠data.

New ⁠problem: prepare the magic ⁠state , encoded, fault tolerantly. It is a known state, made offline: bad attempts can be ⁠discarded.

Gottesman, arXiv:quant-ph/9807006 · Gottesman, Chuang, arXiv:quant-ph/9908010 · Bravyi, Kitaev, arXiv:quant-ph/0403025 · Knill, arXiv:quant-ph/0402171

Prepare physically, then encode? Not fault tolerant

One early physical fault becomes a logical error: output error , not suppressed by the distance. Three ⁠repairs:

1

Distillation

Many noisy encoded states → fewer better ⁠ones.

2

Code switching

Do in a code where it is transversal, then ⁠switch.

3

Postselected encoding

Encode, then check so carefully that bad runs are discarded — e.g. ⁠cultivation.

Horsman et al., arXiv:1111.4022 · Li, ⁠arXiv:1410.7808

Repair 1 · Distillation: teleportation through a code

step by step →

Concatenation

Every line is a code patch: noisy encoded in, a logical Clifford circuit, the code on top. Repeat in ⁠rounds.

Error detection

A faulty input is a error. The code's checks catch one or two; three can pass: ⁠.

Postselection

Any fired check rejects the run. Bravyi–Haah: inputs, outputs per ⁠run.

Bravyi, Kitaev, arXiv:quant-ph/0403025 · Bravyi, Haah, arXiv:1209.2426 · Litinski, arXiv:1905.06903 · Experiment, logical 5-to-1 on neutral atoms: ⁠arXiv:2412.15165

Detail · Distillation: many noisy in, fewer purer out

How? And only with the protected Clifford ⁠operations.

Detail · 15-to-1 distillation, step by step

A bent wire is a Bell pair. Apply at one end and project onto : the gate slides around the bend, and the other end carries the magic state ⁠.

The same thing as a circuit. A CNOT prepares the Bell pair; the projection becomes an measurement with a known correction on R — teleportation ⁠again.

Encode the measured half in the code. on every qubit acts as a logical , so R again receives . The readout gives 4 parity checks: accept only if all ⁠pass.

Every is a gate-teleportation gadget. The inputs — noisy magic states — enter here, inside the circuit; the output leaves on R. A faulty input acts as a error on its ⁠line.

One faulty input is caught. Its error flips one outcome, and two of the parity checks fire: the run is ⁠rejected.

Two faulty inputs are caught too. At least one check always fires for one or two ⁠errors.

Three ⁠can ⁠pass. There are 35 such triples: . Bravyi–Haah: many outputs per run. Repeat in rounds for any target ⁠error.

Every line is a code patch. Each input is an encoded noisy magic state, and the whole circuit is a logical Clifford circuit, protected by the ⁠code.

Bravyi, Kitaev, arXiv:quant-ph/0403025 · Bravyi, Haah, arXiv:1209.2426 · Litinski, arXiv:1905.06903 · Experiment, logical 5-to-1 on neutral atoms: ⁠arXiv:2412.15165

Repair 2 · Code switching: transversal in 3D, Clifford gates in 2D

The 15-qubit 3D color code has a transversal : apply to every qubit of ⁠.

Measure the 8 qubits off one face in . The face is the 7-qubit 2D color code, where all Clifford gates are ⁠transversal.

A correction set by the outcomes fixes the face's checks: feedforward ⁠again.

Gauge fixingshown

Anderson, Duclos-Cianci, Poulin, arXiv:1403.2734 · Bombín, ⁠arXiv:1412.5079

Transversal CNOT, teleportation

Daguerre, Kim, ⁠arXiv:2410.07327

Generalized lattice surgery

Wang, Gu, arXiv:2503.19758 — switches directly into codes that are not subsystems of the 3D code, e.g. 3D color → 2D surface ⁠code

Experiment · Innsbruck

First fault-tolerant code switching: 7-qubit color code ↔ 10-qubit code with transversal , trapped ⁠ions.

Pogorelov et al., arXiv:2403.13732 · theory: Butt et al., ⁠arXiv:2306.17686

Experiment · Quantinuum

Magic states by switching from the code to the Steane ⁠code.

Daguerre et al., ⁠arXiv:2506.14169

Repair 3 · Postselected encoding: check, then discard

Inject into a small code, check it, discard every run with a fired detector. The most successful example, cultivation, checks the state with a logical ⁠Clifford:

with : a Clifford operator. So measure ⁠the ⁠logical ⁠ to check the magic state — a Clifford measurement that codes can do fault ⁠tolerantly.

Inject at → check with layers of physical → grow to , check again → escape into a large surface code. 15 at , 53 at ; acceptance 69 % and 14 % at ⁠.

Li, arXiv:1410.7808 · Gidney, Shutty, Jones, arXiv:2409.17595 · variants: Chen et al., arXiv:2503.18657 · Sahay et al., arXiv:2509.05212 · Hetényi et al., arXiv:2603.05429 · experiment: Rosenfeld et al., ⁠arXiv:2512.13908

Simulating magic state factories

why it is hard, and how magna uses the code ⁠structure

in collaboration with Yingfei Gu · ⁠arXiv:2610.22315

One example: RP² cultivation at

To design or compare factories we need and under circuit noise, with the actual gates: 61 qubits, 640 gates, 101 ⁠ ⁠gates, one logical ⁠out.

amplitudes in a state ⁠vector

stabilizer terms for 101 ⁠gates

× draws

repeated for every noisy ⁠draw

  • State ⁠vector: 61 qubits. ⁠Impossible.
  • Stabilizer + , gate by gate: the cost follows the stabilizer rank, at best for gates: ⁠.
  • And ⁠it ⁠is ⁠a ⁠Monte ⁠Carlo: every draw has a different fault pattern, different outcomes and different decoder decisions. Low acceptance and small logical errors need many ⁠draws.

Bravyi, Gosset, arXiv:1601.07601 · Qassim, Pashayan, Gosset, arXiv:2106.07740 · circuit: Chen et al., ⁠arXiv:2503.18657

Existing simulators: two strategies, two bottlenecks

Evolve the state in time

Keep a compact representation of the intermediate state: symbolic Pauli frames, stabilizer coordinates, sparse Pauli sums, matrix product states, and ⁠others.

Bottleneck: the intermediate states carry non-Clifford content that the output does not ⁠need.

RP² : peak amplitudes; 7.5–193 ms per ⁠draw.

SOFT, arXiv:2512.23037 · SymFT, arXiv:2607.28600 · Clifft, arXiv:2604.27058 · PPVM, arXiv:2610.18225 · MPS, arXiv:2609.19116 · Tsim (state-vector strategy), ⁠arXiv:2604.01059

Simplify the whole circuit first

Rewrite the circuit globally (ZX calculus, stabilizer decompositions, cutting), then ⁠evaluate.

Bottleneck: the cost grows with the non-Clifford content left at the output; for output magic states the stabilizer rank of is believed to grow ⁠exponentially.

Kissinger, van de Wetering, arXiv:2109.01076 · Pashayan et al., arXiv:2101.12223 · Tsim (stabilizer decomposition), arXiv:2604.01059 · Wan, Zhong, Zapirain, arXiv:2509.08658 · Surti, Daguerre, Kim, ⁠arXiv:2512.23799

Also needed, and missing from most: decoders inside the circuit — feedforward that is a nonlinear function of the measurement ⁠record.

Peak sizes: our measurement of SymFT, Clifft and PPVM on the same circuit (arXiv:2610.22315, Table II) · Stabilizer rank of : Bravyi, Smith, Smolin, ⁠arXiv:1506.01396

We need the numbers with actual gates

  • The cheap way around the cost: replace by and simulate with Stim — the same circuits and noise, and the same ⁠acceptance.
  • At and , the true logical error is already 2.6× the ⁠proxy's.
  • At the gap grows to about 10×: the proxy misses real failure ⁠modes.

Proxy data: Gidney, Shutty, Jones, arXiv:2409.17595 · proxies shown unreliable: Li et al., arXiv:2512.23037 · Wan, Zapirain, arXiv:2609.18922 · Chan et al., ⁠arXiv:2609.17706

A key observation: many physical gates, little logical magic

ProtocolHidden structurePhysical Logical output
15-to-1 distillationtransversal on 15one
Bravyi–Haahtriorthogonal code copies of
Code switchingtransversal on the 3D color code15one
Cultivation (color, RP²) layers implementing a transversal Clifford check15–101one

The physical count grows with the distance; the logical magic is fixed by the output. Maybe the code structure can remove the ⁠difficulty.

Two questions

Q1

Extract

Can a deterministic ⁠algorithm find this structure — with no human insight and no protocol-specific ⁠input?

Q2

Use

Can the simulation cost follow the logical magic — or even less — instead of the physical ⁠count?

physical count
Q1 · extract
?
Q2 · use
?

15-to-1 is one diagonal layer between stabilizer boundaries

Push a Clifford through rotations: . A Clifford maps a Pauli axis to a Pauli axis, so every rotation stays a rotation with a new axis, and the Cliffords pile up at the ⁠ends.

Start from the gadget form. Every is teleported: ancilla , CNOT, measurement, correction. The logical outcome sets the correction on R. Shown: 3 of the 15 code ⁠qubits.

Make all feedforward coherent (deferred measurement): becomes C from the ancilla, measured at the end; becomes CNOTs from R before the readout. The checks become ⁠projections.

Push the gadget CNOT into the initial Clifford. Passing only changes its axis, : a on the parity ⁠.

Combine the diagonal gates: : a on the code qubit and one on the ⁠ancilla.

Push the CNOTs from R in as well: becomes on the parity ⁠.

One ⁠commuting ⁠layer: rotations about products of 's, which all ⁠commute.

Abstract ⁠the ⁠ends: everything before the layer is one stabilizer state; the readout and checks are stabilizer projections. Any protocol gives this ⁠form.

Related: Pashayan et al., ⁠arXiv:2101.12223

The surviving basis states form a code

Paper · Sec. II C

This network


Compare: a code with transversal

The phase is non-Clifford only in the logical ⁠quotient ⁠space ⁠.

In the networkIn a code
basis states the boundaries do not kill, ↔code words: the classical code
directions in which flipping bits changes only by Clifford terms↔ stabilizers:
the remaining directions↔logical qubits : the genuine magic

We find the Clifford directions by linear algebra over bits — deterministic, polynomial time, no knowledge of the code. how? ⁠→

The network becomes stabilizer terms, known before sampling. Q1: physical count → logical ⁠magic.

Related: Cui, Gottesman, Krishna, ⁠arXiv:1608.06596

Detail · Finding the Clifford directions

Third differences define a trilinear form, , zero exactly for Clifford phases. Clifford directions: , by Gaussian elimination over ⁠.

Finite differences: flips the bits in direction . All values mod ⁠8.

Gatefirst difference second third Level
Pauli
Clifford
non-Clifford
CClifford
Cnon-Clifford
CCnon-Clifford

Remaining directions : the network becomes stabilizer terms. On the 15-qubit code is the 4-dimensional -stabilizer space and ⁠(triorthogonality).

Cui, Gottesman, Krishna, arXiv:1608.06596 · Heyfron, Campbell, arXiv:1712.01557 · Bravyi, Smith, Smolin, ⁠arXiv:1506.01396

Pauli covariance: inserting Paulis does not change the structure

Paper · Sec. II D

A fault in the coherent 15-to-1 circuit: on code qubit 1 after the encoder. Shown: that qubit, its gadget ancilla and R. Push the fault to the readout, gate by ⁠gate.

Through a Clifford, the Pauli changes: the CNOT maps to , ⁠.

Through a rotation, only a sign changes. C is three rotations, about , and ; anticommutes with the first two, so their signs flip. In phases: gains , a Clifford ⁠term.

At ⁠the ⁠readout: commutes with ; flips the ancilla's record — the sign of a boundary ⁠stabilizer.

Only ⁠signs ⁠changed: of rotations, by Clifford terms, and of boundary stabilizers. Every Pauli insertion keeps the same boundaries, the same layer and the same remaining directions. Compile once; per sample, update signs. Faults are the first kind of Pauli insertion; decoder decisions will be the second.

We need two numbers, not a state

Paper · Sec. II B

Run ⁠the ⁠protocol ⁠ with a fault draw . Its ending legs: output , postselection , and irrelevant (records no longer needed, discarded ⁠qubits).

puts the protocol next to its conjugate ⁠.

The ⁠trace joins the irrelevant legs of the two ⁠copies.

becomes projectors on the postselection ⁠legs.

Acceptance ⁠network: the output legs are traced too. Contracting it gives ⁠.

Fidelity ⁠network: the output legs meet a small noiseless target circuit . No intermediate or output state is ever ⁠built.

In the closed networks the output's magic meets its conjugate and cancels: one ⁠stabilizer ⁠term in every -gate benchmark — even Bravyi–Haah 308-to-100, whose output is . Q2: logical magic → Clifford cost.

Nonlinear feedforward: decoders inside the circuit

Paper · Sec. II E
  • Necessary: recall distillation with code patches: each input is an encoded patch; its correction depends on a logical outcome that a decoder infers from noisy syndromes — a nonlinear function ⁠.
  • Difficult ⁠for ⁠us: we reduce the whole circuit globally first, but this circuit is not determined until the outcomes are ⁠drawn.

Causality

A sampling network draws the early outcomes with their correct distribution without applying any later feedforward: the future is trace ⁠preserving.

Pauli covariance

Every feedforward enters as a Pauli insertion on a fixed compiled circuit, which does not change its ⁠structure.

Validation: logical-level 15-to-1, 102 qubits, 15 decoder rounds — the exact formula is reproduced down to . details ⁠→

Detail · Every decision becomes a Pauli insertion

  • The controlled gate is fixed at compile time, on a virtual control qubit. Only depends on the outcomes — exactly like a fault, so Pauli covariance ⁠applies.
  • Nonlinear postselection: the virtual qubit is projected onto in the acceptance and fidelity ⁠networks.

Detail · Sample the outcomes without the future

The sampling network, every outcome leg open (triangles): and as staircases, one step per round. Output and postselection are traced. Nothing is drawn and no decision is known ⁠yet.

Trace every round after round 1. Everything after round 1 is then trace preserving, so the later decisions, still unknown, drop ⁠out.

Draw ⁠ from ⁠.

Only now can the decoder act: enters as a Pauli insertion. Return to the open network, with ⁠fixed.

Trace every round after round 2. The future is trace preserving again, and it needs only ⁠.

Draw ⁠ from ⁠.

Insert , reopen, repeat: each conditional probability uses only decisions already made; each step is a Pauli ⁠update.

Results and usage

benchmarks from the paper · how to run your own ⁠protocol

The circuit grows; the resource does not

Paper · Table I
ProtocolQubitsNon-CliffordOutputRankms / draw
Bravyi–Haah 14-to-23010.0069
Bravyi–Haah 38-to-108610.010
Bravyi–Haah 128-to-4029610.022
Bravyi–Haah 308-to-10071610.050
Cultivation, color 1510.0059
Cultivation, color 4210.010
Cultivation, RP² 2510.0064
Cultivation, RP² 6110.0088
Twisted quantum double, , 5 rounds554 logical qubits43.2
Twisted quantum double, , 6 rounds974 logical qubits46.7
Twisted quantum double, , 8 rounds1514 logical qubits416.5
Code switching, 3D color → 2D surface7010.0071
Logical-level 15-to-1 (15 decoder stages)10211.27

Distillation: 2 to 100 output magic states, 30 to 716 qubits — rank 1 ⁠throughout.

Cultivation: to 5, up to 101 physical gates — rank 1 ⁠throughout.

Twisted ⁠quantum ⁠double: to 5, 288 to 1250 CC gates with a decoder in every round — rank 4 ⁠throughout.

The rank stays fixed as the circuit grows — even with 100 output magic states, it is ⁠one.

Rank: largest representation rank of the reference networks. Times per draw, one thread, Apple M1 Ultra; with a decoder, they include the decoder ⁠calls.

Many outputs: no exponential cost in

Paper · Sec. III A
Paper figure: Bravyi-Haah distillation, (a) joint-output infidelity against p for 14-to-2, 26-to-6 and 38-to-10; (b) preparation and per-draw sampling time against the number of outputs k up to 100, for T and for the matched Clifford S circuit

Bravyi–Haah -to- up to (716 qubits): preparation and sampling time grow polynomially, tracking the matched Clifford circuit with in place of ⁠.

Bravyi, Haah, ⁠arXiv:1209.2426

Cultivation: same answers, a constant resource

Paper · Sec. III B
Paper figure: color and RP2 cultivation at d = 3 and 5; (a) acceptance and (b) output infidelity against p with series expansions and published references; (c) representation size and (d) sampling time per draw for magna, SymFT, Clifft and PPVM
  • (a, ⁠b) Acceptance and output infidelity with the actual gates agree with exact series expansions and published ⁠references.
  • (c) Our resource stays at one stabilizer term at ; state-evolution simulators reach amplitudes on ⁠RP².
  • (d) RP² : 0.0088 ms per draw, against 7.5–193 ⁠ms.
  • Caveat: a newer state-vector strategy in Tsim 1.1 is faster on these fixed circuits. Its cost per draw grows as with an active width that is not bounded as the code family ⁠grows.

Circuits: Gidney, Shutty, Jones, arXiv:2409.17595 · Wan, Zapirain, arXiv:2609.18922 · Chen et al., ⁠arXiv:2503.18657

Decoders inside the circuit: exact agreement

Paper · Sec. III C
Paper figure: logical-level 15-to-1 distillation on surface-code patches; (a) acceptance and (b) conditional infidelity against p, circuit simulation against the exact reference

Logical-level 15-to-1 on distance-3 surface-code patches: 102 qubits, 15 rounds of decoder-controlled feedforward. The simulation recovers the exact logical error rates, to about ⁠.

Protocol: Litinski, ⁠arXiv:1905.06903

Using magna: a Stim-like circuit file and your own decoder

factory.magna Stim syntax + non-Clifford gates + feedforward
R 0 1 2 3
H 0
T 0                      # actual T gates, also CS, CCZ
CX 0 1 0 2
DEPOLARIZE2(0.001) 0 1 0 2
CX 1 3 2 3
M 3
DETECTOR rec[-1]
CONTROL[recovery] det[-1] -> ctrl[0]
CX ctrl[0] 0               # decoder-controlled feedback
POSTSELECT det[0]
task.json what to score
{"schema_version": 1, "outputs": [0, 1, 2],
 "ideal_state": "ideal.magna"}
decoder.py any Python function, e.g. a matching decoder
from magna import ControllerRegistry, load_circuit, run

def recovery(bits, config):   # detectors in, bits out
    return (my_decoder(bits),)

reg = ControllerRegistry()
reg.register("recovery", recovery, version="1")
c = load_circuit("factory.magna", controllers=reg)
r = run(c, task="task.json", shots=10**6)
  • Stim-compatible: gates, noise channels, detectors and records as in Stim; decoders see detectors, as with a Stim detector error ⁠model.
  • Out: acceptance, output error and conditional error, with standard errors; physical sampling, importance sampling or exact low-weight ⁠enumeration.
  • Install: pip install from github.com/itpyi/magna (v0.2.0); command line magna run factory.magna --task ⁠task.json.

Summary

Magna

  • Extract: the code hidden in a factory, by linear algebra over bits — many physical non-Clifford gates, little logical ⁠magic.
  • Use: Pauli faults only flip signs; ask for two numbers, not a state; decoders enter as Pauli ⁠insertions.
  • Result: Clifford-like cost for actual- factories. The rank stays fixed as the circuit grows, even with 100 output magic ⁠states.
Thank you

github.com/itpyi/magna · arXiv:2610.22315
We thank Hong-Ye Hu, Kwok Ho Wan, Ainhoa Zapirain, Zikang Zhang and Tian-Gang Zhou for useful ⁠discussions.

QR code: paper on arXiv
paper
QR code: magna on GitHub
code
QR code: these slides online
slides