Toffoli Gate
The Toffoli gate is the hinge on which the theory of reversible logic turns, and it wears two identities at once. Classically it is the reversible AND: Tommaso Toffoli introduced it in 1980 to answer a foundational worry — that ordinary gates like AND and OR erase information and therefore, by Landauer's principle, must dissipate energy. No one- or two-bit gate is universal for reversible classical computation, but this single three-bit gate is, mapping while remaining its own inverse. Quantum-mechanically it is the doubly-controlled-NOT: the target undergoes a Pauli exactly when both controls are . Because it only permutes basis states it is a real permutation matrix — a classical gate promoted, unchanged, into the quantum circuit model, which is why it bridges both worlds. Two universality facts make it central. First, the Toffoli plus constant ancillas simulates any Boolean circuit, since it yields NAND, NOT, and FANOUT by fixing its inputs. Second, and more surprising, the Toffoli together with the Hadamard gate is universal for quantum computation even though every one of its matrix entries is real — a result of Shi and of Aharonov. Practically it is the workhorse of quantum arithmetic: ripple-carry adders, modular exponentiation in Shor's algorithm, Grover oracles, and syndrome extraction are all counted in Toffolis. Because hardware offers only one- and two-qubit gates, the recurring theme is compiling the Toffoli cheaply — six CNOTs by Barenco et al., or seven gates in the fault-tolerant setting where that count sets the bill.
- The Toffoli gate (controlled-controlled-NOT, CCNOT) is a three-qubit gate that flips a target qubit if and only if both controls are , acting as and leaving the other seven basis states fixed.
- It is universal for reversible classical computation: fixing its inputs yields NAND, NOT, and FANOUT, so any Boolean function is expressible in Toffolis plus ancillas.
- Its matrix is the identity except that it swaps ; it is Hermitian, self-inverse (), and has determinant .
- Combined with Hadamard it is universal for quantum computation, and conjugating its target by turns it into the controlled-controlled- gate that phase oracles need.
- Compiling it costs six CNOTs plus single-qubit gates with no ancilla (Barenco); fault-tolerantly it costs seven non-Clifford gates, which is why Toffoli count is the headline resource metric.
- Key idea: the Toffoli gate flips its target iff both controls are , — self-inverse, universal for reversible classical logic, quantum-universal once paired with Hadamard, and the elementary building block of all quantum arithmetic.
The Toffoli gate flips its target iff both controls are , ; it is self-inverse, universal for reversible classical computation, and — together with Hadamard — universal for quantum computation, while being the elementary building block of all quantum arithmetic.
Where this lives today
The Toffoli is no longer only a theorist's gate — it is a benchmark and a budget line. It has been demonstrated on hardware: Monz and collaborators realized a three-qubit Toffoli in a trapped-ion register in 2009, and superconducting processors implement it by compilation or via engineered higher-level transitions. In the fault-tolerant era it is the unit of account: because Clifford operations are cheap under the surface code but each gate demands a distilled magic state, algorithm cost is quoted in Toffolis or gates. Gidney and Ekerå's 2021 estimate for factoring a 2048-bit RSA key — famously "8 hours on 20 million noisy qubits" — is dominated by roughly Toffoli gates in the modular-exponentiation core. The same accounting drives active research into cheaper Toffolis: measurement-assisted "temporary AND" constructions spending only four gates, catalytic schemes, and layout-aware synthesis under lattice surgery. Every improvement to the humble CCNOT flows straight into the runtime of Shor's algorithm, Grover search, and quantum chemistry — which is why "reduce the Toffoli count" is one of the most consequential optimization targets in the field.
The physical picture
Think of the Toffoli as an AND gate rebuilt so that nothing is thrown away. A classical AND takes two bits to one and loses information; the Toffoli keeps the two inputs as controls and writes their AND into a fresh third wire prepared in , giving . Because both inputs survive, the map is a bijection — run the gate again and it undoes itself. On the eight basis states it does nothing unless both controls are set: seven states are fixed, and only the pair and are exchanged, so the unitary is the identity with a single Pauli- block in the corner. The first figure draws the gate and makes that exchange the whole story.
Universality arrives in two flavours. Classically, by choosing the inputs you turn the one gate into a whole logic family: both controls 1 gives NOT; gives AND; one control 1 copies the other (FANOUT); and gives NAND, the single gate from which all Boolean logic is built. Quantum-mechanically the same permutation, sandwiched between Hadamards on the target, converts the conditional bit-flip into a conditional phase — linking the Toffoli to the controlled-controlled- and the phase oracles of Grover search. The one thing it cannot do alone is create a superposition, since its matrix is real; that is Hadamard's job. The second figure is what an engineer reaches for: the compiled circuit and the gate count that governs its cost.
The core equations
The gate is defined by its action on a basis state: it adds the AND of the two controls, modulo 2, onto the target and leaves the controls untouched.
where: — control bit values, ; — target bit value; — addition modulo 2 (XOR); — logical AND of the controls (ordinary product of bits). Reading: the target flips () precisely when , i.e. when both controls are set, and is otherwise left alone. Ordering the basis by the binary index from (0) to (7), equation (1) becomes an explicit permutation matrix — the identity everywhere except a Pauli- block on the last two states.
where: — the Toffoli unitary. Reading: rows 0–5 carry a diagonal because there ; the lower-right block swaps index 6 () with index 7 (). The same operator, written with projectors, exposes the "controlled-controlled" logic directly: apply to the target only in the sector where both controls project onto .
where: — projectors onto the control states; — identity; — Pauli on the target. Reading: the nested structure reads "if control 1 is set, then if control 2 is set, flip the target" — the definition of a doubly-controlled gate. Three algebraic facts follow immediately and are worth memorizing — the sanity checks a compiler applies to any candidate Toffoli.
where: — the gate composed with itself; — Hermitian conjugate; — determinant. Reading: makes the gate its own inverse; being a real symmetric permutation it is Hermitian; and swapping exactly one pair of states is a single transposition, whose sign is . Conjugating the target — and only the target — by Hadamard gates converts the conditional bit-flip into a conditional sign, turning the Toffoli into the controlled-controlled- that phase oracles use.
where: — Hadamard on the target; — controlled-controlled-, which multiplies by . Reading: it uses only ; because CCZ is diagonal and symmetric in its three qubits, "control" and "target" become interchangeable here. Finally, since hardware supplies only one- and two-qubit gates, the Toffoli must be compiled — the standard exact cost being the Barenco construction.
where: — the doubly-controlled , i.e. the Toffoli; — the phase gate and its inverse; — Hadamard. Reading: six CNOTs is optimal for an exact ancilla-free Toffoli, and the seven / gates are the fault-tolerantly expensive non-Clifford part.
The goal is to prove that this one three-bit gate, with ancilla wires held at fixed constants, can compute any Boolean function whatsoever — the statement that makes it the classical analogue of NAND. A set of reversible gates is classically universal if, allowing ancillas initialized to constants and uncomputing garbage at the end, it realizes every function on bit strings. Since NAND is already functionally complete, it is enough to show the Toffoli produces NAND plus a way to copy bits, then to restore reversibility by uncomputation.
Step 1. Set the target constant to . Then , and is exactly . Why: NAND is functionally complete on its own, so getting it from a fixed input choice already guarantees every Boolean function is reachable.
Step 2. Set both controls to : , the NOT gate on the target. Why: reversible logic must be able to negate a wire, and here it costs nothing but a choice of ancilla values.
Step 3. Set one control to and the target to : , copying onto a second wire. Why: reversible circuits have no free one-bit FANOUT, so this controlled copy is what lets a signal branch to two later gates.
Step 4. Assemble any Boolean circuit from these NAND and FANOUT primitives, tracking the "garbage" bits reversibility forces you to keep; then run a mirror-image copy to uncompute the garbage back to constants, keeping only the answer — Bennett's trick. Why: without uncomputation the accumulated garbage would block ancilla reuse; Bennett's construction shows the reversibility overhead is only a constant factor.
Go deeper · Derivation B · 1 minThe Hadamard sandwich and the controlled-controlled-Z gate
The goal is to show that conjugating the Toffoli's target wire by Hadamard gates produces the controlled-controlled- gate, the identity behind every phase oracle in Grover search and amplitude amplification. Phase oracles mark an answer not by flipping a bit but by attaching a sign, so oracle construction computes a predicate into an ancilla, converts that bit into a sign, and uncomputes. The bit-to-sign conversion is just the single-qubit identity promoted to the controlled setting, which this derivation makes explicit.
Step 1. Write the Toffoli in projector form, , with , on the two controls, so that the target sees only in the control sector. Why: isolating the target factor is what lets us conjugate it without disturbing the control structure.
Step 2. Sandwich the target by . Where the Toffoli applied , leaves it unchanged; where it applied , we get . Why: conjugation acts factor-by-factor on a tensor product, so only the target operator is transformed and the control projectors are untouched.
Step 3. Substitute the fundamental identity
so the sector now applies to the target instead of . Why: this is the exact point at which a conditional bit-flip becomes a conditional phase — the mechanism a phase oracle exploits.
Step 4. Collect: the operator applies only when both controls are , i.e. it multiplies by and fixes the other seven states — exactly . Why: CCZ is diagonal and totally symmetric in its three qubits, so the "control" versus "target" distinction dissolves and it serves as a symmetric three-qubit phase. *The result is confirmed by direct computation: has diagonal and no off-diagonal entries.*
Go deeper · Derivation C · 1 minCounting the Barenco decomposition with a controlled square-root of NOT
The goal is to show why an exact ancilla-free Toffoli reduces to two-qubit gates with a count of six CNOTs, using the controlled- trick at the heart of the Barenco et al. synthesis. Real hardware executes only one- and two-qubit operations, so a three-qubit gate must be compiled, and the elegant route uses the fact that the Pauli has a unitary square root , with and . The construction interleaves controlled-, controlled-, and CNOTs so the net target operation is exactly when both controls are set and the identity otherwise.
Step 1. Assemble the standard sequence
where applies to the target controlled on qubit 1, and controlled on qubit 2. Why: this five-piece pattern is the minimal controlled-root arrangement distinguishing "both controls set" from every other case.
Step 2. Take both controls set, . The two CNOTs leave the control parity so the surviving target factors compose to — the desired flip. Why: is exactly "two half-flips make a full flip", which is why a square root of is the right building block.
Step 3. Take either control unset. Then a and a always meet on the target and cancel, , leaving it untouched. Why: the unitarity of guarantees the cancellation, so the gate does nothing unless both controls are set.
Step 4. Count. Each controlled- or controlled- expands into two CNOTs plus single-qubit rotations; simplifying yields the Barenco circuit of six CNOTs, which in the fault-tolerant -basis is the seven- circuit of the second figure. Without ancillas, six CNOTs is optimal. Why: the count is what gets billed on hardware, and its -part is what a fault-tolerant estimate pays for. *The two key identities and can be confirmed by direct matrix multiplication.*
Limiting cases
The Toffoli collapses to a gallery of simpler gates the moment you pin down one input — which is precisely why it is universal.
| Case | Result | Meaning |
|---|---|---|
| Both controls set | With the target undergoes a NOT; fixing both controls to realizes a single-qubit . | |
| Target initialized to zero | With the gate writes the logical AND of the controls into the target — the reversible AND. | |
| One control set | Fixing reduces the Toffoli to a CNOT between the remaining control and the target. | |
| Either control zero | If at least one control is the gate is the identity on all three qubits. | |
| Target set to one | With the target carries — the functionally complete classical gate. |
Worked examples
1 · The Toffoli generates entanglement (fully worked)
Apply the Toffoli to the product input , where . Find the output state and the probability that a final measurement of all three qubits returns .
Step 1. Expand the input. , so tensoring the target gives
Step 2. Act with the Toffoli term by term. Only has both controls set, so only it changes, mapping to ; the other three are fixed:
Step 3. Normalization check: four terms of amplitude give , so each basis outcome has probability and P(111) = 1/4.
Step 4. Note that does not factor into three single-qubit states — the target is now correlated with "both controls are 1", which no product state can reproduce. From an unentangled input, the Toffoli has produced a genuinely entangled three-qubit state. This is the crux of quantum universality: Hadamards make the superpositions and the Toffoli entangles them, so "Hadamard + Toffoli" reaches beyond classical reversible logic.
2 · The T-count of an 8-bit adder (fully worked resource estimate)
A ripple-carry (Cuccaro-style) adder for two -bit integers computes each carry bit as an AND with a Toffoli and then uncomputes it, using about Toffolis plus a linear number of CNOTs. Estimate the fault-tolerant -count of adding two 8-bit numbers.
Step 1. Count Toffolis. With , the adder computes carry bits and later uncomputes them, using about Toffoli gates. The accompanying CNOTs and NOTs are Clifford and essentially free in the fault-tolerant cost model.
Step 2. Convert each Toffoli to its non-Clifford cost. The exact Toffoli of equation (6) uses 7 / gates, and every gate must be supplied by magic-state distillation — the dominant expense of a surface-code machine.
Step 3. Multiply. The -count is gates — 112 distilled magic states for one 8-bit addition. The 4- "temporary AND" trick would cut this to about .
Step 4. Interpret. This is why serious resource estimates — from Grover oracles to Shor's factoring — are quoted as a Toffoli count or -count rather than a raw gate count: the Cliffords are cheap and the Toffolis are the bill. 16 Toffolis → 112 T gates for one 8-bit add; the Toffoli count is the number to minimize.
3 · Building a Fredkin (controlled-SWAP) from one Toffoli
The controlled-SWAP gate exchanges qubits 2 and 3 when qubit 1 is . Verify the identity on the input , where has control qubit 3 and target qubit 2.
Step 1. First : qubit 3 is , so qubit 2 flips: .
Step 2. (controls 1,2, target 3): both controls are , so qubit 3 flips: .
Step 3. Second : qubit 3 is now , so qubit 2 is unchanged, . The net has swapped qubits 2 and 3, as control demands.
Step 4. Control-off check : the outer CNOTs flip and unflip qubit 2 while the Toffoli does nothing (control 1 is ), returning — no swap. CNOT–Toffoli–CNOT is a Fredkin.
Physics of the portrait
The Toffoli sits where thermodynamics meets computation: it was invented not to build a faster computer but to ask whether computing must cost energy at all.
Origins · 1 minTommaso Toffoli and how this was discovered
Tommaso Toffoli was born in Montecatini Terme, Italy, in 1943, and earned his PhD at the University of Michigan in 1976. At the MIT Laboratory for Computer Science he worked closely with Edward Fredkin on the then-radical program of reversible and conservative computing — the idea that computation, done carefully, need not erase information and so need not pay Landauer's thermodynamic tax. His 1980 report "Reversible Computing" introduced the three-bit controlled-controlled-NOT gate that now bears his name and proved it universal for reversible classical logic; the companion Fredkin gate came from the same collaboration and its billiard-ball model of computation. His later work ranged over cellular automata and lattice-gas models of physics, always pressing the theme that physical law and computation are two views of one thing. When quantum computing arrived his gate was waiting: the CCNOT became the indispensable non-Clifford workhorse of quantum arithmetic and — with Hadamard — a universal set for quantum computation. A concept devised to save energy in classical circuits became a cornerstone of the quantum ones.
Going further
Texts. Nielsen & Chuang, Quantum Computation and Quantum Information, §1.4.1 (reversible logic) and §4.3 (universal gates, Barenco construction, Fig. 4.9) · Barenco et al., "Elementary gates for quantum computation", Phys. Rev. A 52, 3457 (1995) · Toffoli, "Reversible Computing", MIT/LCS/TM-151 (1980); ICALP 1980 · Preskill, Lecture Notes for Physics 219, Ch. 6 · Kitaev, Shen & Vyalyi, Classical and Quantum Computation, §8 · Shi, "Both Toffoli and Controlled-NOT need little help to do universal quantum computation", QIC 3, 84 (2003). Every matrix entry follows from evaluating on the eight basis states, from which one can confirm , , , and . The resource counts — 7 gates per Toffoli and Toffolis for an -bit adder — are standard fault-tolerant figures; the 2048-bit factoring estimate is from Gidney & Ekerå (2021).
Prerequisites. CNOT Gate · Controlled-U Gates · Pauli Gates (X, Y, Z) · Phase Gates (S and T) · Quantum Circuit Model. Leads to. Circuit Identities and Equivalences · Controlled-Z Gate · SWAP Gate.
Ask the tutor: Ask me to walk through the seven- Toffoli circuit gate by gate, or to show how a Grover oracle computes a predicate into an ancilla with Toffolis and converts it to a phase.
Why can no one- or two-bit gate be universal for reversible classical computation, but a three-bit gate can?
The obstruction is a conservation law. Every reversible one- or two-bit gate is linear (affine) over the two-element field, and composing linear gates only ever gives linear functions — so you can never build a nonlinear function like AND, essential to general computation. The Toffoli's output bit contains the product , a genuinely nonlinear term, and three bits is the minimum width at which a reversible gate can carry that nonlinearity while staying a bijection. That single nonlinear term is why one gate suffices.
What is the difference between the Toffoli and the Fredkin gate?
Both are three-bit reversible gates from the same MIT program, but do different jobs. The Toffoli (CCNOT) uses two controls to decide whether to flip a target; the Fredkin (CSWAP) uses one control to decide whether to swap two bits. The Fredkin is "conservative" — it never changes the number of 1s — which the Toffoli is not. Each builds from the other with a couple of CNOTs (Worked Example 3 makes a Fredkin from one Toffoli), and both are universal for reversible classical computation. In quantum computing the Toffoli is the more common primitive, since so much of quantum arithmetic is controlled bit-flips.
What does it mean that the Toffoli is a "non-Clifford" gate, and why does that matter so much?
The Clifford group (generated by , , and CNOT) maps Pauli operators to Pauli operators; by the Gottesman–Knill theorem a circuit of only Clifford gates is efficiently simulable classically, so Cliffords alone give no quantum advantage. The Toffoli lies outside this group, which is exactly what lets Clifford + Toffoli circuits do what classical machines cannot. The catch is cost: fault-tolerantly, Clifford gates are cheap but non-Clifford gates like and Toffoli must be injected through magic-state distillation. So "non-Clifford" is at once the source of the Toffoli's power and the reason it dominates the resource budget.
Does a Toffoli really need seven T gates, or can I do better?
Seven is the count for the standard exact, ancilla-free Toffoli (Nielsen–Chuang, Fig. 4.9). You can do better by relaxing constraints. With one clean ancilla and a measurement, the "temporary AND" construction realizes a Toffoli for only four gates when it will later be uncomputed — common in arithmetic, where carries are computed and cleaned up. Catalytic techniques and magic-state cultivation push the amortized cost lower still. But for a genuine standalone Toffoli whose output you keep, with no ancilla, seven gates is the reference figure — and minimizing the number of Toffolis usually matters more than shaving the -cost of each.
When is it safe to use the cheaper three-CNOT Margolus gate?
The Margolus (relative-phase) Toffoli reproduces the correct bit-flip truth table but attaches an extra phase to one basis state, so it is not the Toffoli as a unitary. It is safe precisely when that phase is guaranteed to cancel. The classic pattern is compute-then-uncompute: compute a value into an ancilla with a Margolus gate, do some Clifford work, then erase it with the inverse — the stray phase appears twice and squares to . Keep the Margolus output as a lasting part of the register, though, and the extra phase corrupts the computation.
Why the specific pairing "Hadamard plus Toffoli" for quantum universality?
Each gate contributes what the other lacks. The Toffoli is a real permutation — it entangles and computes nonlinear Boolean functions but can never build a superposition. The Hadamard builds superpositions and lets the real-amplitude dynamics encode the missing structure. Shi (2003) and Aharonov (2003) proved this set is computationally universal: it simulates any quantum circuit to arbitrary accuracy even though every reachable state has real amplitudes (the imaginary parts ride an extra encoding qubit). It is one of the smallest and most surprising universal sets, dramatizing that quantum speedup needs no explicit complex numbers — only the interference that Hadamard's superpositions and Toffoli's entanglement together provide.