Quantum Computing Master Course — 055. Uncomputation
Lesson 055: Uncomputation
Smartyfy Quantum Computing Master Course
Prerequisites and objectives
Reversible computation often stores intermediate values in workspace. Leaving those values correlated with the input can obstruct later interference. Uncomputation removes temporary information by reversing the calculation that created it, while preserving the intended output elsewhere. You will derive the method, verify a four-qubit example, and distinguish coherent cleanup from measurement or reset.
The method is sometimes called compute, copy, uncompute. Here copying means a CNOT-based transfer of computational-basis function labels into a target. It does not mean duplicating an arbitrary unknown quantum state. Keeping that distinction explicit is essential to reconciling the method with no cloning.
Why unwanted records matter
Start with a data qubit in |+⟩ and workspace in |0⟩. A CNOT from data to workspace produces (|00⟩ + |11⟩)/√2. The workspace now records which computational-basis branch is present.
If you apply H to the data while ignoring the workspace, its measurement outcomes are fifty-fifty. By contrast, H applied to the original |+⟩ would produce zero with certainty. The record prevents the two data alternatives from interfering as if they shared an identical workspace state.
Applying the same CNOT again restores |+⟩|0⟩. Now H on the data again produces zero certainly. Uncomputation has coherently removed the unwanted record, not learned it and thrown away a classical note.
Derive compute, copy, uncompute
Let R be a reversible circuit that begins with input x and clean workspace. On basis inputs it produces retained information, temporary values g(x), and designated output f(x). Assume the output is encoded in computational-basis bits, so it can control CNOTs into a separate result register.
Initialize the result register to zero. Apply R, then CNOT each designated output bit into its matching result bit. The state now contains the original computed registers plus a result label f(x). Apply R† to the registers on which R acted, leaving the new result register untouched.
Because R† reverses the computation, it restores the input x and all its original clean workspace. The new result remains f(x). On an arbitrary input superposition, linearity gives Σx αx|x,0,f(x)⟩ as the final state.
The result register need not start at zero. If it starts at a basis string y, CNOT updates it to y ⊕ f(x), and uncomputation still restores the workspace. The full clean interface is therefore the reversible oracle pattern from Lesson 053.
Worked example retaining an AND result
Use register order |a,b,t,k⟩. The input bits are a,b, temporary workspace is t, and retained result is k. Start t=k=0. Apply Toffoli a,b→t to compute t=ab, CNOT t→k to record the result, then Toffoli a,b→t again to clear t.
For a=b=1, the basis sequence is 1100→1110→1111→1101. The final workspace bit is zero and the result bit is one. For any other input pair, both temporary and result bits remain zero throughout.
On input |++⟩ for a,b, the final state is (|0000⟩ + |0100⟩ + |1000⟩ + |1101⟩)/2. Every term has t=0, so that workspace factors out as a clean pure state. The retained result remains correlated with the inputs, as it should.
Cleanup does not make every useful register independent. If the requested output distinguishes input branches, that intended correlation remains. The purpose is to remove unnecessary workspace correlations, not to erase the function’s legitimate output.

