What does a qubit do that a bit does not?
A bit is 0 or 1. A qubit is described by two numbers, one for each answer, and a measurement turns them into a single 0 or 1 by chance. You will build that rule yourself, in a short Python file you can read. It runs on an ordinary computer, so it is a simulator, not a quantum computer, and it cannot show whether a quantum computer is faster. What it shows is the rules.
By the end of this lesson
Note
I can make a qubit, apply a gate, measure it, and say what the counts show.
Key words
- Qubit: the quantum version of a bit, described by two amplitudes.
- Amplitude: a number for one outcome. Its square is the chance of that outcome.
- Gate: a step that changes the amplitudes, such as X or H.
- Measurement: reading the qubit, which gives one 0 or 1 by chance.
- Shot: one run of the whole thing, ending in a measurement.
- Simulator: a program on an ordinary computer that follows the rules.
Starter: what you already know
Note
Answer in your head. 1. How many different values can 2 bits hold at once? 2. If a fair coin is flipped 1000 times, about how many heads do you expect? 3. What does squaring 0.5 give? Answers: 4 different patterns, though only one at a time; about 500; 0.25.
Before you start
Note
Press Start the lab on the bar at the top of this lesson and wait until it says it is running. There is one container, jupyter, and the terminal reaches its Python with docker exec jupyter, which runs one command inside it. The JupyterLab link opens its web page, port 8888. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.
A simulator is a file you can read
ls lists the work folder: qsim.py, the simulator, under 70 lines of NumPy, the notebook start.ipynb and the folder steps. The first script makes a qubit in the state |0>. The first line it prints, 1.000|0>, reads "amplitude 1 for the answer 0". The chances are [1. 0.]: certain to read 0. Two qubits take 4 amplitudes, one for each of 00, 01, 10 and 11.
docker exec jupyter lsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
docker exec jupyter python3 steps/qubit.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A gate changes the amplitudes
The X gate flips 0 to 1, so |0> becomes 1.000|1>. The H gate makes an equal mix: 0.707|0> + 0.707|1>. Each amplitude is 0.707, and 0.707 squared is 0.5, so the chances are [0.5 0.5].
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.
A measurement gives one answer, by chance
The script measures the H state 1000 times, with a fixed seed so every run is the same. Seed 1 gives 507 zeros and 493 ones. Seed 2 gives 503 and 497. Seed 1 again gives exactly the first answer. Close to half each, never exactly half: that is chance.
docker exec jupyter python3 steps/measure.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Check
Tip
The H state has an amplitude of 0.707 for each answer. Why are the counts close to 500 and 500, and not 707 and 707? Answer: the chance is the amplitude squared, 0.707 squared is 0.5, and the two chances add up to 1.
Two qubits can be tied together
H on the first qubit and then CNOT gives a Bell state: 0.707|00> + 0.707|11>. The counts hold only 00 and 11 (507 and 493). Never 01 or 10: the two qubits always agree, though each one on its own is a coin toss.
docker exec jupyter python3 steps/bell.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Amplitudes can cancel
H then H returns 1.000|0>, and H then Z then H returns 1.000|1>. The second H adds the amplitudes, and with the Z in between one of the two answers cancels out. This is interference, and it is how a quantum program steers the chances.
docker exec jupyter python3 steps/interfere.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Steering the chances toward an answer
Four items are numbered 00, 01, 10 and 11, and 11 is marked. Every item starts with amplitude 0.500, so each has a chance of 0.25. Flipping the sign of the marked one changes no chance, but one more step, which reflects every amplitude about the average, leaves 1.000|11>: the marked item is now certain.
docker exec jupyter python3 steps/amplify.pyShown 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 a quantum computer tries every answer at once and simply shows the best. A measurement gives one answer, chosen by chance from the squared amplitudes. The skill is arranging the amplitudes, with gates and interference, so the answer you want is the likely one.
What a simulator cannot show
This is qsim.py running on an ordinary computer. It stores every amplitude, so each extra qubit doubles the work, and it cannot show any speed-up. It follows the rules exactly, which is why it is good for learning them, and it says nothing about how a real machine is built or how fast it would be.
The same work in a notebook
Open JupyterLab from the bar and double-click start.ipynb. It does what the scripts did, one cell at a time, and ends with a bar chart of the counts of a Bell state. Click a cell and press Shift and Enter to run it. The last cell is where your own question goes.
Exit check
Tip
Answer without looking back. 1. What is the chance of an outcome, given its amplitude? 2. What does the H gate do to |0>? 3. In a Bell state, which pairs of answers appear? 4. Can this simulator show that a quantum computer is faster? Answers: the amplitude squared; it makes an equal mix, 0.707 each; 00 and 11 only; no, it is a simulator on an ordinary computer.
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one service, JupyterLab with a small NumPy simulator the students can read, and nothing here marks itself: the student hands in a notebook of their own that uses qsim.py, downloaded from JupyterLab, and a sentence on what it showed. Key learning points: 1. A qubit is two amplitudes and the chance is the square. 2. Gates change amplitudes and a measurement gives one answer by chance. 3. Interference steers the chances. Common mistake: "tries every answer at once". Equipment: the lab and a browser. Tip: ask students to predict each output before they press Run. The seeds are fixed, so every copy prints the same numbers, and the lab says plainly that it cannot show speed. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.