Lesson 15 of 15

Quantum Key Distribution

Unbreakable Encryption

Quantum Key Distribution (QKD) uses quantum mechanics to share a secret key between two parties — with the guarantee that any eavesdropping is physically detectable.

The BB84 protocol (Bennett & Brassard, 1984) works as follows:

  1. Alice prepares qubits in one of two bases:

    • Z basis (computational): 0o0angle0 o |0 angle, 1o1angle1 o |1 angle
    • X basis (diagonal): 0o+angle0 o |+ angle, 1oangle1 o |- angle
  2. Bob measures each qubit in a randomly chosen basis.

  3. Sifting: Alice and Bob publicly announce which bases they used. They keep only the results where their bases matched — these form the secret key.

When bases match, measurement is deterministic: Bob gets exactly what Alice sent. When they don't match, the result is random and discarded.

def sift_keys(alice_bits, alice_bases, bob_bases):
    alice_key, bob_key = [], []
    for a_bit, a_base, b_base in zip(alice_bits, alice_bases, bob_bases):
        if a_base == b_base:  # Bases matched!
            alice_key.append(a_bit)
            bob_key.append(a_bit)  # Deterministic when bases agree
    return alice_key, bob_key

Your Task

Implement sift_keys(alice_bits, alice_bases, bob_bases) that returns the matching key bits for Alice and Bob.

Python runtime loading...
Loading...
Click "Run" to execute your code.