Erase a temporary AND by measurement
A scratch qubit t holds a AND b. The textbook cleanup applies the same Toffoli again, so every temporary AND costs two Toffolis.
Measure t in the X basis and reset it. If the outcome m is 1, the branches where t was 1 picked up a minus sign; a CZ on a and b puts a second minus sign on exactly those branches, and the two cancel.
Before: Uncompute with a second Toffoli, 2. After: Measure and fix, 1 (toffolis per temporary and).
- Toffolis: Lower. 2 → 1 per temporary AND.
- Qubits: Same. Same scratch qubit; adds one classical bit.
- Toffoli × qubit scores: Lower. Falls by the share of Toffolis that were cleanup; close to half in adder-heavy circuits.
- Q² × Toffoli (DQI): Lower. Same Toffoli gain; width is untouched.
from qiskit import QuantumCircuit
def before():
a, b, t = 0, 1, 2
qc = QuantumCircuit(3)
qc.ccx(a, b, t) # t = a AND b
# ... use t ...
qc.ccx(a, b, t) # uncompute with a second Toffoli
return qcdef after():
a, b, t = 0, 1, 2
qc = QuantumCircuit(3, 1)
qc.ccx(a, b, t) # t = a AND b
# ... use t ...
# measure in X, reset
qc.h(t); qc.measure(t, 0); qc.reset(t)
with qc.if_test((qc.clbits[0], 1)):
qc.cz(a, b) # repair the sign
return qc- NEEDS
- a and b unchanged since the compute; mid-circuit measurement and classical control
- SEEN IN
- Throughout the public circuits of both contests (Gidney 2018)
- PROOF
- QCP.P01.correct, all inputs and both outcomes