Course Home

Reversible Computation
& Garbage Removal

How to implement any classical function as a quantum unitary — understanding information loss, reversible embeddings, garbage bits, and Bennett's uncomputation trick.

1The Reversibility Requirement

Quantum mechanics demands that every computation be undoable

In quantum computing, every operation on qubits is described by a unitary matrix U. Unitaries have a defining property: they are always invertible. Given any output, you can always recover the input by applying U.

U U† = U† U = I

This means every quantum gate is reversible — no information is ever lost. But most classical logic gates (AND, OR, NAND) are irreversible: their outputs have fewer bits than their inputs, so information is destroyed.

Core Challenge

To run a classical function f on a quantum computer, we must first find a reversible way to compute f, then implement that reversible circuit as a unitary. This page shows you exactly how.

2Information Loss in Classical Gates

Seeing why standard logic gates cannot be quantum gates

A gate is reversible if you can always determine the input from the output — the input–output mapping is a bijection. If two different inputs produce the same output, information has been lost and the gate is irreversible.

Landauer's Principle

Information erasure has a physical cost. Rolf Landauer showed in 1961 that erasing one bit of information must dissipate at least kT ln 2 of energy as heat (about 3×10−21 J at room temperature). Irreversible gates destroy information, so they necessarily generate heat. Reversible gates preserve all information and can, in principle, compute with zero energy dissipation. This isn't just a quantum curiosity — it's a fundamental law of physics.

Use the explorer below to see the input→output mapping for different gates. When multiple inputs collapse to the same output (red arrows), that gate is irreversible.

Gate Reversibility Explorer

3Reversible Gates

A universal set for reversible classical computation

To avoid information loss, reversible gates keep the number of output bits equal to the number of input bits. The key reversible gates are:

NOT Gate

|x⟩ → |x ⊕ 1⟩

