Skip to the content
Kets & Gates homeOpen the app

Lesson 11: Circuits as programs

Universal gate sets, controlled gates, and what a circuit costs

about 55 min13 partsbest after lesson 4 and lesson 6

Start the interactive lesson

The interactive version checks every answer, gives hints and saves your progress in this browser. This page is the same lesson on one page, with the answers folded away.

1 / 13 · Learn

Before you start

Part 2 is about building quantum programs and pricing them, and then about the algorithms that run on them. This lesson treats a circuit as a program: a list of instructions, each with a cost. You will rewrite circuits into equivalent ones, build controlled gates and the Toffoli gate from smaller pieces, find out what a small gate set can and cannot do, and count what a circuit costs.

By the end of this lesson you can:

  • turn a circuit into a matrix product in the right order, and use four standard rewrites;
  • build controlled-ZZ and other controlled gates from CNOT and single-qubit gates;
  • check the 15-gate Toffoli circuit and say why it needs seven TT⁠-type gates;
  • say what it means that {H,T,CNOT}\{H, T, \text{CNOT}\} is universal, and why it can’t mean exact;
  • count a circuit’s gates, depth, two-qubit gates and TT gates, and cut its depth;
  • build a NOT with many controls from Toffolis and work qubits.

Check yourself

A warm-up: make a guess before reading on. The lesson comes back to it.

A quantum computer offers only the gates HH, TT and CNOT and you may use as many gates as you need. Can circuits made from them build every single-qubit gate exactly?

  1. AYes: with enough gates, each of them is built exactly
  2. BNo, but each one can be approximated as closely as you like
  3. CNo: the circuits reach only a few gates, such as XX, ZZ and SS
  4. DNot even approximately: most gates are out of reach
Show the answer

Answer: B. No, but each one can be approximated as closely as you like

Circuits from HH, TT and CNOT can be listed by length, so there are only countably many, while every single-qubit gate form a continuum. So the answer is no, but the set is universal: every gate can be approximated to any accuracy, at a cost that grows slowly with the accuracy.

Why not the others

  • A. Universal means approximately. Circuits from a finite gate set can be listed, first those with one gate, then those with two, and so on: there are countably many. The gates you want form a continuum, which no list can cover. So most of them can be approached as closely as you like, but never reached.
  • C. They reach infinitely many. One product of HH and TT is a rotation by an angle that is an irrational fraction of a full turn, so its powers never repeat and come close to every rotation about its axis.
  • D. Every gate can be approximated to any accuracy you name. What fails is exactness: there are only countably many circuits, so most gates are approached, never reached.

Check yourself

A warm-up: make a guess before reading on. The lesson comes back to it.

Two circuits compute the same thing. Circuit A has 2,000 gates, 300 of them TT gates. Circuit B has 1,200 gates, 900 of them TT gates. All the other gates are HH, SS or CNOT. Both will run on an error-corrected quantum computer. Which is likely to be cheaper to run?

  1. ACircuit A
  2. BCircuit B
  3. CAbout the same, since they compute the same thing
  4. DThere is no way to tell without running both
Show the answer

Answer: A. Circuit A

On an error-corrected machine HH, SS and CNOT are cheap and TT gates are expensive, so the TT⁠-count decides. Circuit A has 300 TT gates against 900. Even at a modest ten to one, it costs about 4,700 units against 9,300, despite having more gates.

Why not the others

  • B. Circuit B has fewer gates, but 900 of them are TT gates, and on an error-corrected machine the TT gates dominate the cost. Even if a TT gate cost only ten times as much as each other gate, B would come to about 9,300 units against 4,700.
  • C. Cost belongs to the circuit, not to the job it does: two circuits for the same unitary can differ a lot. Here they differ in the gate that dominates the cost on an error-corrected machine, the TT gate.
  • D. The counts are enough for a good estimate. On an error-corrected machine HH, SS and CNOT are cheap and each TT gate costs many times more, so compare the TT⁠-counts.

2 / 13 · Learn

A circuit is a program

A quantum circuit is a program. Each gate is an instruction, and the instructions run from left to right. Each gate is also a unitary matrix, so the whole program is a single matrix: the product of its gates. The order needs care. If a circuit applies AA, then BB, then CC to ∣ψ⟩\ket\psi, the state becomes

C(B(A∣ψ⟩))=CBA∣ψ⟩.C\big(B(A\ket\psi)\big) = CBA\ket\psi.

The first gate in time is the factor next to the ket, at the right end of the product, and each later gate multiplies from the left. You met this in Lesson 4. It matters more now, because long circuits are compiled, rewritten and costed as products, and most pairs of gates don’t commute.

Time runs left to right along the wire, but the operator is written right to left: the first gate, AA, is the factor next to the ket.

Check yourself

A circuit applies HH, then TT, then XX to one qubit in the state ∣ψ⟩\ket\psi. Which product of matrices equals the whole circuit?

  1. AHTXHTX
  2. BH+T+XH + T + X
  3. CXTHXTH
  4. DH⊗T⊗XH\otimes T\otimes X
Hint
  1. Which gate acts on ∣ψ⟩\ket\psi first, and where must it stand in the product?
Show the answer

Answer: C. XTHXTH

HH acts first, so it sits next to the ket: X(T(H∣ψ⟩))=XTH∣ψ⟩X\big(T(H\ket\psi)\big) = XTH\ket\psi. Time runs left to right in the circuit and right to left in the product.

Why not the others

  • A. That lists the gates in circuit order. The first gate acts on ∣ψ⟩\ket\psi first, so it stands next to the ket: X(T(H∣ψ⟩))=XTH∣ψ⟩X\big(T(H\ket\psi)\big) = XTH\ket\psi. And the order matters here, since HTX≠XTHHTX \ne XTH.
  • B. Gates in sequence compose: each acts on the output of the one before, which makes a product, not a sum. A sum of unitary matrices is usually not even unitary.
  • D. A tensor product describes gates side by side on different qubits, and this one would be an 8×88\times8 matrix. Gates one after another on the same qubit multiply.

Two more gates join XX, YY, ZZ and HH. They are phase gates: each leaves ∣0⟩\ket0 alone and multiplies ∣1⟩\ket1 by a phase.

S=[100i],T=[100eiπ/4]S = \begin{bmatrix}1&0\\0&i\end{bmatrix},\qquad T = \begin{bmatrix}1&0\\0&e^{i\pi/4}\end{bmatrix}

On the Bloch sphere, SS is a quarter turn about the zz axis and TT an eighth of a turn. Phases multiply, so two TT gates make an SS, since eiπ/4⋅eiπ/4=ie^{i\pi/4}\cdot e^{i\pi/4} = i, and two SS gates make a ZZ. The inverses S†S^\dagger and T†T^\dagger carry the phases −i-i and e−iπ/4e^{-i\pi/4}. TT matters twice in this lesson: it is the gate that makes small gate sets universal, and on an error-corrected machine it is the expensive one.

Check yourself

T=[100eiπ/4]T = \begin{bmatrix}1&0\\0&e^{i\pi/4}\end{bmatrix}. What is the smallest number of TT gates in a row (at least one) whose product equals ZZ exactly?

Hints
  1. TT leaves ∣0⟩\ket0 alone and multiplies ∣1⟩\ket1 by eiπ/4e^{i\pi/4}. What does a row of kk of them do to ∣1⟩\ket1?
  2. TkT^k multiplies ∣1⟩\ket1 by eikπ/4e^{ik\pi/4}. Which phase does ZZ put on ∣1⟩\ket1?
Show the answer

Answer: 44

