Ancilla Qubits
Quantum circuits must be reversible — every gate is a unitary and can be undone. This creates a practical problem: when you need scratch space to hold an intermediate result, how do you get rid of it without disturbing your data? The answer is a technique called uncomputation.
What is an ancilla qubit?
An ancilla (Latin for "helper") is a qubit that starts in a known state, typically , is temporarily used to hold an intermediate value, and is then returned to its original state before the end of the computation. A clean ancilla:
- does not become entangled with the output, so measuring or discarding it causes no damage,
- can be reused in later parts of the same circuit.
If you forget to uncompute, the ancilla stays entangled with the rest of the register. Tracing it out would collapse a superposition — the quantum analogue of a memory leak that corrupts your answer.
A worked example: AND via Toffoli
Suppose you want to flip a target qubit only when both and . The classical AND of two bits cannot be done with a two-qubit reversible gate alone — reversibility demands as many outputs as inputs. The standard fix is to borrow an ancilla initialised to and use the Toffoli gate :
After , the ancilla holds while the data qubits are unchanged. You can then use as a control for further gates. Once you are done, apply again:
Because for any bit , the second Toffoli undoes the first and returns the ancilla exactly to . This pattern — compute, use, uncompute — is the backbone of every large quantum algorithm that must manage qubit overhead.
Circuit picture
A critical constraint for uncomputation is that the Toffoli's control qubits must be unchanged between the two Toffoli calls. If the "use" step modifies one of those controls, the second Toffoli no longer sees the same inputs and cannot undo the first. To avoid this pitfall the standard pattern uses a dedicated output qubit that is separate from both data qubits.
For a four-qubit register with , , (ancilla), (output):
- : ancilla becomes . State is .
- : output is flipped (controlled on ancilla ). State is .
- : and are still 1, so and the ancilla flips back: . State is .
The ancilla is clean (), and are unchanged, and has been flipped exactly when both inputs were 1. Because the "use" step (the CX on line 2) touches only — not or — the second Toffoli is a perfect inverse of the first.
The correct order is:
- Compute: — ancilla now holds .
- Use: — this modifies (an output qubit, not a Toffoli control).
- Uncompute: — with unchanged and .
Trace through: starting from ,
- After step 1: (ancilla ).
- After step 2: ( flipped because ).
- After step 3: , so ancilla flips back: .
The ancilla is clean () and the output has been set to , implemented without leaving any garbage.
Try it
Complete the three TODOs: the two Toffoli gates and the intermediate CX. The grader verifies the full statevector of the four-qubit register, so the ancilla (q2) must end in and the output (q3) must end in .
Sign in on the full site to ask questions and join the discussion.