← Back to section · Quantum Computing
Toffoli, Fredkin, and SWAP: Three-Qubit Gates and the Exchange Gate
Why the Toffoli gate is universal for classical reversible computation, why its exact decomposition needs at least six CNOTs, how relative-phase variants halve that cost, and the Fredkin gate, the three-CNOT decomposition of SWAP, and SWAP networks in hardware routing.
One step above the two-qubit controlled gates stand the three-qubit gates. They look like a modest increment — one more control qubit — but their status is entirely different: three-qubit gates are the universal bricks of classical reversible computation, the basic components of oracles and arithmetic circuits in quantum algorithms, and the first overhead item scrutinized in any cost ledger. This article covers three representatives: Toffoli, Fredkin, and SWAP.
The Toffoli gate: universal for classical reversible computation
The Toffoli gate (CCNOT) has two control qubits and one target: it flips the target only when both controls are 1. It is its own inverse, and its truth table is the direct generalization of classical logic. Its status rests on a classical result: the Toffoli gate alone is universal for classical reversible computation. The reason is concrete: preset the target to 1, and the Toffoli’s output is the NAND of the two controls — and NAND is universal for classical logic, so any classical circuit can be rewritten reversibly as a Toffoli network. A quantum computer therefore contains a complete classical computer inside it, and the large “classical logic segments” inside Grover oracles and quantum arithmetic (adders, multipliers) are Toffoli networks. The multi-controlled generalization (a NOT with controls) decomposes, given clean ancillas, into a linear number of Toffoli gates, which sets the basic scale for costing large oracles.
Decomposing the Toffoli: the six-CNOT lower bound
Almost no hardware offers a native Toffoli, so it must be decomposed into single-qubit gates and CNOTs. The classic standard decomposition uses , , , and 6 CNOTs — and this is not a failure of ingenuity: Shende and Markov proved that 6 CNOTs is a lower bound for an exact decomposition without ancillas. The cost ledger is therefore hard: every Toffoli is worth at least 6 two-qubit gates, and in a fault-tolerant context it must further be multiplied by the synthesis cost of gates. If one is willing to relax the requirement, a relative-phase Toffoli (Margolus gate) achieves the job with only 3 CNOTs, at the price of attaching a phase to individual computational basis states. It differs from a true Toffoli only in phase, so it can safely be substituted in two situations: when gates come in pairs and the phases cancel automatically in a compute–uncompute structure, and when the gate serves purely classical logic whose phase does not affect measurement statistics. Compilers use such variants extensively in oracle synthesis, halving the Toffoli cost outright. Another cost-reduction route borrows an ancilla together with measurement and feedforward to press the count of a Toffoli down to 4 in a fault-tolerant framework — but that belongs to the specialized topic of fault-tolerant synthesis.
What a Toffoli is worth inside algorithms
The Toffoli is not only a logic brick; it is also the unit of account in resource estimation. The cost of Shor’s algorithm comes almost entirely from the quantum arithmetic inside modular exponentiation, and the body of quantum adders and multipliers is a Toffoli network — early resource estimates denominated the scale of fault-tolerant machine needed to factor a 2048-bit integer directly in Toffoli counts (and the corresponding counts). The same holds for Grover oracles: reversibly computing the decision function and uncomputing it again is, in both directions, a Toffoli-dominated stretch of classical logic that must be paid in full at every iteration of the search. A ripple-carry adder is the plainest one-dimensional chain of Toffolis, while carry-lookahead structures trade more ancillas for less depth — the depth–width tradeoff made concrete at the three-qubit-gate level. Multi-controlled gates also appear in state preparation: selecting subsets from a uniform superposition by mask, implementing multiplexers, and similar operations all take multi-controlled gates as their elementary parts. Precisely because the Toffoli is so frequent and so expensive, every cost reduction it receives — the six-CNOT bound, the relative-phase variant, the 4- ancilla-and-measurement construction — directly rewrites the conclusions of whole-machine resource estimates.
The Fredkin gate and the SWAP gate
The Fredkin gate is a controlled SWAP: when the control is 1, it swaps the other two qubits. It preserves the total number of 1s in a bit string (hence “conservative logic”), is likewise universal for classical reversible computation on its own, and appears in controlled form in quantum protocols such as the SWAP test, used to estimate the inner product of two quantum states. SWAP itself exchanges the states of two qubits; its matrix interchanges and among the middle basis states. It has a decomposition worth memorizing:
three CNOTs in alternating directions send exactly to , as the reader can verify by hand on any basis state. SWAP creates no entanglement itself; its entire value lies in transport — moving information on a chip to a place where interaction is possible.
SWAP networks: the routing tax on connectivity-limited hardware
Qubits on a real chip couple directly only to a few neighbors (as in grid or heavy-hex layouts), while algorithm circuits often assume any two qubits can interact directly. The compiler must “route” the logical circuit onto the physical topology: insert SWAPs to carry quantum states along the coupling graph into adjacent positions, perform the target gate, then decide whether to carry them back. Each SWAP is worth 3 CNOTs, and depth and error accumulate accordingly; routing is often the largest single block of overhead in a NISQ circuit. One tool for reducing the transport is the bridge identity: if qubit lies between and on the coupling graph, then the sequence CNOT, CNOT, CNOT, CNOT implements exactly a remote CNOT while restoring unchanged — four CNOTs buy one remote interaction, saving the SWAP pair that would move states back and forth. A compiler’s mapping and routing algorithms (such as heuristic-search-based routers) are, in essence, performing a global cost tradeoff among SWAP insertion, bridging, and gate reordering; when judging a hardware platform’s usability, how well its coupling topology matches the algorithm’s circuit often decides success or failure before the qubit count does.
Summary
Three-qubit gates complete the circuit model’s expressive power: Toffoli and Fredkin prove that universal classical reversible computation lives inside quantum circuits, and SWAP moves information around a physical topology. Their shared theme is cost — the Toffoli’s six-CNOT lower bound, the halving trick of relative-phase variants, the SWAP tax in routing — and the engineering of quantum circuits is, to a large extent, the discipline of fighting these constant factors.
References
- Toffoli, T., Reversible Computing, MIT/LCS/TM-151, 1980.
- Shende, V. V. & Markov, I. L., On the CNOT-cost of Toffoli gates, Quantum Information & Computation 9, 461 (2009). https://arxiv.org/abs/0803.2316
- Nielsen, M. A. & Chuang, I. L., Quantum Computation and Quantum Information, Cambridge University Press, 2010. https://www.cambridge.org/9780521635035
Tags: Toffoli GateReversible ComputingSWAPQuantum Routing