-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_alice.py
78 lines (65 loc) · 2.55 KB
/
node_alice.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
from cqc.pythonLib import CQCConnection, qubit
import random
import json
import time
N_QUBIT = 10
with open("n_qubit.config") as config_file:
N_QUBIT = int(next(config_file).split()[0])
with CQCConnection("Alice") as Alice:
# Preparing my qubits
h_vector = [random.choice([0, 1]) for _ in range (N_QUBIT)]
x_vector = [random.choice([0, 1]) for _ in range (N_QUBIT)]
q_vector = []
for _ in range(N_QUBIT):
q_vector.append(qubit(Alice))
for i in range(N_QUBIT):
if x_vector[i] == 1:
q_vector[i].X()
if h_vector[i] == 1:
q_vector[i].H()
# Ask to Charlie (the commutor node as chosen for the network architecture)
# if I am the master, stating who I am
print("~Alice # Am I the master, stating who I am (?_?)")
Alice.sendClassical("Charlie", json.dumps( {"name": "Alice"} ).encode("utf-8"))
charlie_attempt_response = Alice.recvClassical()
im_master = json.loads(charlie_attempt_response.decode("utf-8"))
# If Charlie responded than it's ready for receiving my qubits, I send them
print("~Alice # I'm sending the qubits to Charlie (T_T)")
for qubit in q_vector:
Alice.sendQubit(qubit, "Charlie")
# Receive the resulting matrix from Charlie
matrix = json.loads(Alice.recvClassical().decode("utf-8"))
time.sleep(1)
# Send vector H
Alice.sendClassical("Bob", json.dumps(h_vector).encode("utf-8"))
# Read vector H
hother_vector = json.loads(Alice.recvClassical().decode("utf-8"))
if im_master:
# Flips the necessary bits based on matrix correlation
for i in range(N_QUBIT):
if matrix[i][1] == 0:
x_vector[i] = "b"
continue
if h_vector[i] != hother_vector[i]:
x_vector[i] = "h"
continue
if h_vector[i] == 1 and matrix[i][0] == 0:
continue
x_vector[i] = 1 if x_vector[i] == 0 else 0
else:
# Remove errors
for i in range(N_QUBIT):
if matrix[i][1] == 0:
x_vector[i] = "b"
continue
if h_vector[i] != hother_vector[i]:
x_vector[i] = "h"
continue
# Filtering key
key = []
for i in range(N_QUBIT):
if type(x_vector[i]) is not str:
key.append(x_vector[i])
# Print the key obtained
print("~Alice # " + repr(key))
print("~Alice # Key length: " + str(len(key)))