OQCL   HomeStart Here7 SessionsTextbookProjectsQUEST LabSubmitUse CasesInstructor GuideReferences

Quantum in 7 Sessions › Session 7

1234567

Session 7 — Communication, benchmarking, and your own project

Session 7 of 7Self-studySimulator only

By the end of this session you can explain how BB84 uses two measurement bases to share a key, state what a benchmark can and cannot claim, and plan a reproducible project of your own.

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

1. Read

Read the chapters 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.

How much error does an eavesdropper cause? Look only at rounds where Alice and Bob used the same basis.

  1. Eve guesses the basis. She is right half the time, and then causes no error.
  2. 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.
  3. Error rate in the sifted key: ½ × ½ = ¼, or 25%.
  4. 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

Tips

7. Check yourself

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

  1. 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.

  2. 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.

  3. 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