TkT^k leaves ∣0⟩\ket0 alone and multiplies ∣1⟩\ket1 by eikπ/4e^{ik\pi/4}. ZZ puts the phase −1=eiπ-1 = e^{i\pi} on ∣1⟩\ket1, so kπ/4=4π/4k\pi/4 = 4\pi/4 and k=4k = 4.

Common slips

  • 22: Two TT gates give the phase eiπ/2=ie^{i\pi/2} = i: that is SS. ZZ needs −1=eiπ-1 = e^{i\pi}, twice that phase.
  • 88: Eight TT gates give the phase e2iπ=1e^{2i\pi} = 1: that is the identity. ZZ is reached halfway, after four.

3 / 13 · Explore

Rewriting circuits

A program can often be rewritten into a shorter or faster one that does the same job, and compilers for quantum circuits do this all the time. The rewrites rest on identities between small circuits. You know two of them from Lesson 4: HXH=ZHXH = Z and HZH=XHZH = X. They say that HH swaps the xx and zz axes of the Bloch sphere, turning a bit flip into a phase flip and back.

The same idea works on two qubits, with a surprising result. Before you see the list, test your intuition on one circuit: HH on both qubits, then a CNOT with its control on top, then HH on both qubits again.

Predict, then see

The circuit is (H⊗H) CNOT (H⊗H)(H\otimes H)\,\text{CNOT}\,(H\otimes H), with the CNOT’s control on the top qubit. The input is ∣01⟩\ket{01}: top qubit 0, bottom qubit 1. What comes out?

  1. A∣01⟩\ket{01}: the control is 0, so the CNOT does nothing and the Hadamards cancel
  2. BA random basis state: the Hadamards spread the state out
  3. C∣11⟩\ket{11}: the top qubit flips
  4. D∣00⟩\ket{00}: the bottom qubit flips

Why did the top qubit, the CNOT’s control, change? A sentence or two is enough.

Show what happens

What happens: C. ∣11⟩\ket{11}: the top qubit flips

After the first Hadamards the input is ∣+⟩∣−⟩\ket+\ket-. The CNOT flips the bottom qubit in the branch where the top is 1, and flipping ∣−⟩\ket- gives −∣−⟩-\ket-, so the top qubit’s ∣1⟩\ket1 branch gains a minus sign: ∣+⟩∣−⟩\ket+\ket- becomes ∣−⟩∣−⟩\ket-\ket-. The last Hadamards turn that into ∣1⟩∣1⟩\ket1\ket1. The truth table shows the pattern: the top qubit flips exactly when the bottom one is 1. The whole circuit is a CNOT with control and target swapped.

Why not the others

  • A. The Hadamards don’t cancel, because the CNOT sits between them. Inside, the CNOT sees ∣+⟩∣−⟩\ket+\ket-, not ∣0⟩∣1⟩\ket0\ket1, and a flip of ∣−⟩\ket- is only a sign, which lands on the top qubit.
  • B. The second layer of Hadamards undoes the spreading of the first. The output is one basis state, every time.
  • D. The bottom qubit comes out as it went in. In the middle it is ∣−⟩\ket-, and X∣−⟩=−∣−⟩X\ket- = -\ket-: flipping ∣−⟩\ket- only changes a sign.

Here are the four rewrites to know by heart. Each one holds exactly, as an equation between matrices.

  • HXH=ZHXH = Z and HZH=XHZH = X: a bit flip between Hadamards is a phase flip, and the other way round.
  • Hadamards on both qubits, before and after a CNOT, turn it upside down: control and target trade places. This is what you just saw.
  • An XX on the control before and after a CNOT makes it fire when the control is 0 instead of 1. That is an anti-control, drawn as an open circle.

You can check each one by multiplying matrices, or by following every basis state through both sides. The figure collects them.

Four rewrites. In each panel the circuit on the left equals the one on the right exactly, with no leftover phase: HXH=ZHXH = Z, HZH=XHZH = X, a CNOT turned upside down, and an anti-control.

Check yourself

Build a gate that flips the bottom qubit exactly when the top qubit is 0, using XX gates and one CNOT. A control dot and an XX in the same column make a CNOT. The truth table shows what your circuit does to each basis input.

Build it in the circuit builder (gates: X, control).

Hints
  1. Which rewrite turns a control into an anti-control?
  2. Put an XX on the top wire before the CNOT and another after it.
Show the answer

One circuit that works: X on q0, then CNOT q0→q1, then X on q0.

XX on the top wire turns a 0 into a 1, so the CNOT fires; the second XX restores the top qubit. Another answer works too: a CNOT followed by an XX on the bottom wire flips the bottom qubit 1⊕a1\oplus a times, which is once exactly when a=0a = 0.

4 / 13 · Learn

Controlled gates

CNOT is one member of a family. For any single-qubit gate UU, controlled-UU applies UU to the target in the branch where the control is ∣1⟩\ket1 and does nothing where it is ∣0⟩\ket0:

∣0⟩∣ψ⟩↦∣0⟩∣ψ⟩,∣1⟩∣ψ⟩↦∣1⟩ U∣ψ⟩.\ket0\ket\psi \mapsto \ket0\ket\psi, \qquad \ket1\ket\psi \mapsto \ket1\,U\ket\psi.

CNOT is controlled-XX. Controlled-ZZ, written CZ, leaves ∣00⟩\ket{00}, ∣01⟩\ket{01} and ∣10⟩\ket{10} alone and multiplies ∣11⟩\ket{11} by −1-1. That sign belongs to both qubits equally, so CZ has no real control and target. You can build it with a rewrite: put HH on the target before and after a CNOT. Where the control is 1, the target then gets HXH=ZHXH = Z; where it is 0, it gets HH=IHH = I.

Check yourself

Build controlled-ZZ from Hadamards and one CNOT. The truth table should show every input unchanged except ∣11⟩\ket{11}, which picks up a phase of π\pi (a minus sign).

Build it in the circuit builder (gates: H, control, X).

Hints
  1. Which rewrite turns XX into ZZ?
  2. HXH=ZHXH = Z: put HH on the target wire before and after the CNOT.
Show the answer

One circuit that works: H on q1, then CNOT q0→q1, then H on q1.

HH on the target, then the CNOT, then HH on the target: where the control is 1 the target gets HXH=ZHXH = Z, and where it is 0 it gets HH=IHH = I. That is CZ. Because CZ is symmetric, the same circuit upside down (Hadamards on the top wire and a CNOT controlled from the bottom) works too.

Check yourself

What is CZ ∣+⟩∣1⟩\text{CZ}\,\ket+\ket1? (CZ multiplies ∣11⟩\ket{11} by −1-1 and leaves the other basis states alone.)

  1. A∣−⟩∣1⟩\ket-\ket1
  2. B∣+⟩∣1⟩\ket+\ket1, unchanged
  3. C∣+⟩∣0⟩\ket+\ket0
  4. D−∣+⟩∣1⟩-\ket+\ket1
Hints
  1. Write the input as a sum of two basis states.
  2. Only the ∣11⟩\ket{11} term changes sign.
Show the answer

Answer: A. ∣−⟩∣1⟩\ket-\ket1

Written out, the input is 12(∣01⟩+∣11⟩)\tfrac{1}{\sqrt2}(\ket{01} + \ket{11}). CZ changes the sign of ∣11⟩\ket{11} only, giving 12(∣01⟩−∣11⟩)=∣−⟩∣1⟩\tfrac{1}{\sqrt2}(\ket{01} - \ket{11}) = \ket-\ket1. The top qubit changed, so for CZ the words control and target are only labels.

