Gates & Circuits
Programming a quantum computer: rotations, controls and the circuit model.
A quantum program is a list of gates. On one qubit, every gate is simply a rotation of the Bloch sphere; on two qubits, a gate can bind them into a single entangled state. In this lesson you will rotate, flip, phase and entangle qubits by hand, and see why a toolbox of just three gates is enough to run any quantum algorithm.
What you’ll learn
- Picture the single-qubit gates , , , , and as rotations of the Bloch sphere.
- Read a circuit diagram and turn a sequence of gates into a product of matrices, in the right order.
- Use the CNOT to entangle two qubits and the Toffoli gate to compute AND reversibly.
- Explain why quantum gates must be reversible and what makes a gate set universal.
Gates are rotations
In an ordinary computer, logic gates such as NOT, AND and OR transform bits. A quantum gate transforms qubits under two strict rules: it must keep the total probability equal to 1, and it must be reversible, so no information is ever destroyed. Matrices that obey both rules are called unitary.
For a single qubit this has a beautiful geometric meaning: every one-qubit gate is a rotation of the Bloch sphere. The three Pauli gates , and are half-turns (180°) about the , and axes.
Think first left and exactly where they were. Is it a useless gate?
No. and : on a pole it only multiplies the state by a sign, an unobservable global phase. On a superposition, though, the sign lands on one amplitude only and becomes a relative phase: turns into , a half-turn around the equator. A rotation leaves the points on its own axis fixed and moves everything else.
Hadamard and the phase gates
Two more gates appear in almost every algorithm. The Hadamard gate is a half-turn about the diagonal axis halfway between and . It swaps the poles with the equator, and , and applying it twice brings you home.
The phase gates turn the sphere about the -axis by smaller angles: by a quarter-turn (90°) and the T gate by an eighth of a turn (45°). Like , they never change the odds of measuring 0 or 1. They only shift the relative phase.
| Gate | Rotation of the Bloch sphere | Example |
|---|---|---|
| half-turn about | (bit flip) | |
| half-turn about | (phase flip) | |
| half-turn about the – diagonal | and | |
| quarter-turn about | ||
| eighth-turn about | a point 45° further along the equator |
How many gates in a row bring every state back to exactly where it started?
Show the answer
8 T gates
Each is a 45° turn about , so eight of them make a full 360° turn: exactly. Along the way, and .
Reading a circuit
A circuit diagram is a quantum program written like a musical score. Each horizontal line, a wire, is a qubit that starts in on the left. Time runs from left to right, and each box is a gate applied at that moment.
There is one trap. If a circuit applies first and then , the state becomes , so the whole circuit is the matrix product . Matrices are written in the reverse of the order in which you read the circuit. And order matters: in general .
A circuit applies first and then . Which matrix describes the whole circuit?
Show the answer
C.
The first gate acts first on the ket, so it sits rightmost: . Check on : , whereas .
Two qubits: the controlled-NOT
With two qubits, a gate can make one qubit's fate depend on the other's. The CNOT (controlled-NOT) has a control, drawn as a dot, and a target, drawn as . It flips the target if and only if the control is .
| Input | Output | What happened |
|---|---|---|
| control is 0: nothing | ||
| control is 0: nothing | ||
| control is 1: target flipped | ||
| control is 1: target flipped |
On basis states the CNOT is ordinary reversible logic. The quantum surprise comes when the control is in superposition: the CNOT acts on both branches at once, and the two qubits become entangled. Neither qubit has a state of its own any more; only the pair does.
Toffoli: logic without erasure
Ordinary AND is not reversible: if it outputs 0, you cannot tell whether the inputs were 00, 01 or 10. The Toffoli gate avoids this by keeping its inputs. It has two controls and one target, and flips the target only when both controls are :
With the target starting in , it writes there. Since AND and NOT can build any Boolean function, a quantum computer can run any classical computation, reversibly.
Why can't a quantum circuit simply use an ordinary AND gate, with two inputs and one output?
- AND is too slow on quantum hardware
- AND only accepts the inputs 0 and 1, never superpositions
- AND erases information (output 0 has three possible inputs), but quantum gates must be reversible
- Quantum computers never need AND; they only use rotations
Show the answer
C. AND erases information (output 0 has three possible inputs), but quantum gates must be reversible
Every quantum gate is unitary, hence invertible: the input must be recoverable from the output. A two-to-one AND cannot be inverted. The Toffoli keeps its inputs, so it is reversible and works on superpositions too.
A small toolbox is enough
How many different gates does a quantum computer need? Remarkably few. Sequences of just and can approximate any single-qubit rotation as closely as you like, and adding the CNOT gives a universal gate set: can approximate any quantum computation on any number of qubits.
Think first If and only approximate most rotations, how can a computer run an algorithm exactly?
It does not need to. Errors from approximating each gate add up at most linearly, so if each of gates is within of its target, the whole circuit is within . The Solovay–Kitaev theorem guarantees that this costs only a modest overhead: the number of and gates per rotation grows like a power of .
The circuit model, and what comes next
Put it all together and you have the circuit model of quantum computing: prepare qubits in , apply a sequence of gates, then measure. Two numbers describe a circuit's size: its width (how many qubits) and its depth (how many layers of gates must run one after another). Depth matters because real qubits suffer noise: the longer the circuit runs, the more errors creep in.
| Excited State lesson | What you will do there |
|---|---|
| Single-Qubit Gates as Rotations | Derive and write every gate, , and included, as a rotation. |
| Multi-Qubit & Controlled Gates | Build CZ, SWAP and controlled- gates out of CNOTs, and prove the identities you used here. |
| Universality & Gate Sets | See why is universal, and meet the Solovay–Kitaev and Gottesman–Knill theorems. |
| The Circuit Model | Formalize depth and width, mid-circuit measurement and classical control. |
Key takeaways
- Single-qubit gates are rotations of the Bloch sphere: , , are half-turns, swaps the poles with the equator, and and are quarter- and eighth-turns about .
- Gates are unitary, hence reversible. A circuit that applies and then is the matrix product , and order matters.
- Phase gates change only the relative phase, yet sandwiched between Hadamards a phase flip becomes a bit flip: .
- The CNOT flips its target when the control is ; with the control in superposition it creates entanglement. The Toffoli computes AND reversibly.
- is universal. The Clifford gates , and CNOT alone are classically simulable; supplies the missing power.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which gate takes to ?
Show the answer
C.
is a half-turn about the – diagonal; it swaps the north pole with the point on the equator, so .
Which gates never change the probabilities of measuring 0 or 1, whatever state they act on?
- , and
- and
- Only the identity
- and
Show the answer
A. , and
, and are rotations about the -axis. They change the longitude (the relative phase) but never the latitude , which alone sets .
A CNOT with qubit 0 as control acts on . What comes out?
Show the answer
D.
The control (left bit) is 1, so the target (right bit) flips: .
Starting from , which circuit produces the Bell state ?
- A CNOT, then on qubit 0
- on qubit 0, then a CNOT with control 0 and target 1
- on both qubits
- on qubit 0, then a CNOT
Show the answer
B. on qubit 0, then a CNOT with control 0 and target 1
puts the control in ; the CNOT then flips the target only in the branch, giving .
Which gate set is universal, able to approximate any quantum computation?
Show the answer
C.
and generate rotations that approximate any single-qubit gate, and CNOT supplies entanglement; together they are universal.
Go deeper
- Single-Qubit Gates as Rotations · Pauli, Hadamard, phase and rotation gates on the Bloch sphere.
- Multi-Qubit & Controlled Gates · CNOT, CZ, SWAP, Toffoli and controlled-U.
- Universality & Gate Sets · A finite set of gates can approximate any computation.
- The Circuit Model · Reading circuits, depth and width, measurement and classical control.
Further reading
- M. A. Nielsen & I. L. Chuang, Quantum Computation and Quantum Information, §1.3 and §§4.1–4.3 (Cambridge, 10th anniv. ed., 2010).
- R. de Wolf, Quantum Computing: Lecture Notes, chapter on the circuit model (arXiv:1907.09415).