In the preceding chapters, we used single-qubit gates, CNOT gates, and reversible oracle boxes to build quantum computations. On an actual quantum computer, however, any multi-qubit unitary transformation must be constructed step by step from a small set of elementary gates.
We first set out the rules for reading and composing multi-qubit circuit diagrams and build standard gate families. We then ask which gate sets can construct any unitary operation and how to count the resources a circuit requires.
What separates an operation we can write down as a matrix from one we can build efficiently out of elementary gates?
In Chapter 2, we saw that every Boolean function can be built from a small set of classical gates. In quantum computation, the space of allowed operations is much larger: any unitary matrix on \(n\) qubits defines a valid quantum step.
Can a small, fixed collection of one- and two-qubit gates build—or closely approximate—every \(n\)-qubit unitary matrix? And if so, how do we diagram, assemble, and measure the cost of the resulting circuits?
Reading guide · preparation, aims, and assumptions
Translate abstract operations into circuits and account for the work hidden inside a gate box.
Before you begin
Use the query algorithm, phase marking, unitary evolution, and joint-state notation.
What you should be able to do
Read circuit order and controlled operations.
Distinguish exact construction from approximation.
Count gates, depth, and workspace separately.
The example to follow
Decompose an oracle and a reflection into gates, then use these constructions in the search algorithm that follows.
Scope and assumptions
A finite discrete gate set generally approximates arbitrary unitaries; universality does not imply every transformation has a short exact circuit.
8.1
Circuit Representation
We have used circuit diagrams since our first single-qubit programs in Chapter 5. When a circuit spans multiple wires and mixes unitary gates with measurements, three foundational questions arise: when can two gates execute in parallel, what is hidden inside a macroscopic subroutine box, and how does coherent quantum control differ from classical control driven by a measurement outcome?
In a multi-qubit quantum circuit, each horizontal wire tracks one qubit over time from left to right, and each box represents a unitary gate or measurement applied to the wire(s) it touches. Remember that a wire labels a subsystem index, not an isolated state vector: as soon as multi-qubit gates entangle the wires, individual wires no longer possess standalone state vectors, and we track the joint state vector of the entire register.
Notice the reversal between visual reading order and algebraic matrix multiplication: if a circuit applies gate \(U\) first (on the left) and gate \(V\) second (on the right) to an input state \(\ket{\psi}\), the output state is \(V(U\ket{\psi}) = (VU)\ket{\psi}\), so the matrix product \(VU\) is written from right to left.
When can two gates in a circuit run at the same time? Whenever they act on completely disjoint sets of wires! If gate \(U\) acts on the first wire and gate \(V\) acts on the second wire, applying \(U\) then \(V\) or applying \(V\) then \(U\) routes every product-basis state \(\ket{ij}\) along the exact same transition arrows with product weight \(\bra{i'}U\ket{i}\bra{j'}V\ket{j}\):
Because operations on disjoint wires commute, they can be drawn in the same vertical column (a single parallel layer) and executed simultaneously if the hardware allows. By contrast, two gates that share a wire generally do not commute—for example, \(XZ = -ZX\) and \(HX = ZH\)—so their left-to-right order along the shared wire must be strictly respected.
The two wires identify subsystems. U followed by V on the first wire gives VU; W on the separate wire may run independently.Problems
A circuit applies \(H\) to qubit 1, then \(X\) to qubit 2, then CNOT with qubit 1 as control and qubit 2 as target, starting from \(\ket{00}\). Write the state after each step.
Show solutionHide solution
\(H\): \((\ket{00}+\ket{10})/\sqrt2\). \(X\) on qubit 2: \((\ket{01}+\ket{11})/\sqrt2\). CNOT flips qubit 2 where qubit 1 is \(1\): \((\ket{01}+\ket{10})/\sqrt2\).
Apply, to \(\ket{00}\), first \(H\) on qubit 1 and then CNOT, and in the other order. Do the two operations commute?
Show solutionHide solution
\(H\) then CNOT gives \((\ket{00}+\ket{11})/\sqrt2\). CNOT then \(H\) gives \((\ket{00}+\ket{10})/\sqrt2\), because CNOT leaves \(\ket{00}\) unchanged. They do not commute; the two gates share qubit 1.
Prove that \((U\otimes I)(I\otimes V)=(I\otimes V)(U\otimes I)=U\otimes V\): gates on different wires commute.
Show solutionHide solution
On a product state \(\ket a\ket b\), both orders give \(U\ket a\otimes V\ket b\), which is also \((U\otimes V)\ket a\ket b\). The three matrices agree on every product basis state, and every state is a combination of these, so by linearity they are equal.
When we draw a high-level algorithm as a circuit diagram, a single large box often stands for an entire subroutine. In Deutsch's algorithm from Chapter 7, the two initial \(H\) gates prepare \(\ket{+}\ket{-}\), the box labeled \(U_f\) kicks back the phase \((-1)^{f(x)}\), and the final \(H\) interferes the two branches. Drawing \(U_f\) as a single box specifies its unitary input–output behavior, but conceals the internal network of elementary gates needed to compute \(f\).
In Chapter 9, we will build Grover's search algorithm by repeating a two-box cycle: a phase-oracle box \(O_f\) that flips the sign of marked bit strings, followed by a diffusion box \(D\) that reflects amplitudes about the uniform superposition. Before we can judge the true cost of running such an algorithm on hardware, we must open both boxes and see how they are constructed out of elementary one- and two-qubit gates.
Deutsch’s algorithm uses one oracle gate between its preparation and readout gates. The Uf box specifies a joint operation, not its internal gate count.Problems
In the four-item search circuit, how many oracle uses and how many Hadamard gates does one Grover iteration contain, and how many Hadamard gates prepare the start?
Show solutionHide solution
One iteration has one oracle use, the first controlled-\(Z\), and four Hadamard gates in the reflection, two before and two after the reflection about \(\ket{00}\). Two more Hadamard gates prepare \(\ket s\) from \(\ket{00}\).
List the gates of Deutsch’s algorithm in time order, starting from \(\ket{00}\), with the qubits each acts on.
Show solutionHide solution
\(X\) on qubit 2; \(H\) on qubit 1 and \(H\) on qubit 2, which can act at the same time; \(U_f\) on qubits 1 and 2; \(H\) on qubit 1; measurement of qubit 1.
An oracle box \(O_f\) on \(n\) qubits marks one fixed string. It is built from \(X\) gates and a phase flip controlled on all \(n\) qubits, which in turn uses Toffoli gates and \(n-2\) workspace qubits. Count the gates in such a construction.
Show solutionHide solution
Up to \(n\) \(X\) gates before and \(n\) after, on the positions where the string has \(0\). The phase flip computes the AND of \(n-1\) qubits into the workspace with \(n-2\) Toffoli gates, applies a controlled-\(Z\) between the last workspace qubit and the remaining qubit, and uncomputes with \(n-2\) more Toffoli gates. Altogether about \(2n\) \(X\) gates, \(2(n-2)\) Toffoli gates, and one controlled-\(Z\): one box hides a number of gates proportional to \(n\).
Does it make a physical difference whether a target gate is controlled coherently by an unmeasured qubit or controlled classically by the bit obtained from measuring that qubit? Let us compare both on the exact same two-qubit input state \(\ket{+}\ket{0} = \bigl(\ket{00}+\ket{10}\bigr)/\sqrt{2}\).
First, apply a coherent controlled gate—the unitary CNOT gate—directly to \(\ket{+}\ket{0}\). Because CNOT acts linearly on the superposition without measuring the control wire, it produces the pure entangled Bell state \(\ket{\Phi^+} = (\ket{00}+\ket{11})/\sqrt{2}\). Second, suppose instead that we measure the control wire in the computational basis, record the classical outcome bit \(m\in\{0,1\}\), and apply \(X\) to the target wire if \(m=1\)—a classically controlled gate. On half the runs the measurement yields \(m=0\) and leaves \(\ket{00}\); on the other half it yields \(m=1\) and flips the target to leave \(\ket{11}\). Instead of the coherent Bell state \(\ket{\Phi^+}\), the classically controlled procedure produces a \(50/50\) classical mixture of \(\ket{00}\) and \(\ket{11}\)! As we saw in Section 6.2.3, measuring both qubits in the \(X\) basis immediately exposes the difference: \(\ket{\Phi^+}\) yields perfectly correlated \(X\)-basis outcomes (\(++\) or \(--\)), whereas the classical mixture yields completely uncorrelated \(X\)-basis outcomes.
Circuit notation keeps this distinction unmistakable: single lines carry coherent quantum states, whereas double lines (or dashed lines) emerging from a measurement box \(\mathrm{M}\) carry classical bits that trigger classically controlled operations.
Quantum teleportation from Section 6.4 uses both types of control in their proper places: Alice's CNOT gate is a coherent two-qubit gate acting before measurement, whereas Bob's recovery gates \(X^n\) and \(Z^m\) are classically controlled by the two bits Alice measures and transmits. Replacing Alice's coherent CNOT with a measurement-and-classical-control step would destroy entanglement and ruin teleportation, while replacing Bob's classical control with coherent quantum control would require Alice to ship her physical qubits to Bob's lab!
Coherent versus classical control in teleportation: Alice's CNOT on the left is a coherent quantum gate coupling two wires, whereas Bob's \(X\) and \(Z\) corrections on the right are classically controlled by the measurement outcomes transmitted over dashed lines.Problems
Bob receives a bit \(b\) and applies \(X\) only if \(b=1\). Write his operation for each value of \(b\).
Show solutionHide solution
\(b=0\): the identity \(I\). \(b=1\): \(X\). The bit is known when the gate is chosen, so this is a classical choice, not a quantum gate.
For the input \((\alpha\ket0+\beta\ket1)\ket0\), compare (a) CNOT followed by measuring the control with (b) measuring the control first and then applying \(X\) to the target if the result is \(1\). Show that the outcomes and final states agree.
Show solutionHide solution
(a) CNOT gives \(\alpha\ket{00}+\beta\ket{11}\). Measuring the control gives \(0\) with probability \(|\alpha|^2\), leaving \(\ket{00}\), and \(1\) with probability \(|\beta|^2\), leaving \(\ket{11}\). (b) Measuring first gives the same probabilities, leaving \(\ket{00}\) or \(\ket{10}\), and the classical \(X\) turns \(\ket{10}\) into \(\ket{11}\). The results agree: a measurement of the control may be moved past a gate that it controls.
Now the control is not measured at the end. Compare CNOT with “measure the control, then apply \(X\) if \(1\)” on the input \(\ket+\ket-\), by applying \(H\) to the control afterward and measuring it.
Show solutionHide solution
CNOT gives \(\ket-\ket-\), by phase kickback; \(H\) on the control gives \(\ket1\), so the result is \(1\) with certainty. Measuring first leaves the control in \(\ket0\) or \(\ket1\), with the target still \(\ket-\), up to sign; \(H\) then gives \(0\) or \(1\) with probability \(1/2\). A classically controlled gate is not a substitute for a coherent one when the control’s phase matters.
8.2
Gate Constructions
Every quantum circuit is assembled from a small repertoire of elementary gates. In this section, we review the standard single-qubit gates—the Pauli matrices, the Hadamard gate, continuous Pauli rotations, and the discrete phase gates \(S\) and \(T\)—express two-qubit controlled gates algebraically using projectors, and construct the multi-qubit phase-marking and reflection circuits that we will use for quantum search in Chapter 9.
Let us gather our elementary single-qubit gates in one place. We have already worked extensively with the Pauli matrices \(I, X, Y, Z\) and the Hadamard gate
While \(X, Y, Z,\) and \(H\) perform fixed discrete reflections and permutations of amplitudes, general single-qubit transformations require adjustable angles. In Section 5.3.2, we built the continuous rotation \(R_x(\theta)\) from \(X\). Because every Pauli matrix \(P\in\{X,Y,Z\}\) is Hermitian (\(P^\dagger = P\)) and squares to the identity (\(P^2 = I\)), the exact same formula defines a unitary rotation gate around any Pauli axis \(P\):
We can verify unitarity in one line: since \(P^\dagger = P\) and \(P^2 = I\), multiplying \(R_P(\theta)^\dagger = \cos(\theta/2)I + i\sin(\theta/2)P\) by \(R_P(\theta)\) cancels the \(\pm i\sin(\theta/2)\cos(\theta/2)P\) cross terms and leaves \(\bigl(\cos^2(\theta/2) + \sin^2(\theta/2)\bigr)I = I\).
When quantum hardware—especially fault-tolerant hardware—restricts us to a finite catalog of discrete gates, two special members of the phase gate family \(R_\phi = \operatorname{diag}(1, e^{i\phi})\) play a central role. They are the phase gates \(S\) (the quarter-turn phase gate, \(\phi = \pi/2\)) and \(T\) (the eighth-turn phase gate, \(\phi = \pi/4\)):
Because multiplying diagonal phase gates adds their phase angles, squaring \(T\) gives \(T^2 = S\), and squaring \(S\) gives \(S^2 = Z\) (so \(S\) is a square root of \(Z\), distinct from \(\sqrt{X}\)). Note also that since \(S\) and \(T\) have complex diagonal entries, their inverses are their adjoints \(S^\dagger = \operatorname{diag}(1, -i)\) and \(T^\dagger = \operatorname{diag}(1, e^{-i\pi/4})\).
Interactive figure. This single-qubit circuit tester applies sequences of \(X\), \(Z\), and \(H\) gates to \(\ket{0}\) and displays the resulting state vector and measurement bars. Compare the sequence \(H\to H\) with \(H\to Z\to H\) to see how \(H\) converts phase flips into bit flips.
What to observe
Applying \(H\) twice cancels the off-diagonal phases and returns \(\ket{0}\) with certainty. Inserting \(Z\) between the two Hadamard gates flips the relative phase of \(\ket{1}\), so the second \(H\) interferes constructively on \(\ket{1}\) instead (\(HZH=X\)).
These are separate single-qubit circuits. Rotation parameters are continuous; S and T are fixed phase operations.Problems
Compute \(R_X(\pi)\) and \(R_Z(\pi)\), and compare them with \(X\) and \(Z\).
Show solutionHide solution
\(R_P(\pi)=\cos\tfrac\pi2I-i\sin\tfrac\pi2P=-iP\). So \(R_X(\pi)=-iX\) and \(R_Z(\pi)=-iZ\): the Pauli gates, up to the global phase \(-i\).
Verify by multiplying matrices that \(HZH=X\), \(HXH=Z\), \(S^2=Z\), and \(T^2=S\).
Show solutionHide solution
\(HZ=\tfrac1{\sqrt2}\begin{pmatrix}1&-1\\1&1\end{pmatrix}\), and multiplying by \(H\) gives \(\begin{pmatrix}0&1\\1&0\end{pmatrix}=X\). Similarly, \(HXH=Z\). For the phase gates, \(S^2=\operatorname{diag}(1,i^2)=\operatorname{diag}(1,-1)=Z\) and \(T^2=\operatorname{diag}(1,e^{i\pi/2})=\operatorname{diag}(1,i)=S\).
Show that \(R_Z(\theta)=\operatorname{diag}(e^{-i\theta/2},e^{i\theta/2})\), and that up to a global phase it is \(\operatorname{diag}(1,e^{i\theta})\). Which values of \(\theta\) give \(S\) and \(T\)?
Show solutionHide solution
\(\cos\tfrac\theta2I-i\sin\tfrac\theta2Z=\operatorname{diag}\bigl(\cos\tfrac\theta2-i\sin\tfrac\theta2,\;\cos\tfrac\theta2+i\sin\tfrac\theta2\bigr)=\operatorname{diag}(e^{-i\theta/2},e^{i\theta/2})\). Factoring out \(e^{-i\theta/2}\) leaves \(\operatorname{diag}(1,e^{i\theta})\). So \(S\) is \(R_Z(\pi/2)\) and \(T\) is \(R_Z(\pi/4)\), each up to a global phase.
How do we write a general two-qubit controlled-\(U\) gate using the projector notation from Section 4.2.2? On branches where the control wire is in \(\ket{0}\), the target wire undergoes the identity \(I\); on branches where the control wire is in \(\ket{1}\), the target wire undergoes \(U\). Because the projectors \(\ketbra{0}{0}\) and \(\ketbra{1}{1}\) select the \(\ket{0}\) and \(\ket{1}\) components of the control wire respectively, we can write controlled-\(U\) compactly as a sum of two tensor products:
\[\text{controlled-}U = \ketbra{0}{0}\otimes I + \ketbra{1}{1}\otimes U.\]
Here each tensor product \(A\otimes B\) acts independently on the first and second qubits, with transition amplitude \(\bra{jk}(A\otimes B)\ket{il} = \bra{j}A\ket{i}\,\bra{k}B\ket{l}\).
Setting \(U = X\) gives \(\operatorname{CNOT} = \ketbra{0}{0}\otimes I + \ketbra{1}{1}\otimes X\). Setting \(U = Z\) gives the controlled-\(Z\) gate (\(\operatorname{CZ}\)), which leaves \(\ket{00}, \ket{01}, \ket{10}\) unchanged and multiplies only \(\ket{11}\) by \(-1\):
\[\operatorname{CZ} = \ketbra{0}{0}\otimes I + \ketbra{1}{1}\otimes Z = \operatorname{diag}(1, 1, 1, -1).\]
Notice that \(\operatorname{CZ}\) is completely symmetric between the two qubits—it flips the phase if and only if both qubits are \(1\). Even better, because conjugation by \(H\) turns \(X\) into \(Z\),
we can build controlled-\(Z\) directly from CNOT by placing a Hadamard gate on the target wire immediately before and after the CNOT: \((I\otimes H)\operatorname{CNOT}(I\otimes H) = \operatorname{CZ}\).
Remember that a controlled gate is an entangling gate because it can create entanglement from suitable product states—for instance, \(\operatorname{CNOT}(\ket{+}\ket{0}) = \ket{\Phi^+}\) and \(\operatorname{CZ}(\ket{+}\ket{+}) = (\ket{00}+\ket{01}+\ket{10}-\ket{11})/2\)—even though it leaves computational basis states like \(\ket{00}\) unentangled.
Converting CNOT (controlled-\(X\)) into controlled-\(Z\) (\(\operatorname{CZ}\)): surrounding the target of CNOT with Hadamard gates implements \(HXH = Z\) whenever the control qubit is \(\ket{1}\).Problems
Apply controlled-\(Z\) to the four basis states. Show that exchanging the roles of control and target gives the same operation.
Show solutionHide solution
It fixes \(\ket{00}\), \(\ket{01}\), and \(\ket{10}\), and sends \(\ket{11}\) to \(-\ket{11}\): the matrix \(\operatorname{diag}(1,1,1,-1)\). The sign appears exactly when both qubits are \(1\), a condition symmetric in the two qubits.
Verify that \((I\otimes H)\,\mathrm{CNOT}\,(I\otimes H)\) acts as controlled-\(Z\) on \(\ket{10}\) and \(\ket{11}\).
Show solutionHide solution
\(\ket{10}\to\ket1\ket+\to\ket1X\ket+=\ket1\ket+\to\ket{10}\). \(\ket{11}\to\ket1\ket-\to\ket1X\ket-=-\ket1\ket-\to-\ket{11}\). Only \(\ket{11}\) gains the sign, as for controlled-\(Z\).
Let \(\ket v\) be an eigenvector of \(U\) with \(U\ket v=e^{i\phi}\ket v\). Show that controlled-\(U\) sends \(\ket+\ket v\) to \(\tfrac1{\sqrt2}(\ket0+e^{i\phi}\ket1)\otimes\ket v\). Is the result entangled?
Show solutionHide solution
Controlled-\(U\) leaves \(\ket0\ket v\) unchanged and sends \(\ket1\ket v\) to \(\ket1U\ket v=e^{i\phi}\ket1\ket v\). By linearity, \(\ket+\ket v\) becomes \(\tfrac1{\sqrt2}(\ket0+e^{i\phi}\ket1)\otimes\ket v\), a product state. The eigenvalue moves onto the control as a relative phase; phase kickback in Deutsch’s algorithm is the case \(U=X\), \(\ket v=\ket-\), \(\phi=\pi\).
Suppose we want to search \(N = 2^n\) bit strings for a marked target string \(w\in\{0,1\}^n\). As we will see in Chapter 9, quantum search alternates two multi-qubit unitary operations: a phase oracle \(O_f\) that multiplies \(\ket{w}\) by \(-1\) while leaving all other basis states unchanged, and a diffusion operator \(D\) that reflects the state vector about the uniform superposition of all \(N\) strings. How do we build both operations from elementary gates?
First, write \(\ket{0^n} = \ket{0\cdots 0}\) for the \(n\)-qubit all-zero state, and apply \(H^{\otimes n}\) (one Hadamard gate on each wire) to prepare the uniform superposition
How do we build a phase gate that flips the sign of a single chosen string \(w\)? An \(n\)-qubit controlled-\(Z\) gate flips the sign of the all-ones state \(\ket{11\cdots 1}\) and leaves every other \(n\)-bit basis state untouched. To flip the sign of \(\ket{w}\) instead, we simply wrap the multi-controlled-\(Z\) gate in a layer of \(X\) gates applied to every wire \(k\) where the bit \(w_k = 0\): the initial \(X\) gates map \(\ket{w}\mapsto\ket{11\cdots 1}\), the controlled-\(Z\) attaches a \(-1\) phase to \(\ket{11\cdots 1}\), and the matching final \(X\) gates map \(\ket{11\cdots 1}\) back to \(\ket{w}\)! (More generally, when \(w\) is recognized by a Boolean predicate circuit \(f(x)=1\), the compute, phase, uncompute construction from Section 7.3.3 implements \(O_f\) without our knowing \(w\) in advance.)
Second, how do we build the reflection \(D = 2\ketbra{s}{s} - I\) about the uniform superposition \(\ket{s}\)? Notice how \(D\) acts geometrically: the projector \(\ketbra{s}{s}\) extracts the component of any state along \(\ket{s}\), so \(D = 2\ketbra{s}{s} - I\) preserves \(\ket{s}\) (\(D\ket{s} = 2\ket{s}-\ket{s} = \ket{s}\)) and negates every vector orthogonal to \(\ket{s}\). Because \(\ket{s} = H^{\otimes n}\ket{0^n}\) and \((H^{\otimes n})^\dagger = H^{\otimes n}\), we can conjugate a reflection about \(\ket{0^n}\) by Hadamard layers:
The middle operation \(2\ketbra{0^n}{0^n} - I\) simply attaches \(+1\) to \(\ket{0^n}\) and \(-1\) to every nonzero bit string! For two qubits (\(n=2\)), the diagonal matrix \(2\ketbra{00}{00} - I = \operatorname{diag}(1, -1, -1, -1)\) is up to an overall global minus sign equal to \(\operatorname{diag}(-1, 1, 1, 1)\), which flips the sign of \(\ket{00}\) alone—implemented by applying \(X\otimes X\), then controlled-\(Z\), then \(X\otimes X\). Surrounding that core with \(H\otimes H\) on both sides implements \(-D\) (where the overall \(-1\) is an unobservable global phase unless the reflection itself is used as a controlled subroutine). For general \(n\), the \(n\)-qubit controlled-\(Z\) gate can be decomposed into a linear number of Toffoli and CNOT gates using ancilla workspace.
Two-qubit oracle and reflection circuits for \(N=4\): a controlled-\(Z\) marks \(\ket{11}\) with a minus sign, and surrounding \((X\otimes X)\operatorname{CZ}(X\otimes X)\) with \(H\otimes H\) layers implements the reflection \(D\) about \(\ket{s}\) up to a global minus sign.Problems
Which gates mark the string \(01\) on two qubits, that is, multiply \(\ket{01}\) by \(-1\) and leave the other basis states alone?
Show solutionHide solution
\(X\) on qubit 1, controlled-\(Z\), then \(X\) on qubit 1. The first \(X\) turns \(\ket{01}\) into \(\ket{11}\), controlled-\(Z\) gives it the sign, and the second \(X\) restores the label. Every other basis state avoids \(\ket{11}\) and is unchanged.
For two qubits, verify that \((H\otimes H)(2\ket{00}\bra{00}-I)(H\otimes H)\) and \(2\ket s\bra s-I\) agree on \(\ket{00}\).
Show solutionHide solution
Left side: \((H\otimes H)\ket{00}=\ket s\); the reflection gives \(2\ket{00}\braket{00}s-\ket s=\ket{00}-\ket s\), since \(\braket{00}s=\tfrac12\); and \(H\otimes H\) gives \(\ket s-\ket{00}\). Right side: \(2\ket s\braket s{00}-\ket{00}=\ket s-\ket{00}\). They agree.
Show that \((X\otimes X)\,CZ\,(X\otimes X)=-(2\ket{00}\bra{00}-I)\). Why does this sign not matter in an ordinary Grover iteration, and when would it matter?
Show solutionHide solution
Conjugating by \(X\otimes X\) moves the sign of controlled-\(Z\) from \(\ket{11}\) to \(\ket{00}\), giving \(\operatorname{diag}(-1,1,1,1)=-\operatorname{diag}(1,-1,-1,-1)=-(2\ket{00}\bra{00}-I)\). In a Grover iteration the factor \(-1\) is a global phase and changes no probability. If the iteration were controlled by another qubit, the sign would apply only in the branch where the control is \(1\), becoming a relative phase that affects later interference.
8.3
Universal Gate Sets
In Chapter 2, we proved that the NAND gate (or the reversible Toffoli gate) is universal for classical Boolean computation: any Boolean function on \(n\) bits can be built from a finite network of those gates. What is the quantum counterpart of universality? In this section, we see why single-qubit gates alone are never universal, state the universality theorem for CNOT plus single-qubit gates, use a parameter-counting argument to show that most \(n\)-qubit unitaries require exponentially many gates, and explain how a finite discrete gate set such as Clifford+\(T\) approximates any unitary to arbitrary accuracy.
Can a circuit composed solely of single-qubit gates ever implement every unitary transformation on an \(n\)-qubit register? No: any sequence of single-qubit gates on \(n\) wires combines into a tensor product of single-qubit unitaries, \(U_1\otimes U_2\otimes\cdots\otimes U_n\). Acting on any product state \(\ket{\psi_1}\otimes\cdots\otimes\ket{\psi_n}\), it produces another product state:
No matter how many single-qubit gates we stack on individual wires, a circuit without multi-qubit gates can never turn \(\ket{00}\) into the entangled Bell state \(\ket{\Phi^+}\). Every universal quantum gate set must therefore include at least one multi-qubit entangling gate.
No wire connection appears in this circuit. Each output depends only on the input on its own wire, so local gates cannot create entanglement from a product input.Problems
Apply \(H\otimes H\) to \(\ket{00}\), and check that the result is a product state.
Show solutionHide solution
\((H\otimes H)\ket{00}=\ket+\ket+=(\ket{00}+\ket{01}+\ket{10}+\ket{11})/2\), a product of the two local states.
A circuit applies \(H\) to qubit 1, then \(X\) to qubit 2, then \(H\) to qubit 1 again. Find the result on \(\ket{00}\), and explain why it had to be a product state.
Show solutionHide solution
The two \(H\) gates cancel on qubit 1, and \(X\) acts on qubit 2, so the result is \(\ket{01}\). Every gate acts on one qubit, so the circuit is \((HH)\otimes X\), which maps product states to product states.
Show that one-qubit operations cannot turn an entangled two-qubit state into a product state.
Show solutionHide solution
Suppose \((A\otimes B)\ket\Phi=\ket u\otimes\ket v\) for unitaries \(A,B\). Applying \(A^\dagger\otimes B^\dagger\) gives \(\ket\Phi=A^\dagger\ket u\otimes B^\dagger\ket v\), a product state, contradicting the assumption that \(\ket\Phi\) is entangled. Local operations can neither create nor remove entanglement.
Once we supplement arbitrary single-qubit unitary gates with a single two-qubit entangling gate—namely CNOT—do we need any three-qubit or four-qubit primitives? A fundamental theorem of quantum computation says no: the set consisting of CNOT together with all single-qubit unitary gates is universal, meaning that every unitary matrix \(U\) on an \(n\)-qubit register can be synthesized exactly as a finite circuit of CNOTs and single-qubit gates. (Notice that universality is a property of the combined set: CNOT alone only permutes computational basis strings, and single-qubit gates alone never entangle, but together they generate the entire unitary group on \(2^n\) dimensions.)
How is this exact synthesis theorem proved? First, standard matrix elimination decomposes any \(2^n\times 2^n\) unitary matrix into a product of "two-level" unitaries, each of which acts nontrivialy on only two computational basis states at a time; second, Gray-code routing and multi-controlled rotations decompose each two-level unitary into CNOTs and single-qubit gates.
Crucially, universality tells us what is possible in principle, not whether the resulting circuit is short! How many elementary gates are needed to build a typical \(n\)-qubit unitary matrix? We can find out by counting continuous real parameters. Specifying an arbitrary \(2^n\times 2^n\) unitary matrix (up to a global phase) requires \(4^n - 1\) independent real numbers. Each single-qubit or two-qubit gate in a circuit supplies at most a fixed constant number \(k\) of adjustable real parameters. A circuit of \(L\) gates can therefore tune at most \(kL\) real parameters, so reaching a generic \(n\)-qubit unitary requires \(kL \ge 4^n - 1\)—a gate count \(L\) that grows exponentially with the number of qubits \(n\)! Just as with classical Boolean functions, almost all \(n\)-qubit unitary matrices are exponentially expensive to build; practical quantum algorithms work by targeting structured unitaries (such as arithmetic circuits, phase oracles, reflections, and Fourier transforms) that can be assembled from polynomially many gates.
A representative circuit uses arbitrary single-qubit gates and CNOT. This finite example shows the gate repertoire; it does not claim that two CNOTs synthesize every two-qubit unitary.Problems
Which of these gate sets can prepare a Bell state from \(\ket{00}\): \(\{H,X,Z\}\); \(\{H,\text{CNOT}\}\); \(\{\text{CNOT},X\}\)?
Show solutionHide solution
Only \(\{H,\text{CNOT}\}\). The first set has only one-qubit gates, which keep product states. The third has only permutations of basis states, which never create a superposition.
Show that circuits of CNOT and \(X\) gates only permute computational basis states.
Show solutionHide solution
Each gate sends every basis state to a basis state and is one-to-one, so its matrix is a permutation matrix. A product of permutation matrices is a permutation matrix, so the whole circuit permutes the basis states, and from \(\ket{00}\) it reaches only another basis state.
A reversible circuit on \(m\) wires uses \(g\) gates, each an \(X\), CNOT, or Toffoli on chosen wires. Show that there are at most \((3m^3)^g\) such circuits, and deduce that most Boolean functions of \(n\) bits need at least about \(2^n/\log_2(3m^3)\) gates.
Show solutionHide solution
Each gate is one of three types placed on at most \(m^3\) choices of wires, so there are at most \(3m^3\) choices per gate and \((3m^3)^g\) circuits of \(g\) gates. There are \(2^{2^n}\) Boolean functions of \(n\) bits, and a circuit computes one function on a chosen output wire, so computing all of them needs \((3m^3)^g\geq2^{2^n}\), that is, \(g\geq2^n/\log_2(3m^3)\). For \(n=20\) and \(m=30\), this is about \(64{,}000\) gates. Universality guarantees that every function can be built, not that it can be built cheaply.
The exact universality theorem above assumed we could apply single-qubit rotations \(R_P(\theta)\) for any continuous real angle \(\theta\). Realistic fault-tolerant quantum architectures, however, provide only a finite discrete set of elementary gates. Can a finite gate set construct every unitary matrix with zero error? Never: from a finite alphabet of gates, there are only countably many finite circuits in total, whereas the set of unitary matrices forms an uncountable continuum. With a finite gate set, universality must mean approximating any target unitary \(U\) to within any desired accuracy \(\epsilon > 0\).
How do we measure the distance between an ideal unitary \(U\) and an approximating circuit \(V\)? We use the worst-case state-vector error over all normalized input states \(\ket{\psi}\):
This quantity is called the operator-norm error (or spectral-norm distance). Guaranteeing \(\|U - V\| \le \epsilon\) ensures that for every possible input state \(\ket{\psi}\), the output vector \(V\ket{\psi}\) lies within Euclidean distance \(\epsilon\) of the ideal output \(U\ket{\psi}\), which in turn keeps all outcome probabilities within \(2\epsilon\) of their ideal values. (When comparing standalone circuits, we match global phases before computing \(\|U - V\|\); if \(U\) is used inside a controlled gate, its phase becomes a relative phase and must be matched as well.)
The standard finite universal gate set is Clifford+T, consisting of just four gates: the Hadamard gate \(H\), the phase gate \(S\), the two-qubit CNOT gate, and the \(\pi/4\)-phase gate \(T\). While circuits built from the Clifford gates \(\{H, S, \operatorname{CNOT}\}\) alone can be simulated efficiently on a classical computer and cover only a finite group of rotations on one qubit, adding the single gate \(T\) makes the set dense in the unitary group: the Solovay–Kitaev theorem shows that any single-qubit gate can be approximated to error \(\delta\) using a remarkably short sequence of \(H\) and \(T\) gates (growing only polylogarithmically in \(1/\delta\)).
How do gate approximation errors accumulate across a multi-gate circuit? Suppose a two-gate circuit replaces the ideal gates \(U_1, U_2\) with approximations \(V_1, V_2\). By adding and subtracting \(V_2 U_1\), we can split the total error into two pieces:
Because unitary matrices \(V_2\) and \(U_1\) preserve vector lengths, the triangle inequality implies \(\|V_2 V_1 - U_2 U_1\| \le \|V_1 - U_1\| + \|V_2 - U_2\|\)! By induction, in a circuit of \(L\) gates where gate \(i\) has approximation error at most \(\epsilon_i\), the errors add at most linearly: \(\|V_L\cdots V_1 - U_L\cdots U_1\| \le \sum_{i=1}^L \epsilon_i\). Thus, to guarantee an overall circuit accuracy of \(\epsilon\) across \(L\) gates, it suffices to synthesize each individual gate to tolerance \(\epsilon/L\).
Each Vj approximates the corresponding Uj with a specified operator-norm error. The diagram compares two circuits; it does not assert an exact finite-alphabet synthesis.Problems
Each of \(1000\) gates is approximated within \(10^{-6}\). What error bound holds for the whole circuit?
Show solutionHide solution
Errors add at most, so the bound is \(1000\times10^{-6}=10^{-3}\).
A circuit has \(10^6\) gates and its total error must be at most \(10^{-2}\). What accuracy must each gate have if the error is split equally?
Show solutionHide solution
\(10^{-2}/10^6=10^{-8}\) per gate.
Prove that \(\|U_2U_1-V_2V_1\|\leq\|U_2-V_2\|+\|U_1-V_1\|\) for unitaries \(U_1,U_2,V_1,V_2\).
Show solutionHide solution
Write \(U_2U_1-V_2V_1=U_2(U_1-V_1)+(U_2-V_2)V_1\). For every unit vector \(v\), \(\|U_2(U_1-V_1)v\|=\|(U_1-V_1)v\|\leq\|U_1-V_1\|\), since \(U_2\) preserves length, and \(\|(U_2-V_2)V_1v\|\leq\|U_2-V_2\|\), since \(V_1v\) is a unit vector. The triangle inequality gives the result; repeating the argument covers \(L\) gates.
8.4
Implementation Resources
Showing that a unitary transformation can be built from elementary gates is only the first step toward a practical implementation. To evaluate what a quantum algorithm actually costs—both on paper and on physical hardware—we must track circuit size, depth, and workspace, account for device routing and data-loading overhead, and understand how quantum error correction protects fragile superpositions against physical noise.
When we analyze a quantum circuit, three primary resource metrics govern its cost: the gate count (the total number of elementary gates in the circuit), the depth (the number of sequential time layers when gates on disjoint wires run in parallel), and the width (the total number of qubits used by the circuit, including both input qubits and working ancilla qubits initialized in \(\ket{0}\)).
Just as in classical computer architecture, space (width) and time (depth) can often be traded against each other. Providing extra ancilla qubits allows intermediate sub-results to be computed in parallel and stored, reducing circuit depth; conversely, uncomputing and recomputing intermediate values on the fly saves ancilla qubits at the price of a larger gate count and greater depth.
One crucial theoretical requirement prevents a circuit family from hiding uncomputable or intractable work inside its wiring design. Because a circuit has a fixed number of input wires \(n\), solving a problem for inputs of arbitrary length requires a family of circuits \(\{C_n\}\)—one for each input size \(n\). A circuit family is called uniform if there is a single efficient (polynomial-time) classical algorithm that, given \(n\), outputs the gate-by-gate description of \(C_n\). Uniformity ensures that the work of designing the circuit is itself accounted for rather than smuggled in for free.
Three initial Hadamards run in parallel, followed by two successive CNOTs. This example has five gates, depth three, and width three when these gates are available directly.Problems
A circuit on three qubits applies \(H\) to each qubit, then CNOT from qubit 1 to qubit 2, then CNOT from qubit 2 to qubit 3. Give its gate count, depth, and width.
Show solutionHide solution
Gate count \(5\). Depth \(3\): the three \(H\) gates form one layer, and the two CNOT gates must follow one another, since both use qubit 2. Width \(3\).
Compute the AND of \(n=8\) bits with a chain of Toffoli gates: \(w_1=x_1x_2\), \(w_2=w_1x_3\), and so on, then the output, then uncompute the workspace. Give the gate count, depth, and width.
Show solutionHide solution
Computing uses \(n-1=7\) Toffoli gates (six into workspace bits, one into the output), and uncomputing uses \(6\) more: \(13\) gates. Each gate needs the previous result, so the depth is also \(13\). Width: \(8\) inputs, \(6\) workspace bits, and \(1\) output, \(15\) qubits.
Compute the AND of the same \(8\) bits with a balanced tree instead: combine bits in pairs, then pairs of results, and so on. Compare gate count, depth, and workspace with the chain.
Show solutionHide solution
The tree uses \(4\) Toffoli gates on the input pairs, \(2\) on their results, and \(1\) into the output: \(7\) gates in \(3\) layers. Uncomputing the \(6\) workspace bits takes \(6\) gates in \(2\) layers. Total: \(13\) gates, depth \(5\), and \(6\) workspace bits. The chain has the same gate count and workspace but depth \(13\). For \(n\) bits, the tree’s depth grows like \(2\log_2n\) rather than \(2n\).
On paper, we can draw a CNOT gate between the top wire and the bottom wire just as easily as between two adjacent wires. On physical quantum hardware—such as a superconducting chip arranged on a two-dimensional grid—two-qubit gates can usually act only between physically adjacent qubits. This hardware constraint is the device's connectivity graph. To apply a CNOT between two distant qubits on a sparse connectivity graph, the compiler must route their quantum states next to each other using a chain of two-qubit SWAP gates (where each \(\operatorname{SWAP}\ket{a}\ket{b} = \ket{b}\ket{a}\) can be built from three alternating CNOTs), increasing both gate count and circuit depth.
A second hidden cost is input preparation. If an algorithm assumes that a large classical table of \(N = 2^n\) numbers has already been loaded into the \(2^n\) amplitudes of an \(n\)-qubit state \(\ket{\psi_{\mathrm{in}}}\), preparing that state from \(\ket{0^n}\) can require \(2^n\) gates—completely swamping any polynomial or \(\sqrt{N}\) speedup of the subsequent algorithm! Similarly, when an oracle \(U_f\) is replaced by an explicit logic circuit, the gate complexity of evaluating \(f\) multiplies the query count.
Any realistic assessment of quantum advantage must therefore specify the elementary gate set, the hardware connectivity overhead, the cost of state preparation and oracle synthesis, and the number of measurement repetitions required.
For a linear three-qubit layout, SWAP brings the third logical qubit next to the first. CNOT acts there, and the second SWAP restores the original ordering. Each SWAP itself requires implementation.Problems
Three qubits sit on a line, \(1\)–\(2\)–\(3\), and only neighbours can interact. How many SWAP gates are needed to apply a CNOT between qubits 1 and 3 and restore the original placement?
Show solutionHide solution
Two: swap qubits 1 and 2, so that qubit 1’s state sits next to qubit 3; apply the CNOT; then swap back.
A SWAP gate costs three CNOT gates. Count the CNOT gates in the construction above, and for a CNOT between the two ends of a line of \(n\) qubits.
Show solutionHide solution
For three qubits: \(2\times3+1=7\) CNOT gates instead of \(1\). For \(n\) qubits, moving one end next to the other takes \(n-2\) SWAP gates, and moving back takes \(n-2\) more: \(6(n-2)+1=6n-11\) CNOT gates.
A data set of \(N=2^n\) numbers is loaded into amplitudes using one gate per number, and then searched with about \(\sqrt N\) queries. When does the quadratic query speedup survive in the total cost?
Show solutionHide solution
Loading costs about \(N\), more than the \(\sqrt N\) search, so a single search costs about \(N\), no better than scanning the data classically. The speedup survives only if the state is cheap to prepare, for example because it is produced by a short computation rather than loaded from data, or because one loaded state is used for many searches.
Even when a circuit is synthesized exactly on paper, physical quantum gates and idle wires suffer from environmental noise (decoherence, stray fields, and control imperfections). In classical computing, we protect against bit flips by making redundant copies (\(0\mapsto 000\), \(1\mapsto 111\)) and taking a majority vote. In quantum computing, two obstacles immediately seem to block that strategy: the no-cloning theorem forbids copying an unknown superposition \(a\ket{0}+b\ket{1}\) into \((a\ket{0}+b\ket{1})^{\otimes 3}\), and measuring the qubits to check for errors would collapse the superposition!
How can quantum error correction detect and fix an error without cloning the state and without measuring the encoded information? Let us trace the three-qubit bit-flip code. Instead of cloning \(a\ket{0}+b\ket{1}\), we apply two CNOT gates from our data qubit onto two ancilla qubits initialized in \(\ket{0}\), encoding the single logical qubit into an entangled three-qubit state:
Now suppose a stray bit-flip error \(X\) strikes the second physical qubit, corrupting the state into \(a\ket{010} + b\ket{101}\). How can we locate which qubit flipped without learning anything about \(a\) and \(b\)?
The secret is to measure two-qubit parities rather than individual qubit values! Notice that in both branches \(\ket{010}\) and \(\ket{101}\) of the corrupted state, qubits \(1\) and \(2\) disagree (parity \(x_1\oplus x_2 = 1\)), and qubits \(2\) and \(3\) also disagree (parity \(x_2\oplus x_3 = 1\)). By applying CNOTs from the data qubits into two fresh syndrome ancillas, we write the two parities \((x_1\oplus x_2,\, x_2\oplus x_3)\) into the ancillas and measure only the ancillas. Because both branches \(\ket{010}\) and \(\ket{101}\) have the exact same parities \((1,1)\), the ancilla measurement yields \((1,1)\) with certainty and leaves the superposition \(a\ket{010} + b\ket{101}\) completely uncollapsed! Those measured parity bits form the error syndrome: syndrome \((0,0)\) means no qubit flipped, \((1,0)\) means qubit \(1\) flipped, \((1,1)\) means qubit \(2\) flipped, and \((0,1)\) means qubit \(3\) flipped. Applying a corrective \(X\) gate to qubit \(2\) restores \(a\ket{000} + b\ket{111}\) exactly.
Full quantum error correction extends this parity-check principle to simultaneously correct arbitrary bit flips (\(X\)), phase flips (\(Z\)), and combined errors (\(Y\))—and fault-tolerant circuit design ensures that errors occurring inside the correction gates themselves do not spread uncontrollably. While fault-tolerant architecture lies beyond the scope of this book, the lesson is clear: quantum mechanics not only introduces continuous phase errors, but also supplies the entangled subspaces and projective syndrome measurements needed to correct them.
Two CNOTs encode one unknown qubit into a joint three-qubit state. This is the repetition-code encoding used for bit-flip protection; it is not a complete error-correction or fault-tolerance circuit.Problems
Each gate fails independently with probability \(10^{-3}\). What is the probability that a circuit of \(1000\) gates runs without any failure?
Show solutionHide solution
\((1-10^{-3})^{1000}\approx e^{-1}\approx0.37\).
How small must the failure probability per gate be for a circuit of \(10^9\) gates to run without failure with probability at least \(0.9\)?
Show solutionHide solution
We need \((1-q)^{10^9}\geq0.9\). Since \((1-q)^N\approx e^{-qN}\), this requires \(q\lesssim-\ln0.9/10^9\approx1.05\times10^{-10}\), far below what physical gates achieve. Long computations need error correction.
A repetition code stores a classical bit as three identical bits and decodes by majority. With independent flip probability \(0<q<1\), derive the failure probability and find when encoding helps. Now consider \(\alpha\ket{000}+\beta\ket{111}\): what does a phase flip \(Z\) on its first qubit do, and why does majority voting miss it?
Show solutionHide solution
Decoding fails after two or three flips: \(3q^2(1-q)+q^3=3q^2-2q^3\). This is smaller than \(q\) exactly for \(0<q<1/2\). But a phase flip gives \(\alpha\ket{000}-\beta\ket{111}\), which generally changes the encoded quantum state without changing any computational-basis bit pattern. Majority voting sees no error. Protecting quantum information therefore requires protection of relative phase as well as of bit values.
Summary
Reading circuits. Diagrams run left to right while matrix products act right to left. Gates on disjoint wires commute and can run in parallel, and a labelled box such as \(U_f\) hides gates whose cost the diagram does not show.
Two kinds of control. Classical control uses a measured bit to choose a later gate; coherent control acts on an unmeasured qubit and preserves superposition. Teleportation needs both.
Building blocks. Rotation gates \(R_P(\theta)=\cos\tfrac\theta2\,I-i\sin\tfrac\theta2\,P\) give turns of any size, and controlled-\(U=\ket0\bra0\otimes I+\ket1\bra1\otimes U\). Hadamards on the target turn CNOT into controlled-\(Z\).
Oracles and reflections are circuits too. A phase oracle marks a string with \(X\) gates around a multi-controlled phase flip, or by compute–phase–uncompute. The reflection about the uniform state is \(D=H^{\otimes n}(2\ket{0^n}\bra{0^n}-I)H^{\otimes n}\).
Universality needs entanglement. One-qubit gates alone map product states to product states. CNOT together with arbitrary one-qubit unitaries builds every finite-register unitary. That settles possibility, not cost: a generic unitary needs an exponentially long circuit.
Finite gate sets approximate. A finite set such as Clifford+T cannot synthesize every unitary exactly, but it can approximate one to any tolerance. The errors of approximated gates add at most linearly, \(\sum_i\epsilon_i\).
Real cost is more than a query count. It also includes gate count, depth, width (ancillas), uniform circuit families, connectivity (extra SWAPs), data loading and readout, and noise. Error correction protects logical information without cloning it.
Questions and worked problems
Attempt each question before opening a hint. Explain your reasoning, then compare it with the solution.
Compute \(R_Y(\pi/2)=\cos\frac\pi4I-i\sin\frac\pi4Y\) as a matrix. Compare \(R_Y(\pi/2)\ket0\) with \(H\ket0\), and \(R_Y(\pi/2)\ket1\) with \(H\ket1\). Are the two gates the same? Express \(R_Y(\pi/2)\) using \(H\) and one Pauli gate.
Hint 1
\(-iY=\begin{pmatrix}0&-1\\1&0\end{pmatrix}\).
Hint 2
Compare the columns of the two matrices.
Worked solution
\(R_Y(\pi/2)=\frac{1}{\sqrt2}\begin{pmatrix}1&-1\\1&1\end{pmatrix}\). On \(\ket0\) both gates give \((\ket0+\ket1)/\sqrt2\). On \(\ket1\), \(R_Y(\pi/2)\) gives \((-\ket0+\ket1)/\sqrt2=-\ket-\), while \(H\) gives \(\ket-\). The sign is relative to the \(\ket0\) column, so the gates differ: on \(\ket+\), for example, \(R_Y(\pi/2)\) gives \(\ket1\) and \(H\) gives \(\ket0\). In fact \(R_Y(\pi/2)=HZ\): apply \(Z\), then \(H\). Agreement on one input does not identify a gate; a unitary is fixed by its action on every basis state, including phases.
A gate \(V\) equals \(U\) up to the global phase \(i\): \(V=iU\). Apply controlled-\(U\) and controlled-\(V\) to \(\ket+\ket\psi\) and compare the results. What one-qubit gate on the control accounts for the difference?
Hint 1
Controlled-\(V\) applies \(iU\) only on the control-\(1\) branch.
Hint 2
\(S=\mathrm{diag}(1,i)\).
Worked solution
Controlled-\(U\) gives \(\frac{1}{\sqrt2}(\ket0\ket\psi+\ket1U\ket\psi)\). Controlled-\(V\) gives \(\frac{1}{\sqrt2}(\ket0\ket\psi+i\ket1U\ket\psi)\). The factor \(i\) appears only on the control-\(1\) branch, so it is now a relative phase between branches: controlled-\(V\) equals controlled-\(U\) followed by \(S\) on the control. A phase that is invisible when \(U\) is applied alone becomes observable once \(U\) is controlled, which is why approximations used inside controlled operations must fix the phase, as Section 8.3.3 warns.
Build the complete two-qubit Grover circuit for the marked item \(10\): the oracle from an \(X\) gate and a controlled-\(Z\), and the reflection from \(H\), \(X\), and controlled-\(Z\) gates. Verify that one iteration from \(\ket s\) gives \(10\).
Hint 1
The oracle flips the sign of \(\ket{10}\): apply \(X\) to the qubit that should be \(0\).
Hint 2
The reflection is \(H^{\otimes2}(X\otimes X)\,CZ\,(X\otimes X)H^{\otimes2}\).
Worked solution
Oracle: \(X\) on qubit 2, controlled-\(Z\), \(X\) on qubit 2. It sends \(\ket{10}\) to \(\ket{11}\), applies the sign, and restores \(\ket{10}\). Starting from \(\ket s=\tfrac12(1,1,1,1)\), the oracle gives \(\tfrac12(1,1,-1,1)\). The reflection circuit implements \(-D\), and \(D\) maps this state to \(\ket{10}\), so the result is \(-\ket{10}\): the outcome is \(10\) with certainty.
Show that the reflection \(D=2\ket s\bra s-I\) about the uniform state acts on amplitudes as “inversion about the mean”: if \(\ket\psi=\sum_xa_x\ket x\) and \(\bar a\) is the average of the \(a_x\), then \(D\ket\psi=\sum_x(2\bar a-a_x)\ket x\). Use it to redo one Grover iteration on two qubits with marked item \(10\).
After the oracle, the amplitudes are \(\frac12,\frac12,-\frac12,\frac12\).
Worked solution
Since \(\braket s\psi=\sqrt N\,\bar a\), we have \(2\ket s\braket s\psi=2\sqrt N\,\bar a\cdot\frac{1}{\sqrt N}\sum_x\ket x=\sum_x2\bar a\ket x\). Subtracting \(\ket\psi\) gives amplitudes \(2\bar a-a_x\): each amplitude is reflected about the mean. For two qubits, \(\ket s\) has all amplitudes \(\frac12\). The oracle for \(10\) makes them \(\frac12,\frac12,-\frac12,\frac12\), with mean \(\frac14\). Reflecting gives \(2\cdot\frac14-\frac12=0\) for the three unmarked items and \(2\cdot\frac14+\frac12=1\) for \(10\). One iteration finds the marked item with certainty. The negative amplitude lies far below the mean, so the reflection throws it far above.
On two qubits, build the phase oracle \((-1)^{x_1\lor x_2}\) for OR. (a) Show that \(X\otimes X\), then controlled-\(Z\), then \(X\otimes X\), multiplies only \(\ket{00}\) by \(-1\). (b) Explain why this circuit implements the OR phase oracle up to a global phase. (c) When would that global phase matter?
Hint 1
\(X\otimes X\) maps \(\ket{00}\) to \(\ket{11}\), the only state that controlled-\(Z\) changes.
Hint 2
Compare the sign patterns \((-,+,+,+)\) and \((+,-,-,-)\).
Worked solution
(a) \(X\otimes X\) sends \(\ket{00}\) to \(\ket{11}\), controlled-\(Z\) multiplies it by \(-1\), and \(X\otimes X\) returns it to \(\ket{00}\). Every other basis state is mapped away from \(\ket{11}\) and back without a sign. So the circuit has signs \((-,+,+,+)\) on \(00,01,10,11\). (b) OR is \(0\) only on \(00\), so its phase oracle has signs \((+,-,-,-)\), exactly \(-1\) times the circuit. Used by itself, as in a search iteration, the factor \(-1\) multiplies the whole state and changes no probability. (c) If the oracle is controlled by another qubit, the \(-1\) acts only on the control-\(1\) branch and becomes a \(Z\) on the control, so the correct sign must be restored, for example by a \(Z\) on that control.
Grover search over \(N=2^{20}\) items uses about \((\pi/4)\sqrt N\) iterations, each compiled into \(300\) elementary gates. (a) Estimate the total gate count. (b) If each gate fails independently with probability \(10^{-4}\), estimate the probability that the whole circuit runs without a failure. (c) What failure probability per gate would give at least \(0.9\)?
Hint 1
\((1-p)^G\approx e^{-pG}\) for small \(p\).
Hint 2
\(\sqrt{2^{20}}=1024\).
Worked solution
(a) About \(\frac\pi4\times1024\approx804\) iterations, so roughly \(804\times300\approx2.4\times10^5\) gates. (b) \(e^{-10^{-4}\times2.4\times10^5}=e^{-24}\approx3\times10^{-11}\): essentially never. (c) We need \(e^{-pG}\ge0.9\), so \(p\le0.105/(2.4\times10^5)\approx4\times10^{-7}\). An ideal algorithm whose circuit grows like \(\sqrt N\) requires error rates that shrink like \(1/\sqrt N\). Without error correction, a quadratic query advantage can be erased by noise long before \(N\) is large. This is the reliability requirement of Section 8.4.3 in numbers.
A student says: “CNOT and one-qubit gates are universal, so any unitary on \(50\) qubits can be compiled into a manageable circuit.” Use a parameter count to estimate how many gates a generic \(50\)-qubit unitary needs, and say what universality does and does not promise.
Hint 1
An \(n\)-qubit unitary is a \(2^n\times2^n\) matrix with about \(4^n\) real parameters.
Hint 2
Each one- or two-qubit gate contributes only a bounded number of parameters.
Worked solution
A unitary on \(n\) qubits has \(4^n\) real parameters (up to a constant), and each one- or two-qubit gate contributes at most \(16\). A circuit for a generic unitary therefore needs on the order of \(4^n/16\) gates. For \(n=50\), \(4^{50}\approx1.3\times10^{30}\), so roughly \(10^{29}\) gates, far beyond any device. Universality guarantees that every unitary can be built, or approximated, from the gate set. It does not promise that the circuit is short. Useful algorithms use unitaries with structure, such as oracles, reflections, and Fourier transforms, that admit polynomial-size circuits.
A search over \(N=2^n\) items uses \((\pi/4)\sqrt N\) iterations. The oracle is compiled into \(20n\) gates and the reflection into \(10n\) gates. Give the total gate count as a function of \(n\), and evaluate it for \(n=30\).
Hint 1
Multiply the number of iterations by the gates per iteration.
Hint 2
\(\sqrt N=2^{n/2}\).
Worked solution
The total is about \((\pi/4)\,2^{n/2}\cdot30n\). For \(n=30\): \((\pi/4)\times32{,}768\times900\approx2.3\times10^7\) gates. The query count, about \(2.6\times10^4\), hides a factor of \(30n=900\) gates per query.
Use counting to bound the cost of approximation with a finite gate set. Single-qubit unitaries up to global phase form a three-parameter family, so at least about \((1/\epsilon)^3\) circuits are needed to approximate every one within \(\epsilon\). A gate set has \(k\) single-qubit gates. (a) Show that circuits of length at most \(g\) can meet this target only if \(g\gtrsim3\log(1/\epsilon)/\log k\). (b) Evaluate for \(k=3\) (\(H,S,T\)) and \(\epsilon=10^{-10}\). (c) What does the bound say about how gate count grows as \(\epsilon\) shrinks?
Hint 1
There are at most about \(k^g\) circuits of length \(g\).
Hint 2
Require \(k^g\ge(1/\epsilon)^3\) and take logarithms.
Worked solution
(a) With \(k\) gates there are at most about \(k^g\) circuits of length \(g\) (a little more counting shorter ones, which does not change the estimate). Covering all unitaries within \(\epsilon\) needs \(k^g\gtrsim(1/\epsilon)^3\), so \(g\gtrsim3\ln(1/\epsilon)/\ln k\). (b) \(3\ln10^{10}/\ln3\approx3\times23.0/1.10\approx63\) gates. (c) The required length grows only logarithmically in \(1/\epsilon\): each additional decimal digit of accuracy costs a few more gates. Good synthesis methods for Clifford+\(T\) achieve length proportional to \(\log(1/\epsilon)\), so this counting bound is attainable up to a constant factor, which is why rotation synthesis is cheap per gate but adds up over a long circuit.