# Toffoli gate

In logic circuits, the Toffoli gate (also CCNOT gate), invented by Tommaso Toffoli, is a universal reversible logic gate, which means that any classical reversible circuit can be constructed from Toffoli gates. It is also known as the "controlled-controlled-not" gate, which describes its action. It has 3-bit inputs and outputs; if the first two bits are both set to 1, it inverts the third bit, otherwise all bits stay the same.

## Background

An input-consuming logic gate L is reversible if it meets the following conditions: L(x) = y is a gate where for any output y, there is a unique input x. The gate L is reversible if there is a gate L′(y) = x which maps y to x. From common logic gates, NOT is reversible, as can be seen from its truth table below.

INPUT OUTPUT
0 1
1 0

The common AND gate is not reversible, because the inputs 00, 01 and 10 are all mapped to the output 0.

Reversible gates have been studied since the 1960s. The original motivation was that reversible gates dissipate less heat (or, in principle, no heat).

More recent motivation comes from quantum computing. In quantum mechanics the quantum state can evolve in two ways, by Schrödinger's equation (unitary transformations), or by their collapse. Logic operations for quantum computers, of which the Toffoli gate is an example, are unitary transformations and therefore evolve reversibly.

## Universality and Toffoli gate

Any reversible gate that consumes its inputs and allows all input computations must have no more input bits than output bits, by the pigeonhole principle. For one input bit, there are two possible reversible gates. One of them is NOT. The other is the identity gate, which maps its input to the output unchanged. For two input bits, the only non-trivial gate is the controlled NOT gate, which XORs the first bit to the second bit and leaves the first bit unchanged.

Truth table Permutation matrix form
INPUT OUTPUT
0   0   0   0
0 1 0 1
1 0 1 1
1 1 1 0

${\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&0&1\\0&0&1&0\\\end{bmatrix}}$ Unfortunately, there are reversible functions that cannot be computed using just those gates. In other words, the set consisting of NOT and XOR gates is not universal. To compute an arbitrary function using reversible gates, another gate is needed. One possibility is the Toffoli gate, proposed in 1980 by Toffoli.

This gate has 3-bit inputs and outputs. If the first two bits are set, it flips the third bit. The following is a table of the input and output bits:

Truth table Permutation matrix form
INPUT OUTPUT
0   0   0   0   0   0
0 0 1 0 0 1
0 1 0 0 1 0
0 1 1 0 1 1
1 0 0 1 0 0
1 0 1 1 0 1
1 1 0 1 1 1
1 1 1 1 1 0

${\begin{bmatrix}1&0&0&0&0&0&0&0\\0&1&0&0&0&0&0&0\\0&0&1&0&0&0&0&0\\0&0&0&1&0&0&0&0\\0&0&0&0&1&0&0&0\\0&0&0&0&0&1&0&0\\0&0&0&0&0&0&0&1\\0&0&0&0&0&0&1&0\\\end{bmatrix}}$ It can be also described as mapping bits {a, b, c} to {a, b, c XOR (a AND b)}. This can also be understood as a Modulo operation on bit c: {a, b, c} → {a, b, (c+ab) mod 2} often written as {a, b, c} → {a, b, c $\oplus$ ab}

The Toffoli gate is universal; this means that for any Boolean function f(x1, x2, ..., xm), there is a circuit consisting of Toffoli gates that takes x1, x2, ..., xm and some extra bits set to 0 or 1 to outputs x1, x2, ..., xm, f(x1, x2, ..., xm), and some extra bits (called garbage). A NOT gate, for example, can be constructed from a Toffoli gate by setting the three input bits to {a, 1, 1}, making the third output bit (1 XOR (a AND 1)) = NOT a; (a AND b) is the third output bit from {a, b, 0}. Essentially, this means that one can use Toffoli gates to build systems that will perform any desired Boolean function computation in a reversible manner.

## Related logic gates

• The Margolus gate (named after Norman Margolus), also called simplified Toffoli is a quantum logic gate very similar to a Toffoli gate but with a -1 in the diagonal: $\mathrm {RCCX} =\operatorname {diag} (1,1,1,1,1,-1,X)$ . The Margolus gate is also universal for reversible circuits and acts very similar to a Toffoli gate, with the advantage that it can be constructed with about half of the CNOTS compared to the Toffoli gate.