forked from kparvataneni12/2022_microsoft_ionq_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqkdelephone
151 lines (134 loc) · 4.78 KB
/
qkdelephone
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
from azure.quantum.qiskit import AzureQuantumProvider
provider = AzureQuantumProvider(
subscription_id = "b1d7f7f8-743f-458e-b3a0-3e09734d716d",
resource_group = "aq-hackathons",
name = "aq-hackathon-01",
location = "eastus"
)
backend = provider.get_backend("ionq.simulator")
import qiskit
import operator
from qiskit import QuantumCircuit, execute
from qiskit.visualization import plot_histogram, plot_bloch_multivector
import numpy as np
from random import randint
np.random.seed(seed=1)
circ_1 = QuantumCircuit(1, 1)
circ_2 = QuantumCircuit(1, 1)
for i in range(10):
if i % 2 == 0:
circ_1.h(0)
circ_2.h(0)
else:
circ_1.x(0)
circ_2.x(0)
print(circ_1)
print("This program is a modular virtual game of QKDelephone.")
print(circ_2)
information = input("Enter a message to encode with the distributed key: ")
n = 30 # the number of qubits to encode the message
def main():
infoBytes = [((bin(i))[2:]) for i in bytearray("information", "utf8")]
infoBits = []
for byte in infoBytes:
for bit in byte:
infoBits.append(int(bit))
infoBits = np.array(infoBits)
#these are the bits to be encoded
bits = [randint(0, 1) for i in range(n)]
#these are the sender's bases
senderBases = [randint(0, 1) for i in range(n)]
#The message they are trying to send
message = encode_bits(bits, senderBases)
#INTERCEPTION!!!!
#Decide whether to intercept using a quantum non-pseudorandom number generator
random_number_generator = QuantumCircuit(1, 1)
random_number_generator.h(0)
random_number_generator.measure(0, 0)
counts = execute(random_number_generator, backend, shots = 1).result().get_counts(random_number_generator)
zero_or_one = max(counts.items(), key=operator.itemgetter(1))[0]
if zero_or_one == 0:
interceptor = kInterceptors(9, message)
#The recipients random bases
recipientBases = [randint(0, 1) for i in range(n)]
#The measurement of the message with those bases
recipientResult = measure_message(message, recipientBases)
#Get the keys
senderKey = good_bits(senderBases, recipientBases, bits)
recipientKey = good_bits(senderBases, recipientBases, recipientResult)
# sample_size = 15
# #print(len(recipientKey))
# bit_selection = [randint(0, n) for i in range(sample_size)]
# recipientSample = sample_bits(recipientKey, bit_selection)
# print("Receiver key sample = " + str(recipientSample))
# senderSample = sample_bits(senderKey, bit_selection)
# print("Sender key sample = " + str(senderSample))
infoBitsEncoded = infoBits * int(sum(senderKey))
infoBitsDecoded = infoBitsEncoded / sum(recipientKey)
if sum(infoBits == infoBitsDecoded) == len(infoBits):
print(f"The message is {information}.")
else:
print("Your message has been intercepted!")
#This encodes the message
def encode_bits(bits, bases):
message = []
for i in range(n):
qc = QuantumCircuit(1,1)
if bases[i] == 0: # Prepare qubit in Z-basis
if bits[i] == 0:
pass
else:
qc.x(0)
else: # Prepare qubit in X-basis
if bits[i] == 0:
qc.h(0)
else:
qc.x(0)
qc.h(0)
qc.barrier()
message.append(qc)
return message
def measure_message(message, bases):
measurements = []
for q in range(n):
print()
print(message[q])
if bases[q] == 0: # measuring in Z-basis
message[q].measure(0,0)
if bases[q] == 1: # measuring in X-basis
message[q].h(0)
message[q].measure(0,0)
counts = execute(message[q], backend, shots = 1).result().get_counts(message[q])
measured_bit = max(counts.items(), key=operator.itemgetter(1))[0]
measurements.append(measured_bit)
return measurements
def good_bits(x_bases, y_bases, bits):
good_bits = []
for q in range(n):
if x_bases[q] == y_bases[q]:
# If both used the same basis, add
# this to the list of 'good' bits
good_bits.append(int(bits[q]))
return good_bits
# def sample_bits(bits, selection):
# sample = []
# for i in selection:
# # use np.mod to make sure the
# # bit we sample is always in
# # the list range
# i = np.mod(i, len(bits))
# # pop(i) removes the element of the
# # list at index 'i'
# sample.append(bits.pop(i))
# return sample
def kInterceptors(k, message):
interceptors = []
for i in range(k):
person = []
bases = [randint(0, 1) for i in range(n)]
person.append(bases)
person.append(measure_message(message, bases))
interceptors.append(person)
return kInterceptors
if __name__ == "__main__":
main()