A VALUE-EXACT constant-propagation pass on reversible circuits #
Category: 1-Mathlib (CSD-free; staged as a Mathlib-upstream candidate).
Constant propagation on a reversible circuit: a forward abstract interpretation over the gate
list that tracks each wire as Zero / One / Unknown (seeded from the classical initial state —
ancillas are |0⟩) and folds the Toffolis it can decide:
- a
CCXwith a control known0never fires → drop it; - a
CCXwith a control known1acts as aCXon the other control → fold toCX.
Both folds are value-exact — they change the gate list but not the function computed — so the
pass is a proved circuit-to-circuit transform, cprop_denote, and every Toffoli it removes is a
Toffoli the resource count (Cost.lean) no longer sees. The abstract state is deliberately
conservative (any written wire becomes Unknown; only swap moves knowledge), which is sound and
already captures the main benefit — a fresh |0⟩ ancilla stays known-0 until it is written, so
every Toffoli reading it as a control while it is still 0 is dropped. (The transform was first
needed as the dominant Toffoli lever of the reversible-arithmetic application that lives in the
Ecdsafail repository, specs/ecdsa/ecdlp-resource-plan.md there, where it is applied
informally; here it is the theorem.)
Abstract wire values: none = Unknown, some b = provably b``.
Equations
- Reversible.Abs m = (Fin m → Option Bool)
Instances For
Folding one gate against the abstract state #
Fold a gate given known-constant wires. Only CCX folds: a degenerate CCX (target is a control)
is the identity → drop; a control known 0 → drop; a control known 1 → collapse to a CX on the other
control. Every other gate is kept. VALUE-EXACT (foldGate_denote).
Equations
- One or more equations did not get rendered due to their size.
- Reversible.foldGate α x✝ = [x✝]
Instances For
Forward-updating the abstract state #
Conservative forward abstract-state update. X flips a known bit; swap exchanges; a CX/CCX
write makes the target Unknown (sound over-approximation). Wires not written keep their knowledge — so a
fresh |0⟩ ancilla stays known-0 until something writes it.
Equations
- Reversible.stepAbs α (Reversible.Gate.X i) = Function.update α i none
- Reversible.stepAbs α (Reversible.Gate.CX c t) = Function.update α t none
- Reversible.stepAbs α (Reversible.Gate.CCX c₁ c₂ t) = Function.update α t none
- Reversible.stepAbs α (Reversible.Gate.swap i j) = fun (k : Fin m) => if k = i then α j else if k = j then α i else α k
Instances For
stepAbs is sound. If α describes s, the updated abstract state describes the gate's output
state. Every write-target becomes Unknown (vacuously sound); swap exchanges knowledge; untouched
wires keep it.
The pass and its value-exactness #
The constant-propagation pass. Fold each gate against the running abstract state, threading the
forward update. Seeded with α describing the input (e.g. ancillas some false).
Equations
- Reversible.cprop α [] = []
- Reversible.cprop α (g :: gs) = Reversible.foldGate α g ++ Reversible.cprop (Reversible.stepAbs α g) gs
Instances For
THE HEADLINE — cprop is VALUE-EXACT. For any input s the seed α correctly describes, the
propagated circuit computes exactly the same function: denote (cprop α c) s = denote c s. So folding
provably-determined Toffolis changes the gate list (fewer/cheaper gates) but NOT the computed value — the
frontier's key lever, machine-checked.
A concrete win: a known-0 ancilla control drops its Toffoli #
cprop is a sound REDUCING optimization (cost side) #
cprop_denote shows the pass preserves the computed VALUE. These show it never increases — and, on a
known-constant control, strictly decreases — the emitted Toffoli count. Together: cprop is a certified
value-exact Toffoli-reducing pass, exactly the frontier's key lever, proved both correct and beneficial.
cprop never increases the Toffoli count. So constant-propagation is a valid optimization: it
computes the same value (cprop_denote) with no more Toffolis.
The reduction mechanism. A non-degenerate CCX with a control known to be false folds AWAY (to
[]): its Toffoli is dropped entirely. This is where constant propagation buys Toffolis.
A real multi-Toffoli win on the AND-adder's carry cell. The three-CCX carry cell
[CCX a b g, CCX a c g, CCX b c g] with the carry-in c a known-0 ancilla constant-propagates to a
SINGLE Toffoli — a value-exact 3 → 1 (67%) Toffoli reduction, exactly the structure constant folding
exploits in a ripple/AND adder whose low carry-in is |0⟩.