@@ -76,12 +76,14 @@ def e91_protocol(n_bits: int = 2000) -> dict:
7676 if n_bits <= 0 :
7777 raise ValueError ("Number of bits must be > 0." )
7878 if n_bits > 10000 :
79- raise ValueError ("Number of bits is too large to simulate efficiently (>10000)." )
79+ raise ValueError (
80+ "Number of bits is too large to simulate efficiently (>10000)."
81+ )
8082
8183 # Define the measurement angles for Alice and Bob's bases as constants.
8284 # The keys correspond to the basis name, and values are angles in radians.
83- ALICE_BASES = {'A1' : 0 , 'A2' : np .pi / 8 , 'A3' : np .pi / 4 }
84- BOB_BASES = {'B1' : np .pi / 8 , 'B2' : np .pi / 4 , 'B3' : 3 * np .pi / 8 }
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 }
8587
8688 # Lists to store the choices and results for each bit.
8789 alice_chosen_bases , bob_chosen_bases = [], []
@@ -98,8 +100,8 @@ def e91_protocol(n_bits: int = 2000) -> dict:
98100 bob_angle = BOB_BASES [bob_basis_name ]
99101
100102 # Create a quantum circuit for one entangled pair.
101- qr = QuantumRegister (2 , 'q' )
102- cr = ClassicalRegister (2 , 'c' )
103+ qr = QuantumRegister (2 , "q" )
104+ cr = ClassicalRegister (2 , "c" )
103105 circuit = QuantumCircuit (qr , cr )
104106
105107 # Create a Bell state |Φ+⟩ = (|00⟩ + |11⟩)/sqrt(2)
@@ -123,21 +125,20 @@ def e91_protocol(n_bits: int = 2000) -> dict:
123125 alice_results .append (int (result [1 ]))
124126 bob_results .append (int (result [0 ]))
125127
126-
127128 # Sift for generating the secret key.
128129 # The key is formed when Alice and Bob choose compatible bases.
129130 # Here, compatible means A2/B1 or A3/B2, where angles are identical.
130131 alice_key , bob_key = [], []
131132 for i in range (n_bits ):
132- is_a2b1 = alice_chosen_bases [i ] == 'A2' and bob_chosen_bases [i ] == 'B1'
133- is_a3b2 = alice_chosen_bases [i ] == 'A3' and bob_chosen_bases [i ] == 'B2'
133+ is_a2b1 = alice_chosen_bases [i ] == "A2" and bob_chosen_bases [i ] == "B1"
134+ is_a3b2 = alice_chosen_bases [i ] == "A3" and bob_chosen_bases [i ] == "B2"
134135 if is_a2b1 or is_a3b2 :
135136 alice_key .append (alice_results [i ])
136137 bob_key .append (bob_results [i ])
137138
138139 # Sift for the CHSH inequality test (Eve detection).
139140 # We use four specific combinations of bases for the test: a = A1, a' = A3 | b = B1, b' = B3
140- chsh_correlations = {'ab' : [], ' ab_' : [], ' a_b' : [], ' a_b_' : []}
141+ chsh_correlations = {"ab" : [], " ab_" : [], " a_b" : [], " a_b_" : []}
141142
142143 for i in range (n_bits ):
143144 # Convert results {0, 1} to {-1, 1} for calculating correlation.
@@ -148,22 +149,22 @@ def e91_protocol(n_bits: int = 2000) -> dict:
148149 alice_basis = alice_chosen_bases [i ]
149150 bob_basis = bob_chosen_bases [i ]
150151
151- if alice_basis == 'A1' and bob_basis == 'B1' :
152- chsh_correlations ['ab' ].append (product )
153- elif alice_basis == 'A1' and bob_basis == 'B3' :
154- chsh_correlations [' ab_' ].append (product )
155- elif alice_basis == 'A3' and bob_basis == 'B1' :
156- chsh_correlations [' a_b' ].append (product )
157- elif alice_basis == 'A3' and bob_basis == 'B3' :
158- chsh_correlations [' a_b_' ].append (product )
152+ if alice_basis == "A1" and bob_basis == "B1" :
153+ chsh_correlations ["ab" ].append (product )
154+ elif alice_basis == "A1" and bob_basis == "B3" :
155+ chsh_correlations [" ab_" ].append (product )
156+ elif alice_basis == "A3" and bob_basis == "B1" :
157+ chsh_correlations [" a_b" ].append (product )
158+ elif alice_basis == "A3" and bob_basis == "B3" :
159+ chsh_correlations [" a_b_" ].append (product )
159160
160161 # Calculate the expectation value (average correlation) for each combination.
161162 E = {}
162163 for key , values in chsh_correlations .items ():
163164 E [key ] = np .mean (values ) if values else 0
164165
165166 # Calculate the S-value: S = E(a,b) - E(a,b') + E(a',b) + E(a',b')
166- s_value = E ['ab' ] - E [' ab_' ] + E [' a_b' ] + E [' a_b_' ]
167+ s_value = E ["ab" ] - E [" ab_" ] + E [" a_b" ] + E [" a_b_" ]
167168
168169 # Check for eavesdropper: |S| > 2 indicates security.
169170 eavesdropper_detected = abs (s_value ) <= 2
@@ -174,7 +175,7 @@ def e91_protocol(n_bits: int = 2000) -> dict:
174175 "s_value" : s_value ,
175176 "eavesdropper_detected" : eavesdropper_detected ,
176177 "key_match" : alice_key == bob_key ,
177- "key_length" : len (alice_key )
178+ "key_length" : len (alice_key ),
178179 }
179180
180181
@@ -188,7 +189,11 @@ def e91_protocol(n_bits: int = 2000) -> dict:
188189 print (f"Final key length: { results ['key_length' ]} " )
189190 print (f"Keys match: { results ['key_match' ]} " )
190191
191- if not results ['eavesdropper_detected' ] and results ['key_match' ] and results ['key_length' ] > 0 :
192+ if (
193+ not results ["eavesdropper_detected" ]
194+ and results ["key_match" ]
195+ and results ["key_length" ] > 0
196+ ):
192197 print ("\n Protocol successful! Secret key generated securely." )
193198 print (f" Alice's key: { results ['alice_key' ]} " )
194199 print (f" Bob's key: { results ['bob_key' ]} " )
0 commit comments