Lesson 11: Circuits as programs
Universal gate sets, controlled gates, and what a circuit costs
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- and other controlled gates from CNOT and single-qubit gates;
- check the 15-gate Toffoli circuit and say why it needs seven -type gates;
- say what it means that is universal, and why it can’t mean exact;
- count a circuit’s gates, depth, two-qubit gates and 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 , and CNOT and you may use as many gates as you need. Can circuits made from them build every single-qubit gate exactly?
- AYes: with enough gates, each of them is built exactly
- BNo, but each one can be approximated as closely as you like
- CNo: the circuits reach only a few gates, such as , and
- 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 , 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 and 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 gates. Circuit B has 1,200 gates, 900 of them gates. All the other gates are , or CNOT. Both will run on an error-corrected quantum computer. Which is likely to be cheaper to run?
- ACircuit A
- BCircuit B
- CAbout the same, since they compute the same thing
- DThere is no way to tell without running both
Show the answer
Answer: A. Circuit A
On an error-corrected machine , and CNOT are cheap and gates are expensive, so the -count decides. Circuit A has 300 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 gates, and on an error-corrected machine the gates dominate the cost. Even if a 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 gate.
- D. The counts are enough for a good estimate. On an error-corrected machine , and CNOT are cheap and each gate costs many times more, so compare the -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 , then , then to , the state becomes
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, , is the factor next to the ket.
Check yourself
A circuit applies , then , then to one qubit in the state . Which product of matrices equals the whole circuit?
- A
- B
- C
- D
Hint
- Which gate acts on first, and where must it stand in the product?
Show the answer
Answer: C.
acts first, so it sits next to the ket: . 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 first, so it stands next to the ket: . And the order matters here, since .
- 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 matrix. Gates one after another on the same qubit multiply.
Two more gates join , , and . They are phase gates: each leaves alone and multiplies by a phase.
On the Bloch sphere, is a quarter turn about the axis and an eighth of a turn. Phases multiply, so two gates make an , since , and two gates make a . The inverses and carry the phases and . 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
. What is the smallest number of gates in a row (at least one) whose product equals exactly?
Hints
- leaves alone and multiplies by . What does a row of of them do to ?
- multiplies by . Which phase does put on ?
Show the answer
Answer:
leaves alone and multiplies by . puts the phase on , so and .
Common slips
- : Two gates give the phase : that is . needs , twice that phase.
- : Eight gates give the phase : that is the identity. 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: and . They say that swaps the and 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: on both qubits, then a CNOT with its control on top, then on both qubits again.
Predict, then see
The circuit is , with the CNOT’s control on the top qubit. The input is : top qubit 0, bottom qubit 1. What comes out?
- A: the control is 0, so the CNOT does nothing and the Hadamards cancel
- BA random basis state: the Hadamards spread the state out
- C: the top qubit flips
- D: 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. : the top qubit flips
After the first Hadamards the input is . The CNOT flips the bottom qubit in the branch where the top is 1, and flipping gives , so the top qubit’s branch gains a minus sign: becomes . The last Hadamards turn that into . 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 , not , and a flip of 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 , and : flipping only changes a sign.
Here are the four rewrites to know by heart. Each one holds exactly, as an equation between matrices.
- and : 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 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.
H X H = Z
H Z H = X
Hadamards reverse a CNOT
X around a control
Four rewrites. In each panel the circuit on the left equals the one on the right exactly, with no leftover phase: , , 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 gates and one CNOT. A control dot and an 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
- Which rewrite turns a control into an anti-control?
- Put an 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.
on the top wire turns a 0 into a 1, so the CNOT fires; the second restores the top qubit. Another answer works too: a CNOT followed by an on the bottom wire flips the bottom qubit times, which is once exactly when .
4 / 13 · Learn
Controlled gates
CNOT is one member of a family. For any single-qubit gate , controlled- applies to the target in the branch where the control is and does nothing where it is :
CNOT is controlled-. Controlled-, written CZ, leaves , and alone and multiplies by . That sign belongs to both qubits equally, so CZ has no real control and target. You can build it with a rewrite: put on the target before and after a CNOT. Where the control is 1, the target then gets ; where it is 0, it gets .
Check yourself
Build controlled- from Hadamards and one CNOT. The truth table should show every input unchanged except , which picks up a phase of (a minus sign).
Build it in the circuit builder (gates: H, control, X).
Hints
- Which rewrite turns into ?
- : put 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.
on the target, then the CNOT, then on the target: where the control is 1 the target gets , and where it is 0 it gets . 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 multiplies by and leaves the other basis states alone.)
- A
- B, unchanged
- C
- D
Hints
- Write the input as a sum of two basis states.
- Only the term changes sign.
Show the answer
Answer: A.
Written out, the input is . CZ changes the sign of only, giving . 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 lands on its branch, which turns into or back.
- C. CZ never flips a bit: on basis states it only changes signs. The qubit in stays .
- D. The minus sign goes on the term only, not on both terms. Write the input out: , and only the second term changes sign.
Any controlled gate from two CNOTs
Every single-qubit gate can be written as , for some angle and single-qubit gates , , with . Then controlled- is: on the target, a CNOT, on the target, a CNOT, on the target, and the phase gate on the control. When the control is 0, the CNOTs do nothing and the target gets . When it is 1, the target gets , and the phase gate supplies . 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 , six CNOTs and seven gates that are or , 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, on only. Every other gate is a CNOT or a -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 or . How many are there?
Hint
- Look for boxes labelled or on all three wires, not just the target.
Show the answer
Answer:
Four gates and three gates: seven in all. Four of them sit on the target wire, two on and one on .
Common slips
- : That counts the gates only. The gates are -type too, and they cost the same: there are three of them.
- : That counts the gates only. There are also four gates.
- : That counts every gate. Leave out the two gates and the six CNOTs.
- : Six is the number of CNOTs. Count the boxes labelled or .
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?
- AIt is enough to match the 0/1 outputs; the phases on basis outputs never matter
- BIt isn’t enough: the measurement statistics must be compared as well
- CIt isn’t enough: every superposition must be tested too
- 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 is . 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 to would match the 0/1 outputs, yet on a superposition such as 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 by . A product such as is hard to reach with phase gates, but parities are easy. A CNOT can place a parity such as on a wire, a on that wire then adds the phase when the parity is 1, and a subtracts it. The bridge is an identity for bits:
Multiply by : the phase is a sum of seven parity terms, four with a plus sign (a each) and three with a minus sign (a 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 : what is ?
Hint
- , and .
Show the answer
Answer:
. Times that is a phase of , the minus sign on . With any bit equal to 0 the seven terms cancel to 0.
Common slips
- : Look at the last term: , since three 1s make an odd count. So the sum is .
- : That treats as 1, like OR. XOR is addition mod 2, so , and the three pair terms vanish.
- : Zero is what the sum gives when at least one bit is 0. With all three bits equal to 1 it is .
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, and 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 is universal.
Approximation errors are easy to control, because they add up. If each of gates is replaced by one within of it (on every input the two outputs differ by at most ), the whole circuit is within of the original.
Check yourself
A circuit has 500 gates. A compiler replaces each one by an approximation within . At most how far can the new circuit’s output be from the original’s?
Hint
- Errors of approximated gates add up.
Show the answer
Answer:
The errors add: . To keep the whole circuit within , approximate each gate to within .
Common slips
- : That is the error of one gate. Each of the 500 gates adds its own, and in the worst case they add up: .
Universal does not mean exact. Imagine listing every circuit that can be built from , 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 about the axis alone form a continuum, one for every real angle , 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 , a rotation by 1 radian about the axis, into , and CNOT gates. What can it deliver?
- AA circuit within any error you name, say , but never an exact one
- BNothing useful: a rotation by 1 radian is not one of the reachable gates
- CAn exact circuit, provided it may use extra work qubits
- DAn exact circuit, if it searches long enough
Show the answer
Answer: A. A circuit within any error you name, say , but never an exact one
The gate set is universal, so some circuit comes within any you name. None is exact: the circuits are countable, and the entries of their matrices are built from and in a way that can never produce .
Why not the others
- B. Not reachable exactly, but reachable as closely as you like. That is what universal means, and an error of 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 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 by about gates from the set, where is a fixed constant. The first proofs give close to 4, later ones less. You need only the statement: the cost grows like a power of , so doubling the number of correct digits multiplies the cost by a fixed factor rather than squaring it.
For and , methods found since then do better still. A rotation about the axis needs about gates, plus cheaper gates such as and . Each extra decimal digit costs about more gates.
The rule of thumb for one rotation: each factor of 100 in accuracy adds about 20 gates, a straight line in the number of digits.
Check yourself
Use the rule of thumb from this lesson: about how many gates does it take to approximate a rotation about the axis to within ?
Hints
- The rule of thumb is .
- .
Show the answer
Answer:
: about ten gates for each decimal digit of accuracy. Any answer within a few of 90 is fine, since the rule is approximate.
Common slips
- : That is itself, about 30. The rule of thumb is three times that: .
- : That is the number of decimal digits. Each digit is a factor of 10, and , so the count is about .
Refresher: Big-O and repeated trials How , , and 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 gate types there are at most circuits of gates. To come within of every rotation , the circuits must be spread around the whole circle of angles with gaps of order , so you need on the order of of them. That forces to be at least of order , so grows at least like . So the logarithmic growth of 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.
- -count: on a future error-corrected machine, , and CNOT are cheap and each 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, -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?
- ACircuit A
- BCircuit B, because it has fewer gates
- CAbout equally likely, since they compute the same thing
- 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 , B about . 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 , B about .
- 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 , or , commutes with a CNOT when it sits on the control, because the CNOT only reads the control’s 0/1 value. An commutes with a CNOT when it sits on the target, because the CNOT applies either nothing or there.
As written
T on q0 moved first
The same five gates, twice. As written, the on waits for the CNOT and needs a fourth layer. A 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?
- AMove the on to the very start
- BMove the on to just after the CNOT
- CMove the first on to the end of its wire
- DNothing: five gates take five layers
Show the answer
Answer: A. Move the on to the very start
An on the target commutes with the CNOT, and it commutes with the gates on , so it can move to the start and share layer 1 with the first . The layers become and , then , then , then the CNOT: depth 4. The chain on can’t be reordered, since and don’t commute.
Why not the others
- B. A on the control would commute with the CNOT, but to get there it must pass the after it, and doesn’t commute with . That move changes the circuit.
- C. commutes neither with 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 on sits on the CNOT’s target, where it commutes with the CNOT, so it can move to the front and run alongside the first .
Check yourself
A circuit on three qubits applies these gates, in this order:
- on
- a CNOT with control and target
- on
- on
- a CNOT with control and target
- on
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
- Put each gate in the earliest layer that comes after every earlier gate on its wires.
- A CNOT uses two wires: it waits for both, and later gates on either wire wait for it.
Show the answer
Answer:
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:
- on : layer 1 (nothing earlier on its wires)
- a CNOT with control and target : layer 2 (after gate 1)
- on : layer 3 (after gate 2)
- on : layer 1 (nothing earlier on its wires)
- a CNOT with control and target : layer 4 (after gate 3)
- on : layer 3 (after gate 2)
The deepest layer is 4, so the depth is 4.
Common slips
- : That counts the gates. Gates on different wires can share a layer, so the depth is smaller than the gate count here.
- : 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 controls, which flips the target only when all controls are 1; Grover search, later in this part, needs one in every round. For it is the Toffoli. For larger , build it from Toffolis and extra work qubits that start in :
- Compute. A Toffoli writes into . The next writes into , and so on, until holds the AND of all controls.
- Act. A CNOT from flips the target when that AND is 1.
- Uncompute. The Toffolis of step 1 run again in reverse order. Each one is its own inverse, so every work qubit returns to .
In all: Toffolis, one CNOT and work qubits.
A NOT with three controls. Two Toffolis compute into , by way of ; a CNOT flips the target when is 1; two more Toffolis return both work qubits to .
Check yourself
Why run the Toffolis again at the end, instead of leaving the work qubits as they are?
- ABecause a Toffoli gate only works when it comes in a pair
- BTo undo the flip of the target, so the gate can be reversed
- CThere is no real need: the work qubits can simply be ignored
- DLeft alone, the work qubits would stay entangled with the controls and spoil later interference; uncomputing returns them to
Show the answer
Answer: D. Left alone, the work qubits would stay entangled with the controls and spoil later interference; uncomputing returns them to
After the compute half, holds , 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 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, ends up holding in each branch, so branches that differ in also differ on 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
- How many Toffolis does the compute half use, and how many new work qubits does each one write to?
Show the answer
Answer:
holds , and each of the controls needs one more work qubit: .
Common slips
- : One per control is one too many. The first Toffoli combines two controls at once, so work qubits are enough.
- : That is the number of Toffolis, . The uncompute half reuses the same work qubits, so there are only .
A cheaper version
The target itself can stand in for the last work qubit. Compute into work qubits, let one Toffoli with controls and the last work qubit flip the target directly, and uncompute. That uses Toffolis and work qubits: for , 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 , six CNOTs and seven -type gates.
Worked example
A NOT with three controls
Use the construction above with controls. First the example is worked for you; then you do .
Parts
Toffolis and one CNOT, with work qubits. With three controls and one target, that makes qubits.
T-count
Each Toffoli brings seven -type gates and the middle CNOT none: .
CNOT count
Each Toffoli brings six CNOTs, and there is one more in the middle: .
Your turn: Toffolis
Now take 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
- Compute uses Toffolis, act uses one CNOT, uncompute repeats the Toffolis. There are work qubits.
- Each Toffoli circuit has six CNOTs and seven -type gates.
Show the answer
Answer:
Toffolis compute the AND and the same number uncompute it: .
Common slips
- : That is the compute half only. The work qubits must return to , so the same Toffolis run again in reverse: .
- : 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: .
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 -type gates ( or ) does it use?
Hints
- Compute uses Toffolis, act uses one CNOT, uncompute repeats the Toffolis. There are work qubits.
- Each Toffoli circuit has six CNOTs and seven -type gates.
Show the answer
Answer:
There are Toffolis with seven -type gates each, and the middle CNOT has none: .
Common slips
- : That is the compute half only. The work qubits must return to , so the same Toffolis run again in reverse, which doubles the count: .
- : That counts only the gates. Each Toffoli also has three gates, which cost the same: seven -type gates per Toffoli.
- : That is the number of Toffolis, 8. Each one is replaced by a circuit with seven -type gates.
One more, fully worked, with .
Another worked example
Another example: four controls
Parts
Toffolis and one CNOT, with work qubits: qubits in all.
T-count
-type gates.
CNOT count
CNOTs. Compare the cheaper version: Toffolis, so -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
- Compute uses Toffolis, act uses one CNOT, uncompute repeats the Toffolis. There are work qubits.
- Each Toffoli circuit has six CNOTs and seven -type gates.
Show the answer
Answer:
8 controls, work qubits and one target: .
Common slips
- : That leaves out the 7 work qubits.
- : 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:
- on
- a CNOT with control and target
- on
- a CNOT with control and target
- on
- on
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
- Put each gate in the earliest layer that comes after every earlier gate on its wires.
- A CNOT uses two wires: it waits for both, and later gates on either wire wait for it.
Show the answer
Answer:
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:
- on : layer 1 (nothing earlier on its wires)
- a CNOT with control and target : layer 2 (after gate 1)
- on : layer 3 (after gate 2)
- a CNOT with control and target : layer 3 (after gate 2)
- on : layer 4 (after gate 4)
- on : layer 5 (after gate 5)
The deepest layer is 5, so the depth is 5.
Common slips
- : That counts the gates. Gates on different wires can share a layer, so the depth is smaller than the gate count here.
- : 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-. A dot and a in the same column make a CZ.
Build it in the circuit builder (gates: H, control, Z).
Hints
- Which rewrite turns into ?
- : put 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.
, 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 .
Check yourself
A circuit contains 5 Toffoli gates and, besides them, 9 gates and 2 CNOTs. You replace every Toffoli with the 15-gate circuit from this lesson. What is the -count (the number of and gates) of the new circuit?
Hints
- How many CNOTs and how many -type gates are in the 15-gate Toffoli circuit?
- It has two , six CNOTs, and seven gates that are or .
Show the answer
Answer:
Each Toffoli brings seven -type gates: .
Common slips
- : That counts only the gates in each Toffoli. The three gates cost the same as gates, so each Toffoli adds seven.
- : That counts the Toffolis’ -type gates but leaves out the 9 gates the circuit already had.
- : That counts every gate in each Toffoli circuit. Only seven of the fifteen are or ; the rest are two and six CNOTs.
11 / 13 · Explain
Explain it
Explain it
The set is called universal, yet no circuit made from it equals the rotation 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 there is a circuit from the set within of it, and for one qubit the number of gates grows only like a power of ; modern methods need about 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 with one for every real , and no countable list covers a continuum. So almost every gate, 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 for every real ), so most are missing from the list
- The cost of accuracy grows slowly, like a power of
12 / 13 · Mastery check
Mastery check
Check yourself
A quantum computer offers only the gates , 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?
- AYes: with enough gates, each of them is built exactly
- BNo, but each one can be approximated as closely as you like
- CNo: the circuits reach only a few gates, such as , and
- 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 , 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 and 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 , then , then to one qubit in the state . Which product of matrices equals the whole circuit?
- A
- B
- C
- D
Hint
- Which gate acts on first, and where must it stand in the product?
Show the answer
Answer: A.
acts first, so it sits next to the ket: . 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 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 first, so it stands next to the ket: . And the order matters here, since .
Check yourself
Which single gate equals the matrix product , up to an overall phase?
- A
- B
- C
- D, the identity
Hint
- Apply the product to and to , one gate at a time, starting from the right.
Show the answer
Answer: A.
On basis states: and . That is , which is up to an overall sign.
Why not the others
- B. and don’t cancel: sits between them, and doesn’t commute with . turns the Bloch sphere a quarter turn about the axis, which carries the axis onto the axis.
- C. The product flips bits, which never does: .
- D. Only adjacent and cancel. Follow : . The product flips bits.
Check yourself
Two circuits compute the same thing. Circuit A has 5,000 gates, 400 of them gates. Circuit B has 3,000 gates, 1,500 of them gates. All the other gates are , or CNOT. Both will run on an error-corrected quantum computer. Which is likely to be cheaper to run?
- ACircuit A
- BCircuit B
- CAbout the same, since they compute the same thing
- DThere is no way to tell without running both
Show the answer
Answer: A. Circuit A
On an error-corrected machine , and CNOT are cheap and gates are expensive, so the -count decides. Circuit A has 400 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 gates, and on an error-corrected machine the gates dominate the cost. Even if a 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 gate.
- D. The counts are enough for a good estimate. On an error-corrected machine , and CNOT are cheap and each gate costs many times more, so compare the -counts.
Check yourself
What is ? (CZ multiplies by and leaves the other basis states alone.)
- A
- B, unchanged
- C
- D
Hints
- Write the input as a sum of two basis states.
- Only the term changes sign.
Show the answer
Answer: A.
Written out, the input is . CZ changes the sign of only, giving . 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 lands on its branch, which turns into or back.
- C. CZ never flips a bit: on basis states it only changes signs. The qubit in stays .
- D. The minus sign goes on the term only, not on both terms. Write the input out: , and only the second term changes sign.
Check yourself
A circuit contains 3 Toffoli gates and, besides them, 10 gates and 11 CNOTs. You replace every Toffoli with the 15-gate circuit from this lesson. What is the -count (the number of and gates) of the new circuit?
Hints
- How many CNOTs and how many -type gates are in the 15-gate Toffoli circuit?
- It has two , six CNOTs, and seven gates that are or .
Show the answer
Answer:
Each Toffoli brings seven -type gates: .
Common slips
- : That counts only the gates in each Toffoli. The three gates cost the same as gates, so each Toffoli adds seven.
- : That counts the Toffolis’ -type gates but leaves out the 10 gates the circuit already had.
- : That counts every gate in each Toffoli circuit. Only seven of the fifteen are or ; the rest are two and six CNOTs.
Check yourself
A circuit on three qubits applies these gates, in this order:
- on
- on
- a CNOT with control and target
- a CNOT with control and target
- a CNOT with control and target
- on
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
- Put each gate in the earliest layer that comes after every earlier gate on its wires.
- A CNOT uses two wires: it waits for both, and later gates on either wire wait for it.
Show the answer
Answer:
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:
- on : layer 1 (nothing earlier on its wires)
- on : layer 2 (after gate 1)
- a CNOT with control and target : layer 1 (nothing earlier on its wires)
- a CNOT with control and target : layer 3 (after gate 2)
- a CNOT with control and target : layer 4 (after gate 4)
- on : layer 5 (after gate 5)
The deepest layer is 5, so the depth is 5.
Common slips
- : That counts the gates. Gates on different wires can share a layer, so the depth is smaller than the gate count here.
- : 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 -type gates ( or ) does it use?
Hints
- Compute uses Toffolis, act uses one CNOT, uncompute repeats the Toffolis. There are work qubits.
- Each Toffoli circuit has six CNOTs and seven -type gates.
Show the answer
Answer:
There are Toffolis with seven -type gates each, and the middle CNOT has none: .
Common slips
- : That is the compute half only. The work qubits must return to , so the same Toffolis run again in reverse, which doubles the count: .
- : That counts only the gates. Each Toffoli also has three gates, which cost the same: seven -type gates per Toffoli.
- : That is the number of Toffolis, 10. Each one is replaced by a circuit with seven -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.
A circuit applies , then , then . Write it as a matrix product, and state the four rewrites from this lesson.
Show the key points
- : the first gate stands next to the ket
- and
- Hadamards on both qubits turn a CNOT upside down
- before and after the control makes an anti-control
How is the Toffoli gate built from one- and two-qubit gates, and why does it need seven -type gates?
Show the key points
- on the target turns the flip into the sign
- is a signed sum of seven parities, each a phase of : four , three
- Six CNOTs place the parities; 15 gates in all
What does it mean that 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 gates per rotation; Solovay–Kitaev: a power of
- Gate count, depth, two-qubit count, -count; move commuting gates to cut depth
- A -control NOT: Toffolis and work qubits, compute, act, uncompute
Key ideas
A circuit is a program whose matrix is the product of its gates in reverse order: , then , then is .
, ; Hadamards on both qubits reverse a CNOT; around a control gives an anti-control. CZ is a CNOT with on the target, and it is symmetric.
The Toffoli gate is exactly two , six CNOTs and seven -type gates.
is universal: it approximates any unitary to any accuracy, never all of them exactly. A rotation costs about gates.
Gate count, depth, two-qubit count and -count measure different costs. Moving gates that commute can cut the depth.
A NOT with controls takes Toffolis and work qubits: compute, act, uncompute.
Go further
Free resources for when you want more than this lesson covers.
- Fundamentals of quantum algorithms: Quantum algorithmic foundationsJohn Watrous, IBM Quantum · Course
Measures what a computation costs and how that grows with the input, with arithmetic, GCDs and factoring as the examples.
- Quantum Computing: Lecture NotesRonald de Wolf · Lecture notes
Compact, free lecture notes that set up the circuit model and universal gate sets, then work through the main algorithms.
- Solovay–Kitaev theoremWikipedia · Article
The exact statement, and how later proofs pushed the exponent c in the gate count down from 3.97 to below 1.5.
- Clifford gatesWikipedia · Article
Why circuits of H, S and CNOT alone are easy to simulate classically, and why the T gate lies outside that set.
Math refreshers
The toolkit topics this lesson leans on.
- Big-O and repeated trialsHow log n, √N, n, n² and 2ⁿ grow, and how repeating a test drives its failure chance down