Why not the others

  • B. The top qubit is drawn as the control, but that only means it is never flipped on basis inputs. Here it is in a superposition, and the sign on ∣11⟩\ket{11} lands on its ∣1⟩\ket1 branch, which turns ∣+⟩\ket+ into ∣−⟩\ket- or back.
  • C. CZ never flips a bit: on basis states it only changes signs. The qubit in ∣1⟩\ket1 stays ∣1⟩\ket1.
  • D. The minus sign goes on the ∣11⟩\ket{11} term only, not on both terms. Write the input out: 12(∣01⟩+∣11⟩)\tfrac{1}{\sqrt2}(\ket{01} + \ket{11}), and only the second term changes sign.
Any controlled gate from two CNOTs

Every single-qubit gate can be written as U=eiαAXBXCU = e^{i\alpha}AXBXC, for some angle α\alpha and single-qubit gates AA, BB, CC with ABC=IABC = I. Then controlled-UU is: CC on the target, a CNOT, BB on the target, a CNOT, AA on the target, and the phase gate diag(1,eiα)\text{diag}(1, e^{i\alpha}) on the control. When the control is 0, the CNOTs do nothing and the target gets ABC=IABC = I. When it is 1, the target gets AXBXCAXBXC, and the phase gate supplies eiαe^{i\alpha}. So any controlled single-qubit gate costs at most two CNOTs.

5 / 13 · Explore

The Toffoli gate from T gates

The Toffoli gate from Lesson 6 flips its target when both controls are 1. It acts on three qubits, yet it can be built exactly from one- and two-qubit gates: two HH, six CNOTs and seven gates that are TT or T†T^\dagger, fifteen in all. The two Hadamards play the same trick as in CZ: they turn the Toffoli’s flip of the target into a sign, −1-1 on ∣111⟩\ket{111} only. Every other gate is a CNOT or a TT⁠-type gate, and together they only change phases: the CNOTs flip bits along the way but undo every flip. Step through the circuit on a few inputs, then read the truth table: it lists every basis input with its output, and it would mark any output that picked up a phase.

Check yourself

Count the gates in the circuit above that are TT or T†T^\dagger. How many are there?

Hint
  1. Look for boxes labelled TT or T†T^\dagger on all three wires, not just the target.
Show the answer

Answer: 77

Four TT gates and three T†T^\dagger gates: seven in all. Four of them sit on the target wire, two on bb and one on aa.

Common slips

  • 44: That counts the TT gates only. The T†T^\dagger gates are TT⁠-type too, and they cost the same: there are three of them.
  • 33: That counts the T†T^\dagger gates only. There are also four TT gates.
  • 1515: That counts every gate. Leave out the two HH gates and the six CNOTs.
  • 66: Six is the number of CNOTs. Count the boxes labelled TT or T†T^\dagger.

Check yourself

The truth table shows that the circuit matches the Toffoli gate on all eight basis inputs, phases included. Why is that enough to show that the two are the same gate?

  1. AIt is enough to match the 0/1 outputs; the phases on basis outputs never matter
  2. BIt isn’t enough: the measurement statistics must be compared as well
  3. CIt isn’t enough: every superposition must be tested too
  4. DBoth are linear, so agreeing on every basis state means agreeing on every superposition of them
Show the answer

Answer: D. Both are linear, so agreeing on every basis state means agreeing on every superposition of them

A gate is linear, so its action on ∑xαx∣x⟩\sum_x\alpha_x\ket x is ∑xαxU∣x⟩\sum_x\alpha_x U\ket x. Two gates that give the same outputs on all basis states, phases included, are the same matrix.

Why not the others

  • A. The phases do matter. A circuit that sent ∣111⟩\ket{111} to −∣110⟩-\ket{110} would match the 0/1 outputs, yet on a superposition such as ∣11⟩∣+⟩\ket{11}\ket+ it would give a different state. The truth table marks any row whose output picks up a phase, and here it marks none, which is why the match is exact.
  • B. Measurement statistics follow from the output states. If the two gates give the same state for every input, every statistic agrees too.
  • C. Linearity does that work. A superposition is a sum of basis states, and a linear map sends a sum to the sum of the images, so two gates that agree on the basis agree everywhere.

Why seven? Apart from the two Hadamards, the circuit must multiply ∣abc⟩\ket{abc} by (−1)abc=eiπ abc(-1)^{abc} = e^{i\pi\,abc}. A product such as abcabc is hard to reach with phase gates, but parities are easy. A CNOT can place a parity such as a⊕ca\oplus c on a wire, a TT on that wire then adds the phase π/4\pi/4 when the parity is 1, and a T†T^\dagger subtracts it. The bridge is an identity for bits:

4abc=a+b+c−(a⊕b)−(a⊕c)−(b⊕c)+(a⊕b⊕c)\begin{aligned}4abc = {}&a + b + c - (a\oplus b) - (a\oplus c) \\ &- (b\oplus c) + (a\oplus b\oplus c)\end{aligned}

Multiply by π/4\pi/4: the phase π abc\pi\,abc is a sum of seven parity terms, four with a plus sign (a TT each) and three with a minus sign (a T†T^\dagger each). The six CNOTs bring each parity onto a wire when its gate needs it, and put the wires back afterwards.

Check yourself

Check the identity for a=b=c=1a = b = c = 1: what is a+b+c−(a⊕b)−(a⊕c)−(b⊕c)+(a⊕b⊕c)a + b + c - (a\oplus b) - (a\oplus c) - (b\oplus c) + (a\oplus b\oplus c)?

Hint
  1. 1⊕1=01\oplus1 = 0, and 1⊕1⊕1=11\oplus1\oplus1 = 1.
Show the answer

Answer: 44

1+1+1−0−0−0+1=4=4abc1 + 1 + 1 - 0 - 0 - 0 + 1 = 4 = 4abc. Times π/4\pi/4 that is a phase of π\pi, the minus sign on ∣111⟩\ket{111}. With any bit equal to 0 the seven terms cancel to 0.

Common slips

  • 33: Look at the last term: 1⊕1⊕1=11\oplus1\oplus1 = 1, since three 1s make an odd count. So the sum is 3−0+1=43 - 0 + 1 = 4.
  • 11: That treats 1⊕11\oplus1 as 1, like OR. XOR is addition mod 2, so 1⊕1=01\oplus1 = 0, and the three pair terms vanish.
  • 00: Zero is what the sum gives when at least one bit is 0. With all three bits equal to 1 it is 1+1+1−0−0−0+1=41 + 1 + 1 - 0 - 0 - 0 + 1 = 4.

6 / 13 · Learn

Universal gate sets

Which gates does a quantum computer need? A gate set is universal if circuits made from it can approximate every unitary, on any number of qubits, as closely as you like. Two facts give a small universal set. First, CNOT together with all single-qubit gates builds every unitary exactly. Second, HH and TT alone approximate every single-qubit gate: one product of them is a rotation by an angle that is an irrational fraction of a full turn, so its powers never repeat and come as close as you like to any rotation about its axis, and a second such rotation about another axis does the rest. So {H,T,CNOT}\{H, T, \text{CNOT}\} is universal.

Approximation errors are easy to control, because they add up. If each of mm gates is replaced by one within ϵ\epsilon of it (on every input the two outputs differ by at most ϵ\epsilon), the whole circuit is within mϵm\epsilon of the original.

Check yourself

A circuit has 500 gates. A compiler replaces each one by an approximation within ϵ=0.0001\epsilon = 0.0001. At most how far can the new circuit’s output be from the original’s?

Hint
  1. Errors of approximated gates add up.
Show the answer

Answer: 120\frac{1}{20}

