OQCL   HomeStart Here7 SessionsTextbookProjectsQUEST LabSubmitUse CasesInstructor GuideReferences

Quantum in 7 Sessions › Session 2

1234567

Session 2 — Measurement, gates, and interference

Session 2 of 7Self-studySimulator only

By the end of this session you can predict the result of the X, Z, and H gates on a single qubit, explain measurement as sampling, and show interference with a three-gate circuit.

On this page: Read · Key ideas · Worked example · Try it · Second experiment · Common mistakes · Check yourself · Project

1. Read

Read the chapter first. The summary below is a reminder, not a replacement.

2. Key ideas

3. Worked example

Work through this with pen and paper before touching the code.

Follow the amplitudes through H, Z, H starting from |0⟩.

  1. H|0⟩ = (|0⟩ + |1⟩)/√2.
  2. Z flips the sign of |1⟩: (|0⟩ − |1⟩)/√2. The probabilities are still 50/50.
  3. Apply H to each part: H|0⟩ = (|0⟩ + |1⟩)/√2 and H|1⟩ = (|0⟩ − |1⟩)/√2.
  4. Combine: ½[(|0⟩ + |1⟩) − (|0⟩ − |1⟩)] = ½[2|1⟩] = |1⟩.

The two |0⟩ contributions cancel and the two |1⟩ contributions add. Without the Z, the signs are the other way round and the result is |0⟩.

4. Try it

Run three small circuits and compare them. Predict each result before you look. Write down what you expect before you run the code.

from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator

sim = AerSimulator(seed_simulator=7)

def run(build, label):
    qc = QuantumCircuit(1, 1)
    build(qc)
    qc.measure(0, 0)
    print(label, sim.run(qc, shots=1000).result().get_counts())

run(lambda qc: qc.h(0), "H        ")                          # about half 0, half 1
run(lambda qc: (qc.h(0), qc.h(0)), "H, H     ")              # always 0
run(lambda qc: (qc.h(0), qc.z(0), qc.h(0)), "H, Z, H  ")     # always 1

Output (counts will vary slightly from run to run):

H         {'1': 486, '0': 514}
H, H      {'0': 1000}
H, Z, H   {'1': 1000}

What to notice

One H gives a random-looking result. Two H gates give 0 every time, so the randomness was not noise: it was a superposition that could be undone. Inserting Z, which changes no probability on its own, turns the answer into 1 every time.

5. Second experiment

Replace the Z in the middle with X and predict the result first. Predict first.

from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator

qc = QuantumCircuit(1, 1)
qc.h(0)
qc.x(0)          # X instead of Z in the middle
qc.h(0)
qc.measure(0, 0)
print(AerSimulator(seed_simulator=7).run(qc, shots=1000).result().get_counts())

Output:

{'0': 1000}

What it shows

The answer is 0 every time. X swaps the two amplitudes of H|0⟩, but they are equal, so nothing changes and the second H returns the qubit to |0⟩. Equivalently, HXH = Z, and Z leaves |0⟩ alone.

6. Common mistakes and tips

Common mistakes

Tips

7. Check yourself

Answer each question on paper, then open the answer to compare.

  1. If H made the qubit “randomly 0 or 1”, what would H followed by H give? Why is that not what happens?
    Show answer

    A random bit would still be random after the second H, giving about 50/50. The experiment gives 0 every time, so the state after one H was a definite superposition, not a random bit.

  2. What does X do between the two H gates instead of Z? Predict, then test.
    Show answer

    Always 0. See the second experiment: X swaps two equal amplitudes, which changes nothing.

  3. How many shots do you need before P(0) for a single H is reliably within 0.05 of 0.5?
    Show answer

    The standard error of P(0) is √(0.25/N). Two standard errors within 0.05 needs N of about 400. With 100 shots you will often miss by more than 0.05.

8. Do a project

Pick one. Each follows the routine from Guide 1: predict, run the baseline, change one variable, analyze.

← Session 1Session 3: Entanglement →

For instructors: Teacher notes for Session 2