# Toffoli gate

In logic circuits, the Toffoli gate, also known as the CCNOT gate (“controlled-controlled-not”), invented by Tommaso Toffoli in 1980 is a CNOT gate with two control qubits and one target qubit. That is, the target qubit (third qubit) will be inverted if the first and second qubits are both 1. It is a universal reversible logic gate, which means that any classical reversible circuit can be constructed from Toffoli gates.

## Description

The truth table and permutation matrix are as follows (the permutation can be written (7,8) in cycle notation):
| Truth table | Permutation matrix |
| Input Output 000000 001001 010010 011011 100100 101101 110111 111110 | ${\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}}$ |

## Background

An input-consuming logic gate L is reversible if it meets the following conditions: (1) L(x) = y is a gate where for any output y, there is a unique input x; (2) The gate L is reversible if there is a gate L´(y) = x which maps y to x, for all y.
An example of a reversible logic gate is a NOT, which can be described from its truth table below:
| Input | Output | Condition (1) | Condition (2) |
| 0 | 1 | x = 0 y = 1 | y = 1 x = 0 |
| 1 | 0 | x = 1 y = 0 | y = 0 x = 1 |
The common AND gate is not reversible, because the inputs 00, 01 and 10 are all mapped to the output 0.
| Input | Output | Condition (1) |
| 00 | 0 | x not unique for y = 0 |
| 01 | 0 | x not unique for y = 0 |
| 10 | 0 | x not unique for y = 0 |
| 11 | 1 | x = 11 y = 1 |
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.

## Hardware description

The classical Toffoli gate implemented in the hardware description language Verilog:

## 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 (up to symmetry) is the controlled NOT gate (CNOT), which XORs the first bit to the second bit and leaves the first bit unchanged.
| Truth table | Permutation matrix form |
| Input Output 0000 0101 1011 1110 | ${\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. For example, AND cannot be achieved by those gates. In other words, the set consisting of NOT and XOR gates is not universal. To compute an arbitrary function using reversible gates, the Toffoli gate, proposed in 1980 by Toffoli, can indeed achieve the goal. 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 ⨁ ab}.
The Toffoli gate is universal; this means that for any Boolean function f(x$_1$, x$_2$, ..., x$_m$), there is a circuit consisting of Toffoli gates that takes x$_1$, x$_2$, ..., x$_m$ and some extra bits set to 0 or 1 to outputs x$_1$, x$_2$, ..., x$_m$, f(x$_1$, x$_2$, ..., x$_m$), 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 Fredkin gate is a universal reversible 3-bit gate that swaps the last two bits if the first bit is 1; a controlled-swap operation.
* The n-bit Toffoli gate is a generalization of the Toffoli gate. It takes n bits x$_1$, x$_2$, ..., x$_n$ as inputs and outputs n bits. The first n − 1 output bits are just x$_1$, ..., x$_{n−1}$. The last output bit is (x$_1$ AND ... AND x$_{n−1}$) XOR x$_n$.
* The Toffoli gate can be realized by five two-qubit quantum gates, but it can be shown that it is not possible using fewer than five.
* Another universal gate, the Deutsch gate, can be realized by five optical pulses with neutral atoms. The Deutsch gate is a universal gate for quantum computing.
* The Margolus gate (named after Norman Margolus), also called simplified Toffoli, is very similar to a Toffoli gate but with a −1 in the diagonal: RCCX = 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.
* The iToffoli gate was implemented in superconducting qubits with pair-wise coupling by simultaneously applying noncommuting operations.

## Relation to quantum computing

Any reversible gate can be implemented on a quantum computer, and hence the Toffoli gate is also a quantum operator. However, the Toffoli gate cannot be used for universal quantum computation, though it does mean that a quantum computer can implement all possible classical computations. The Toffoli gate has to be implemented along with some inherently quantum gate(s) in order to be universal for quantum computation. Specifically any single-qubit gate with real coefficients that can create a nontrivial quantum state suffices.
A Toffoli gate based on quantum mechanics was successfully realized in January 2009 at the University of Innsbruck, Austria. While the implementation of an n-qubit Toffoli with circuit model requires $2n$ CNOT gates, the best known upper bound stands at $6n-12$ CNOT gates. It has been suggested that trapped Ion Quantum computers may be able to implement an n-qubit Toffoli gate directly. The application of many-body interaction could be used for direct operation of the gate in trapped ions, Rydberg atoms, and superconducting circuit implementations. Following the dark-state manifold, Khazali-Mølmer C$_n$-NOT gate operates with only three pulses, departing from the circuit model paradigm. The iToffoli gate was implemented in a single step using three superconducting qubits with pair-wise coupling.