Quantum Cryptography Simulation in Python — Core Concepts

What Quantum Cryptography Does

Quantum cryptography uses quantum mechanical properties to establish shared secret keys between two parties with provable security. The security guarantee doesn’t rely on computational difficulty (like RSA or AES) — it relies on the laws of physics.

The primary application is Quantum Key Distribution (QKD): generating a shared random key that both parties can use for encrypting messages with classical methods like one-time pads.

The BB84 Protocol

Proposed by Bennett and Brassard in 1984, BB84 is the foundational QKD protocol.

The Two Bases

Quantum information can be encoded in different bases — think of them as different languages for writing 0s and 1s:

  • Rectilinear basis (+): horizontal (→) = 0, vertical (↑) = 1
  • Diagonal basis (×): 45° (↗) = 0, 135° (↖) = 1

If you measure a photon in the same basis it was prepared in, you get the correct value. If you measure in the wrong basis, you get a random result (50/50 chance of either value).

Protocol Steps

Step 1: Quantum Transmission Alice randomly generates bits and randomly chooses a basis for each. She prepares and sends a photon for each bit.

Step 2: Measurement Bob randomly picks a basis for each received photon and measures it. When his basis matches Alice’s, he gets the correct bit. When it doesn’t, he gets garbage.

Step 3: Basis Reconciliation Over a public channel, Alice and Bob share which basis they used for each photon (but not the measurement results). They discard all bits where they chose different bases. On average, they keep ~50% of the bits.

Step 4: Eavesdropper Detection They publicly compare a random subset of their remaining bits. If these match perfectly, no eavesdropper was present. If the error rate exceeds a threshold (~11% for BB84), they abort — someone was listening.

Step 5: Key Distillation The remaining (uncompared) bits become the shared secret key. Error correction and privacy amplification ensure the final key is both correct and secret.

Why Eavesdropping Is Detectable

Eve’s problem: she doesn’t know which basis Alice used. If she measures in the wrong basis, she disturbs the photon’s state. When she resends it to Bob, the disturbed state introduces errors that Alice and Bob can detect statistically.

The no-cloning theorem prevents Eve from making a copy and measuring the copy — quantum states cannot be duplicated. She must interact with the original, and that interaction leaves traces.

The E91 Protocol

Proposed by Artur Ekert in 1991, E91 uses entanglement instead of prepared states:

  1. A source generates entangled photon pairs and sends one to Alice, one to Bob
  2. Both choose random measurement bases
  3. When they measure in the same basis, they get perfectly correlated results
  4. They use Bell’s inequality to verify no eavesdropper has tampered with the entanglement

E91’s advantage: the security proof is based on Bell’s theorem, providing a deeper physical guarantee. If the Bell inequality is violated (proving genuine entanglement), the key is secure.

Real-World QKD

MilestoneYearAchievement
First QKD demo199232 cm free-space
First fiber demo199310 km fiber
Satellite QKD20171,200 km (Micius satellite)
QKD network20214,600 km backbone (China)
Commercial QKD2024+Multiple vendors, metro networks

Companies like ID Quantique, Toshiba, and QuantumCTek sell commercial QKD systems. Several countries operate metropolitan QKD networks for government and financial communications.

Simulating in Python

Python simulation of QKD lets you:

  • Verify protocol correctness statistically
  • Model different eavesdropping strategies and measure their detection rates
  • Test error correction and privacy amplification algorithms
  • Explore noise effects and realistic channel models

You can use quantum computing frameworks (Qiskit, Cirq) for circuit-level simulation or write direct mathematical simulations using NumPy for higher performance on larger key sizes.

Common Misconception

“Quantum cryptography makes all current encryption obsolete.” It doesn’t replace encryption — it replaces key exchange. You still use classical encryption (AES, one-time pad) to actually encrypt data. QKD provides a quantum-secure way to share the key. Also, QKD requires dedicated hardware (photon sources, detectors, optical fiber or free-space links) — it can’t run on the internet like software-based encryption.

The Quantum Threat to Classical Crypto

QKD is partly motivated by the threat quantum computers pose to current cryptography:

  • RSA, ECC: Broken by Shor’s algorithm on a large enough quantum computer
  • AES-256: Weakened but not broken by Grover’s algorithm
  • Post-quantum cryptography: Classical algorithms believed resistant to quantum attack (NIST standardized in 2024)

QKD provides “information-theoretic” security — secure even against an adversary with unlimited computational power, including future quantum computers.

One thing to remember: Quantum cryptography’s security doesn’t depend on how smart or powerful the attacker is — it depends on the laws of physics, which is why it’s considered the ultimate form of secure key distribution.

pythonquantum-computingcryptographyquantum-key-distribution

See Also