The Toffoli gate, also called the CCNOT gate, is a three-qubit reversible gate that flips the target qubit if and only if both control qubits are 1 and leaves every other input unchanged. In quantum computing, and specifically in the study of Quantum gates & circuits, it is the standard bridge between classical reversible logic and quantum algorithms, because it can compute AND, NAND and copy operations without discarding information.
Tommaso Toffoli introduced the gate in 1980 as a universal gate for reversible classical computation. In a quantum circuit it acts as a unitary operator on eight basis states, and together with the Hadamard gate it forms a computationally universal gate set for real-amplitude quantum computation.
How the Toffoli gate acts on three qubits
The Toffoli gate maps the computational basis state |a, b, c⟩ to |a, b, c XOR (a AND b)⟩. The two control qubits pass through untouched, while the target qubit receives the logical AND of the controls added modulo 2. Because the mapping is a permutation of basis states, it is its own inverse: applying the gate twice returns the original state.
As a matrix, the gate is an 8 × 8 permutation matrix equal to the identity except for a swap of the |110⟩ and |111⟩ entries. On superposition inputs it acts linearly, so a control register in superposition produces an entangled output in which the target depends on every branch of the control state.
The following truth table lists every basis input and the resulting output.
| Input a b c | Output a b c | Target changed |
|---|---|---|
| 000 | 000 | No |
| 001 | 001 | No |
| 010 | 010 | No |
| 011 | 011 | No |
| 100 | 100 | No |
| 101 | 101 | No |
| 110 | 111 | Yes |
| 111 | 110 | Yes |
Why the Toffoli gate matters for reversible logic
Ordinary Boolean gates such as AND are irreversible: two input bits produce one output bit, and the lost information carries an energy cost described by Landauer’s principle. The Toffoli gate avoids this loss by keeping the inputs and writing the result into a third bit, which makes any classical function implementable reversibly.
Setting the target to a fixed value turns the gate into familiar classical operations. This property explains why quantum oracles for algorithms such as Grover’s search are usually compiled from Toffoli and CNOT networks: a classical predicate is first written as reversible logic and then embedded in the quantum circuit.
| Input setting | Operation obtained | Output location |
|---|---|---|
| Target fixed to 0 | AND of a and b | Target qubit |
| Target fixed to 1 | NAND of a and b | Target qubit |
| Control b fixed to 1 | CNOT from a to c | Target qubit |
| Both controls fixed to 1 | NOT of c | Target qubit |
| Control b fixed to 1, target 0 | Copy (fan-out) of a | Target qubit |
Decomposing the Toffoli gate into native gates
No current hardware applies a three-qubit Toffoli gate directly at scale, so compilers decompose it into one- and two-qubit gates from the native gate set. The textbook decomposition uses six CNOT gates, seven T or T-dagger gates and two Hadamard gates, and it is known that six CNOTs is the minimum for an exact decomposition with CNOT as the only entangling gate.
The cost matters most in fault-tolerant machines, where T gates are expensive because each one consumes a distilled magic state. Designers therefore track the T-count and T-depth of every Toffoli. Several cheaper variants exist when the exact phase does not matter or when ancilla qubits and mid-circuit measurement are available.
| Implementation | CNOT count | T-count | Typical use |
|---|---|---|---|
| Standard exact decomposition | 6 | 7 | General-purpose compilation |
| Relative-phase Toffoli | 3 | 4 | Pairs that cancel the phase later |
| Ancilla-assisted, T-depth 1 | About 16 | 7 | Shallow fault-tolerant circuits |
| Measurement-based uncompute | 3 plus measurement | 4 then 0 | Compute and erase AND results |
| Native three-qubit pulse | 0 | Not applicable | Experimental trapped-ion and superconducting demos |
On hardware with limited connectivity, the three qubits may not be mutually coupled. The compiler then inserts SWAP gates, which can double or triple the two-qubit gate count and increase circuit depth considerably.
How it compares with related controlled gates
The Toffoli gate belongs to the family of multi-controlled gates. It generalizes the CNOT gate by adding a second control, and it differs from the Fredkin gate, which conditionally swaps two targets rather than flipping one. Placing Hadamard gates on the target converts it into a controlled-controlled-Z gate, which is symmetric in all three qubits.
| Gate | Qubits | Action | Clifford gate |
|---|---|---|---|
| CNOT gate | 2 | Flips target if one control is 1 | Yes |
| Toffoli gate | 3 | Flips target if both controls are 1 | No |
| CCZ gate | 3 | Adds a minus sign to |111⟩ | No |
| Fredkin gate | 3 | Swaps two targets if control is 1 | No |
| Multi-controlled NOT | n + 1 | Flips target if all n controls are 1 | No |
Because it is not a Clifford gate, the Toffoli gate cannot be simulated efficiently by the Gottesman–Knill method. This is precisely why it supplies the non-Clifford resource needed for universal computation.
Limitations and common mistakes
The most frequent misconception is that Toffoli plus Hadamard is universal for all quantum computation. Strictly, this pair is universal only for real-amplitude circuits; complex phases require an additional encoding qubit, which is efficient but not free. Practitioners should also keep the following pitfalls in mind:
- Forgetting to uncompute intermediate AND results leaves garbage qubits entangled with the output and destroys interference.
- Counting a Toffoli as one gate hides its true cost of roughly 15 native operations and a large share of circuit depth.
- Using relative-phase variants unpaired introduces phase errors that only appear in superposition inputs, not in classical truth-table tests.
- Ignoring connectivity underestimates the SWAP overhead on linear or heavy-hex qubit layouts.
The idea in brief
The Toffoli gate is a switch with two keys. It changes a third bit only when both keys are turned on. In every other case, nothing happens.
This simple rule is powerful. It can build any ordinary logic circuit without throwing information away. That matters for quantum computers, which cannot erase data without disturbing the calculation. So the Toffoli gate lets quantum programs run normal arithmetic and checks inside a quantum circuit. The catch is cost: real machines must assemble it from about fifteen smaller steps.
A practical scenario
A team estimating resources for Shor’s algorithm must implement modular multiplication on a 2,048-bit number. The arithmetic is written as reversible adders, and each adder bit uses one or two Toffoli gates to compute carries. The full circuit contains billions of Toffoli gates, so the team reports its cost directly as a Toffoli count rather than a total gate count.
On the target fault-tolerant architecture, each Toffoli is produced from a dedicated magic-state factory. By switching to measurement-based uncomputation of carries, the team halves the number of Toffolis in the adder. The result is a substantially shorter estimated runtime and fewer physical qubits devoted to magic-state production.