Skip to content
Current section
The Quantum Computing Enthusiast’s Hub
Saved
All sections

All sections

Saved
Skip Learn navigation
Encyclopedia
Topic

Toffoli gate and reversible Boolean computation

A three-qubit controlled-controlled-X gate that supplies nonlinear reversible Boolean logic.

Also called CCX, controlled-controlled-NOT

Edition released Coverage: Overview · Technical
On this page

Definition and significance

The Toffoli gate flips a target when two controls are both 1. Its computational action is (a,b,c)->(a,b,c XOR ab). It is reversible even though the Boolean AND function alone is not. Reversible embeddings make classical predicates usable inside quantum algorithms without destroying coherence.

Mechanism and mathematical treatment

Because ab XOR ab=0, applying Toffoli twice restores the input. With c=0 its target records ab while the controls retain a and b. With a=b=1 it acts as X on the target; with either control 0 it acts as identity. A sequence of Toffoli and NOT gates can implement reversible classical computations, but these gates alone do not provide arbitrary quantum phases.

Clean ancillas start in known states and should usually be restored. A reversible subroutine U_f:|x>|0>->|x>|f(x)> can compute a predicate, apply a phase conditioned on its output, and uncompute. The combined operation on the input becomes |x>->(-1)^f(x)|x>, while the work register returns to |0>.

Fully worked reference example

Prepare the two controls in |++> and the target in |0>. Toffoli gives (|000>+|010>+|100>+|111>)/2 in control-control-target order. Apply Z to the target. Only |111> changes sign. Apply Toffoli again. The target returns to |0>, and the controls are (|00>+|01>+|10>-|11>)/2. The result is exactly a CZ on the two controls, obtained through computing AND, phase marking, and uncomputing.

If the final Toffoli were omitted, the target would remain entangled with the input. Discarding it would erase coherence between the marked and unmarked sectors, changing the subsequent algorithm.

Limitations and common misconceptions

A Toffoli gate in a high-level circuit is not necessarily a native three-body interaction. Its Clifford+T cost depends on ancilla availability, exact versus relative-phase behavior, measurement and feed-forward. Relative-phase Toffoli variants can be useful, but substituting them without checking the surrounding circuit can introduce unwanted phases.

Scientific relationships

Toffoli supplies reversible nonlinear Boolean operations used in arithmetic.

Controlled-X is a basic component in decompositions of reversible multi-control logic.

A reversible full-adder constructed from Boolean identities

For input bits a,b and incoming carry c, the sum bit is s=a XOR b XOR c and the outgoing carry is t=ab XOR ac XOR bc. The carry expression equals the majority function: it is one when at least two inputs are one. Introduce clean output qubits s and t, retaining all three input qubits. Three CNOTs from a,b,c into s compute the sum. Three Toffoli gates with control pairs (a,b), (a,c), (b,c) and common target t compute the carry. Because the target is updated by XOR, this is a reversible embedding of the full-adder relation.

Check the four possible input Hamming weights. Weight zero gives s=0,t=0. Weight one gives s=1,t=0. Weight two gives s=0,t=1. Weight three gives s=1,t=1, since the three pairwise products XOR to one. In every case the integer identity a+b+c=s+2t holds. This is a complete proof for all eight inputs, compressed by symmetry rather than by omitting cases.

On a superposition of inputs, the same circuit retains coherent correlations with both outputs. For the uniform three-bit input, the joint output is a superposition of eight distinct five-bit labels, each with amplitude 1/sqrt(8). Measuring the two output bits yields probabilities 1/8,3/8,3/8,1/8 for (s,t)=(0,0),(1,0),(0,1),(1,1), respectively. Those multiplicities come from the number of input strings of each weight.

In-place arithmetic requires a different workspace contract

The simple full-adder above preserves inputs and appends two outputs. It should not be advertised as an optimized in-place ripple-carry adder. To use it as part of a larger coherent algorithm, specify which inputs will remain, which outputs will be needed, and which temporary carries must return to zero. Copying a computational carry into another clean register with CNOT is permitted, but the original carry can be uncomputed only if the inputs required to reconstruct it are still available.

For a two-bit addition example, let a=a0+2a1 and b=b0+2b1, with a0 and b0 the least significant arithmetic bits even if they occupy later tensor positions. The low-bit result is s0=a0 XOR b0, with carry c1=a0b0. The high bit is s1=a1 XOR b1 XOR c1, and the final carry is c2=a1b1 XOR a1c1 XOR b1c1. For a=3 and b=1, the low step gives s0=0,c1=1 and the high step gives s1=0,c2=1, so the three-bit result is 100, namely four.

The distinction between arithmetic significance and tensor-factor position is deliberate. A document can use q0 as the leftmost tensor factor while placing the least significant arithmetic bit on a different named wire. A reversible arithmetic operation requires both mappings to specify how its input and output registers are related. Otherwise a correct Boolean network may be read as the wrong integer transformation.

A dirty-workspace identity and its limits

Let d be an ancillary bit whose value is unknown. A Toffoli controlled by a,b transforms d into d XOR ab. That does not leave a clean record of ab. If d is used as the control of a phase Z on another register, the unknown contribution from d can introduce an unwanted phase. A valid dirty-ancilla construction must cancel the dependence on d while restoring d exactly, including its possible entanglement with external systems.

A simple cancellation identity illustrates the requirement. Apply Toffoli(a,b;d), then CNOT(d;t), then Toffoli(a,b;d), then CNOT(d;t). The first CNOT updates t by d XOR ab; after the second Toffoli, d is restored; the last CNOT updates t by d again. The two d terms cancel, leaving t XOR ab. Thus the four-gate sequence implements a Toffoli from a,b to t while restoring d, though it is not an efficient replacement for a directly available Toffoli.

Because the net basis map is independent of d and restores it, the identity holds for a superposed or externally entangled dirty ancilla. This stronger condition distinguishes genuine workspace restoration from a check performed only on d=0. Resource optimization can exploit dirty ancillas, but each substitution requires its own exact phase analysis when relative-phase gates replace exact Toffolis.

For these examples, a classical Boolean truth-table evaluation is a complete and inexpensive reference for the permutation. It does not certify phases of a quantum decomposition. A decomposition using H, T and CNOT must additionally be checked as a unitary, or by a symbolic phase representation, because an incorrect relative phase can preserve every classical truth-table output.

Sources

  • A. Barenco et al. · · Research paper · Publication status unspecified · arXiv · DOI 10.1103/PhysRevA.52.3457
    Verification scope

    Selected source text checked ·

    Source anchors gate-decomposition conventions. New worked instances are independent mathematical constructions, not reported experiments.