Verify the construction locally
The following standard-library program was executed during lesson preparation. It checks the coherent uniform-input example and all eight clean-workspace basis cases with either initial result bit. It uses no quantum hardware or external service.
from math import sqrt
def controlled_flip(state, controls, target):
output = [0j] * len(state)
for index, amplitude in enumerate(state):
enabled = all(index & (1 << bit) for bit in controls)
destination = index ^ ((1 << target) if enabled else 0)
output[destination] = amplitude
return output
# Register order |a,b,t,k>: t is workspace, k is the retained result.
initial = [0j] * 16
for a in (0, 1):
for b in (0, 1):
initial[8*a + 4*b] = 0.5
computed = controlled_flip(initial, (3, 2), 1)
copied = controlled_flip(computed, (1,), 0)
cleaned = controlled_flip(copied, (3, 2), 1)
expected = [0j] * 16
for a in (0, 1):
for b in (0, 1):
expected[8*a + 4*b + a*b] = 0.5
assert max(abs(a-b) for a, b in zip(cleaned, expected)) < 1e-12
assert sum(abs(z)**2 for i, z in enumerate(cleaned) if i & 2) < 1e-12
assert abs(sum(abs(z)**2 for z in cleaned) - 1) < 1e-12
print('AND output retained; workspace exactly zero within tolerance')
# Check the full clean-workspace interface for both result-target values.
for a in (0, 1):
for b in (0, 1):
for y in (0, 1):
v = [0j] * 16
v[8*a + 4*b + y] = 1
v = controlled_flip(v, (3, 2), 1)
v = controlled_flip(v, (1,), 0)
v = controlled_flip(v, (3, 2), 1)
expected_index = 8*a + 4*b + (y ^ (a*b))
assert abs(v[expected_index] - 1) < 1e-12
print('All eight clean-workspace basis cases passed')
Why resetting is a different operation
Suppose the data and workspace are in Φ+. Measuring the workspace and forgetting its result leaves the data in an equal mixture of |0⟩ and |1⟩. Resetting the workspace afterward can make that register zero, but it does not restore the data’s lost phase coherence.
The resulting joint description is I/2 on the data tensored with |0⟩⟨0| on the workspace, rather than |+⟩|0⟩. A later H still gives fifty-fifty data outcomes. A physically clean workspace alone is therefore not proof that coherent uncomputation succeeded.
A known reversible inverse uses the retained correlations to undo the record without measuring which branch occurred. If necessary information has already been discarded into an inaccessible environment, reversing only the remaining gates may not recover the original coherent state.
Order and phase matter in the inverse
For a circuit whose chronological gates are G1,G2,G3, the inverse runs G3†,G2†,G1†. Self-inverse gates such as X, CNOT, and Toffoli simplify individual inverses, but they do not remove the general need to reverse order.
If R contains a phase gate or another non-self-inverse operation, use its actual adjoint. Repeating the same circuit is not generally uncomputation. A phase error that preserves every classical basis label can still spoil the intended coherent cleanup.
The result-copying step must also avoid altering the computed registers beyond using their basis labels as controls. If an unrelated operation modifies a register needed by R†, the inverse may no longer restore its initial value. This is an interface requirement, not merely a coding convention.
A phase-only use of temporary computation
Instead of copying t=ab to a result bit, apply Z to t and then uncompute it. Z multiplies the branch by −1 exactly when ab=1. After the final Toffoli, t returns to zero while the data acquire phase (−1) raised to ab.
For data |++⟩, the output is (|00⟩ + |01⟩ + |10⟩ − |11⟩)/2, with clean workspace. This is a useful way to apply a function-dependent phase while leaving no temporary basis record. The relative sign can influence later interference even though immediate Z probabilities remain uniform.
This example also shows why “undo the computation” does not mean undo every desired effect. The intermediate phase operation or retained result changes what the larger sequence accomplishes. R† removes the temporary computation while the deliberately extracted effect remains.
Four exercises with worked solutions
-
Trace the AND cleanup circuit for input a=1,b=0,t=0,k=1.
Solution. The AND is zero, so the first Toffoli leaves t=0. CNOT does not flip k, and the second Toffoli again leaves t=0. The final string is 1001, consistent with k→k⊕ab.
-
How many logical gates does the AND compute-copy-uncompute example use?
Solution. It uses two Toffolis and one CNOT, with one temporary qubit and one result target. This is a logical count; any Toffoli decomposition or physical routing adds implementation costs.
-
Does the final uniform-input state factor into the original |++⟩ and an independent result qubit?
Solution. No. Result one occurs only with input 11, so the result is correlated with the data. The temporary workspace factors out, but the intended input-output correlation remains.
-
For the phase-only version, what happens to basis input 11 and workspace zero?
Solution. Computing AND sets the workspace to one, Z contributes a minus sign, and uncomputation returns the workspace to zero. The data branch is −|11⟩. Relative to other input branches, this is a meaningful phase change.
Five explained multiple-choice questions
-
The purpose of uncomputation is to: A. Clone the unknown input. B. Discard the environment blindly. C. Measure every temporary bit. D. Restore temporary registers coherently.
Answer: D. The inverse removes unwanted records without selecting an input branch.
-
The result-copying step is valid because it: A. Copies computational-basis labels by a linear CNOT operation. B. Measures the function output first. C. Clones every unknown state. D. Ignores phase by definition.
Answer: A. On superpositions it creates intended correlations rather than independent clones.
-
After AND cleanup, the workspace t is: A. Always one. B. Zero for all clean-workspace inputs. C. Entangled with k in every case. D. Measured but unrecorded.
Answer: B. The second Toffoli cancels the first computed AND value.
-
Resetting one half of Φ+ locally restores |+⟩ on the other half: A. No, the discarded coherence is not recovered that way. B. If the target label is zero. C. Always in the ideal model. D. Whenever the reset is deterministic.
Answer: A. The untouched subsystem remains maximally mixed after the record is discarded.
-
Computing ab, applying Z to its workspace, and uncomputing produces: A. No possible observable effect. B. A classical list of all AND values. C. A data phase of −1 on the ab=1 branch. D. A universal clone.
Answer: C. The temporary record disappears while the desired phase pattern remains.
Pitfalls and summary
Preserve the information required by the inverse, reverse both order and individual gates, and distinguish temporary garbage from intended output correlations. Compute, copy, uncompute gives a clean reversible interface; measurement or reset is not an automatic substitute for coherent cleanup.
Primary sources and further study
These sources support the definitions and context. The numerical examples, exercises, and explanations here were prepared for this lesson. Research papers are optional advanced reading.