Lesson 13: Reversible computing and uncomputation
Compute, copy, uncompute: why leftover qubits wreck interference
The interactive version checks every answer, gives hints and saves your progress in this browser. This page is the same lesson on one page, with the answers folded away.
1 / 12 · Learn
Before you start
Lesson 6 built classical logic from reversible gates, and Lesson 9 used an oracle without asking how it is made. This lesson builds oracles from gates and finds the catch: scratch work left behind in extra qubits quietly breaks the interference that quantum algorithms depend on.
By the end of this lesson you can:
- build for small Boolean functions from , CNOT and Toffoli gates, with anti-controls and work qubits;
- explain why a work qubit left holding wrecks the interference in Deutsch’s algorithm, and what a mixed state is;
- clean up with compute, copy, uncompute, and count what it costs;
- turn a clean bit oracle into a phase oracle.
Check yourself
A warm-up: make a guess before reading on. The lesson comes back to it.
Deutsch’s algorithm for , with one change: the oracle uses a work qubit , starting in , for scratch work, and leaves it holding a copy of the input, . The output qubit is in as usual, and nobody measures . What does the 0/1 measurement of the input qubit give after the last ?
- A0 every time
- BThe answer moves into : measuring would give
- C1 every time, as in the algorithm without
- D0 or 1, each with probability
Show the answer
Answer: D. 0 or 1, each with probability
Before the last the state is , so the input is entangled with . The last sends each branch to both outcomes of the input, but the branches land on different values of , so their amplitudes never add or cancel. Each outcome has probability .
Why not the others
- A. Nothing flips the answer. A leftover that records stops the two signs from meeting at all, so neither outcome is certain.
- B. only ever holds , which says nothing about ; measured, it gives the input’s value, 0 or 1 at random. With in , the values of become signs on the input’s branches, not bits in .
- C. Nobody measures , but it is not idle: the branch carries and the branch carries . The last can only combine amplitudes that sit on the same basis state of every qubit, so the two signs never meet, and the input reads 0 or 1 at random.
Check yourself
A warm-up: make a guess before reading on. The lesson comes back to it.
Start with , a work qubit and the output . Compute into (one Toffoli from onto ). Copy into with a CNOT. Then uncompute: the same Toffoli again. What are and at the end?
- A,
- B,
- C,
- D,
Show the answer
Answer: D. ,
Compute: . Copy: , and is left as it was. Uncompute: the compute gates in reverse order turn back into 0, and they never touch . So and .
Why not the others
- A. That treats the uncompute as undoing everything. It runs only the compute gates backwards, and those never touch . The copy wrote into , and nothing undoes it: ends at .
- B. The uncompute does clear . The copy only read , so still holds when the uncompute starts, and running the compute gates backwards turns it back into 0.
- C. That leaves the answer stuck in . The copy CNOT has control , so it does flip , to ; then the uncompute returns to 0.
2 / 12 · Learn
Oracles from reversible gates
Three reversible gates from Lesson 6 do all of classical logic:
- is NOT: .
- CNOT adds its control into its target: . With it copies a bit, and two CNOTs into one target compute XOR.
- Toffoli adds an AND: . With it computes .
None of them destroys its inputs, and each is its own inverse. A gate that sent to just would map three inputs to the same output 0, which no unitary can do. So to compute you build the oracle of Lesson 9, : keep the input, and add the value into an output qubit .
Check yourself
Build for (XOR), with the inputs on the top two wires and on the bottom. The truth table shows what your circuit does to every basis input.
Build it in the circuit builder (gates: control, X).
Hints
- A CNOT adds one bit into its target. How many bits do you need to add into ?
- One CNOT from onto , and one from onto .
Show the answer
One circuit that works: CNOT q0→q2, then CNOT q1→q2.
Each CNOT adds one input into , so two of them give . Both inputs pass through unchanged, as requires.
Some functions need a gate that fires on a 0. An open circle, ○, is an anti-control: the gate acts when that qubit is 0. In code it is an before and after on that qubit, or Qiskit’s ctrl_state: qc.ccx(0, 1, 2, ctrl_state='00') fires only when q0 and q1 are both 0.
OR then follows from De Morgan’s law, . A Toffoli with two anti-controls flips only on , which computes NOR. An on afterwards flips every row of the truth table, and turns NOR into OR. In general, a function that is 1 on most inputs is easiest to build from its complement plus one .
Check yourself
Build for (OR). The palette’s ○ places an anti-control.
Build it in the circuit builder (gates: control, anti-control, X).
Hints
- On which single input is OR equal to 0?
- Flip on with a Toffoli that has two anti-controls; then flip on every input.
Show the answer
One circuit that works: controlled-X q0=0,q1=0→q2, then X on q2.
The anti-controlled Toffoli flips only on (NOR); the then flips it on every input. Net effect: is flipped on , and , exactly where . Three Toffolis with ordinary controls, one per input with , would also work, at three times the cost.
3 / 12 · Learn
Work qubits
A Toffoli has only two controls, so a function of three bits often needs an intermediate result. Take . First compute into a work qubit that starts at 0 (Lesson 6 called such a helper an ancilla). Then one Toffoli from and writes the answer into .
The program below does this on the input : its first two lines only prepare that input. Run it and read the final ket, then change the input lines and run it again.
Check yourself
A program on five qubits (q0–q2 the input, q3 the work qubit , q4 the output ) runs qc.ccx(0, 1, 3, ctrl_state='00'), then qc.x(3), then qc.ccx(2, 3, 4). The input is , and and start at 0. What are and afterwards?
- A,
- B,
- C,
- D,
Show the answer
Answer: D. ,
: the anti-controlled Toffoli computes NOR, and the turns it into OR. Then . The work qubit keeps , since nothing clears it.
Why not the others
- A. The work qubit is not cleared: no gate here resets it. The first two gates leave , and it stays 1. The last Toffoli sets .
- B. The work qubit is not cleared: no gate here resets it. The first two gates leave , and it stays 1.
- C. The last Toffoli sets .
Look at what is left over: still holds . On a classical computer that is harmless junk, to be ignored or overwritten later. In a quantum circuit it is not harmless, as the next experiment shows.
4 / 12 · Explore
The trouble with leftovers
Predict, then see
A program runs Deutsch’s algorithm for with one change. The oracle works in two steps: it copies into a work qubit , then writes into , and it leaves as it is. Without the work qubit, the measurement of the input gives 1 every time ( is balanced). What do 1000 runs of this program give?
- A0 every time: the extra CNOT flips the answer
- B0 and 1, about 500 each
- C1 every time: is never measured, so it can’t matter
- DMostly 1, with a few 0s
Why does a qubit that nobody measures change the result? A sentence or two is enough.
Show what happens
What happens: B. 0 and 1, about 500 each
With holding a copy of , the input reads 0 or 1 at random, about 500 each. Adding qc.cx(0, 1) after the write undoes the scratch work: returns to 0 in both branches, and the input reads 1 every time again.
Why not the others
- A. The extra CNOT doesn’t flip the answer to the other certainty. It records which branch is which, and then neither outcome is certain.
- C. A qubit nobody looks at seems to be irrelevant. But took part in the circuit: it is entangled with the input, and the counts show the interference is gone.
- D. A perfect copy spoils the interference completely, not a little: the counts are close to 50/50. A partial record does give something in between, as you will see shortly.
Follow the state, with in throughout (it is left out of the kets below). After the first the input is and . The scratch CNOT gives : the input and are now entangled. Writing into kicks back the sign : .
The branches carry opposite signs, just as in Deutsch’s algorithm. But the last sends each branch to both and on the input, and the branch lands on while the branch lands on . Amplitudes add or cancel only when they sit on the same basis state of every qubit. Here nothing meets, so nothing cancels: four outcomes, each with probability .
Nothing left in w
A copy of x left in w
Deutsch’s algorithm for as a ledger. Each branch of the input sends an arrow carrying to each outcome of the last it can reach. With nothing left in , both branches reach the same two outcomes, so the arrows add on “reads 1” and cancel on “reads 0”. With a copy of left in , the branches reach different outcomes, so nothing cancels and each of the four has probability .
Check yourself
Deutsch’s algorithm for , where the oracle leaves its work qubit in the state below. In which cases does the input still read 1 every time?
Select all that apply.
- A, a copy of the input
- B for both inputs
- C for both inputs
- D, the input flipped
- E for both inputs
Hint
- Does differ between the branches and ?
Show the answer
Answers: B. for both inputs; C. for both inputs; E. for both inputs
What matters is whether differs between the branches. If it is the same state for and , whatever state that is, it factors out and the branches interfere as usual. If it differs, as with or , it records the branch and the interference is lost.
Why not the others
- A. A copy differs between the branches: for , for . It records which branch is which, so the branches can’t interfere, and the input reads 0 or 1 at random.
- D. Flipping the copy doesn’t help. still differs between the branches, so it still records which branch is which, and the interference is just as lost as with .
5 / 12 · Learn
A qubit with no ket: mixed states
In the leaky circuit, after the write into , the input and share the state , a Bell state up to a sign. The input has no ket of its own (Lesson 7). You can still ask what any measurement of the input alone would give: each in the 0/1 basis, and each in the basis too, because the branches can’t interfere. The same goes for .
Here is a way to picture that. Read a Bloch vector off the statistics:
For any qubit with a ket this vector has length 1 and points at the state. For the leaky input it is : the centre of the sphere.
Check yourself
In the clean algorithm for , the input is just before the last , so its Bloch vector’s component is . In the leaky circuit, with holding a copy of , what is just before the last ?
Hints
- What are and for the leaky input?
- Measuring in the basis means an and then a 0/1 measurement, and with holding that gives each outcome half the time.
Show the answer
Answer:
, so the component is . The other two components are 0 as well: the vector has shrunk to the centre.
Common slips
- : That is the clean value. With holding , the branches can’t interfere in a measurement either, so .
- : That is one of the two probabilities. The component is their difference: .
- : A component of would mean the input is certainly . Here the measurement is a fair coin.
A qubit whose Bloch vector is shorter than 1 is in a mixed state. A mixed state is not a superposition. In Lesson 2, and a coin that gives or both gave 50/50 in the 0/1 basis, but in the ± basis gave every time while the coin stayed 50/50. The leaky input behaves like the coin: 50/50 in every basis. Gates on the input alone only rotate its Bloch vector, so none of them can make it longer: the information that would restore the interference now sits in .
Qubits with a ket sit on the surface of the sphere; mixed states sit inside it. The tool that makes this precise is the density matrix, which describes a qubit even when it has no ket of its own. This lesson doesn’t need it; the density-matrix course under Go further introduces it.
Check yourself
The leaky circuit has run up to just before the last , with holding a copy of . Which statement is true?
- AAn on the input makes it definite again, as it does for
- BMeasuring and throwing the result away restores the interference
- CNo gate on the input alone can make it read 1 every time again
- DThe input is still ; only adds noise when the input is measured
Show the answer
Answer: C. No gate on the input alone can make it read 1 every time again
The input’s Bloch vector has length 0, and gates on the input alone only rotate the vector; they can’t lengthen it. What decides the branch is recorded in , so only a gate that acts on too, undoing the copy, can restore the interference.
Why not the others
- A. That works for a superposition, whose branches can interfere. Here the branches are tagged by , so an on the input leaves it 50/50: it acts like the coin from Lesson 2, not like .
- B. Measuring tells you , which leaves the input in that one branch. A single branch gives 0 or 1 at random after the last . Only undoing the copy, in every branch at once, brings the interference back.
- D. If the input were , the last would give 1 every time. It is entangled with and has no ket of its own: its Bloch vector has length 0, not 1.
A full copy is the extreme case. Scratch work can also leave a partial record: say ends in when and in when . How alike the two records are is measured by their overlap, .
The branches can now interfere only through the part where the records agree. The same calculation as before gives an input Bloch vector of length , and the algorithm gives the right answer with probability . Identical records () do no harm. Orthogonal records (), such as a full copy, leave a coin toss. In between, the answer is right more often than not, but not every time.
The input qubit just before the last , in Deutsch’s algorithm for ; each disc is the – slice of the Bloch sphere. The scratch work leaves in for and in the state shown for . With no record the vector reaches and the answer is certain; a full copy shrinks it to the centre. is the probability of the right answer, .
The two records in w
The input before the last H
The two records in , drawn as arrows in the plane of real amplitudes, apart. Their overlap is the shadow of on , marked by the bar. The input before the last has a Bloch vector of exactly that length. Identical records do no harm; orthogonal ones leave a coin toss.
Check yourself
In Deutsch’s algorithm for a balanced , the oracle’s scratch work leaves the work qubit in when and in when . What is the probability that the algorithm gives the right answer?
Hints
- Only the part of the two records that agrees can interfere. How much do they overlap?
- The overlap is , and the right answer comes out with probability .
- Here is the amplitude of : .
Show the answer
Answer:
The records overlap by , so the right answer comes out with probability .
Before the last the state is with signs , and since is balanced. The last makes the part with the input at 1, the right answer, equal to . Its squared length is . Here , so .
Common slips
- : That would need the two records to be identical. These overlap only partly, so part of the interference is lost.
- : A coin toss is what orthogonal records give, such as a full copy of . These records overlap, and the overlapping part still interferes.
- : That squares the overlap. The overlap enters the probability once, through the cross term: .
6 / 12 · Learn
Compute, copy, uncompute
The cure is to clean up after yourself. Call the scratch part of an oracle : it writes , and whatever else it needs on the way, into work qubits that start at 0. Then:
- Compute: run . The work qubits now hold , which includes .
- Copy: CNOT the qubit holding into the output .
- Uncompute: run , the gates of in reverse order. , CNOT and Toffoli are their own inverses, so uses the same gates, last one first.
Step 3 returns every work qubit to 0, in every branch at once. The net effect is : exactly , with nothing left behind.
The pattern
Three-input AND, input 111
Compute, copy, uncompute. The pattern holds for any ; the shaded cells are the ones each step changes. The second table runs the three-input AND on , with two work bits and . The answer reaches in the copy step and stays there while the work bits are cleared.
Why doesn’t step 3 erase the copied answer too? consists of the gates of , and never touched ; only the copy wrote into it. And really does undo , because the copy left the work qubits exactly as left them: on each basis branch a CNOT leaves its control unchanged.
This is Charles Bennett’s trick from 1973. The answer is copied out before the scratch work is undone, and the work qubits end where they began.
Check yourself
This is the leaky circuit from the experiment: on the input, the scratch CNOT (), the write into (which starts in ), and the last . Column 4 is empty. Add one gate there so the input reads 1 every time.
Build it in the circuit builder (gates: control, X).
Hints
- Which step left holding ? Undo it, after the write and before the last .
- A CNOT from onto , in column 4.
Show the answer
One circuit that works: H on q0, then CNOT q0→q1, then CNOT q1→q2, then CNOT q0→q1, then H on q0.
The scratch CNOT runs again after the write: in both branches. The state before the last is , and the last gives . The same CNOT after the last comes too late: by then the branches have already failed to interfere.
Two practical points. Order: runs the gates of backwards. Each gate can only be undone while the qubits it reads still hold what they held when it ran, and that is true of the last gate first.
Cost: if uses gates and the answer has bits, the clean oracle uses to compute, CNOTs to copy and to uncompute, in all: roughly twice the gates. In exchange the work qubits come back at 0, free to be used again by the next step of a larger circuit. Often the last step of can write straight into instead of a work qubit, which saves a qubit and two gates; the worked example below does that.
Check yourself
This compute part writes into q4, using q3 for scratch: qc.x(0), then qc.ccx(0, 1, 3), then qc.ccx(2, 3, 4). A CNOT then copies q4 into the output qubit. Put the three gates of the uncompute in order, first at the top.
Put these in order.
qc.ccx(2, 3, 4)qc.x(0)qc.ccx(0, 1, 3)
Hints
- Which gate of ran last? Its effect has to be removed first.
- The uncompute is read from the end: same gates, reverse order.
Show the answer
qc.ccx(2, 3, 4)qc.ccx(0, 1, 3)qc.x(0)
Undo the last gate first: qc.ccx(2, 3, 4), then qc.ccx(0, 1, 3), then qc.x(0). Each gate is its own inverse, and each one can only be undone while the qubits it reads hold what they held when it ran. In the original order, qc.x(0) would restore first, and then qc.ccx(0, 1, 3) would add to q3 instead of removing .
7 / 12 · Worked example
Worked example: oracles that clean up
In the first programs below, q0–q2 hold the input, q3 is the work qubit and q4 is the output.
Worked example
A three-input AND, then your turn
Goal: for . A Toffoli has two controls, so the AND of three bits needs one intermediate result in the work qubit.
Compute
qc.ccx(0, 1, 3)writes into the work qubit (q3).Copy
qc.ccx(2, 3, 4)flips when and , so becomes . The last AND happens inside the copy step, which saves a second work qubit.Uncompute
qc.ccx(0, 1, 3)again: held , and the Toffoli adds once more, giving 0. Trace for , : . Three gates, one work qubit, nothing left behind.Your turn: uncompute
The program computes the way the “Work qubits” step did, but it stops before the uncompute and leaves q3 holding .
Your turn
Add the uncompute lines at the end, so that q3 returns to 0 on every input. The grader tries all 16 basis inputs (, and = 0 or 1), so don’t add gates that set the input.
from qiskit import QuantumCircuit qc = QuantumCircuit(5) # q0, q1, q2: the input x; q3: work w; q4: output y # compute x0 OR x1 into the work qubit q3 qc.ccx(0, 1, 3, ctrl_state='00') # flips q3 when q0 = 0 and q1 = 0 qc.x(3) # so now q3 = x0 OR x1 # copy: AND the work qubit with x2 into the output qc.ccx(2, 3, 4) # uncompute: undo the compute lines, last one firstWrite the program in the app’s code editor (Python with Qiskit’s circuit methods), run it, and check it.
Hints
- Which lines computed q3? Only those need undoing; the copy line stays.
- Undo them last one first:
qc.x(3), then the anti-controlled Toffoli.
Show the answer
One program that works:
from qiskit import QuantumCircuit qc = QuantumCircuit(5) # q0, q1, q2: the input x; q3: work w; q4: output y # compute x0 OR x1 into the work qubit q3 qc.ccx(0, 1, 3, ctrl_state='00') # flips q3 when q0 = 0 and q1 = 0 qc.x(3) # so now q3 = x0 OR x1 # copy: AND the work qubit with x2 into the output qc.ccx(2, 3, 4) # uncompute: undo the compute lines, last one first qc.x(3) qc.ccx(0, 1, 3, ctrl_state='00')The compute part was the anti-controlled Toffoli and then
qc.x(3). Run backwards:qc.x(3), thenqc.ccx(0, 1, 3, ctrl_state='00'). That returns q3 to 0 on every input and leaves q4 holding .
One more, fully worked: , where the order of the uncompute matters.
Another worked example
Another example: a negated input
Compute
qc.x(0)flips the input for a moment, thenqc.ccx(0, 1, 3)writes into . The compute part changed an input qubit, so the uncompute must change it back.Copy
qc.ccx(2, 3, 4)adds into .Uncompute
Reverse order:
qc.ccx(0, 1, 3)first, while is still flipped, so it removes from ; thenqc.x(0)restores . Trace for , : . The other order would restore too early and leave behind.
Check yourself
Now without help: write for . q0–q3 hold , q4 and q5 are work qubits that must end at 0, and q6 is the output . The grader tries all 32 basis inputs.
from qiskit import QuantumCircuit
qc = QuantumCircuit(7) # q0..q3: the input x; q4, q5: work; q6: output y
# compute, copy into q6, uncomputeWrite the program in the app’s code editor (Python with Qiskit’s circuit methods), run it, and check it.
Hints
- Build the AND up one input at a time: into q4, then that with into q5.
- The copy can be a Toffoli from q5 and into q6. Then undo the q5 line, then the q4 line.
Show the answer
One program that works:
from qiskit import QuantumCircuit
qc = QuantumCircuit(7) # q0..q3: the input x; q4, q5: work; q6: output y
qc.ccx(0, 1, 4) # compute: q4 = x0 AND x1
qc.ccx(2, 4, 5) # q5 = x0 AND x1 AND x2
qc.ccx(3, 5, 6) # copy: y ^= q5 AND x3
qc.ccx(2, 4, 5) # uncompute, last line first
qc.ccx(0, 1, 4)Compute into q4 and into q5; a Toffoli from q5 and q3 writes the answer into q6; the two compute Toffolis then run again in reverse order. Five Toffolis in all, and both work qubits end at 0.
8 / 12 · Learn
Phase oracles from bit oracles
In Lesson 9, a target in turned the bit oracle into signs: . That works for any clean , including one built with work qubits, since compute, copy, uncompute is . In circuit terms: put the output in , compute, copy into the output (the sign kicks back onto the branch), uncompute.
The result is a phase oracle, , with the work qubits back at 0 and the output still , so both can be reused. Without the uncompute the sign lands on instead, and any later interference fails, exactly as before. Grover’s search, later in this part, needs phase oracles built this way.
Check yourself
The inputs start in and in . Build the phase oracle for OR, so that the inputs end in and stays .
Build it in the circuit builder (gates: control, anti-control, X).
Hints
- A bit oracle for OR, with in , is already a phase oracle.
- Use the OR oracle you built earlier: a Toffoli with two anti-controls, then on .
Show the answer
One circuit that works: controlled-X q0=0,q1=0→q2, then X on q2.
The OR oracle flips on , and , and flipping gives : those three branches pick up a minus sign. The anti-controlled Toffoli alone also passes. It flips only on , which gives the opposite signs, and that is the same state up to a global phase of .
Check yourself
Both inputs start in , a work qubit in and the output in . Compute into (a Toffoli from onto ), then CNOT into , then run the compute gates again in reverse order. What is the state of the two inputs at the end?
- A, with back in : a clean phase oracle
- B: the uncompute undoes the signs along with the scratch work
- CEntangled with : the signs sit on , and the inputs have no ket of their own
- D, unchanged: the inputs are only controls
Show the answer
Answer: A. , with back in : a clean phase oracle
Compute: . Copy into : each branch gains the sign . Uncompute: returns to in every branch, so the inputs are left in , a product with .
Why not the others
- B. The uncompute undoes the compute gates, not the copy. The signs came from the copy into , and they stay: the inputs end in .
- C. The uncompute returns to in every branch, so factors out. What is left on the inputs is the sign pattern, .
- D. Kickback: the CNOT into multiplies each branch by . Those signs differ between branches, so the inputs change even though they only act as controls.
9 / 12 · Practice
Practice
Three items, each with new numbers when it comes back. Hints are there if you need them.
Check yourself
Build for : inputs (top) and , output (bottom), so that . The truth table shows what your circuit does to every basis input.
Build it in the circuit builder (gates: control, anti-control, X).
Hints
- On which inputs is equal to 1? A Toffoli flips on exactly one input pattern, and an anti-control ○ asks for a 0 in that place.
- If is 1 on three of the four inputs, flip on the one input where is 0, then flip on every input with an .
- Here is 1 on .
Show the answer
One circuit that works: Toffoli q0,q1→q2.
One Toffoli: it flips only on . The truth table then matches on all eight rows.
Check yourself
Deutsch–Jozsa with two input qubits and the balanced function . The oracle is built with a work qubit and leaves it holding ; the output is in as usual. What is the probability that the two input qubits read at the end?
Hints
- On every input contributes , but contributions add only when they also share the value of .
- Group the four inputs by . In each group add the signs and divide by 4; then square each group’s amplitude and add.
- Here for , and for and and .
Show the answer
Answer:
Grouped by : : input , sign , amplitude ; : inputs and and , signs , amplitude . So .
After the last Hadamards, the amplitude of is over the inputs with , because only those land on that basis state. : input , sign , amplitude . : inputs and and , signs , amplitude . Square and add: . Without the leftover all four signs would share one sum, giving .
Common slips
- : That is the answer without the leftover. Here holds , so on only inputs with the same value of can add or cancel.
- : That squares each input’s contribution on its own, as if every input had a different value of . Inputs with the same do share a basis state, so their contributions add before you square.
Check yourself
The compute part of an oracle uses 7 gates, each an , CNOT or Toffoli, and leaves a 1-bit answer in work qubits. The clean oracle computes, copies each answer bit into an output qubit with one CNOT, and uncomputes. How many gates does it use?
Hints
- What does the uncompute consist of, and how long is it?
- The uncompute is backwards: 7 gates again. The copy is one CNOT per answer bit.
Show the answer
Answer:
7 to compute, 1 to copy, 7 to uncompute: .
Common slips
- : That leaves out the uncompute. Running backwards costs another 7 gates, the price of leaving nothing behind.
- : That leaves out the copy: one CNOT per answer bit, 1 more.
- : The answer is copied once, between the compute and the uncompute: one CNOT per answer bit, not two.
10 / 12 · Explain
Explain it
Explain it
A friend says: “The work qubit is never measured, so whatever the oracle leaves in it can’t change the answer.” Using Deutsch’s algorithm, explain what goes wrong, and how compute, copy, uncompute fixes it.
Show a model answer
If the oracle leaves holding , the state before the last is : the input is entangled with . The last sends each branch to both outcomes of the input, but the two branches land on different values of , and amplitudes only add or cancel on the same basis state of every qubit. So the signs never meet, and the input reads 0 or 1 at random; nobody has to measure for this to happen. Uncomputing runs the scratch gates backwards after the answer has been copied into . That returns to in every branch, so it factors out, the branches meet again, and the answer is certain.
A good answer covers
- The leftover in differs between the branches, so the input is entangled with
- Interference needs the branches to land on the same basis state of all the qubits; with different they can’t add or cancel, so the result is 50/50
- Uncomputing after the copy returns to in every branch, so it factors out and the interference is back
- The copied answer survives the uncompute, because the uncompute never touches
11 / 12 · Mastery check
Mastery check
Check yourself
Deutsch’s algorithm for , with one change: the oracle uses a work qubit , starting in , for scratch work, and leaves it holding a copy of the input, . The output qubit is in as usual, and nobody measures . What does the 0/1 measurement of the input qubit give after the last ?
- A0 or 1, each with probability
- B0 every time
- C1 every time, as in the algorithm without
- DThe answer moves into : measuring would give
Show the answer
Answer: A. 0 or 1, each with probability
Before the last the state is , so the input is entangled with . The last sends each branch to both outcomes of the input, but the branches land on different values of , so their amplitudes never add or cancel. Each outcome has probability .
Why not the others
- B. Nothing flips the answer. A leftover that records stops the two signs from meeting at all, so neither outcome is certain.
- C. Nobody measures , but it is not idle: the branch carries and the branch carries . The last can only combine amplitudes that sit on the same basis state of every qubit, so the two signs never meet, and the input reads 0 or 1 at random.
- D. only ever holds , which says nothing about ; measured, it gives the input’s value, 0 or 1 at random. With in , the values of become signs on the input’s branches, not bits in .
Check yourself
Build for : inputs (top) and , output (bottom), so that . The truth table shows what your circuit does to every basis input.
Build it in the circuit builder (gates: control, anti-control, X).
Hints
- On which inputs is equal to 1? A Toffoli flips on exactly one input pattern, and an anti-control ○ asks for a 0 in that place.
- If is 1 on three of the four inputs, flip on the one input where is 0, then flip on every input with an .
- Here is 1 on .
Show the answer
One circuit that works: Toffoli q0,q1→q2.
One Toffoli: it flips only on . The truth table then matches on all eight rows.
Check yourself
Deutsch–Jozsa with two input qubits and the constant function . The oracle is built with a work qubit and leaves it holding ; the output is in as usual. What is the probability that the two input qubits read at the end?
Hints
- On every input contributes , but contributions add only when they also share the value of .
- Group the four inputs by . In each group add the signs and divide by 4; then square each group’s amplitude and add.
- Here for and , and for and .
Show the answer
Answer:
Grouped by : : inputs and , signs , amplitude ; : inputs and , signs , amplitude . So .
After the last Hadamards, the amplitude of is over the inputs with , because only those land on that basis state. : inputs and , signs , amplitude . : inputs and , signs , amplitude . Square and add: . Without the leftover all four signs would share one sum, giving .
Common slips
- : That is the answer without the leftover. Here holds , so on only inputs with the same value of can add or cancel.
- : That squares each input’s contribution on its own, as if every input had a different value of . Inputs with the same do share a basis state, so their contributions add before you square.
Check yourself
Start with , a work qubit and the output . Compute into (a Toffoli, then on ). Copy into with a CNOT. Then uncompute: on , then the Toffoli. What are and at the end?
- A,
- B,
- C,
- D,
Show the answer
Answer: B. ,
Compute: . Copy: , and is left as it was. Uncompute: the compute gates in reverse order turn back into 0, and they never touch . So and .
Why not the others
- A. That treats the uncompute as undoing everything. It runs only the compute gates backwards, and those never touch . The copy wrote into , and nothing undoes it: ends at .
- C. The uncompute does clear . The copy only read , so still holds when the uncompute starts, and running the compute gates backwards turns it back into 0.
- D. That leaves the answer stuck in . The copy CNOT has control , so it does flip , to ; then the uncompute returns to 0.
Check yourself
This compute part writes into q6, using q4 and q5 for scratch: qc.ccx(0, 1, 4), then qc.ccx(2, 4, 5), then qc.ccx(3, 5, 6). A CNOT then copies q6 into the output qubit. Put the three gates of the uncompute in order, first at the top.
Put these in order.
qc.ccx(2, 4, 5)qc.ccx(3, 5, 6)qc.ccx(0, 1, 4)
Show the answer
qc.ccx(3, 5, 6)qc.ccx(2, 4, 5)qc.ccx(0, 1, 4)
Undo the last gate first: qc.ccx(3, 5, 6), then qc.ccx(2, 4, 5), then qc.ccx(0, 1, 4). Each gate is its own inverse, and each one can only be undone while the qubits it reads hold what they held when it ran. In the original order, qc.ccx(0, 1, 4) would clear q4 first, and then qc.ccx(2, 4, 5) could no longer clear q5, because it needs q4 to still hold .
Check yourself
Both inputs start in , a work qubit in and the output in . Compute into (a Toffoli from onto ), then CNOT into , then run the compute gates again in reverse order. What is the state of the two inputs at the end?
- A: the uncompute undoes the signs along with the scratch work
- B, with back in : a clean phase oracle
- CEntangled with : the signs sit on , and the inputs have no ket of their own
- D, unchanged: the inputs are only controls
Show the answer
Answer: B. , with back in : a clean phase oracle
Compute: . Copy into : each branch gains the sign . Uncompute: returns to in every branch, so the inputs are left in , a product with .
Why not the others
- A. The uncompute undoes the compute gates, not the copy. The signs came from the copy into , and they stay: the inputs end in .
- C. The uncompute returns to in every branch, so factors out. What is left on the inputs is the sign pattern, .
- D. Kickback: the CNOT into multiplies each branch by . Those signs differ between branches, so the inputs change even though they only act as controls.
Check yourself
Write for . q0–q2 hold , q3 is a work qubit that starts at 0 and must end at 0, and q4 is the output . For a negated input use an anti-control: an before and after on that input, or ctrl_state with a 0 in that place (its rightmost character belongs to the first control). The grader tries all 16 basis inputs (, and = 0 or 1), so don’t add gates that set the input.
from qiskit import QuantumCircuit
qc = QuantumCircuit(5) # q0, q1, q2: the input x; q3: work w; q4: output y
# compute into q3, combine with the third input into q4, then uncompute q3Write the program in the app’s code editor (Python with Qiskit’s circuit methods), run it, and check it.
Show the answer
One program that works:
from qiskit import QuantumCircuit
qc = QuantumCircuit(5) # q0, q1, q2: the input x; q3: work w; q4: output y
qc.ccx(0, 1, 3, ctrl_state='10') # compute: w = NOT x0 AND x1
qc.ccx(2, 3, 4, ctrl_state='10') # copy: y ^= w AND NOT x2
qc.ccx(0, 1, 3, ctrl_state='10') # uncompute wCompute the first two literals into , combine with the third into , and undo the first step: qc.ccx(0, 1, 3, ctrl_state='10'), qc.ccx(2, 3, 4, ctrl_state='10'), qc.ccx(0, 1, 3, ctrl_state='10'). In ctrl_state the rightmost character belongs to the first control, and a 0 makes that control an anti-control. An before and after on a negated input works too.
12 / 12 · Recap
Recap
From memory
Before reading the summary, write down what you remember. Pulling it back from memory is what makes it last.
Why does a work qubit left holding spoil Deutsch’s algorithm? When is a leftover harmless?
Show the key points
- The leftover is entangled with the input: the branches carry different values of
- Amplitudes add or cancel only on the same basis state of every qubit, so the last can’t make the branches interfere
- A leftover that is the same in every branch factors out and does no harm
Describe compute, copy, uncompute, and what it costs.
Show the key points
- Compute into work qubits with ; CNOT the answer into the output; run the gates of in reverse order
- The work qubits return to 0 and the copied answer stays, since never touches the output
- About twice the gates of : ; the work qubits are free again afterwards
How do you get a phase oracle from gates?
Show the key points
- Put the output in : the copy step kicks back
- Uncompute, so the work qubits are back at 0 and the inputs carry only the signs
- Without the uncompute the signs sit on and later interference fails
Key ideas
, CNOT and Toffoli, with anti-controls (○) where a 0 should fire the gate, build for any Boolean . Functions of more bits need work qubits for intermediate results.
A work qubit left holding is entangled with the input. Branches with different can’t interfere, so Deutsch’s algorithm becomes a coin toss when holds a copy of .
A qubit with no ket of its own still has measurement statistics, and they give a Bloch vector shorter than 1: a mixed state. Records that overlap by leave length and success probability .
Compute, copy, uncompute gives with gates. The uncompute runs the gates backwards, last one first, and leaves the copied answer alone.
With the output in , a clean bit oracle is a phase oracle: .
Go further
Free resources for when you want more than this lesson covers.
- Fundamentals of quantum algorithms: Simulating classical computationsJohn Watrous, IBM Quantum · Course
Turns any Boolean circuit into Toffoli gates and shows why the garbage has to be cleaned up before it spoils interference.
- UncomputationWikipedia · Article
A short reference on clearing work qubits by running a computation backwards, and how it changes interference.
- Landauer’s principleWikipedia · Article
Why erasing a bit must give off heat, the original motive for computing reversibly.
- General formulation of quantum information: Density matricesJohn Watrous, IBM Quantum · Course
Goes past this lesson’s mixed states to density matrices, which describe one part of an entangled system exactly.