Reversible Arithmetic on Qubits
Ask anyone what Shor's algorithm does and you will hear about the quantum Fourier transform, phase
estimation, interference peaks. All true — and almost none of it is where the machine's effort goes.
Profile an actual implementation of
Shor's
algorithm and the picture is comically lopsided: the QFT is a sliver, and
nearly every qubit and nearly every gate is spent on modular exponentiation —
a^x \bmod N, computed by reversible adders, reversible multipliers,
reversible comparators. In other words: the payoff of a quantum computer is delivered by
classical reversible arithmetic, the very circuits of
the synthesis
module, now running on qubits. This is the lesson where your two skill sets fuse: the
reversible-synthesis toolbox is the quantum compiler backend.
The arithmetic tower
Shor needs the unitary |x\rangle|1\rangle \mapsto |x\rangle|a^x \bmod N\rangle
for an n-bit modulus N. Nobody builds that in one
piece; it is compiled down a tower, and every storey of the tower is a construction you already know:
- Exponentiation → multiplications. Classical square-and-multiply: write
x in binary and, for each of its \approx 2n bits,
conditionally multiply the accumulator by a precomputed constant
a^{2^i} \bmod N. Each conditional multiply is a controlled modular
multiplier — controlled on one qubit of |x\rangle.
- Multiplications → additions. A modular multiply by a constant is a shift-and-add
loop: about n modular additions, each a plain reversible
adder followed by a compare-and-conditionally-subtract-N (itself an adder
run in reverse plus a comparator — which is an adder too).
- Additions → Toffolis. Each adder is exactly the VBE or Cuccaro ripple-carry
circuit from the synthesis module — carries computed by Toffolis, sums by CNOTs, carries
uncomputed by the mirror-image Toffolis. On qubits, gate for gate, unchanged.
Multiply it out: 2n multipliers \times\; n
additions \times\; O(n) Toffolis — an
O(n^3)-Toffoli engine of pure classical reversible logic, wrapped around a
whisper-thin layer of Hadamards, a QFT and a measurement. For N of
cryptographic size, that is billions of Toffolis of grade-school arithmetic done without ever
forgetting a carry.
Uncomputation, wall to wall
The tower would collapse without the discipline of the
previous
lesson. Every storey manufactures intermediates — carry chains, partial products, the
"did we overflow N?" comparison bit — and every one of them is entangled with
|x\rangle. Left in place, they are which-path records that would erase the
periodic interference pattern the final QFT exists to read. So every block is a compute–copy–uncompute
sandwich: the Cuccaro adder uncomputes its own carries as it goes; the modular multiplier computes
b \cdot a^{2^i} \bmod N into a fresh register, then uncomputes the old
accumulator by running the inverse multiplication (the swap-and-unmultiply trick); the comparison
ancilla is returned to |0\rangle by re-running the comparison backwards.
Roughly half the gates in a real Shor circuit are there purely to unhappen the other
half — Bennett's bargain, paid at industrial scale.
What it costs, in the currency that matters
On fault-tolerant hardware, gates are not created equal. Clifford gates (CNOT, H, S) are cheap under
error correction; the expensive resource is the T gate, manufactured offline by magic-state
distillation — and a Toffoli costs a small constant number of T gates (a standard decomposition uses
seven, or four with tricks). So quantum engineers budget circuits in Toffoli count or
T-count, and mostly ignore everything else. That convention quietly concedes this
lesson's thesis: the fault-tolerant cost of Shor is the cost of its classical reversible
arithmetic, because that is where all the Toffolis are. The same synthesis skills you practised — trade
ancillas against depth, shave a Toffoli here, uncompute earlier there — are precisely what shrinks a
factoring machine.
There is one genuinely quantum-native alternative worth knowing as contrast:
Draper's QFT adder. It adds a constant not with carry chains but in phase
space: Fourier-transform the register, apply single-qubit phase rotations
R_k (addition becomes phase accumulation — no carries, no ancillas at all),
and transform back. Elegant, qubit-frugal — and made of many small rotations, each of which must be
synthesised from T gates on fault-tolerant hardware, so it usually loses the T-count contest to the
humble Toffoli ripple adder. The trade-off table every implementer keeps in their head:
| Adder | Style | Ancillas | Fault-tolerant cost |
| VBE ripple-carry | classical reversible | ≈ n carry qubits | ≈ 4n Toffolis |
| Cuccaro ripple-carry | classical reversible | 1 | ≈ 2n Toffolis |
| Draper QFT adder | quantum-native (phase) | 0 | ≈ n²/2 small rotations — T-expensive |
- Shor's modular exponentiation compiles as: \approx 2n controlled modular
multiplications → \approx 2n^2 modular additions →
O(n^3) Toffolis, via the VBE/Cuccaro adders of classical reversible
synthesis;
- intermediate registers (carries, partial products, comparison bits) are uncomputed everywhere —
about half the circuit is Bennett-style cleanup;
- fault-tolerant cost is measured in Toffoli/T-count, which the arithmetic
dominates; the QFT and Hadamard layers are a rounding error;
- quantum-native arithmetic (Draper's phase-space adder) trades ancillas for rotations — usually
dearer after error correction;
- conclusion: the classical reversible-synthesis toolbox is the quantum compiler
backend.
Put numbers on the tower for n = 2048, the modulus size guarding much of the
internet. Logical requirements, by the standard estimates: about 3n \approx 6{,}000
logical qubits (input register plus accumulator plus workspace) and a few billion
Toffolis of modular arithmetic. Now apply the fault-tolerance exchange rate — each logical qubit is a
patch of error-corrected physical qubits, roughly a thousand physical per logical at plausible error
rates — and you land near the famous Gidney–Ekerå 2019 estimate: about 20 million noisy physical
qubits, running for about 8 hours. For scale, today's largest machines have on the order of a
thousand physical qubits, so the gap is four orders of magnitude — daunting, but a matter of
engineering, not of principle, which is why post-quantum cryptography is being deployed now.
And notice what those 20 million qubits would spend 8 hours doing: additions. Reversible, carry-uncomputing,
Cuccaro-style additions. The apocalypse of classical cryptography, if it comes, will be powered by the
contents of your synthesis module.
A classic misreading of Shor: "the quantum Fourier transform is the heart of the algorithm, so that must
be what the qubits and gates are for." Backwards, on both counts. The QFT on
2n qubits costs O(n^2) small gates — and in the
semiclassical (measurement-based) variant it nearly vanishes — while the modular exponentiation costs
O(n^3) Toffolis: for n = 2048, upwards of 99% of
everything. The QFT is the algorithm's idea; the arithmetic is its cost. When you read
a headline resource estimate for factoring, you are reading an estimate of classical reversible
arithmetic — which is why every improvement in reversible adders and multipliers (a beloved genre of
quantum-computing papers to this day) moves the "when can it break RSA?" date.