Quantum in 7 Sessions › Session 7
Session 7 — Communication, benchmarking, and your own project
Session 7 of 7Self-studySimulator only
On this page: Read · Key ideas · Worked example · Try it · Second experiment · Common mistakes · Check yourself · Project
1. Read
- Chapter 11 — Quantum Communication and BB84
- Chapter 12 — Benchmarking and Reproducible Quantum Experiments
Read the chapters first. The summary below is a reminder, not a replacement.
2. Key ideas
- In BB84 Alice encodes each bit in one of two bases, chosen at random. Bob measures in a basis he chooses at random.
- When their bases match, Bob’s result equals Alice’s bit. When they differ, his result is random, so those rounds are discarded (sifting).
- An eavesdropper who measures in the wrong basis disturbs the state and causes errors in the sifted key, which Alice and Bob can detect by comparing a sample.
- A benchmark describes one device, one circuit, and one day. Keep raw data, settings, and controls so that someone else can repeat it, and claim only what the evidence supports.
3. Worked example
Work through this with pen and paper before touching the code.
How much error does an eavesdropper cause? Look only at rounds where Alice and Bob used the same basis.
- Eve guesses the basis. She is right half the time, and then causes no error.
- The other half, she measures in the wrong basis and sends on a state in that wrong basis. Bob, measuring in Alice’s basis, now gets a random result and is wrong half the time.
- Error rate in the sifted key: ½ × ½ = ¼, or 25%.
- If Alice and Bob compare k bits, Eve escapes detection on each with probability ¾. For k = 20 that is (¾)²⁰ ≈ 0.003, about 0.3%.
4. Try it
Simulate 32 rounds of BB84 with no eavesdropper and compare the sifted keys. Write down what you expect before you run the code.
import random
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
random.seed(7)
sim = AerSimulator(seed_simulator=7)
n = 32
key_alice, key_bob = [], []
for _ in range(n):
bit, basis_a, basis_b = random.randint(0, 1), random.choice("ZX"), random.choice("ZX")
qc = QuantumCircuit(1, 1)
if bit:
qc.x(0) # Alice encodes her bit
if basis_a == "X":
qc.h(0) # ... in her chosen basis
if basis_b == "X":
qc.h(0) # Bob measures in his chosen basis
qc.measure(0, 0)
result = int(list(sim.run(qc, shots=1).result().get_counts())[0])
if basis_a == basis_b: # sifting: keep only the rounds where bases match
key_alice.append(bit)
key_bob.append(result)
print("rounds kept:", len(key_alice), "of", n)
print("Alice:", "".join(map(str, key_alice)))
print("Bob :", "".join(map(str, key_bob)))
print("keys identical:", key_alice == key_bob)
Output (counts will vary slightly from run to run):
rounds kept: 19 of 32 Alice: 0110111011111011010 Bob : 0110111011111011010 keys identical: True
What to notice
About half of the rounds survive sifting, and the two keys are identical. This is a protocol demonstration, not a security proof. The interesting experiment is the next one: add an eavesdropper and measure how often the keys now disagree.
5. Second experiment
Add an eavesdropper who measures each qubit in a random basis and sends on what she saw. Predict first.
import random
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
random.seed(7)
sim = AerSimulator(seed_simulator=7)
def measure(qc):
qc.measure(0, 0)
return int(list(sim.run(qc, shots=1).result().get_counts())[0])
def prepare(bit, basis):
qc = QuantumCircuit(1, 1)
if bit:
qc.x(0)
if basis == "X":
qc.h(0)
return qc
kept = errors = 0
for _ in range(400):
bit, basis_a, basis_e, basis_b = (random.randint(0, 1), random.choice("ZX"),
random.choice("ZX"), random.choice("ZX"))
# Eve intercepts: measures in her own random basis ...
qc = prepare(bit, basis_a)
if basis_e == "X":
qc.h(0)
eve_bit = measure(qc)
# ... and sends Bob a fresh qubit prepared from what she saw
qc = prepare(eve_bit, basis_e)
if basis_b == "X":
qc.h(0)
bob_bit = measure(qc)
if basis_a == basis_b:
kept += 1
errors += (bob_bit != bit)
print("rounds kept:", kept, " errors:", errors, " error rate:", round(errors / kept, 3))
Output:
rounds kept: 207 errors: 52 error rate: 0.251
What it shows
About a quarter of the sifted bits are now wrong, matching the hand calculation of 25%. Without Eve the error rate was zero. That jump is what Alice and Bob look for.
6. Common mistakes and tips
Common mistakes
- Thinking the basis announcement leaks the key. Bases are revealed only after the measurements, and the bit values never are.
- Keeping the bits used for the error check. They were announced publicly and must be discarded.
- Treating a simulated protocol as a security proof, or a single benchmark run as a general statement about a device.
Tips
- Write a small table by hand first: Alice’s bit and basis, Bob’s basis and result, keep or discard.
- With real hardware some errors appear even without an eavesdropper. The protocol must set a threshold.
- For any benchmark, record device, date, shots, circuit, and seed. Without them the number means little.
7. Check yourself
Answer each question on paper, then open the answer to compare.
- Add Eve: before Bob’s gates, measure the qubit in a random basis and prepare it again from her result. What error rate do you predict in the sifted key?
Show answer
About 25%, as the worked example and the second experiment show.
- Why must Alice and Bob sacrifice part of the key to check for errors?
Show answer
Comparing bits is the only way to estimate the error rate, and bits that have been announced are no longer secret, so they are thrown away.
- Pick one result from an earlier session. What would another student need from you to reproduce it exactly?
Show answer
The code, the package versions, the random seed, the number of shots, the backend, and the raw counts.
8. Do a project
Pick one. Each follows the routine from Guide 1: predict, run the baseline, change one variable, analyze.
← Session 6Choose your project →
For instructors: Teacher notes for Session 7