B10 · Lesson 45 · Quantum gates and circuits
Building computations from one- and two-qubit operations
Lesson 26 turned a qubit with a timed microwave pulse: a single-qubit gate. Lesson 44 showed that two qubits can share an entangled state with no classical counterpart. A quantum computer is built from just these pieces: single-qubit rotations and one two-qubit gate that can entangle, arranged in a circuit and followed by measurement. This lesson writes the gates as matrices, builds an entangled pair from scratch, runs a small algorithm that beats any classical one, and counts what errors cost, which is why a superconducting processor's gate fidelity is quoted to three or four nines.
- A circuit of gates, each succeeding with fidelity , gives the right answer with probability about . The curve shows that against .
- At 99% per gate, a hundred gates leave a 37% chance of success; at 99.9%, 90%. Deep circuits need either much better gates or error correction.
Predict
A Hadamard gate takes to . What does a second Hadamard do to that state?
It returns it to : . The two paths to arrive with opposite signs and cancel, while the paths to add. That interference, impossible with classical probabilities, is what quantum algorithms exploit: arrange the paths so wrong answers cancel and right ones add.
Name it
Single-qubit gates
With and , gates are unitary matrices:
flips (a NOT gate), flips the phase, makes equal superpositions, and turns the phase. On hardware each is a calibrated pulse, a rotation on lesson 26's Bloch sphere.
Two qubits and the CNOT
Two qubits live in a four-dimensional space with basis (first qubit first). The controlled-NOT flips the second qubit when the first is 1:
A gate on one qubit alone is a Kronecker product with the identity: on the first qubit is . Single-qubit gates plus CNOT are universal: any unitary on any number of qubits can be built from them.
A Bell pair
Start in , apply to the first qubit, then CNOT:
Measuring either qubit gives 0 or 1 at random, but the two always agree: lesson 44's entangled state, made with two gates.
Measurement
A measurement in the basis returns each outcome with probability and leaves the qubit in that state. To learn an amplitude you repeat the whole circuit many times; to measure in another basis you rotate first.
Errors
Each gate succeeds with fidelity , so a circuit of gates succeeds with probability about , and each qubit loses its state over its coherence time . With 30 ns gates and = 100 µs, a qubit lasts about 3300 gate times. Error correction encodes one logical qubit in many physical ones, and only works once is below a threshold near 1%.
Worked example
Apply twice to , writing out the matrices.
Show the solution
- First Hadamard.
- Second Hadamard.
- Why |1⟩ cancels. The amplitude is : two paths, one through and one through , with opposite signs.
- Measuring in between. Measuring between the two Hadamards would destroy that cancellation and leave a 50-50 result.
Answer two Hadamards bring back to .
Worked example
Deutsch's problem: a black-box function takes one bit to one bit. Is it constant () or balanced? Classically you must evaluate twice. Show how one quantum evaluation suffices.
Show the solution
- Prepare. Start in and apply to both qubits.
- Query the box once. It maps . The second qubit, in , turns the query into a phase on the first qubit's , so the first qubit becomes
- Hadamard and measure. Apply to the first qubit and measure it. The Hadamard sends that state to if is constant and if balanced.
- One query. One query, a certain answer: the global property was read out through interference.
Answer after one query and a final Hadamard, the first qubit reads 0 if is constant and 1 if it's balanced.
Worked example
A processor's two-qubit gates have 99.5% fidelity and its single-qubit gates 99.95%. Estimate the success probability of a circuit with 40 two-qubit and 120 single-qubit gates.
Show the solution
- Two-qubit gates.
- Single-qubit gates.
- The whole circuit.
- Which gates cost more. The two-qubit gates, fewer but ten times as error-prone, cost three times as much. That's why circuit compilers work hardest to minimize two-qubit gates, and why two-qubit fidelity is the headline number for a quantum processor.
Answer the circuit succeeds with probability of about 0.771.
Check
A circuit has 100 gates, each with an error probability of 0.001. About what is the chance all of them succeed?
Check
Start in and apply a Hadamard gate.
(a)
What is the amplitude of afterward?
(b)
And of ?
Check
Using NumPy, build H, I2 and CNOT as matrices and start from the two-qubit state whose basis index is k = 1 ( are 0 to 3). Apply to the first qubit (np.kron(H, I2)), then CNOT. Set psi to the final state and probs to the four measurement probabilities.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Simulate Deutsch's algorithm for the one-bit function picked by f = 1: 0 is , 1 is , 2 is , 3 is . Build the oracle as a matrix mapping , start from , apply , the oracle, then , and set p1 to the probability that the first qubit is 1, and balanced to whether p1 > 0.5.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A qubit's coherence time is 50 µs and its gates take 30 ns. About how many gate times fit in one coherence time?
Check
Apply to a general state with real amplitudes.
Step 1
What is the new amplitude of ?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
And of ?
Step 3
Apply again. What is the amplitude of now?
Check
Why does a Hadamard on the first qubit followed by a CNOT entangle two qubits, while either gate alone does not?
Explain why
Why is a circuit's success probability so sensitive to the error rate per gate, and what does that imply for building a useful quantum computer?
Every gate must succeed for the computation to be right, so the success probabilities multiply: gates at error give about . That falls off exponentially once is near 1, so a circuit's useful depth is roughly gates: a hundred at 1% error, ten thousand at 0.01%. Useful algorithms need millions of gates or more, far beyond any physical error rate, so a practical machine must use error correction, spreading each logical qubit over many physical ones and repeatedly detecting and fixing errors, which works only once the physical error rate is below the code's threshold.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.