2727Reference: https://en.wikipedia.org/wiki/Quantum_key_distribution#E91_protocol:_Artur_Ekert_.281991.29
2828"""
2929
30- import math
3130import random
31+
3232import numpy as np
33- import qiskit
3433from qiskit import ClassicalRegister , QuantumCircuit , QuantumRegister
3534from qiskit_aer import AerSimulator
3635
@@ -82,8 +81,8 @@ def e91_protocol(n_bits: int = 2000) -> dict:
8281
8382 # Define the measurement angles for Alice and Bob's bases as constants.
8483 # The keys correspond to the basis name, and values are angles in radians.
85- ALICE_BASES = {"A1" : 0 , "A2" : np .pi / 8 , "A3" : np .pi / 4 }
86- BOB_BASES = {"B1" : np .pi / 8 , "B2" : np .pi / 4 , "B3" : 3 * np .pi / 8 }
84+ alice_bases = {"A1" : 0 , "A2" : np .pi / 8 , "A3" : np .pi / 4 }
85+ bob_bases = {"B1" : np .pi / 8 , "B2" : np .pi / 4 , "B3" : 3 * np .pi / 8 }
8786
8887 # Lists to store the choices and results for each bit.
8988 alice_chosen_bases , bob_chosen_bases = [], []
@@ -94,10 +93,10 @@ def e91_protocol(n_bits: int = 2000) -> dict:
9493
9594 for _ in range (n_bits ):
9695 # Alice and Bob randomly choose their measurement bases.
97- alice_basis_name = random .choice (list (ALICE_BASES .keys ()))
98- bob_basis_name = random .choice (list (BOB_BASES .keys ()))
99- alice_angle = ALICE_BASES [alice_basis_name ]
100- bob_angle = BOB_BASES [bob_basis_name ]
96+ alice_basis_name = random .choice (list (alice_bases .keys ()))
97+ bob_basis_name = random .choice (list (bob_bases .keys ()))
98+ alice_angle = alice_bases [alice_basis_name ]
99+ bob_angle = bob_bases [bob_basis_name ]
101100
102101 # Create a quantum circuit for one entangled pair.
103102 qr = QuantumRegister (2 , "q" )
@@ -117,7 +116,7 @@ def e91_protocol(n_bits: int = 2000) -> dict:
117116
118117 # Execute the circuit and get the result.
119118 job = backend .run (circuit , shots = 1 )
120- result = list ( job .result ().get_counts ().keys ())[ 0 ]
119+ result = next ( iter ( job .result ().get_counts ().keys ()))
121120
122121 # Store choices and results. Qiskit's bit order is reversed.
123122 alice_chosen_bases .append (alice_basis_name )
@@ -137,7 +136,8 @@ def e91_protocol(n_bits: int = 2000) -> dict:
137136 bob_key .append (bob_results [i ])
138137
139138 # Sift for the CHSH inequality test (Eve detection).
140- # We use four specific combinations of bases for the test: a = A1, a' = A3 | b = B1, b' = B3
139+ # We use four specific combinations of bases for the test:
140+ # a = A1, a' = A3 | b = B1, b' = B3
141141 chsh_correlations = {"ab" : [], "ab_" : [], "a_b" : [], "a_b_" : []}
142142
143143 for i in range (n_bits ):
@@ -159,12 +159,12 @@ def e91_protocol(n_bits: int = 2000) -> dict:
159159 chsh_correlations ["a_b_" ].append (product )
160160
161161 # Calculate the expectation value (average correlation) for each combination.
162- E = {}
162+ e = {}
163163 for key , values in chsh_correlations .items ():
164- E [key ] = np .mean (values ) if values else 0
164+ e [key ] = np .mean (values ) if values else 0
165165
166- # Calculate the S-value: S = E (a,b) - E (a,b') + E (a',b) + E (a',b')
167- s_value = E ["ab" ] - E ["ab_" ] + E ["a_b" ] + E ["a_b_" ]
166+ # Calculate the S-value: S = e (a,b) - e (a,b') + e (a',b) + e (a',b')
167+ s_value = e ["ab" ] - e ["ab_" ] + e ["a_b" ] + e ["a_b_" ]
168168
169169 # Check for eavesdropper: |S| > 2 indicates security.
170170 eavesdropper_detected = abs (s_value ) <= 2
0 commit comments