-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathalgorithms.rs
352 lines (350 loc) · 12 KB
/
algorithms.rs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use expect_test::{expect, Expect};
// Each file in the samples/algorithms folder is compiled and run as two tests and should
// have matching expect strings in this file. If new samples are added, this file will
// fail to compile until the new expect strings are added.
pub const BELLSTATE_EXPECT: Expect = expect![[r#"
Bell state |Φ+〉:
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
Bell state |Φ-〉:
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: −0.7071+0.0000𝑖
Bell state |Ψ+〉:
STATE:
|01⟩: 0.7071+0.0000𝑖
|10⟩: 0.7071+0.0000𝑖
Bell state |Ψ-〉:
STATE:
|01⟩: 0.7071+0.0000𝑖
|10⟩: −0.7071+0.0000𝑖
[(Zero, Zero), (One, One), (One, Zero), (One, Zero)]"#]];
pub const BELLSTATE_EXPECT_DEBUG: Expect = expect![[r#"
Bell state |Φ+〉:
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
Bell state |Φ-〉:
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: −0.7071+0.0000𝑖
Bell state |Ψ+〉:
STATE:
|01⟩: 0.7071+0.0000𝑖
|10⟩: 0.7071+0.0000𝑖
Bell state |Ψ-〉:
STATE:
|01⟩: 0.7071+0.0000𝑖
|10⟩: −0.7071+0.0000𝑖
[(Zero, Zero), (One, One), (One, Zero), (One, Zero)]"#]];
pub const BERNSTEINVAZIRANI_EXPECT: Expect = expect![[r#"
Successfully decoded bit string as int: 127
Successfully decoded bit string as int: 238
Successfully decoded bit string as int: 512
[127, 238, 512]"#]];
pub const BERNSTEINVAZIRANI_EXPECT_DEBUG: Expect = expect![[r#"
Successfully decoded bit string as int: 127
Successfully decoded bit string as int: 238
Successfully decoded bit string as int: 512
[127, 238, 512]"#]];
pub const BERNSTEINVAZIRANINISQ_EXPECT: Expect = expect!["[One, Zero, One, Zero, One]"];
pub const BERNSTEINVAZIRANINISQ_EXPECT_DEBUG: Expect = expect!["[One, Zero, One, Zero, One]"];
pub const BITFLIPCODE_EXPECT: Expect = expect![[r#"
STATE:
|001⟩: 0.4472+0.0000𝑖
|110⟩: 0.8944+0.0000𝑖
STATE:
|000⟩: 0.4472+0.0000𝑖
|111⟩: 0.8944+0.0000𝑖
One"#]];
pub const BITFLIPCODE_EXPECT_DEBUG: Expect = expect![[r#"
STATE:
|001⟩: 0.4472+0.0000𝑖
|110⟩: 0.8944+0.0000𝑖
STATE:
|000⟩: 0.4472+0.0000𝑖
|111⟩: 0.8944+0.0000𝑖
One"#]];
pub const CATSTATE_EXPECT: Expect = expect![[r#"
STATE:
|00000⟩: 0.7071+0.0000𝑖
|11111⟩: 0.7071+0.0000𝑖
[Zero, Zero, Zero, Zero, Zero]"#]];
pub const CATSTATE_EXPECT_DEBUG: Expect = expect![[r#"
STATE:
|00000⟩: 0.7071+0.0000𝑖
|11111⟩: 0.7071+0.0000𝑖
[Zero, Zero, Zero, Zero, Zero]"#]];
pub const DEUTSCHJOZSA_EXPECT: Expect = expect![[r#"
SimpleConstantBoolF is constant
SimpleBalancedBoolF is balanced
ConstantBoolF is constant
BalancedBoolF is balanced
[(SimpleConstantBoolF, true), (SimpleBalancedBoolF, false), (ConstantBoolF, true), (BalancedBoolF, false)]"#]];
pub const DEUTSCHJOZSA_EXPECT_DEBUG: Expect = expect![[r#"
SimpleConstantBoolF is constant
SimpleBalancedBoolF is balanced
ConstantBoolF is constant
BalancedBoolF is balanced
[(SimpleConstantBoolF, true), (SimpleBalancedBoolF, false), (ConstantBoolF, true), (BalancedBoolF, false)]"#]];
pub const DEUTSCHJOZSANISQ_EXPECT: Expect =
expect!["([One, Zero, Zero, Zero, Zero], [Zero, Zero, Zero, Zero, Zero])"];
pub const DEUTSCHJOZSANISQ_EXPECT_DEBUG: Expect =
expect!["([One, Zero, Zero, Zero, Zero], [Zero, Zero, Zero, Zero, Zero])"];
pub const DOTPRODUCTVIAPHASEESTIMATION_EXPECT: Expect = expect![[r#"
Computing inner product of vectors (cos(Θ₁/2), sin(Θ₁/2))⋅(cos(Θ₂/2), sin(Θ₂/2)) ≈ -cos(x𝝅/2ⁿ)
Θ₁=0.4487989505128276, Θ₂=0.6283185307179586.
x = 16, n = 4.
Computed value = 1.0, true value = 0.995974293995239
(16, 4)"#]];
pub const DOTPRODUCTVIAPHASEESTIMATION_EXPECT_DEBUG: Expect = expect![[r#"
Computing inner product of vectors (cos(Θ₁/2), sin(Θ₁/2))⋅(cos(Θ₂/2), sin(Θ₂/2)) ≈ -cos(x𝝅/2ⁿ)
Θ₁=0.4487989505128276, Θ₂=0.6283185307179586.
x = 16, n = 4.
Computed value = 1.0, true value = 0.995974293995239
(16, 4)"#]];
pub const ENTANGLEMENT_EXPECT: Expect = expect![[r#"
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
(Zero, Zero)"#]];
pub const ENTANGLEMENT_EXPECT_DEBUG: Expect = expect![[r#"
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
(Zero, Zero)"#]];
pub const GHZ_EXPECT: Expect = expect![[r#"
STATE:
|000⟩: 0.7071+0.0000𝑖
|111⟩: 0.7071+0.0000𝑖
[Zero, Zero, Zero]"#]];
pub const GHZ_EXPECT_DEBUG: Expect = expect![[r#"
STATE:
|000⟩: 0.7071+0.0000𝑖
|111⟩: 0.7071+0.0000𝑖
[Zero, Zero, Zero]"#]];
pub const GROVER_EXPECT: Expect = expect![[r#"
Number of iterations: 4
Reflecting about marked state...
Reflecting about marked state...
Reflecting about marked state...
Reflecting about marked state...
[Zero, One, Zero, One, Zero]"#]];
pub const GROVER_EXPECT_DEBUG: Expect = expect![[r#"
Number of iterations: 4
Reflecting about marked state...
Reflecting about marked state...
Reflecting about marked state...
Reflecting about marked state...
[Zero, One, Zero, One, Zero]"#]];
pub const HIDDENSHIFT_EXPECT: Expect = expect![[r#"
Found 170 successfully!
Found 512 successfully!
Found 999 successfully!
[170, 512, 999]"#]];
pub const HIDDENSHIFT_EXPECT_DEBUG: Expect = expect![[r#"
Found 170 successfully!
Found 512 successfully!
Found 999 successfully!
[170, 512, 999]"#]];
pub const HIDDENSHIFTNISQ_EXPECT: Expect = expect!["[One, Zero, Zero, Zero, Zero, One]"];
pub const HIDDENSHIFTNISQ_EXPECT_DEBUG: Expect = expect!["[One, Zero, Zero, Zero, Zero, One]"];
pub const JOINTMEASUREMENT_EXPECT: Expect = expect![[r#"
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
STATE:
|11⟩: 1.0000+0.0000𝑖
STATE:
|11⟩: 1.0000+0.0000𝑖
(Zero, [One, One])"#]];
pub const JOINTMEASUREMENT_EXPECT_DEBUG: Expect = expect![[r#"
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
STATE:
|11⟩: 1.0000+0.0000𝑖
STATE:
|11⟩: 1.0000+0.0000𝑖
(Zero, [One, One])"#]];
pub const MEASUREMENT_EXPECT: Expect = expect!["(Zero, [Zero, Zero])"];
pub const MEASUREMENT_EXPECT_DEBUG: Expect = expect!["(Zero, [Zero, Zero])"];
pub const PHASEFLIPCODE_EXPECT: Expect = expect![[r#"
STATE:
|000⟩: 0.4743+0.0000𝑖
|001⟩: 0.1581+0.0000𝑖
|010⟩: −0.1581+0.0000𝑖
|011⟩: −0.4743+0.0000𝑖
|100⟩: −0.1581+0.0000𝑖
|101⟩: −0.4743+0.0000𝑖
|110⟩: 0.4743+0.0000𝑖
|111⟩: 0.1581+0.0000𝑖
STATE:
|000⟩: 0.4743+0.0000𝑖
|001⟩: −0.1581+0.0000𝑖
|010⟩: −0.1581+0.0000𝑖
|011⟩: 0.4743+0.0000𝑖
|100⟩: −0.1581+0.0000𝑖
|101⟩: 0.4743+0.0000𝑖
|110⟩: 0.4743+0.0000𝑖
|111⟩: −0.1581+0.0000𝑖
One"#]];
pub const PHASEFLIPCODE_EXPECT_DEBUG: Expect = expect![[r#"
STATE:
|000⟩: 0.4743+0.0000𝑖
|001⟩: 0.1581+0.0000𝑖
|010⟩: −0.1581+0.0000𝑖
|011⟩: −0.4743+0.0000𝑖
|100⟩: −0.1581+0.0000𝑖
|101⟩: −0.4743+0.0000𝑖
|110⟩: 0.4743+0.0000𝑖
|111⟩: 0.1581+0.0000𝑖
STATE:
|000⟩: 0.4743+0.0000𝑖
|001⟩: −0.1581+0.0000𝑖
|010⟩: −0.1581+0.0000𝑖
|011⟩: 0.4743+0.0000𝑖
|100⟩: −0.1581+0.0000𝑖
|101⟩: 0.4743+0.0000𝑖
|110⟩: 0.4743+0.0000𝑖
|111⟩: −0.1581+0.0000𝑖
One"#]];
pub const QRNG_EXPECT: Expect = expect![[r#"
Sampling a random number between 0 and 100:
46"#]];
pub const QRNG_EXPECT_DEBUG: Expect = expect![[r#"
Sampling a random number between 0 and 100:
46"#]];
pub const QRNGNISQ_EXPECT: Expect = expect!["[Zero, Zero, One, One, One]"];
pub const QRNGNISQ_EXPECT_DEBUG: Expect = expect!["[Zero, Zero, One, One, One]"];
pub const QUANTUMHELLOWORLD_EXPECT: Expect = expect![[r#"
Hello world!
Zero"#]];
pub const QUANTUMHELLOWORLD_EXPECT_DEBUG: Expect = expect![[r#"
Hello world!
Zero"#]];
pub const RANDOMBIT_EXPECT: Expect = expect!["Zero"];
pub const RANDOMBIT_EXPECT_DEBUG: Expect = expect!["Zero"];
pub const SHOR_EXPECT: Expect = expect![[r#"
*** Factorizing 187, attempt 1.
Estimating period of 182.
Estimating frequency with bitsPrecision=17.
Estimated frequency=126158
Found period=80
Found factor=17
Found factorization 187 = 17 * 11
(17, 11)"#]];
pub const SHOR_EXPECT_DEBUG: Expect = expect![[r#"
*** Factorizing 187, attempt 1.
Estimating period of 182.
Estimating frequency with bitsPrecision=17.
Estimated frequency=126158
Found period=80
Found factor=17
Found factorization 187 = 17 * 11
(17, 11)"#]];
pub const SIMPLEBELLPAIR_EXPECT: Expect = expect![[r#"
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
(Zero, Zero)"#]];
pub const SIMPLEBELLPAIR_EXPECT_DEBUG: Expect = expect![[r#"
STATE:
|00⟩: 0.7071+0.0000𝑖
|11⟩: 0.7071+0.0000𝑖
(Zero, Zero)"#]];
pub const SIMPLEISING_EXPECT: Expect =
expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"];
pub const SIMPLEISING_EXPECT_DEBUG: Expect =
expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"];
pub const SIMPLEVQE_EXPECT: Expect = expect![[r#"
Beginning descent from value 0.43300000000000005.
Value improved to 0.35300000000000004.
Value improved to 0.3454.
Value improved to 0.3422.
Value improved to 0.3216.
Descent done. Attempts: 52, Step: 0.0009765625, Arguments: [1.5, 1.0625], Value: 0.3216.
0.3216"#]];
pub const SIMPLEVQE_EXPECT_DEBUG: Expect = expect![[r#"
Beginning descent from value 0.43300000000000005.
Value improved to 0.35300000000000004.
Value improved to 0.3454.
Value improved to 0.3422.
Value improved to 0.3216.
Descent done. Attempts: 52, Step: 0.0009765625, Arguments: [1.5, 1.0625], Value: 0.3216.
0.3216"#]];
pub const SUPERDENSECODING_EXPECT: Expect = expect!["((false, true), (false, true))"];
pub const SUPERDENSECODING_EXPECT_DEBUG: Expect = expect!["((false, true), (false, true))"];
pub const SUPERPOSITION_EXPECT: Expect = expect!["Zero"];
pub const SUPERPOSITION_EXPECT_DEBUG: Expect = expect!["Zero"];
pub const TELEPORTATION_EXPECT: Expect = expect![[r#"
Teleporting state |0〉
STATE:
|0⟩: 1.0000+0.0000𝑖
Received state |0〉
STATE:
|0⟩: 1.0000+0.0000𝑖
Teleporting state |1〉
STATE:
|1⟩: 1.0000+0.0000𝑖
Received state |1〉
STATE:
|1⟩: 1.0000+0.0000𝑖
Teleporting state |+〉
STATE:
|0⟩: 0.7071+0.0000𝑖
|1⟩: 0.7071+0.0000𝑖
Received state |+〉
STATE:
|0⟩: 0.7071+0.0000𝑖
|1⟩: 0.7071+0.0000𝑖
Teleporting state |-〉
STATE:
|0⟩: 0.7071+0.0000𝑖
|1⟩: −0.7071+0.0000𝑖
Received state |-〉
STATE:
|0⟩: 0.7071+0.0000𝑖
|1⟩: −0.7071+0.0000𝑖
[Zero, One, Zero, One]"#]];
pub const TELEPORTATION_EXPECT_DEBUG: Expect = expect![[r#"
Teleporting state |0〉
STATE:
|0⟩: 1.0000+0.0000𝑖
Received state |0〉
STATE:
|0⟩: 1.0000+0.0000𝑖
Teleporting state |1〉
STATE:
|1⟩: 1.0000+0.0000𝑖
Received state |1〉
STATE:
|1⟩: 1.0000+0.0000𝑖
Teleporting state |+〉
STATE:
|0⟩: 0.7071+0.0000𝑖
|1⟩: 0.7071+0.0000𝑖
Received state |+〉
STATE:
|0⟩: 0.7071+0.0000𝑖
|1⟩: 0.7071+0.0000𝑖
Teleporting state |-〉
STATE:
|0⟩: 0.7071+0.0000𝑖
|1⟩: −0.7071+0.0000𝑖
Received state |-〉
STATE:
|0⟩: 0.7071+0.0000𝑖
|1⟩: −0.7071+0.0000𝑖
[Zero, One, Zero, One]"#]];
pub const THREEQUBITREPETITIONCODE_EXPECT: Expect = expect!["(true, 0)"];
pub const THREEQUBITREPETITIONCODE_EXPECT_DEBUG: Expect = expect!["(true, 0)"];