This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ReferenceImplementation.qs
181 lines (140 loc) · 6.14 KB
/
ReferenceImplementation.qs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains reference solutions to all tasks.
// The tasks themselves can be found in Tasks.qs file.
// but feel free to look up the solution if you get stuck.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.KeyDistribution {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
//////////////////////////////////////////////////////////////////
// Part I. Preparation
//////////////////////////////////////////////////////////////////
// Task 1.1. Diagonal polarization
operation DiagonalBasis_Reference (qs : Qubit[]) : Unit is Adj {
ApplyToEachA(H, qs);
}
// Task 1.2. Equal superposition.
operation EqualSuperposition_Reference (q : Qubit) : Unit {
// The easiest way to do this is to convert the state of the qubit to |+⟩
H(q);
// Other possible solutions include X(q); H(q); to prepare |-⟩ state,
// and anything that adds any relative phase to one of the states.
}
//////////////////////////////////////////////////////////////////
// Part II. BB84 Protocol
//////////////////////////////////////////////////////////////////
// Task 2.1. Generate random array
operation RandomArray_Reference (N : Int) : Bool[] {
mutable array = [false, size = N];
for i in 0 .. N - 1 {
set array w/= i <- DrawRandomBool(0.5);
}
return array;
}
// Task 2.2. Prepare Alice's qubits
operation PrepareAlicesQubits_Reference (qs : Qubit[], bases : Bool[], bits : Bool[]) : Unit is Adj {
for i in 0 .. Length(qs) - 1 {
if bits[i] {
X(qs[i]);
}
if bases[i] {
H(qs[i]);
}
}
}
// Task 2.3. Measure Bob's qubits
operation MeasureBobsQubits_Reference (qs : Qubit[], bases : Bool[]) : Bool[] {
for i in 0 .. Length(qs) - 1 {
if bases[i] {
H(qs[i]);
}
}
return ResultArrayAsBoolArray(MultiM(qs));
}
// Task 2.4. Generate the shared key!
function GenerateSharedKey_Reference (basesAlice : Bool[], basesBob : Bool[], measurementsBob : Bool[]) : Bool[] {
// If Alice and Bob used the same basis, they will have the same value of the bit.
// The shared key consists of those bits.
mutable key = [];
for (a, b, bit) in Zipped3(basesAlice, basesBob, measurementsBob) {
if a == b {
set key += [bit];
}
}
return key;
}
// Task 2.5. Check if error rate was low enough
function CheckKeysMatch_Reference (keyAlice : Bool[], keyBob : Bool[], errorRate : Int) : Bool {
let N = Length(keyAlice);
mutable mismatchCount = 0;
for i in 0 .. N - 1 {
if keyAlice[i] != keyBob[i] {
set mismatchCount += 1;
}
}
return IntAsDouble(mismatchCount) / IntAsDouble(N) <= IntAsDouble(errorRate) / 100.0;
}
// Task 2.6. Putting it all together
operation T26_BB84Protocol_Reference () : Unit {
let threshold = 1;
use qs = Qubit[20];
// 1. Choose random basis and bits to encode
let basesAlice = RandomArray_Reference(Length(qs));
let bitsAlice = RandomArray_Reference(Length(qs));
// 2. Alice prepares her qubits
PrepareAlicesQubits_Reference(qs, basesAlice, bitsAlice);
// 3. Bob chooses random basis to measure in
let basesBob = RandomArray_Reference(Length(qs));
// 4. Bob measures Alice's qubits
let bitsBob = MeasureBobsQubits_Reference(qs, basesBob);
// 5. Generate shared key
let keyAlice = GenerateSharedKey_Reference(basesAlice, basesBob, bitsAlice);
let keyBob = GenerateSharedKey_Reference(basesAlice, basesBob, bitsBob);
// 6. Ensure at least the minimum percentage of bits match
if CheckKeysMatch_Reference(keyAlice, keyBob, threshold) {
Message($"Successfully generated keys {keyAlice}/{keyBob}");
}
}
//////////////////////////////////////////////////////////////////
// Part III. Eavesdropping
//////////////////////////////////////////////////////////////////
// Task 3.1. Eavesdrop!
operation Eavesdrop_Reference (q : Qubit, basis : Bool) : Bool {
return ResultAsBool(Measure([basis ? PauliX | PauliZ], [q]));
}
// Task 3.2. Catch the eavesdropper
operation T32_BB84ProtocolWithEavesdropper_Reference () : Unit {
let threshold = 1;
use qs = Qubit[20];
// 1. Choose random basis and bits to encode
let basesAlice = RandomArray_Reference(Length(qs));
let bitsAlice = RandomArray_Reference(Length(qs));
// 2. Alice prepares her qubits
PrepareAlicesQubits_Reference(qs, basesAlice, bitsAlice);
// Eve eavesdrops on all qubits, guessing the basis at random
for q in qs {
let n = Eavesdrop_Reference(q, DrawRandomBool(0.5));
}
// 3. Bob chooses random basis to measure in
let basesBob = RandomArray_Reference(Length(qs));
// 4. Bob measures Alice's qubits'
let bitsBob = MeasureBobsQubits_Reference(qs, basesBob);
// 5. Generate shared key
let keyAlice = GenerateSharedKey_Reference(basesAlice, basesBob, bitsAlice);
let keyBob = GenerateSharedKey_Reference(basesAlice, basesBob, bitsBob);
// 6. Ensure at least the minimum percentage of bits match
if CheckKeysMatch_Reference(keyAlice, keyBob, threshold) {
Message($"Successfully generated keys {keyAlice}/{keyBob}");
} else {
Message($"Caught an eavesdropper, discarding the keys {keyAlice}/{keyBob}");
}
}
}