Lesson 22 · 85 min · advanced
Toffoli, Reversible Logic & Universality
The Toffoli gate computes NAND and FANOUT reversibly, so every classical Boolean circuit can be simulated by a quantum circuit. Toffoli itself decomposes exactly into six CNOTs, seven gates and two Hadamards, which is checked here state by state through the phase bookkeeping with . Any factors into at most two-level unitaries, hence into CNOTs and single-qubit gates (exact universality), and the finite set approximates any unitary to accuracy with gates (Solovay–Kitaev).
Prerequisites: Controlled Gates: CNOT, CZ, SWAP
Objectives and key results
Write the permutation matrix of , show , and use and to obtain NAND and FANOUT.
Prove that NAND generates NOT, AND and OR, and conclude that Toffoli is universal for reversible classical computation.
Verify the decomposition of into CNOTs, and by computing the phase on all eight basis states and proving .
Prove that every is a product of at most two-level unitaries, carry out the elimination on a example, and turn two-level unitaries into Gray-code-conjugated multi-controlled gates built from CNOTs and single-qubit gates.
Derive for the rotation , prove that is irrational, explain the density argument, and state the Solovay–Kitaev theorem with its length bound.
- identity · Toffoli action
- algorithm · Clifford+T Toffoli
- theorem · Two-level decomposition
- formula · Irrational rotation
- theorem · Solovay–Kitaev
The Toffoli gate and reversible classical logic#
Definition
Toffoli gate (CCX)
The Toffoli gate or controlled-controlled-NOT is the three-qubit unitary defined on the computational basis by for : the target bit is flipped exactly when both controls are . In block form . With qubit as the most significant bit, has index , so exchanges indices and and fixes the other six.
Unitary verified: (max deviation 0.0e+0)
is a self-inverse permutation
- Step 1:Apply the definition twice; the controls are unchanged, and for every bit value of .
- Step 2:A linear map that fixes every basis vector is the identity. The matrix is real and symmetric (it swaps indices and ), so it equals its adjoint, and with it is unitary.
- Step 3:A self-inverse map is invertible. Hence the classical Toffoli function loses no information, unlike AND or NAND on two bits, which map four inputs to two outputs.
Result
Circuit · 3 qubits
on qubits and prepares (index ); the Toffoli then flips the target because , and the statevector panel shows amplitude on (index ).
NAND and FANOUT from one Toffoli
- Step 1:Set the target to and use for a bit .
- Step 2:Evaluate on the four inputs: only gives and hence output .
- Step 3:Set the second control to and the target to : the target receives a copy of the classical bit (FANOUT). This copies only basis states; it is not a cloner for superpositions.
- Step 4:With both controls fixed to the Toffoli is a NOT on the target, so constant ancilla bits turn it into NOT as well.
Result
NAND is universal for classical Boolean logic
Every Boolean function can be computed by a circuit of NAND gates together with FANOUT (copying wires) and the constants . Consequently, with ancilla bits prepared in constant states and extra output (garbage) bits, every classical Boolean circuit can be simulated by a circuit of Toffoli gates acting reversibly on basis states.
Proof (6 steps)
- Step 1:Feed one bit into both inputs (this uses FANOUT); since the output is NOT .
- Step 2:Double negation, with the NOT from the previous step applied to .
- Step 3:De Morgan's law , checked on the four inputs: , , , .
- Step 4:Disjunctive normal form: the inner AND equals exactly when , so the OR over all with equals (if no such exists, is the constant ). Hence NOT, AND and OR, with copies of the inputs, compute every .
- Step 5:Substitute the three NAND expressions into the DNF circuit.
- Step 6:Replace each NAND by and each FANOUT by , each using fresh ancilla bits; the inputs are kept, the intermediate results remain as garbage, and every step is a permutation of basis states.
∎
Quick check
Which input makes copy the bit of qubit onto qubit ?
Toffoli from H, T, T† and CNOT#
The Toffoli gate is not a native gate on most hardware, but it can be built exactly from the Clifford+ set with , and . The construction has two ingredients: a Hadamard on the target turns into the diagonal gate , and a diagonal gate is built by placing and on wires that carry suitable parities of the input bits. On a basis state of one wire, and , and a CNOT maps , so every gate of the middle part sends basis states to basis states times a power of .
- Step 1:The controlled-controlled- applies to the target exactly when ; on it multiplies by .
- Step 2:Each term has the form with a projector on the controls; the mixed-product rule gives .
- Step 3:and (Lesson on Hadamard and phase gates).
- Step 4:, so the CCZ phase is raised to .
Result
Unitary verified: (max deviation 0.0e+0)
Circuit · 3 qubits
The decomposition with CNOTs, and . The unitary panel shows the permutation matrix of exactly, with no global phase.
Wire contents and phases of the diagonal part
- Step 1:The CNOT adds the control bit to the target; the then contributes with the current target bit.
- Step 2:The target accumulates ; the contributes (only the new factor is written).
- Step 3:Adding a second time cancels it, since .
- Step 4:Adding again restores the target bit ; on qubit and on qubit contribute and .
- Step 5:These four gates act on the controls only and therefore commute with , so they can be placed after it. The middle qubit carries while acts, and the last CNOT restores .
- Step 6:Every basis state returns to itself, so the diagonal part multiplies by with the sum of the seven exponents.
Result
The seven exponents for each of the eight basis states (columns in circuit order, ):
Seven rows sum to and only gives , i.e. the phase : the diagonal part is exactly .
The T-count phase identity
For all bits , . Hence the diagonal part implements , which is , and the full circuit with the two gates equals exactly.
Proof (6 steps)
- Step 1:Check the four cases: , , , .
- Step 2:Apply the identity twice, first with and then with , .
- Step 3:Substitute the arithmetic forms of the four parities.
- Step 4:Collect the coefficients of each monomial.
- Step 5:Every linear and quadratic coefficient vanishes.
- Step 6:, and the conjugation identity derived above turns into .
∎
Quick check
How many and gates (together) does the decomposition use?
Quick check
If the two gates are removed, what is the process fidelity of the remaining circuit with ?
Exact universality: two-level unitaries#
Definition
Two-level unitary
A unitary is two-level if there are indices such that acts non-trivially only on : for all and maps to itself by a unitary . The adjoint and the identity are two-level, and a two-level unitary of embedded as is two-level in .
Decomposition into two-level unitaries
Every with is a product of two-level unitaries with .
Proof (9 steps)
- Step 1:Elimination step. Given two entries (with ) of a column, define as this block on the pair and as the identity on all other basis vectors. If take .
- Step 2:Multiply by its conjugate transpose entry by entry; the off-diagonal entries cancel and the diagonal entries equal . So is a two-level unitary.
- Step 3:zeros the entry , puts the real number in position , and leaves every other entry of the column unchanged.
- Step 4:Apply the step to column with the pairs in this order. touches only rows and , so the entries already zeroed (rows ) stay zero, and after the last step entry is real and non-negative (it is , or if all remaining entries were ).
- Step 5:A product of unitaries is unitary, so its columns have norm ; the only non-zero entry is , hence .
- Step 6:The rows of a unitary also have norm ; row starts with the entry , so the rest of the row vanishes. The remaining block is unitary because the whole matrix is.
- Step 7:Repeat on with columns ; each step uses pairs inside the indices , so the 's remain two-level in , and column needs factors. The process stops when a block on indices remains (for take and ).
- Step 8:Multiply by on the left; each is two-level, and so is .
- Step 9:Count the factors; factors equal to can be dropped, which only lowers the count, so .
∎
Worked example in
- Step 1:A real orthogonal (hence unitary) matrix: e.g. column dotted with column gives and each column has squared norm .
- Step 2:First elimination step on the pair of column : (all entries real, so conjugation does nothing).
- Step 3:Row is , row is , and row is unchanged.
- Step 4:Second step on the pair of column ; as it must be for the last step of a unit column.
- Step 5:Row : . Row : . Row is exactly as the proof predicts.
- Step 6:and are real symmetric, so . Three factors, which equals for .
Result
Unitary verified: (max deviation 0.0e+0)
A two-level unitary as a Gray-code-conjugated multi-controlled gate
- Step 1:On qubits () the two basis strings differ in at most bits; flipping the differing bits one at a time gives a Gray code from to .
- Step 2:flips the single bit where and differ, controlled on the other bits taking the common values of (a -control is an ordinary control conjugated by ). It is an -controlled NOT that swaps exactly these two basis states and fixes all others.
- Step 3:The swaps move the amplitude of along the code to , a neighbour of ; is never touched by .
- Step 4:Apply to that bit, controlled on the other bits equal to their common values, with the basis order chosen so that the row/column of of corresponds to .
- Step 5:Undo the swaps. The result acts as on and as the identity elsewhere. It uses multi-controlled NOTs and one multi-controlled single-qubit gate.
- Step 6:Example: flips qubit when , flips qubit when , then acts on qubit controlled by , followed by and again.
Result
Multi-controlled gates from CNOTs and single-qubit gates
- Step 1:Choose a square root of (diagonalise and take ). The product is written right to left: the circuit applies first.
- Step 2:applies ; the CNOT puts on qubit , so applies ; the second CNOT restores ; applies . Powers of one matrix commute.
- Step 3:Use . The target receives exactly when : this is .
- Step 4:The construction of the controlled-gates lesson: with , so each controlled single-qubit gate costs CNOTs, three single-qubit gates and a phase gate on the control.
- Step 5:The ancilla , , , (each a Toffoli onto a ancilla) holds the AND of all controls; controlled by acts exactly when all ; running the Toffolis in reverse returns the ancillas to . In total Toffolis, each CNOTs, plus one .
- Step 6:Combine the three constructions: two-level factors, Gray-code conjugation and multi-controlled gates. Hence is exactly universal, with gates for a generic .
Result
Quick check
What is the upper bound on the number of two-level factors for a three-qubit unitary ()?
Approximate universality and Solovay–Kitaev#
Definition
Approximation error
For unitaries on the same space the approximation error is the operator-norm distance . A gate set is approximately universal if for every unitary and every there is a finite product of gates from with for some global phase . The errors of a circuit add up: , by the telescoping sum and .
is a rotation by with
- Step 1:Factor out ; with .
- Step 2:, and .
- Step 3:Multiply the two factorisations; the global phases add to .
- Step 4:Expand the product of the two brackets; .
- Step 5:, so ; factor out of the three Pauli terms.
- Step 6:Check that the bracket has the form : with , .
- Step 7:Read off from the identity coefficient and normalise the axis; .
- Step 8:Half-angle formula with .
Result
The angle λ is an irrational multiple of π
Let be defined by . Then .
Proof (5 steps)
- Step 1:Double-angle formula and .
- Step 2:Isolate and square.
- Step 3:Suppose for contradiction that is rational; then is a root of unity, i.e. a root of the monic integer polynomial , hence an algebraic integer, and so is .
- Step 4:Algebraic integers form a ring, so the sum is an algebraic integer.
- Step 5:is irrational because is, so its minimal polynomial has degree and is the monic quadratic found above. An algebraic integer has a monic minimal polynomial with integer coefficients (Gauss's lemma), but : contradiction. Hence is irrational.
∎
Density of the group generated by H and T
The products of and approximate every single-qubit unitary up to a global phase to arbitrary accuracy. Together with CNOT, is approximately universal: any circuit of CNOT and single-qubit gates can be approximated to accuracy by approximating each single-qubit gate to accuracy .
Proof (7 steps)
- Step 1:If then for an integer , so would be rational, contradicting the proposition.
- Step 2:Pigeonhole: distinct points in and intervals of length , so two points share an interval.
- Step 3:Consecutive multiples of are less than apart, so every angle lies within of some , i.e. of modulo .
- Step 4:has eigenvalues . Rotation angles matter only modulo up to the global phase , so approximates every within up to phase.
- Step 5:Conjugation by maps on the Bloch sphere, so (up to phase) rotates by the same about , which is not parallel to ; the same argument approximates every .
- Step 6:For non-parallel rotation axes every is such a finite product (Nielsen–Chuang Exercise 4.11; with it is the usual Euler decomposition). Approximating each of the factors to accuracy gives to accuracy by additivity of errors.
- Step 7:For a multi-qubit circuit (exactly universal by the previous section), keep every CNOT and replace each single-qubit gate by an word within .
∎
Solovay–Kitaev theorem
Let be a finite gate set that is closed under inverses and generates a dense subgroup of (for example and rescaled to determinant , together with their inverses). With the exponent , the following holds: for every and every there is a product of at most gates of with , and can be found by a classical algorithm in time .
Proof (8 steps)
- Step 1:Proof sketch. Base case: because the generated group is dense and is compact, all words of some length form an -net; is fixed small enough for the recursion to contract. Level returns the nearest net point , with error and gates.
- Step 2:Level : the remaining error of the level- approximation is a unitary close to the identity.
- Step 3:Balanced group commutator: a rotation by a small angle equals the commutator of two rotations by angles of order about perpendicular axes.
- Step 4:Approximate and recursively at level by . The commutator suppresses their errors: the first-order terms cancel and the remainder is of order .
- Step 5:uses once and the level- words for and their inverses (closure of under inverses) once each: five words of length .
- Step 6:Multiply the error recursion by and iterate; the error decreases doubly exponentially in because .
- Step 7:Take logarithms of the bound of the previous step; the smallest such is .
- Step 8:Write and insert the bound on , which is up to a constant factor.
∎
Quick check
Compute to three decimals.
Quick check
Which property of makes the powers of approximate every rotation about ?
Lab and references#
In the lab you build the Toffoli gate from with at most six CNOTs. The grader compares your unitary with through ; a circuit that builds instead scores .
References
- M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, 10th anniversary ed., Cambridge University Press (2010), §3.2.5 (reversible computation), §4.3 (Fig. 4.9), §4.5 (universal quantum gates), Appendix 3 (Solovay–Kitaev).doi:10.1017/CBO9780511976667
- A. Barenco, C. H. Bennett, R. Cleve, D. P. DiVincenzo, N. Margolus, P. Shor, T. Sleator, J. A. Smolin and H. Weinfurter, Elementary gates for quantum computation, Physical Review A 52, 3457–3467 (1995).doi:10.1103/PhysRevA.52.3457
- T. Toffoli, Reversible computing, in Automata, Languages and Programming (ICALP 1980), Lecture Notes in Computer Science 85, Springer, 632–644 (1980).doi:10.1007/3-540-10003-2_104
- C. M. Dawson and M. A. Nielsen, The Solovay–Kitaev algorithm, Quantum Information and Computation 6, 81–95 (2006); arXiv:quant-ph/0505030 (2005).arxiv.org
Hands-on labs
Decompose the Toffoli gate
1 step · auto-gradedOpen labBuild the Toffoli gate (controls , target ) on three qubits from the Clifford+ set with at most six CNOTs and fifteen gates in total.