The errors add: 500×0.0001=0.05500\times0.0001 = 0.05. To keep the whole circuit within ϵtotal\epsilon_{\text{total}}, approximate each gate to within ϵtotal/m\epsilon_{\text{total}}/m.

Common slips

  • 00: That is the error of one gate. Each of the 500 gates adds its own, and in the worst case they add up: 500×0.0001500\times0.0001.

Universal does not mean exact. Imagine listing every circuit that can be built from HH, TT and CNOT: first all circuits with one gate, then all with two gates, and so on. There are only finitely many of each length, so every circuit gets a place in the list: the circuits are countable. The gates they are meant to build are not. The rotations Rz(θ)R_z(\theta) about the zz axis alone form a continuum, one for every real angle θ\theta, and no list contains every real number. So most rotations are missing from the list. They can be approached as closely as you like, but never reached. The same argument works for any finite gate set, with or without extra work qubits.

Check yourself

A compiler must turn Rz(1)R_z(1), a rotation by 1 radian about the zz axis, into HH, TT and CNOT gates. What can it deliver?

  1. AA circuit within any error you name, say 10−1010^{-10}, but never an exact one
  2. BNothing useful: a rotation by 1 radian is not one of the reachable gates
  3. CAn exact circuit, provided it may use extra work qubits
  4. DAn exact circuit, if it searches long enough
Show the answer

Answer: A. A circuit within any error you name, say 10−1010^{-10}, but never an exact one

The gate set is universal, so some circuit comes within any ϵ\epsilon you name. None is exact: the circuits are countable, and the entries of their matrices are built from 1/21/\sqrt2 and ii in a way that can never produce eie^{i}.

Why not the others

  • B. Not reachable exactly, but reachable as closely as you like. That is what universal means, and an error of 10−1010^{-10} is as good as exact for any real computation.
  • C. Work qubits make the list of circuits longer but not uncountable, so almost every gate is still missing from it, and Rz(1)R_z(1) is one of them.
  • D. No search can find it, because it doesn’t exist. Circuits from these gates form a countable list, and almost no rotation angle appears in it; 1 radian is one of the missing ones. The search can only get closer.

How many gates does a good approximation take? The Solovay–Kitaev theorem gives a general answer. Take any finite set of single-qubit gates that is universal for one qubit and contains each gate’s inverse. Then every single-qubit gate can be approximated to within ϵ\epsilon by about log⁡c(1/ϵ)\log^c(1/\epsilon) gates from the set, where cc is a fixed constant. The first proofs give cc close to 4, later ones less. You need only the statement: the cost grows like a power of log⁡(1/ϵ)\log(1/\epsilon), so doubling the number of correct digits multiplies the cost by a fixed factor rather than squaring it.

For HH and TT, methods found since then do better still. A rotation about the zz axis needs about 3log⁡2(1/ϵ)3\log_2(1/\epsilon) TT gates, plus cheaper gates such as HH and SS. Each extra decimal digit costs about 3log⁡210≈103\log_2 10 \approx 10 more TT gates.

The rule of thumb 3log⁡2(1/ϵ)3\log_2(1/\epsilon) for one rotation: each factor of 100 in accuracy adds about 20 TT gates, a straight line in the number of digits.

Check yourself

Use the rule of thumb from this lesson: about how many TT gates does it take to approximate a rotation about the zz axis to within ϵ=10−9\epsilon = 10^{-9}?

Hints
  1. The rule of thumb is 3log⁡2(1/ϵ)3\log_2(1/\epsilon).
  2. log⁡2(109)=9log⁡210≈9×3.32\log_2(10^{9}) = 9\log_2 10 \approx 9\times3.32.
Show the answer

Answer: 9090

3log⁡2(109)=3×9×log⁡210≈903\log_2(10^{9}) = 3\times9\times\log_2 10 \approx 90: about ten TT gates for each decimal digit of accuracy. Any answer within a few of 90 is fine, since the rule is approximate.

Common slips

  • 3030: That is log⁡2(1/ϵ)\log_2(1/\epsilon) itself, about 30. The rule of thumb is three times that: 3log⁡2(1/ϵ)≈903\log_2(1/\epsilon) \approx 90.
  • 99: That is the number of decimal digits. Each digit is a factor of 10, and log⁡210≈3.32\log_2 10 \approx 3.32, so the count is about 3×3.32×9≈903\times3.32\times9 \approx 90.

Refresher: Big-O and repeated trials How log⁡n\log n, n\sqrt n, n2n^2 and 2n2^n compare, what big-O notation hides, and why repeating a test makes failure unlikely so fast.

Why no method can do much better

A counting argument gives a floor. With kk gate types there are at most kLk^L circuits of LL gates. To come within ϵ\epsilon of every rotation Rz(θ)R_z(\theta), the circuits must be spread around the whole circle of angles with gaps of order ϵ\epsilon, so you need on the order of 1/ϵ1/\epsilon of them. That forces kLk^L to be at least of order 1/ϵ1/\epsilon, so LL grows at least like log⁡(1/ϵ)\log(1/\epsilon). So the logarithmic growth of 3log⁡2(1/ϵ)3\log_2(1/\epsilon) is the best kind possible, and the only question left is the constant in front. See the refresher for how slowly logarithms grow.

7 / 13 · Learn

What a circuit costs

Two circuits can do the same job at very different prices, and the price can mean several things.

  • Gate count: the total number of gates.
  • Depth: the number of layers, when gates on different qubits run at the same time. It sets the running time.
  • Two-qubit gate count: on today’s machines a CNOT fails roughly ten times as often as a one-qubit gate, so this count often decides whether a run gives usable results.
  • TT⁠-count: on a future error-corrected machine, HH, SS and CNOT are cheap and each TT gate costs many times more. The reason lies in how error-correcting codes work, which is beyond these lessons.

The Toffoli circuit scores 15 gates, 6 two-qubit gates, TT⁠-count 7 and depth 11. Which number matters depends on the machine. The mistake is to think that the gate count alone tells you.

Check yourself

On a noisy machine each CNOT fails with probability about 1% and each one-qubit gate with probability about 0.1%. Circuit A has 60 gates, 8 of them CNOTs. Circuit B has 45 gates, 30 of them CNOTs. Both compute the same thing. Which is more likely to run without any gate failing?

  1. ACircuit A
  2. BCircuit B, because it has fewer gates
  3. CAbout equally likely, since they compute the same thing
  4. DYou can’t tell without the depth of each circuit
Show the answer

Answer: A. Circuit A

Add up the chances of failure: A about 52×0.1%+8×1%≈13%52\times0.1\% + 8\times1\% \approx 13\%, B about 15×0.1%+30×1%≈32%15\times0.1\% + 30\times1\% \approx 32\%. A runs cleanly about 88% of the time and B about 73%.

Why not the others

  • B. B has fewer gates but far more CNOTs, and the CNOTs carry most of the risk. Expected failures: A about 52×0.001+8×0.01≈0.1352\times0.001 + 8\times0.01 \approx 0.13, B about 15×0.001+30×0.01≈0.3215\times0.001 + 30\times0.01 \approx 0.32.
  • C. What the circuit computes doesn’t set its error rate; its gates do. B uses 22 more CNOTs, each ten times riskier than a one-qubit gate.
  • D. Depth matters for how long the qubits must hold their state, but here every gate has its own chance to fail, and the gate counts already settle it: B’s 30 CNOTs dominate.

Depth needs a little care. Put each gate in the earliest layer that comes after every earlier gate on its own wires; the number of layers is the depth. A CNOT ties its two wires together, so a gate after it on either wire must wait for it.

