BB84 protocol
The BB84 protocol, proposed by Charles Bennett and Gilles Brassard in 1984, is a quantum key distribution (QKD) protocol that allows two parties to securely establish a shared secret key.
The protocol is based on transmitting single photons in four possible states, two of which represent 0 and 1 in binary code and the other two of which are in superposition. The sender randomly chooses one of the four possible states for each photon and transmits it to the receiver, which measures the photon randomly. The sender then announces the basis used for each photon, and the receiver discards any measurement results that were made on the wrong basis. The remaining photons are used to establish the secret key, which is derived from the results of the measurement on the same basis. Any attempt by an eavesdropper to measure or intercept the photons will necessarily disturb their state, which will be detected by the receiver, ensuring the security of the communication.
# Import necessary libraries
from qiskit import QuantumCircuit, Aer, execute
from qiskit.tools.visualization import plot_histogram
# Define the length of the key in bits
n = 10
# Define the quantum circuit with n qubits and n classical bits
qc = QuantumCircuit(n, n)
# Alice chooses a random sequence of bits to encode into quantum states
alice_bits = [0, 1, 0, 1, 0, 0, 1, 1, 0, 1] # Example random sequence of bits
# Alice encodes her bits into quantum states
for i in range(n):
if alice_bits[i] == 0: # Horizontal polarization
qc.h(i)
else: # Vertical polarization
qc.rx(pi/2, i) # Rotate qubit by pi/2 around the x-axis
# Alice sends the qubits to Bob over the quantum channel
# Bob chooses a random sequence of bases to measure the qubits
bob_bases = [0, 1, 1, 0, 1, 0, 1, 0, 0, 1] # Example random sequence of bases
# Bob measures the qubits in the chosen bases
for i in range(n):
if bob_bases[i] == 0: # Horizontal/vertical basis
qc.h(i)
else: # Diagonal/anti-diagonal basis
qc.rx(pi/4, i) # Rotate qubit by pi/4 around the x-axis
qc.h(i)
qc.measure(i, i)
# Bob announces the bases he used to measure each qubit
# Alice and Bob compare their bases and discard any qubits measured in the wrong basis
alice_bases = [0, 1, 1, 0, 1, 0, 1, 0, 0, 1] # Example random sequence of bases
discard_indices = []
for i in range(n):
if alice_bases[i] != bob_bases[i]:
discard_indices.append(i)
qc.barrier()
qc.measure(range(n), range(n)) # Measure all qubits again to get key bits
# Alice and Bob compare their key bits to check for errors and establish the secret key
results = execute(qc, Aer.get_backend('qasm_simulator'), shots=1).result().get_counts()
alice_key = [int(bit) for bit in list(results.keys())[0]]
bob_key = [int(bit) for bit in list(results.keys())[0]]
for i in discard_indices:
alice_key.pop(i)
bob_key.pop(i)
if alice_key == bob_key:
print("Secret key established:", alice_key)
else:
print("Error detected.")
# Plot the final state of the qubits
plot_histogram(results)
The code simulates the BB84 quantum key distribution protocol. The protocol establishes a secret key between two parties, Alice and Bob, over an insecure quantum channel. The key is used for secure communication between them.
In the code, Alice chooses a random sequence of bits and encodes them into quantum states using two polarizations: horizontal polarization and vertical polarization. She sends the qubits to Bob over the quantum channel.
Bob chooses a random sequence of bases to measure the qubits. He measures the qubits in the chosen bases and announces the bases he used to measure each qubit.
Alice and Bob compare their bases and discard any qubits measured on the wrong basis. They measure all the qubits again to get the key bits. Alice and Bob compare their key bits to check for errors and establish the secret key.
The final state of the qubits is plotted using a histogram. Note that this is just a simplified pseudo-code implementation and does not include all the necessary error correction and privacy amplification techniques that are commonly used in practical QKD systems. This code is just a pseudo implementation of BB84 protocol using Qiskit SDK.