-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquantumkeydistribution.py
155 lines (133 loc) · 4.7 KB
/
quantumkeydistribution.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
# Import necessary libraries
from qiskit import QuantumCircuit, Aer, execute
import random
import numpy as np
# Set up the quantum and classical channels
qubits = 100
q_channel = QuantumCircuit(qubits, qubits)
c_channel = QuantumCircuit(qubits, qubits)
# Generate random bits for encoding
bits = [random.randint(0,1) for i in range(qubits)]
# Encode the bits using random bases
for i in range(qubits):
if bits[i] == 0:
q_channel.h(i)
else:
q_channel.rx(1.57, i)
basis = random.randint(0,1)
if basis == 0:
q_channel.barrier()
c_channel.measure(i, i)
else:
q_channel.cx(i, i+1)
c_channel.measure(i, i+1)
c_channel.measure(i+1, i)
# Execute the circuits on a simulator
simulator = Aer.get_backend('qasm_simulator')
q_result = execute(q_channel, simulator, shots=1).result()
c_result = execute(c_channel, simulator, shots=1).result()
# Retrieve the key
key = ''
for i in range(qubits):
if c_result.get_counts().keys()[0][i] == c_result.get_counts().keys()[0][i+1]:
key += str(bits[i])
print('Shared secret key: ' + key)
# Set up the quantum and classical channels
qubits = 100
q_channel = QuantumCircuit(qubits, qubits)
c_channel = QuantumCircuit(qubits, qubits)
# Generate random bits for encoding
bits = [random.randint(0,1) for i in range(qubits)]
# Encode the bits using random bases
for i in range(qubits):
if bits[i] == 0:
q_channel.h(i)
else:
q_channel.rx(1.57, i)
basis = random.randint(0,1)
if basis == 0:
q_channel.barrier()
c_channel.measure(i, i)
else:
q_channel.cx(i, i+1)
c_channel.measure(i, i+1)
c_channel.measure(i+1, i)
# Execute the circuits on a simulator
simulator = Aer.get_backend('qasm_simulator')
q_result = execute(q_channel, simulator, shots=1).result()
c_result = execute(c_channel, simulator, shots=1).result()
# Retrieve the key
key = ''
for i in range(qubits):
if c_result.get_counts().keys()[0][i] == c_result.get_counts().keys()[0][i+1]:
key += str(bits[i])
# Apply the Toeplitz matrix to the key
toeplitz = np.random.randint(0, 2, size=(qubits, qubits))
toeplitz = np.triu(toeplitz)
key = np.array([int(b) for b in key])
amplified_key = np.mod(toeplitz @ key, 2)
print('Shared secret key: ' + ''.join(str(k) for k in amplified_key))
# Set up the quantum and classical channels
qubits = 100
q_channel = QuantumCircuit(qubits, qubits)
c_channel = QuantumCircuit(qubits, qubits)
# Generate random bits for encoding
bits = [random.randint(0,1) for i in range(qubits)]
# Encode the bits using random bases
for i in range(qubits):
if bits[i] == 0:
q_channel.h(i)
else:
q_channel.rx(1.57, i)
basis = random.randint(0,1)
if basis == 0:
q_channel.barrier()
c_channel.measure(i, i)
else:
q_channel.cx(i, i+1)
c_channel.measure(i, i+1)
c_channel.measure(i+1, i)
# Execute the circuits on a simulator
simulator = Aer.get_backend('qasm_simulator')
q_result = execute(q_channel, simulator, shots=1).result()
c_result = execute(c_channel, simulator, shots=1).result()
# Retrieve the key
key = ''
for i in range(qubits):
if c_result.get_counts().keys()[0][i] == c_result.get_counts().keys()[0][i+1]:
key += str(bits[i])
# Apply error correction using the Cascade protocol
code_length = 7
parity_bits = int(np.ceil(np.log2(qubits)))
num_blocks = int(np.ceil(qubits/code_length))
blocks = [key[i*code_length:(i+1)*code_length] for i in range(num_blocks)]
codewords = []
for block in blocks:
parity_check = np.zeros(parity_bits)
for i in range(code_length):
if block[i] == '1':
parity_check += np.array([int(b) for b in format(i+1, '0'+str(parity_bits)+'b')])
parity_check = np.mod(parity_check, 2)
codeword = block + ''.join(str(p) for p in parity_check)
codewords.append(codeword)
error_indices = [random.randint(0, code_length+parity_bits-1) for i in range(num_blocks)]
received_codewords = []
for i in range(num_blocks):
error_codeword = list(codewords[i])
error_codeword[error_indices[i]] = str(1 - int(error_codeword[error_indices[i]]))
received_codewords.append(''.join(error_codeword))
final_key = ''
for codeword in received_codewords:
parity_check = np.zeros(parity_bits)
for i in range(code_length+parity_bits):
if i < code_length:
if codeword[i] == '1':
parity_check += np.array([int(b) for b in format(i+1, '0'+str(parity_bits)+'b')])
else:
if codeword[i] != str(int(np.mod(parity_check, 2)[i-code_length])):
error_indices.append(i)
break
else:
final_key += codeword[:code_length]
# Print the final key
print('Final key:', final_key)