Depth can often be cut without changing what the circuit does, by moving a gate past gates it commutes with. Three rules cover most cases. Gates on different wires always commute. A gate that is diagonal in the 0/1 basis, such as ZZ, SS or TT, commutes with a CNOT when it sits on the control, because the CNOT only reads the control’s 0/1 value. An XX commutes with a CNOT when it sits on the target, because the CNOT applies either nothing or XX there.

The same five gates, twice. As written, the TT on q0q_0 waits for the CNOT and needs a fourth layer. A TT on a control commutes with the CNOT, so it can move to the front and share layer 1: depth 3.

Check yourself

The circuit above has depth 5. Which single change lowers the depth without changing what the circuit does?

  1. AMove the XX on q1q_1 to the very start
  2. BMove the TT on q0q_0 to just after the CNOT
  3. CMove the first HH on q0q_0 to the end of its wire
  4. DNothing: five gates take five layers
Show the answer

Answer: A. Move the XX on q1q_1 to the very start

An XX on the target commutes with the CNOT, and it commutes with the gates on q0q_0, so it can move to the start and share layer 1 with the first HH. The layers become HH and XX, then TT, then HH, then the CNOT: depth 4. The chain H,T,HH, T, H on q0q_0 can’t be reordered, since TT and HH don’t commute.

Why not the others

  • B. A TT on the control would commute with the CNOT, but to get there it must pass the HH after it, and TT doesn’t commute with HH. That move changes the circuit.
  • C. HH commutes neither with TT nor with a CNOT on its control, so moving it changes what the circuit does.
  • D. Gates on different wires can share a layer. The XX on q1q_1 sits on the CNOT’s target, where it commutes with the CNOT, so it can move to the front and run alongside the first HH.

Check yourself

A circuit on three qubits applies these gates, in this order:

  1. HH on q0q_0
  2. a CNOT with control q0q_0 and target q1q_1
  3. TT on q1q_1
  4. HH on q2q_2
  5. a CNOT with control q1q_1 and target q2q_2
  6. XX on q0q_0

Gates on different qubits can run in the same layer. What is the depth of the circuit as written, without moving any gate past another?

Hints
  1. Put each gate in the earliest layer that comes after every earlier gate on its wires.
  2. A CNOT uses two wires: it waits for both, and later gates on either wire wait for it.
Show the answer

Answer: 44

The layers of gates 1 to 6 are 1, 2, 3, 1, 4, 3, so the depth is 4. The CNOTs pass the delay from one wire to another, which is why the depth exceeds the 3 gates of the busiest wire.

Place the gates one at a time, each in the first layer after every earlier gate on its wires:

  1. HH on q0q_0: layer 1 (nothing earlier on its wires)
  2. a CNOT with control q0q_0 and target q1q_1: layer 2 (after gate 1)
  3. TT on q1q_1: layer 3 (after gate 2)
  4. HH on q2q_2: layer 1 (nothing earlier on its wires)
  5. a CNOT with control q1q_1 and target q2q_2: layer 4 (after gate 3)
  6. XX on q0q_0: layer 3 (after gate 2)

The deepest layer is 4, so the depth is 4.

Common slips

  • 66: That counts the gates. Gates on different wires can share a layer, so the depth is smaller than the gate count here.
  • 33: That is the number of gates on the busiest wire. But a CNOT ties two wires together: a gate after it must wait for the gates before it on both wires. Follow the layers through the CNOTs.

8 / 13 · Learn

Many controls

Many algorithms need a NOT with kk controls, which flips the target only when all kk controls are 1; Grover search, later in this part, needs one in every round. For k=2k = 2 it is the Toffoli. For larger kk, build it from Toffolis and k−1k - 1 extra work qubits that start in ∣0⟩\ket0:

  1. Compute. A Toffoli writes c1c2c_1c_2 into w1w_1. The next writes w1c3=c1c2c3w_1c_3 = c_1c_2c_3 into w2w_2, and so on, until wk−1w_{k-1} holds the AND of all kk controls.
  2. Act. A CNOT from wk−1w_{k-1} flips the target when that AND is 1.
  3. Uncompute. The Toffolis of step 1 run again in reverse order. Each one is its own inverse, so every work qubit returns to ∣0⟩\ket0.

In all: 2(k−1)2(k-1) Toffolis, one CNOT and k−1k - 1 work qubits.

A NOT with three controls. Two Toffolis compute c1c2c3c_1c_2c_3 into w2w_2, by way of w1w_1; a CNOT flips the target when w2w_2 is 1; two more Toffolis return both work qubits to ∣0⟩\ket0.

Check yourself

Why run the Toffolis again at the end, instead of leaving the work qubits as they are?

  1. ABecause a Toffoli gate only works when it comes in a pair
  2. BTo undo the flip of the target, so the gate can be reversed
  3. CThere is no real need: the work qubits can simply be ignored
  4. DLeft alone, the work qubits would stay entangled with the controls and spoil later interference; uncomputing returns them to ∣0⟩\ket0
Show the answer

Answer: D. Left alone, the work qubits would stay entangled with the controls and spoil later interference; uncomputing returns them to ∣0⟩\ket0

After the compute half, w1w_1 holds c1c2c_1c_2, and so on. With the controls in superposition that is entanglement between the work qubits and the controls, which ruins interference later in the algorithm. Running the Toffolis backwards resets every work qubit to ∣0⟩\ket0 while the target keeps its flip. Lesson 13 is about this compute, act, uncompute pattern.

Why not the others

  • A. A single Toffoli is a perfectly good gate; the compute half uses them one at a time. The second copies are there to clean up, not to complete the first ones.
  • B. The uncompute Toffolis never touch the target: they read the controls and work qubits and write only to work qubits. The target’s flip, made by the middle CNOT, stays.
  • C. Ignoring them doesn’t make them go away. If the controls are in a superposition, w1w_1 ends up holding c1c2c_1c_2 in each branch, so branches that differ in c1c2c_1c_2 also differ on w1w_1 and can no longer interfere. The rest of the circuit then behaves as if the controls had been measured.

Check yourself

With this construction, how many work qubits does a NOT with 6 controls need?

Hint
  1. How many Toffolis does the compute half use, and how many new work qubits does each one write to?
Show the answer

Answer: 55

w1w_1 holds c1c2c_1c_2, and each of the controls c3,…,c6c_3, \ldots, c_6 needs one more work qubit: k−1=5k - 1 = 5.

Common slips

  • 66: One per control is one too many. The first Toffoli combines two controls at once, so k−1=5k - 1 = 5 work qubits are enough.
  • 1010: That is the number of Toffolis, 2(k−1)2(k-1). The uncompute half reuses the same work qubits, so there are only k−1=5k - 1 = 5.
A cheaper version

The target itself can stand in for the last work qubit. Compute c1c2⋯ck−1c_1c_2\cdots c_{k-1} into k−2k - 2 work qubits, let one Toffoli with controls ckc_k and the last work qubit flip the target directly, and uncompute. That uses 2k−32k - 3 Toffolis and k−2k - 2 work qubits: for k=3k = 3, three Toffolis and a single work qubit. Cleverer constructions need fewer work qubits still, at the price of more gates.

9 / 13 · Worked example

Worked example: pricing a many-control NOT

Each Toffoli is replaced by the 15-gate circuit: two HH, six CNOTs and seven TT⁠-type gates.

Worked example

A NOT with three controls