Flips a single bit. Trivially reversible (it's its own inverse).

CNOT Gate 2-bit

|a, b⟩ → |a, b ⊕ a⟩

Controlled-NOT. Flips the target b when the control a is 1. Computes XOR while preserving the control.

Toffoli Gate 3-bit

|a, b, c⟩ → |a, b, c ⊕ (a ∧ b)⟩

Controlled-controlled-NOT. Flips c only when both a and b are 1. Computes AND reversibly.

Fredkin Gate 3-bit

|a, b, c⟩ → |a, a?c:b, a?b:c⟩

Controlled-SWAP. Swaps b and c when the control a is 1. Also universal for reversible computation.

Universality

Both the Toffoli and Fredkin gates are independently universal for reversible classical computation: any Boolean function can be computed using either one (plus ancilla bits initialized to 0 or 1). Since both gates are unitary, this bridges the gap between classical and quantum computation. In practice, quantum circuits most commonly use the Toffoli decomposition.

4The Standard Reversible Embedding

The canonical way to make any function f reversible

Given any classical function f : {0,1}n → {0,1}m, the standard trick is to keep the input and XOR the result into an ancilla register:

Uf : |x⟩|y⟩ → |x⟩|y ⊕ f(x)⟩

This is reversible because Uf is its own inverse — applying it twice gives back the original state:

|x⟩|y⟩ ⟶Uf |x⟩|y ⊕ f(x)⟩ ⟶Uf |x⟩|y ⊕ f(x) ⊕ f(x)⟩ = |x⟩|y⟩

When we set y = 0, the output register stores f(x) directly: |x⟩|0⟩ → |x⟩|f(x)⟩.

The Phase Kickback Trick

Something remarkable happens when we set the ancilla to |−⟩ = (|0⟩ − |1⟩) / √2 instead of |0⟩. For a single-bit function f:

Uf |x⟩|−⟩ = (−1)f(x) |x⟩|−⟩

The ancilla stays unchanged in |−⟩, but the input register picks up a phase factor (−1)f(x). This is called phase kickback — the function's value gets "kicked" from the output register into the phase of the input. The ancilla serves as a catalyst.

Why This Matters

Phase kickback converts function values into quantum phases, which can then be manipulated by interference. This is the engine behind nearly every quantum algorithm: Deutsch's algorithm, Bernstein-Vazirani, Simon's algorithm, Grover's search, and Shor's factoring all rely on applying Uf with the ancilla in |−⟩ to encode f into phases.

Reversible Embedding Uf
Uf

5The Garbage Problem

Why intermediate computation ruins quantum interference

The standard embedding Uf looks clean in theory, but implementing it for a complex function requires intermediate computations — temporary ancilla bits used to store partial results. These leftover bits are called garbage.

A Concrete Example

Consider computing f(a,b,c) = (a ∧ b) ⊕ (b ∧ c). We need two AND operations, each requiring a Toffoli gate with an ancilla:

|a, b, c, 0, 0, 0⟩ ⟶Toffoli(a,b) |a, b, c, a∧b, 0, 0⟩
Toffoli(b,c) |a, b, c, a∧b, b∧c, 0⟩ ⟶CNOTs |a, b, c, a∧b, b∧c, f

The result f is correct, but the registers holding a∧b and b∧c are garbage — they were needed for the computation but aren't part of the answer.

Why Garbage Is Fatal for Quantum Algorithms

Imagine using this circuit in superposition. We want the state on the left, but get the state on the right:

Desired (no garbage)
Σx αx |x⟩|f(x)

Different branches can interfere — the output register only depends on f(x), and two inputs with the same f(x) combine coherently.

Actual (with garbage)
Σx αx |x⟩|f(x)⟩|g(x)

Garbage g(x) differs across branches. Even if f(x₁) = f(x₂), the garbage "tags" each branch uniquely — no interference possible. Tracing out garbage yields a mixed state.

The Fundamental Problem

Garbage bits become entangled with the computation. They act as a "which-path" marker, effectively decohering the quantum state. This destroys the speedup that relies on interference between branches of the superposition.

Interactive: Garbage Kills Interference

This demo shows a 2-input function where f(0) = f(1) (constant). Without garbage, the two branches combine coherently. With garbage, each branch is "tagged" differently and interference is destroyed.

Interference vs. Garbage
step through

6Bennett's Trick (Uncomputation)

Compute → Copy → Uncompute: the elegant solution to garbage

Charles Bennett (1973) discovered a beautiful solution: after computing the result, copy it to a clean output register, then reverse the entire computation to erase all garbage. Since the circuit is reversible, running it backwards is always possible.

The Three Phases

1Compute — Run the circuit forward. The result appears, but garbage accumulates in ancilla registers.

2Copy — Use CNOTs to copy the result into a fresh output register. This is the only step that writes to the final output.

3Uncompute — Run the circuit backwards (apply each gate in reverse order). All garbage bits return to 0.

|x, 0work, 0out⟩ ⟶compute |x, g(x), 0out⟩ ⟶copy |x, g(x), f(x)⟩ ⟶uncompute |x, 0work, f(x)

Step-by-Step Interactive

Walk through Bennett's trick step by step. Choose a function, toggle the input values, and step through each gate to see garbage appear during the forward computation and then vanish during uncomputation.

Bennett's Trick Stepper

Inputs:
1
a
1
b
0
c
=
Speed
Garbage bits (non-zero ancillae): 0
Step 0 / 9
step   Space auto-play   R reset

Why It Works

The key insight is that copying a classical bit value with CNOT does not create garbage — it just fans out the result. And because the entire forward computation is reversible, we can always run it backwards to clean up. The final state has:

Trade-off

Bennett's trick trades time for space cleanliness: we run the computation forward, then backward — roughly doubling the gate count. But we get a clean reversible implementation with no garbage, which is essential for using Uf inside quantum algorithms where interference must be preserved.

7Summary

The complete recipe for implementing classical functions as quantum unitaries

Step 1: Reversible Circuit

Express f using reversible gates (Toffoli + CNOT + NOT), adding ancilla qubits as needed for intermediate values.

Step 2: Identify Garbage

Ancilla registers that are non-zero after the forward computation are garbage — they carry information about the input.

Step 3: Bennett's Trick

Copy the result to a clean output register, then run the computation backwards to erase all garbage.

Step 4: Use as Unitary

The resulting clean circuit implements Uf: |x⟩|0⟩ → |x⟩|f(x)⟩ with no extra entanglement. It can now be used inside any quantum algorithm.

Resource Costs

If the original irreversible circuit for f uses T gates and S ancilla bits:

  • Gate count: ≈ 2T + m (forward + backward + copy CNOTs)
  • Ancilla qubits: S work qubits (all returned to |0⟩) + m output qubits
  • Depth: roughly 2× the forward computation depth

More advanced techniques (pebble games, incremental uncomputation) can reduce the overhead, but Bennett's basic trick captures the essential idea.

Looking Ahead

The Uf embedding and phase kickback are the engine behind nearly every quantum algorithm in this course:

  • Deutsch's Algorithm — uses Uf|x⟩|−⟩ to distinguish constant from balanced functions with one query
  • Simon's Algorithm — uses a clean Uf in superposition to find hidden structure through interference
  • Grover's Search — repeatedly applies a phase oracle (Uf with |−⟩) to amplify the marked item
  • Shor's Factoring — embeds modular exponentiation as a reversible circuit, then uses QFT to extract the period

In every case, the function must be implemented as a clean reversible circuit — garbage would destroy the interference these algorithms rely on.

8Test Your Understanding

Check your grasp of reversible computation and garbage removal