-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbb84.slq
127 lines (89 loc) · 2.58 KB
/
bb84.slq
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
length := 10 : !ℕ;
// A function to generate a random bit string of given length
def randomBitString(length: !ℕ) : !𝔹^length{
string := vector(length, 0:!𝔹);
for i in [0..length){
string[i] = randomizer();
}
return string;
}
// Randomizer based on Hadamard measurement method
def randomizer() : !𝔹 {
q := 0 : 𝔹;
return measure(H(q));
}
// A function to encode a bit string into qubits
def encodeBitString(a_state: !𝔹^length, a_bases: !𝔹^length) : 𝔹^length {
qub := vector(length, 0:𝔹); // default qubit value is 0
for i in [0..length){
if (a_state[i] == 1){ // set qubits to 1 if chosen state is 1
qub[i] := X(qub[i]);
}
if (a_bases[i] == 1){ // use computational if chosen basis is diagonal (1)
qub[i] := H(qub[i])
}
}
return qub;
}
// A function to measure qubits
def measureQubits(qubits:𝔹^length, b_bases: !𝔹^length) : !𝔹^length {
measured := vector(length, 0:!𝔹);
for i in [0..length){
if b_bases[i]{
qubits[i] := H(qubits[i])
}
}
measured = measure(qubits);
return measured;
}
// A function to generate the key by comparing bitstrings
def generateKey(a_string: !𝔹^length, b_string: !𝔹^length, meas_qubits: !𝔹^length) : !𝔹[]{
key_length := 0 : !ℕ;
// Check measuring bases and determine the key length
for i in [0..length){
if (b_string[i] == a_string[i]){
key_length = key_length + 1;
}
}
// Initialize key
k := vector(key_length, 0:!𝔹);
// Temporary index
index := 0 : !ℕ;
// Generate key
for i in [0..length){
if (b_string[i] == a_string[i]){
k[index] = meas_qubits[i];
index = index + 1;
}
}
return k;
}
// Define the BB84 protocol function
def bb84() : !𝔹[] {
// Generate two random bit strings,
// 1. Alice's state
// 2. Alice's bases
a_state := randomBitString(length) : !𝔹^length;
a_bases := randomBitString(length) : !𝔹^length;
// PRINT VALUES (DEBUG)
// print Alice states
print(a_state);
// print Alice bases
print(a_bases);
// Prepare qubits
qubits := encodeBitString(a_state, a_bases) : 𝔹^length;
// Generate another random bitstring
b_bases := randomBitString(length) : !𝔹^length;
// PRINT VALUES (DEBUG)
// print Bob bases:
print(b_bases);
// Measure qubits
measured_qubits := measureQubits(qubits, b_bases) : !𝔹^length;
// Generate the key by comparing bitstrings
key := generateKey(a_state, b_bases, measured_qubits);
return (key);
}
def main() {
gen_key := bb84() : !𝔹[];
print(gen_key);
}