Use the construction above with k=3k = 3 controls. First the example is worked for you; then you do k=5k = 5.

  1. Parts

    2(k−1)=42(k-1) = 4 Toffolis and one CNOT, with k−1=2k - 1 = 2 work qubits. With three controls and one target, that makes 3+2+1=63 + 2 + 1 = 6 qubits.

  2. T-count

    Each Toffoli brings seven TT⁠-type gates and the middle CNOT none: 4×7=284\times7 = 28.

  3. CNOT count

    Each Toffoli brings six CNOTs, and there is one more in the middle: 4×6+1=254\times6 + 1 = 25.

  4. Your turn: Toffolis

    Now take k=5k = 5 controls.

    Your turn

    A NOT with 5 controls is built as in this lesson: Toffolis compute the AND of the controls into work qubits, a CNOT flips the target, and the Toffolis run again to uncompute. How many Toffoli gates does it use?

    Hints
    1. Compute uses k−1k - 1 Toffolis, act uses one CNOT, uncompute repeats the k−1k - 1 Toffolis. There are k−1k - 1 work qubits.
    2. Each Toffoli circuit has six CNOTs and seven TT⁠-type gates.
    Show the answer

    Answer: 88

    k−1=4k - 1 = 4 Toffolis compute the AND and the same number uncompute it: 2(k−1)=82(k-1) = 8.

    Common slips

    • 44: That is the compute half only. The work qubits must return to ∣0⟩\ket0, so the same Toffolis run again in reverse: 2(k−1)=82(k-1) = 8.
    • 55: One Toffoli per control is one too many for the compute half (the first Toffoli takes two controls at once), and the uncompute half doubles it: 2(k−1)=82(k-1) = 8.
  5. Your turn: T-count

    Your turn

    A NOT with 5 controls is built as in this lesson: Toffolis compute the AND of the controls into work qubits, a CNOT flips the target, and the Toffolis run again to uncompute, and each Toffoli is replaced by the 15-gate circuit. How many TT⁠-type gates (TT or T†T^\dagger) does it use?

    Hints
    1. Compute uses k−1k - 1 Toffolis, act uses one CNOT, uncompute repeats the k−1k - 1 Toffolis. There are k−1k - 1 work qubits.
    2. Each Toffoli circuit has six CNOTs and seven TT⁠-type gates.
    Show the answer

    Answer: 5656

    There are 2(k−1)=82(k-1) = 8 Toffolis with seven TT⁠-type gates each, and the middle CNOT has none: 7×8=567\times8 = 56.

    Common slips

    • 2828: That is the compute half only. The work qubits must return to ∣0⟩\ket0, so the same Toffolis run again in reverse, which doubles the count: 7×8=567\times8 = 56.
    • 3232: That counts only the TT gates. Each Toffoli also has three T†T^\dagger gates, which cost the same: seven TT⁠-type gates per Toffoli.
    • 88: That is the number of Toffolis, 8. Each one is replaced by a circuit with seven TT⁠-type gates.

One more, fully worked, with k=4k = 4.

Another worked example

Another example: four controls

  1. Parts

    2(k−1)=62(k-1) = 6 Toffolis and one CNOT, with k−1=3k - 1 = 3 work qubits: 4+3+1=84 + 3 + 1 = 8 qubits in all.

  2. T-count

    6×7=426\times7 = 42 TT⁠-type gates.

  3. CNOT count

    6×6+1=376\times6 + 1 = 37 CNOTs. Compare the cheaper version: 2k−3=52k - 3 = 5 Toffolis, so TT⁠-count 35 and 30 CNOTs, with two work qubits.

Check yourself

A NOT with 8 controls is built as in this lesson: Toffolis compute the AND of the controls into work qubits, a CNOT flips the target, and the Toffolis run again to uncompute. How many qubits in all, counting the controls, the work qubits and the target does it use?

Hints
  1. Compute uses k−1k - 1 Toffolis, act uses one CNOT, uncompute repeats the k−1k - 1 Toffolis. There are k−1k - 1 work qubits.
  2. Each Toffoli circuit has six CNOTs and seven TT⁠-type gates.
Show the answer

Answer: 1616

8 controls, k−1=7k - 1 = 7 work qubits and one target: 8+7+1=168 + 7 + 1 = 16.

Common slips

  • 99: That leaves out the 7 work qubits.
  • 1515: Count again: 8 controls, 7 work qubits and one target.

10 / 13 · Practice

Practice

Three items. The numbered ones change when they come back. Hints are there if you need them.

Check yourself

A circuit on three qubits applies these gates, in this order:

  1. HH on q2q_2
  2. a CNOT with control q2q_2 and target q1q_1
  3. HH on q2q_2
  4. a CNOT with control q0q_0 and target q1q_1
  5. TT on q1q_1
  6. HH on q1q_1

Gates on different qubits can run in the same layer. What is the depth of the circuit as written, without moving any gate past another?

Hints
  1. Put each gate in the earliest layer that comes after every earlier gate on its wires.
  2. A CNOT uses two wires: it waits for both, and later gates on either wire wait for it.
Show the answer

Answer: 55

The layers of gates 1 to 6 are 1, 2, 3, 3, 4, 5, so the depth is 5. The CNOTs pass the delay from one wire to another, which is why the depth exceeds the 4 gates of the busiest wire.

Place the gates one at a time, each in the first layer after every earlier gate on its wires:

  1. HH on q2q_2: layer 1 (nothing earlier on its wires)
  2. a CNOT with control q2q_2 and target q1q_1: layer 2 (after gate 1)
  3. HH on q2q_2: layer 3 (after gate 2)
  4. a CNOT with control q0q_0 and target q1q_1: layer 3 (after gate 2)
  5. TT on q1q_1: layer 4 (after gate 4)
  6. HH on q1q_1: layer 5 (after gate 5)

The deepest layer is 5, so the depth is 5.

Common slips

  • 66: That counts the gates. Gates on different wires can share a layer, so the depth is smaller than the gate count here.
  • 44: That is the number of gates on the busiest wire. But a CNOT ties two wires together: a gate after it must wait for the gates before it on both wires. Follow the layers through the CNOTs.

Check yourself

Now the other way round: build a CNOT with its control on top from Hadamards and one controlled-ZZ. A dot and a ZZ in the same column make a CZ.

Build it in the circuit builder (gates: H, control, Z).

Hints
  1. Which rewrite turns ZZ into XX?
  2. HZH=XHZH = X: put HH on the bottom wire before and after the CZ.
Show the answer

One circuit that works: H on q1, then CZ q0→q1, then H on q1.

HZH=XHZH = X, so Hadamards on the bottom wire around a CZ give a CNOT whose target is the bottom wire. CZ is symmetric, so it doesn’t matter which wire gets the dot and which the ZZ.

Check yourself

A circuit contains 5 Toffoli gates and, besides them, 9 TT gates and 2 CNOTs. You replace every Toffoli with the 15-gate circuit from this lesson. What is the TT⁠-count (the number of TT and T†T^\dagger gates) of the new circuit?

Hints
  1. How many CNOTs and how many TT⁠-type gates are in the 15-gate Toffoli circuit?
  2. It has two HH, six CNOTs, and seven gates that are TT or T†T^\dagger.
Show the answer

Answer: 4444

Each Toffoli brings seven TT⁠-type gates: 7×5+9=447\times5 + 9 = 44.

Common slips

  • 2929: That counts only the TT gates in each Toffoli. The three T†T^\dagger gates cost the same as TT gates, so each Toffoli adds seven.
  • 3535: That counts the Toffolis’ TT⁠-type gates but leaves out the 9 TT gates the circuit already had.
  • 8484: That counts every gate in each Toffoli circuit. Only seven of the fifteen are TT or T†T^\dagger; the rest are two HH and six CNOTs.

11 / 13 · Explain

Explain it

Explain it

