-
Notifications
You must be signed in to change notification settings - Fork 0
/
BB84.py
244 lines (208 loc) · 7.6 KB
/
BB84.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import numpy as np
from qiskit import *
from qiskit.visualization import plot_histogram
from matplotlib.pyplot import plot, draw, show
def SendState(qc1, qc2, qc1_name):
'''
This function takes the output of circuit qc1
(made up of only H and X gates)
and initialises another circuit qc2 with the same state
'''
qs = qc1.qasm().split(sep=';')[4:-1]
# process the code to get the instructions
for i, instruction in enumerate(qs):
qs[i] = instruction.lstrip()
# parse the instructions and apply to new circuit
for instruction in qs:
instruction_gate = instruction[0]
instruction_qubit_list = []
i = 0
while instruction[i] != '[':
i += 1
i += 1
while instruction[i] != ']':
instruction_qubit_list.append(instruction[i])
i += 1
instruction_qubit = 0
for i, dec in enumerate(reversed(instruction_qubit_list)):
instruction_qubit += int(dec)*10**i
if(instruction_gate == 'x'):
old_qr = int(instruction_qubit)
qc2.x(qr[old_qr])
elif instruction_gate == 'h':
old_qr = int(instruction_qubit)
qc2.h(qr[old_qr])
elif instruction_gate == 'm':
# exclude measuring
pass
else:
raise Exception('Unable to parse instruction')
# declare classical and quantum register
n = 16 # for local backend 'n' can go up to 23,
#after which a memery error is raised
qr = QuantumRegister(n, name='q')
cr = ClassicalRegister(n, name='c')
# create Alice's circuit
alice = QuantumCircuit(qr, cr, name='Alice')
# generate a random number expressible by the available qubits
alice_key = np.random.randint(0,high=2**n)
# cast key to binary representation
alice_key = np.binary_repr(alice_key,n)
# encode key as alice qubits
for i, digit in enumerate(alice_key):
if(digit == '1'):
alice.x(qr[i])
# switch randomly about half qubits to Hadamard basis
alice_table = []
for qubit in qr:
if(np.random.rand()>0.5):
alice.h(qubit)
alice_table.append('H') # indicate the Hadamard-basis
else:
alice_table.append('Z') # indicate the Z-basis
# create Bob's circuit
bob = QuantumCircuit(qr, cr, name='Bob')
SendState(alice,bob, 'Alice')
# Bob does not know which basis to use
bob_table = []
for qubit in qr:
if(np.random.rand()>0.5):
bob.h(qubit)
bob_table.append('H') # indicate the Hadamard-basis
else:
bob_table.append('Z') # indicate the Z-basis
# measure all qubits
for i, qubit in enumerate(qr):
bob.measure(qubit, cr[i])
backend = BasicAer.get_backend('qasm_simulator')
# Bob has only one chance of measuring correctly
result = execute(bob, backend=backend, shots=1).result()
#plot_histogram(result.get_counts(bob))
#draw()
#show(block=True)
# result of the measurement is Bob's key candidate
bob_key = list(result.get_counts(bob))[0]
# key is reversed so that first qubit is the first element of the list
bob_key = bob_key[::-1]
# compare basis and discard qubits not measured in the same basis
keep = []
discard = []
print('\n', "Compare Bob's and Alice's basis (without eavesdropping): ")
for qubit, basis in enumerate(zip(alice_table,bob_table)):
if(basis[0] == basis[1]):
print("Same choice for qubit: {}, basis: {}".format(qubit, basis[0]))
keep.append(qubit)
else:
print("Different choice for qubit: {}, Alice has {}, Bob has {}".format(qubit, basis[0], basis[1]))
discard.append(qubit)
# measure the percentage of qubits to be discarded
acc = 0
for bit in zip(alice_key, bob_key):
if(bit[0]==bit[1]):
acc += 1
print('\n Percentage of qubits to be discarded according to table comparison: ',
len(keep)/n)
print('Measurement convergence by additional chance: ', acc/n)
new_alice_key = [alice_key[qubit] for qubit in keep]
new_bob_key = [bob_key[qubit] for qubit in keep]
acc = 0
for bit in zip(new_alice_key, new_bob_key):
if(bit[0] == bit[1]):
acc += 1
print('Percentage of similarity between the keys: ', acc/len(new_alice_key))
if(acc//len(new_alice_key) == 1):
print('Key exchange has been succesfull')
print("New Alice's key: ", new_alice_key)
print("New Bob's key: ", new_bob_key)
else:
print('Key exchange has been tampered! ---> Check for eavesdropper or try again')
print("New Alice's key is invalid: ", new_alice_key)
print("New Bob's key is invalid: ", new_bob_key)
# let's intrude a eavesdropper Eve (which is initialised to Alice's state)
eve = QuantumCircuit(qr, cr, name='Eve')
SendState(alice, eve, 'Alice')
eve_table = []
for qubit in qr:
if(np.random.rand()>0.5):
eve.h(qubit)
eve_table.append('H')
else:
eve_table.append('Z')
for i, qubit in enumerate(qr):
eve.measure(qubit,cr[i])
# Execute (build and run) the quantum circuit
backend = BasicAer.get_backend('qasm_simulator')
result = execute(eve, backend=backend, shots=1).result()
# Result of the measurement is Eve's key
eve_key = list(result.get_counts(eve))[0]
eve_key = eve_key[::-1]
# Update states to new eigenstates (of wrongly chosen basis)
print('\n', "Compare Eve's and Alice's basis: ")
for i, basis in enumerate(zip(alice_table,eve_table)):
if(basis[0] == basis[1]):
print("Same choice for qubit: {}, basis: {}".format(qubit, basis[0]))
keep.append(i)
else:
print("Different choice for qubit: {}, Alice has {}, Eve has {}".format(qubit, basis[0], basis[1]))
discard.append(i)
if eve_key[i] == alice_key[i]:
eve.h(qr[i])
else:
if (basis[0] == 'H' and basis[1] == 'Z'):
alice.h(qr[i])
eve.x(qr[i])
else:
eve.x(qr[i])
eve.h(qr[i])
# Eve's state is now sent to Bob
SendState(eve, bob, 'Eve')
bob_table = []
for qubit in qr:
if(np.random.rand()>0.5):
bob.h(qubit)
bob_table.append('H')
else:
bob_table.append('Z')
for i, qubit in enumerate(qr):
bob.measure(qubit, cr[i])
result = execute(bob, backend, shots=1).result()
#plot_histogram(result.get_counts(bob))
#draw()
#show(block=True)
bob_key = list(result.get_counts(bob))[0]
bob_key = bob_key[::-1]
# Now Alice and Bob will share their table data and perform checking operations
keep = []
discard = []
print('\n', "Compare Bob's and Alice's basis (with eavesdropping by Eve): ")
for qubit, basis in enumerate(zip(alice_table, bob_table)):
if(basis[0] == basis[1]):
print("Same choice for qubit: {}, basis: {}".format(qubit, basis[0]))
keep.append(qubit)
else:
print("Different choice for qubit: {}, Alice has {}, Bob has {}".format(qubit, basis[0], basis[1]))
discard.append(qubit)
# measure the percentage of qubits to be discarded
acc = 0
for bit in zip(alice_key, bob_key):
if(bit[0]==bit[1]):
acc += 1
print('Percentage of qubits to be discarded according to table comparison: ',
len(keep)/n)
print('Measurement convergence by additional chance: ', acc/n)
new_alice_key = [alice_key[qubit] for qubit in keep]
new_bob_key = [bob_key[qubit] for qubit in keep]
acc = 0
for bit in zip(new_alice_key, new_bob_key):
if(bit[0] == bit[1]):
acc += 1
print('\n Percentage of similarity between the keys: ', acc/len(new_alice_key))
if(acc//len(new_alice_key) == 1):
print('Key exchange has been succesfull')
print("New Alice's key: ", new_alice_key)
print("New Bob's key: ", new_bob_key)
else:
print('Key exchange has been tampered! --->',
'Check for eavesdropper or try again')
print("New Alice's key is invalid: ", new_alice_key)
print("New Bob's key is invalid: ", new_bob_key)