Gates
Quantum gates explained
A quantum gate is a fixed rule that rewrites the two amplitudes of a qubit, the number for 0 and the number for 1, without reading it. The six gates a beginner meets first are H, which adds and subtracts the two numbers, X, which swaps them, Z, which flips the sign of the number for 1, S, which multiplies that number by i, Y, which swaps them and adds factors of −i and i, and CNOT, which flips a second qubit wherever the first would read 1.
This page puts all six in one place, with the rule for each, a worked example and a link to the full entry in the Nerdictionary. Write a for the amplitude of 0 and b for the amplitude of 1, so the qubit is a|0⟩ + b|1⟩ in ket notation. The chances of reading 0 and 1 are the squared sizes of a and b.
All six gates at a glance
| Gate | What it does to (a, b) | On |0⟩ | On |1⟩ |
|---|---|---|---|
| H | ((a + b) ÷ √2, (a − b) ÷ √2) | |+⟩ | |−⟩ |
| X | (b, a) | |1⟩ | |0⟩ |
| Y | (−ib, ia) | i|1⟩ | −i|0⟩ |
| Z | (a, −b) | |0⟩ | −|1⟩ |
| S | (a, ib) | |0⟩ | i|1⟩ |
| CNOT | Two qubits. Swaps the amplitudes on 10 and 11, so 00 and 01 stay put, 10 becomes 11, and 11 becomes 10. | ||
A factor such as −1 or i in front of a whole state is a global phase. No measurement can see it, so −|1⟩ and i|1⟩ read just like |1⟩. A factor matters only when it lands on one amplitude and not the other.
What is a quantum gate?
A gate takes the amplitudes in and gives new amplitudes out, by the same rule every time. Three things set quantum gates apart from the logic gates in a laptop.
- They never lose information, so every one can be run backwards. An AND gate that prints 0 cannot tell you whether its inputs were 00, 01 or 10. A quantum gate always can.
- They are not random and they do not read anything. Chances turn into bits only at the measurement at the end.
- They keep the squared sizes adding to 1.
On real hardware a gate is usually a timed pulse of microwaves or laser light. In a drawing it is a box on a wire, and a row of boxes is a circuit.
What does the Hadamard gate do?
H makes the new amplitude for 0 from the sum, a + b, and the new amplitude for 1 from the difference, a − b, then divides both by √2, about 1.41. On |0⟩ it gives 0.71 and 0.71, the |+⟩ state, an even superposition. That does not make the qubit 0 and 1 at once. It holds two numbers, and a reading gives one bit. On |1⟩ it gives 0.71 and −0.71, the |−⟩ state.
H is not a coin toss. Apply it twice and you are back where you started, because the extra routes cancel. So H both makes superpositions and turns hidden signs into readings, which is why it turns up in so many circuits. The Hadamard gate entry has more worked sums.
What does the X gate do?
X swaps a and b. On |0⟩ and |1⟩ that is the ordinary bit flip, so X is often called the quantum NOT. A sign travels with its number, so 0.28 and −0.96 become −0.96 and 0.28. On |+⟩, X does nothing, because swapping two equal numbers changes nothing. See the X gate entry.
What does the Z gate do?
Z leaves a alone and flips the sign of b. The chances do not move, so a reading straight after Z looks just like a reading before it. The change shows after a later gate. Z turns |+⟩ into |−⟩, and an H after that reads 1 every time where it would have read 0. Some books call Z a phase flip, since the sign is the phase. See the Z gate entry.
What does the S gate do?
S leaves a alone and multiplies b by i, the number whose square is −1. Think of a plus sign as pointing at 0 degrees and a minus sign at 180 degrees. Then i points at 90 degrees, so S turns the relative phase by a quarter turn. Apply S twice and b has been multiplied by i × i = −1, which is a Z. Unlike the other gates here, S does not undo itself, and it takes four presses to return to the start. See the S gate entry, and complex number if i is new.
What does the Y gate do?
Y swaps the two amplitudes, then multiplies the new amplitude for 0 by −i and the new amplitude for 1 by i. Written as a table of numbers it is
On |0⟩ it gives i|1⟩, which reads 1 every time, just as X would. The difference shows on a superposition. Y is the same as Z, then X, then an overall factor of i, so it flips the bit and the sign together. See the Y gate entry.
What does the CNOT gate do?
CNOT, the controlled NOT, acts on two qubits. Their four amplitudes sit on 00, 01, 10 and 11, with the first qubit, the control, as the left digit. CNOT flips the second qubit, the target, wherever the control would read 1. So 00 stays 00, 01 stays 01, 10 becomes 11, and 11 becomes 10.
Put H on the control first and CNOT makes entanglement. From 00, H gives 0.71 on 00 and 0.71 on 10, and CNOT moves the second number to 11. The pair now reads 00 or 11, each half the time, and never 01 or 10. That is a Bell state.
CNOT is not a copy machine. On a plain 0 or 1, with the target at 0, it seems to copy the control onto the target, but no gate can copy an unknown qubit.
Worked example, one qubit through each gate
Take a qubit with 0.6 for 0 and 0.8 for 1, so its chances are 0.36 and 0.64. Each row starts from a fresh copy.
| Gate | Amplitude for 0 | Amplitude for 1 | Chance of 0 | Chance of 1 |
|---|---|---|---|---|
| H | 0.99 | −0.14 | 0.98 | 0.02 |
| X | 0.8 | 0.6 | 0.64 | 0.36 |
| Y | −0.8i | 0.6i | 0.64 | 0.36 |
| Z | 0.6 | −0.8 | 0.36 | 0.64 |
| S | 0.6 | 0.8i | 0.36 | 0.64 |
The H row comes from (0.6 + 0.8) ÷ 1.41 and (0.6 − 0.8) ÷ 1.41. The squares are 1.96 ÷ 2 = 0.98 and 0.04 ÷ 2 = 0.02. Z and S leave the chances where they were, and their work shows only after a later gate such as H.
For CNOT, give the control those same numbers and start the target at 0. The four amplitudes on 00, 01, 10 and 11 are 0.6, 0, 0.8 and 0. CNOT swaps the last two, giving 0.6, 0, 0 and 0.8. The pair reads 00 with chance 0.36 and 11 with chance 0.64, and the two qubits always agree.
How do gates combine?
Gates run in order, left to right on the circuit drawing, and the order matters. On |0⟩, H then X gives |+⟩, while X then H gives |−⟩. The note The left-hand gate runs first works both, and The sign is what cancels runs H, Z, H. A few combinations are worth knowing by heart.
- H, then Z, then H acts as X.
- S twice is Z.
- Z, then X, then an overall factor of i is Y.
- H, X, Y, Z and CNOT each undo themselves when applied twice.
H, S and CNOT together build many useful states, every Bell state among them, but not everything. Circuits made only from those three, started from plain 0s and read as 0s and 1s, can be simulated quickly on an ordinary computer. Add one more gate, T, which turns the phase of the amplitude for 1 by an eighth of a full turn, and the set can approximate any circuit.
Practise the gates
Press each gate on the qubit page, where one qubit is drawn as an arrow on the Bloch sphere. Build named states from the gates in the puzzle. When the table above feels familiar, try Which Gate, a quiz on H, X, Y, Z and S, and Which Matrix, which asks what a product of gates equals. New to all this? Start with What is quantum computing? or the reading order in Quantum computing for beginners.
Questions people ask
What is a quantum gate in simple terms?
It is a fixed rule that changes the two numbers a qubit carries, one for 0 and one for 1. It does not read the qubit, it is not random, and it can always be undone.
Which quantum gate creates a superposition?
The Hadamard gate, H. On a qubit certain to read 0 it gives two equal amplitudes of about 0.71, so a reading is then a fair toss between 0 and 1.
What is the difference between the X gate and the Z gate?
X swaps the amplitudes for 0 and 1, so it changes the chances of each reading. Z flips the sign of the amplitude for 1, so it leaves the chances alone and changes what later gates do.
Are quantum gates reversible?
Yes. Every quantum gate can be undone by another gate. H, X, Y, Z and CNOT each undo themselves, and one S is undone by three more presses of S.
Can a CNOT gate copy a qubit?
Only a plain 0 or 1, onto a target that starts at 0. With the control in superposition and the target at 0, CNOT makes an entangled pair, not two copies, and no gate can copy an unknown qubit.