Curriculum / Quantum Cryptography / Simulating BB84 Key Distribution

Lesson 10 of 21Code challengePro+175 XP

Simulating BB84 Key Distribution

Implement a complete BB84 protocol simulation with eavesdropping detection.

Simulating BB84 Key Distribution

BB84 is the first quantum key distribution protocol, proposed by Charles Bennett and Gilles Brassard in 1984. Simulating it gives you hands-on intuition for how quantum randomness, basis mismatch, and eavesdropping detection all work together.

The BB84 Protocol Steps

Step 1: Alice prepares qubits. For each of N rounds, Alice randomly chooses:

  • A bit value (0 or 1)
  • A basis (Z-basis: rectilinear, or X-basis: diagonal)

She then prepares a qubit encoding that bit in that basis:

  • Z-basis bit 0: |0⟩
  • Z-basis bit 1: |1⟩
  • X-basis bit 0: |+⟩ = (|0⟩+|1⟩)/√2
  • X-basis bit 1: |−⟩ = (|0⟩−|1⟩)/√2

Step 2: Bob measures. Bob independently chooses a random basis for each qubit and measures in it. When his basis matches Alice's, he gets the correct bit deterministically. When they differ, his result is random (50% correct, 50% wrong).

Step 3: Basis reconciliation. Alice and Bob publicly announce their basis choices (not the bits). They keep only the bits where bases matched, this is the "sifted key."

Step 4: Eavesdropping check. They compare a random subset of sifted key bits. If Eve intercepted and re-measured, she introduced errors at a predictable rate (~25% when eavesdropping). A high quantum bit error rate (QBER) triggers abort.

Step 5: Privacy amplification. Even without detected eavesdropping, Eve may hold partial information. A hash function compresses the sifted key into a shorter final key that Eve knows almost nothing about.

Why Eavesdropping Is Detectable

The key insight: quantum states cannot be measured without disturbance. When Eve measures Alice's qubit in the wrong basis (50% of the time), she collapses the superposition and is forced to resend a new qubit. This introduces errors even when Eve later gets lucky with basis matching. The result: eavesdropping increases QBER measurably.

This is the opening of the lesson. The full walkthrough, the interactive circuit, and the graded challenge continue inside myqubit.

How this lesson works

A hands-on coding challenge. You write Qiskit-compatible Python in the browser editor, run it instantly via WebAssembly, watch the circuit and Bloch sphere react, and pass automatic output checks. The AI tutor Qubitus gives Socratic hints if you get stuck.

Part of: Quantum Cryptography

Master quantum key distribution, the threat quantum computers pose to classical cryptography, and post-quantum cryptographic standards.

This lesson is part of Pro

Unlock Quantum Cryptography and all 10 tracks with Pro: $12.99/month, $79/year, or $97 lifetime. Start with the free track first if you are new.