The set {H,T,CNOT}\{H, T, \text{CNOT}\} is called universal, yet no circuit made from it equals the rotation Rz(1)R_z(1) exactly. Explain what universal means here, and why no finite gate set can build every gate exactly.

Show a model answer

Universal means approximately: for any unitary and any accuracy ϵ>0\epsilon > 0 there is a circuit from the set within ϵ\epsilon of it, and for one qubit the number of gates grows only like a power of log⁡(1/ϵ)\log(1/\epsilon); modern methods need about 3log⁡2(1/ϵ)3\log_2(1/\epsilon) TT gates per rotation. Exactness is impossible by counting. With finitely many gate types there are finitely many circuits of each length, so all circuits together form a countable list. The gates form a continuum, already the rotations Rz(θ)R_z(\theta) with one for every real θ\theta, and no countable list covers a continuum. So almost every gate, Rz(1)R_z(1) among them, can be approached but never reached.

A good answer covers

  • Universal means every unitary can be approximated to any accuracy, not built exactly
  • Circuits from a finite gate set can be listed by length, so there are countably many
  • Gates form a continuum (for example Rz(θ)R_z(\theta) for every real θ\theta), so most are missing from the list
  • The cost of accuracy grows slowly, like a power of log⁡(1/ϵ)\log(1/\epsilon)

12 / 13 · Mastery check

Mastery check

Check yourself

A quantum computer offers only the gates HH, TT and CNOT, with as many extra work qubits as you like, and you may use as many gates as you need. Can circuits made from them build every single-qubit gate exactly?

  1. AYes: with enough gates, each of them is built exactly
  2. BNo, but each one can be approximated as closely as you like
  3. CNo: the circuits reach only a few gates, such as XX, ZZ and SS
  4. DNot even approximately: most gates are out of reach
Show the answer

Answer: B. No, but each one can be approximated as closely as you like

Circuits from HH, TT and CNOT, with as many extra work qubits as you like, can be listed by length, so there are only countably many, while every single-qubit gate form a continuum. So the answer is no, but the set is universal: every gate can be approximated to any accuracy, at a cost that grows slowly with the accuracy.

Why not the others

  • A. Universal means approximately. Circuits from a finite gate set can be listed, first those with one gate, then those with two, and so on: there are countably many. The gates you want form a continuum, which no list can cover. So most of them can be approached as closely as you like, but never reached.
  • C. They reach infinitely many. One product of HH and TT is a rotation by an angle that is an irrational fraction of a full turn, so its powers never repeat and come close to every rotation about its axis.
  • D. Every gate can be approximated to any accuracy you name. What fails is exactness: there are only countably many circuits, so most gates are approached, never reached.

Check yourself

A circuit applies HH, then TT, then XX to one qubit in the state ∣ψ⟩\ket\psi. Which product of matrices equals the whole circuit?

  1. AXTHXTH
  2. BH⊗T⊗XH\otimes T\otimes X
  3. CH+T+XH + T + X
  4. DHTXHTX
Hint
  1. Which gate acts on ∣ψ⟩\ket\psi first, and where must it stand in the product?
Show the answer

Answer: A. XTHXTH

HH acts first, so it sits next to the ket: X(T(H∣ψ⟩))=XTH∣ψ⟩X\big(T(H\ket\psi)\big) = XTH\ket\psi. Time runs left to right in the circuit and right to left in the product.

Why not the others

  • B. A tensor product describes gates side by side on different qubits, and this one would be an 8×88\times8 matrix. Gates one after another on the same qubit multiply.
  • C. Gates in sequence compose: each acts on the output of the one before, which makes a product, not a sum. A sum of unitary matrices is usually not even unitary.
  • D. That lists the gates in circuit order. The first gate acts on ∣ψ⟩\ket\psi first, so it stands next to the ket: X(T(H∣ψ⟩))=XTH∣ψ⟩X\big(T(H\ket\psi)\big) = XTH\ket\psi. And the order matters here, since HTX≠XTHHTX \ne XTH.

Check yourself

Which single gate equals the matrix product SYS†SYS^\dagger, up to an overall phase?

  1. AXX
  2. BYY
  3. CZZ
  4. DII, the identity
Hint
  1. Apply the product to ∣0⟩\ket0 and to ∣1⟩\ket1, one gate at a time, starting from the right.
Show the answer

Answer: A. XX

On basis states: SYS†∣0⟩=S(i∣1⟩)=−∣1⟩SYS^\dagger\ket0 = S(i\ket1) = -\ket1 and SYS†∣1⟩=SY(−i∣1⟩)=−iS(−i∣0⟩)=−∣0⟩SYS^\dagger\ket1 = SY(-i\ket1) = -iS(-i\ket0) = -\ket0. That is −X-X, which is XX up to an overall sign.

Why not the others

  • B. SS and S†S^\dagger don’t cancel: YY sits between them, and SS doesn’t commute with YY. SS turns the Bloch sphere a quarter turn about the zz axis, which carries the yy axis onto the −x-x axis.
  • C. The product flips bits, which ZZ never does: SYS†∣0⟩=SY∣0⟩=S(i∣1⟩)=−∣1⟩SYS^\dagger\ket0 = SY\ket0 = S(i\ket1) = -\ket1.
  • D. Only adjacent SS and S†S^\dagger cancel. Follow ∣0⟩\ket0: SYS†∣0⟩=SY∣0⟩=S(i∣1⟩)=−∣1⟩SYS^\dagger\ket0 = SY\ket0 = S(i\ket1) = -\ket1. The product flips bits.

Check yourself

Two circuits compute the same thing. Circuit A has 5,000 gates, 400 of them TT gates. Circuit B has 3,000 gates, 1,500 of them TT gates. All the other gates are HH, SS or CNOT. Both will run on an error-corrected quantum computer. Which is likely to be cheaper to run?

  1. ACircuit A
  2. BCircuit B
  3. CAbout the same, since they compute the same thing
  4. DThere is no way to tell without running both
Show the answer

Answer: A. Circuit A

On an error-corrected machine HH, SS and CNOT are cheap and TT gates are expensive, so the TT⁠-count decides. Circuit A has 400 TT gates against 1,500. Even at a modest ten to one, it costs about 8,600 units against 16,500, despite having more gates.

Why not the others

  • B. Circuit B has fewer gates, but 1,500 of them are TT gates, and on an error-corrected machine the TT gates dominate the cost. Even if a TT gate cost only ten times as much as each other gate, B would come to about 16,500 units against 8,600.
  • C. Cost belongs to the circuit, not to the job it does: two circuits for the same unitary can differ a lot. Here they differ in the gate that dominates the cost on an error-corrected machine, the TT gate.
  • D. The counts are enough for a good estimate. On an error-corrected machine HH, SS and CNOT are cheap and each TT gate costs many times more, so compare the TT⁠-counts.

Check yourself

What is CZ ∣−⟩∣1⟩\text{CZ}\,\ket-\ket1? (CZ multiplies ∣11⟩\ket{11} by −1-1 and leaves the other basis states alone.)

  1. A∣+⟩∣1⟩\ket+\ket1
  2. B∣−⟩∣1⟩\ket-\ket1, unchanged
  3. C∣−⟩∣0⟩\ket-\ket0
  4. D−∣−⟩∣1⟩-\ket-\ket1
Hints
  1. Write the input as a sum of two basis states.
  2. Only the ∣11⟩\ket{11} term changes sign.
Show the answer

Answer: A. ∣+⟩∣1⟩\ket+\ket1

