A gate is a step that changes amplitudes
A quantum gate is a step that changes the amplitudes of a qubit. It is a small grid of numbers, a matrix, and applying it is a short piece of multiplying.
Gates matter because a quantum program does nothing else. It applies gates, then it measures. In this lesson you apply the three gates of the lab, X, Z and H, by hand and in the simulator.
By the end of this lesson
Note
I can apply a gate to a qubit.
Key words
| Word | What it means | Example |
|---|---|---|
| Gate | a step that changes a state | |
| Matrix | a grid of numbers in rows and columns | |
| X gate | swaps the two amplitudes, so 0 becomes 1 | |
| Z gate | flips the sign of the amplitude of 1 | |
| H gate | turns a certain answer into an equal mix | Hadamard |
| Apply | to multiply a state by a gate's matrix |
The cells students fill in are left blank in this preview.
A gate is a matrix times a state
A gate for one qubit is a 2 by 2 matrix: two rows of two numbers. To apply it to a state [a, b], make each new amplitude from one row.
The new first amplitude is the first row's first number times a, plus its second number times b. The new second amplitude does the same with the second row.
Worked example: the X gate on two states
| State | New first amplitude | New second amplitude | New state |
|---|---|---|---|
| [1, 0] | 0 x 1 + 1 x 0 = 0 | 1 x 1 + 0 x 0 = 1 | [0, 1] |
| [0.6, 0.8] | 0 x 0.6 + 1 x 0.8 = 0.8 | 1 x 0.6 + 0 x 0.8 = 0.6 | [0.8, 0.6] |
The cells students fill in are left blank in this preview.
Apply a gate in plain Python
X = [[0, 1], [1, 0]]
def apply_gate(gate, state):
result = []
for row in gate:
total = 0
for i in range(len(state)):
total = total + row[i] * state[i]
result.append(total)
return result
# Press Run. Then change the state to [1, 0] and run it again.
print(apply_gate(X, [0.6, 0.8]))
Three gates do three different jobs
The X gate swaps the two amplitudes, so it turns 0 into 1 and 1 into 0. The Z gate leaves the amplitude of 0 alone and flips the sign of the amplitude of 1.
The H gate turns a certain answer into an equal mix. It is built from the number 0.707, which is 1 divided by the square root of 2. It is the gate you will use most.
Worked example: what each gate does
| Gate | Matrix rows | On |0> | On |1> |
|---|---|---|---|
| X | [0, 1] and [1, 0] | |1> | |0> |
| Z | [1, 0] and [0, -1] | |0> | minus |1> |
| H | [1, 1] and [1, -1], all divided by the square root of 2 | 0.707|0> + 0.707|1> | 0.707|0> - 0.707|1> |
The cells students fill in are left blank in this preview.
Run the gates script
docker exec jupyter python3 steps/gates.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Two routes to one state
The script printed X on |0> as 1.000|1> and H on |0> as 0.707|0> + 0.707|1>. The chances after H are [0.5 0.5].
Now try Z after H. It gives 0.707|0> - 0.707|1>. H applied to |1> gives the same state, a different route to the same place.
H, then Z
docker exec jupyter python3 -c "from qsim import *; show(apply(Z, apply(H, zero(1), 0), 0))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
X, then H
docker exec jupyter python3 -c "from qsim import *; show(apply(H, apply(X, zero(1), 0), 0))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think the Z gate does nothing, because the chances are the same after it. A single measurement cannot see a sign, so the chances stay put. But the sign is part of the state, and later gates use it. Lesson 5 shows how.
A gate acts on one chosen qubit
With two qubits, a one-qubit gate acts on just one of them. The lab's apply takes the qubit number as its last argument. Qubit 0 is the left-hand bit and qubit 1 is the right-hand bit.
X on qubit 1 of the state 00 gives 01. X on qubit 0 gives 10.
X on qubit 1 of two
docker exec jupyter python3 -c "from qsim import *; show(apply(X, zero(2), 1))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
X on qubit 0 of two
docker exec jupyter python3 -c "from qsim import *; show(apply(X, zero(2), 0))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.