Uncomputation
Quantum gates are reversible: every operation has an inverse, and running a circuit backwards undoes it exactly. This property is not just a curiosity — it is the foundation of uncomputation, the technique that makes large quantum algorithms practical.
Why uncomputation matters
Many algorithms compute a value into an ancilla (helper) qubit and then need that ancilla reset to so it can be reused or so it does not entangle with the rest of the state. If the ancilla is left in a non- state — or, worse, entangled with the data register — it becomes garbage that leaks information about the computation and destroys interference.
The fix is always the same: after using the ancilla's value, apply the exact inverse sequence of gates that produced it. Because every gate is unitary, the inverse of a gate is its conjugate transpose , and the inverse of a sequence is
The Toffoli as an AND gate
The Toffoli (CCX) gate sets its target qubit to . Starting with the target in , this computes the classical AND of the two controls:
Because CCX is self-inverse (), applying it a second time restores the target to , as long as the controls have not changed. This is the canonical uncomputation pattern for reversible AND.
The uncomputation pattern
A typical three-step pattern looks like this in circuit notation:
- Compute the ancilla: apply the forward circuit to write a result into .
- Use the ancilla: apply some dependent operation that reads (but does not destroy) the ancilla.
- Uncompute the ancilla: apply in reverse to return .
After step 3 the ancilla is disentangled from the data register and is back in , ready to be reused or discarded without affecting the state of the data qubits.
Try it
The starter code initialises , computes with a Toffoli, and then flips with an gate to simulate "using" the ancilla (turning into ). Your task is to uncompute so that the final state of all three qubits matches what the reference solution produces.
After running the reference solution you will see that qubit 2 is back in even though it was both computed into and then modified during the use step. The data qubits and are unchanged — the ancilla was truly freed. Notice that uncomputation had to reverse the entire compute-and-use sequence (first undo the , then undo the CCX), not just the original compute step.
Sign in on the full site to ask questions and join the discussion.