Written out, the input is 12(∣01⟩−∣11⟩)\tfrac{1}{\sqrt2}(\ket{01} - \ket{11}). CZ changes the sign of ∣11⟩\ket{11} only, giving 12(∣01⟩+∣11⟩)=∣+⟩∣1⟩\tfrac{1}{\sqrt2}(\ket{01} + \ket{11}) = \ket+\ket1. The top qubit changed, so for CZ the words control and target are only labels.

Why not the others

  • B. The top qubit is drawn as the control, but that only means it is never flipped on basis inputs. Here it is in a superposition, and the sign on ∣11⟩\ket{11} lands on its ∣1⟩\ket1 branch, which turns ∣+⟩\ket+ into ∣−⟩\ket- or back.
  • C. CZ never flips a bit: on basis states it only changes signs. The qubit in ∣1⟩\ket1 stays ∣1⟩\ket1.
  • D. The minus sign goes on the ∣11⟩\ket{11} term only, not on both terms. Write the input out: 12(∣01⟩−∣11⟩)\tfrac{1}{\sqrt2}(\ket{01} - \ket{11}), and only the second term changes sign.

Check yourself

A circuit contains 3 Toffoli gates and, besides them, 10 TT gates and 11 CNOTs. You replace every Toffoli with the 15-gate circuit from this lesson. What is the TT⁠-count (the number of TT and T†T^\dagger gates) of the new circuit?

Hints
  1. How many CNOTs and how many TT⁠-type gates are in the 15-gate Toffoli circuit?
  2. It has two HH, six CNOTs, and seven gates that are TT or T†T^\dagger.
Show the answer

Answer: 3131

Each Toffoli brings seven TT⁠-type gates: 7×3+10=317\times3 + 10 = 31.

Common slips

  • 2222: That counts only the TT gates in each Toffoli. The three T†T^\dagger gates cost the same as TT gates, so each Toffoli adds seven.
  • 2121: That counts the Toffolis’ TT⁠-type gates but leaves out the 10 TT gates the circuit already had.
  • 5555: That counts every gate in each Toffoli circuit. Only seven of the fifteen are TT or T†T^\dagger; the rest are two HH and six CNOTs.

Check yourself

A circuit on three qubits applies these gates, in this order:

  1. TT on q1q_1
  2. TT on q1q_1
  3. a CNOT with control q2q_2 and target q0q_0
  4. a CNOT with control q1q_1 and target q0q_0
  5. a CNOT with control q2q_2 and target q1q_1
  6. HH on q2q_2

Gates on different qubits can run in the same layer. What is the depth of the circuit as written, without moving any gate past another?

Hints
  1. Put each gate in the earliest layer that comes after every earlier gate on its wires.
  2. A CNOT uses two wires: it waits for both, and later gates on either wire wait for it.
Show the answer

Answer: 55

The layers of gates 1 to 6 are 1, 2, 1, 3, 4, 5, so the depth is 5. The CNOTs pass the delay from one wire to another, which is why the depth exceeds the 4 gates of the busiest wire.

Place the gates one at a time, each in the first layer after every earlier gate on its wires:

  1. TT on q1q_1: layer 1 (nothing earlier on its wires)
  2. TT on q1q_1: layer 2 (after gate 1)
  3. a CNOT with control q2q_2 and target q0q_0: layer 1 (nothing earlier on its wires)
  4. a CNOT with control q1q_1 and target q0q_0: layer 3 (after gate 2)
  5. a CNOT with control q2q_2 and target q1q_1: layer 4 (after gate 4)
  6. HH on q2q_2: layer 5 (after gate 5)

The deepest layer is 5, so the depth is 5.

Common slips

  • 66: That counts the gates. Gates on different wires can share a layer, so the depth is smaller than the gate count here.
  • 44: That is the number of gates on the busiest wire. But a CNOT ties two wires together: a gate after it must wait for the gates before it on both wires. Follow the layers through the CNOTs.

Check yourself

A NOT with 6 controls is built as in this lesson: Toffolis compute the AND of the controls into work qubits, a CNOT flips the target, and the Toffolis run again to uncompute, and each Toffoli is replaced by the 15-gate circuit. How many TT⁠-type gates (TT or T†T^\dagger) does it use?

Hints
  1. Compute uses k−1k - 1 Toffolis, act uses one CNOT, uncompute repeats the k−1k - 1 Toffolis. There are k−1k - 1 work qubits.
  2. Each Toffoli circuit has six CNOTs and seven TT⁠-type gates.
Show the answer

Answer: 7070

There are 2(k−1)=102(k-1) = 10 Toffolis with seven TT⁠-type gates each, and the middle CNOT has none: 7×10=707\times10 = 70.

Common slips

  • 3535: That is the compute half only. The work qubits must return to ∣0⟩\ket0, so the same Toffolis run again in reverse, which doubles the count: 7×10=707\times10 = 70.
  • 4040: That counts only the TT gates. Each Toffoli also has three T†T^\dagger gates, which cost the same: seven TT⁠-type gates per Toffoli.
  • 1010: That is the number of Toffolis, 10. Each one is replaced by a circuit with seven TT⁠-type gates.

13 / 13 · Recap

Recap

From memory

Before reading the summary, write down what you remember. Pulling it back from memory is what makes it last.

  1. A circuit applies AA, then BB, then CC. Write it as a matrix product, and state the four rewrites from this lesson.

    Show the key points
    • CBACBA: the first gate stands next to the ket
    • HXH=ZHXH = Z and HZH=XHZH = X
    • Hadamards on both qubits turn a CNOT upside down
    • XX before and after the control makes an anti-control
  2. How is the Toffoli gate built from one- and two-qubit gates, and why does it need seven TT⁠-type gates?

    Show the key points
    • HH on the target turns the flip into the sign (−1)abc(-1)^{abc}
    • 4abc4abc is a signed sum of seven parities, each a phase of ±π/4\pm\pi/4: four TT, three T†T^\dagger
    • Six CNOTs place the parities; 15 gates in all
  3. What does it mean that {H,T,CNOT}\{H, T, \text{CNOT}\} is universal, and how do you price a circuit?

    Show the key points
    • Any unitary to any accuracy, but not exactly: countably many circuits, a continuum of gates
    • About 3log⁡2(1/ϵ)3\log_2(1/\epsilon) TT gates per rotation; Solovay–Kitaev: a power of log⁡(1/ϵ)\log(1/\epsilon)
    • Gate count, depth, two-qubit count, TT⁠-count; move commuting gates to cut depth
    • A kk⁠-control NOT: 2(k−1)2(k-1) Toffolis and k−1k - 1 work qubits, compute, act, uncompute

Key ideas

  1. A circuit is a program whose matrix is the product of its gates in reverse order: AA, then BB, then CC is CBACBA.

  2. HXH=ZHXH = Z, HZH=XHZH = X; Hadamards on both qubits reverse a CNOT; XX around a control gives an anti-control. CZ is a CNOT with HH on the target, and it is symmetric.

  3. The Toffoli gate is exactly two HH, six CNOTs and seven TT⁠-type gates.

  4. {H,T,CNOT}\{H, T, \text{CNOT}\} is universal: it approximates any unitary to any accuracy, never all of them exactly. A rotation costs about 3log⁡2(1/ϵ)3\log_2(1/\epsilon) TT gates.

  5. Gate count, depth, two-qubit count and TT⁠-count measure different costs. Moving gates that commute can cut the depth.

  6. A NOT with kk controls takes 2(k−1)2(k-1) Toffolis and k−1k - 1 work qubits: compute, act, uncompute.

Go further

Free resources for when you want more than this lesson covers.

Math refreshers

The toolkit topics this lesson leans on.

Start the interactive lesson