Lean, informalized

Nielsen & Chuang · Chapter 4, Quantum circuits · p. 182

Exercise 4.24 - A Toffoli gate from Hadamard, phase and CNOT gates

Figure 4.9, gate by gate.

Hover over a blue or green term in the text, or a node in the graph, to find it in the other. Select it to read its declaration both in Lean and in words.

The Exercise

Figure 4.9 of the book draws a circuit on three qubits made of sixteen gates: two Hadamards on the target wire, six CNOTs, seven gates TT or T†T^\dagger, and one SS. Exercise 4.24, on page 182, asks the reader to verify that this circuit is the Toffoli gate. The point of the figure is that the Toffoli gate, which is not in the standard set, can be assembled from gates that are, and the Toffoli gate in turn gives universal classical computation on a quantum computer.

The library has no figure. It has the circuit as a term, cut into two blocks the way the figure reads, and the exercise as an equation between two unitaries. These notes write out the gates, the two blocks and the equation.

Gates and Wires

Four single-qubit gates occur. The Hadamard HH and the Pauli XX are the usual ones. The others are phase gates: the library has one definition P(α)P(\alpha), the unitary that leaves |0⟩|0\rangle alone and multiplies |1⟩|1\rangle by eiαe^{i\alpha}, and the book’s gates are three values of it, T:=P(π/4),S:=P(π/2),T†:=P(−π/4).T := P(\pi/4), \qquad S := P(\pi/2), \qquad T^{\dagger} := P(-\pi/4) .(1)✦

The register is qubit⊗(qubit⊗qubit)\mathrm{qubit} \otimes (\mathrm{qubit} \otimes \mathrm{qubit}), bracketed to the right, and a gate sits on a wire by being tensored with the identity on the others. We write the wire as a subscript, so the gates on this register are T1:=T⊗id,T2:=id⊗(T⊗id),T3:=id⊗(id⊗T),T2†:=id⊗(T†⊗id),T3†:=id⊗(id⊗T†),S2:=id⊗(S⊗id),H3:=id⊗(id⊗H).\begin{aligned}T_1 &:= T \otimes \mathrm{id}, & T_2 &:= \mathrm{id} \otimes (T \otimes \mathrm{id}), & T_3 &:= \mathrm{id} \otimes (\mathrm{id} \otimes T), \\ T^{\dagger}_2 &:= \mathrm{id} \otimes (T^{\dagger} \otimes \mathrm{id}), & T^{\dagger}_3 &:= \mathrm{id} \otimes (\mathrm{id} \otimes T^{\dagger}), & S_2 &:= \mathrm{id} \otimes (S \otimes \mathrm{id}), \\ H_3 &:= \mathrm{id} \otimes (\mathrm{id} \otimes H) .\end{aligned}(2) A CNOT is the controlled XX, written C(U)C(U) for the gate UU controlled by the first qubit, and which two wires it joins is again read off the tensor structure, CNOT12:=C(X⊗id),CNOT13:=C(id⊗X),CNOT23:=id⊗CNOT.\mathrm{CNOT}_{12} := C(X \otimes \mathrm{id}), \qquad \mathrm{CNOT}_{13} := C(\mathrm{id} \otimes X), \qquad \mathrm{CNOT}_{23} := \mathrm{id} \otimes \mathrm{CNOT} .(3) Composition is written U∘VU \circ V and means VV first, then UU, so in a chain the rightmost gate acts first, the opposite of reading the figure from left to right.✦

The Two Blocks

The figure splits naturally at the Hadamards. Between them lie eight gates, all on the target wire or ending on it, Q=T3∘CNOT13∘T3†∘CNOT23∘T3∘CNOT13∘T3†∘CNOT23,Q = T_3 \circ \mathrm{CNOT}_{13} \circ T^{\dagger}_3 \circ \mathrm{CNOT}_{23} \circ T_3 \circ \mathrm{CNOT}_{13} \circ T^{\dagger}_3 \circ \mathrm{CNOT}_{23} ,(4)✦q1q2q3AxQM.cnotGateAxQM.pauliXGateAxQM.phaseShiftGateT†AxQM.controlledUnitaryAxQM.pauliXGateAxQM.phaseShiftGateTAxQM.cnotGateAxQM.pauliXGateAxQM.phaseShiftGateT†AxQM.controlledUnitaryAxQM.pauliXGateAxQM.phaseShiftGateT read right to left: a CNOT from the second control, a T†T^\dagger, a CNOT from the first control, a TT, and the same four once more. After the second Hadamard come six gates on the two control wires alone, P=S2∘T1∘CNOT12∘T2†∘CNOT12∘T2†.P = S_2 \circ T_1 \circ \mathrm{CNOT}_{12} \circ T^{\dagger}_2 \circ \mathrm{CNOT}_{12} \circ T^{\dagger}_2 .(5)✦q1q2q3AxQM.phaseShiftGateT†AxQM.controlledUnitaryAxQM.pauliXGateAxQM.phaseShiftGateT†AxQM.controlledUnitaryAxQM.pauliXGateAxQM.phaseShiftGateTAxQM.phaseShiftGateS This block never touches the target; its job is to put the right relative phases on the controls.

The Circuit

The whole figure is a Hadamard on the target, the block QQ, a second Hadamard on the target, then the block PP: Fig49=P∘H3∘Q∘H3.\mathrm{Fig}_{49} = P \circ H_3 \circ Q \circ H_3 .(6)✦q1q2q3AxQM.hadamardGateHAxQM.figure49TargetBlockQAxQM.hadamardGateHAxQM.figure49ControlBlockP Only HH, SS, TT and CNOT occur in it, which is what the construction is for.

The Claim

The exercise is the statement that this unitary is the Toffoli gate, Fig49=Toffoli,\mathrm{Fig}_{49} = \mathrm{Toffoli} ,(7)✦q1q2q3AxQM.toffoliFig49CircuitFig₄₉=q1q2q3AxQM.toffoliGateAxQM.pauliXGate where Toffoli=C2(X)\mathrm{Toffoli} = C^2(X) flips the third qubit exactly when the first two are both |1⟩|1\rangle. The standard way to see it is to follow a basis state |q1q2q3⟩|q_1 q_2 q_3\rangle through the circuit and collect the phases: the Hadamards turn a phase flip of the target into a bit flip, QQ supplies the phase (−1)q1q2(-1)^{q_1 q_2} on the target up to a phase on the controls, and PP removes that leftover. The library does not contain this calculation.

Theorem 1. On three qubits, Fig49=Toffoli\mathrm{Fig}_{49} = \mathrm{Toffoli}.✦

What Is Checked

Lean has checked that the two blocks and the circuit are well-typed unitaries on the three-qubit register, built from the phase gate, the Hadamard, the controlled gates and the wire placements above, and that the equation is well typed. It has checked nothing about a proof, since there is none. The subscripted names on this page are the page’s own notation, declared in the displays with :=:=; the matcher unfolds them before it compares a formula with the Lean term, so a wrong wire in one of them would break the certification of every formula that uses it. What it cannot check is that the term is the figure on page 182: that the sixteen gates are in the book’s order and on the book’s wires. The two block definitions and the circuit each carry a drawing derived from the term, which is the material for that judgement.