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
/
Tests.qs
249 lines (201 loc) · 7.54 KB
/
Tests.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
//////////////////////////////////////////////////////////////////////
// This file contains testing harness for all tasks.
// You should not modify anything in this file.
// The tasks themselves can be found in Tasks.qs file.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.MarkingOracles {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;
// ------------------------------------------------------
// The operation that runs the oracle on every bit string and compares the results with those of a classical function
// (also checks that there are no measurements)
operation AssertOracleImplementsFunction (N : Int, oracle : ((Qubit[], Qubit) => Unit), f : (Bool[] -> Bool)) : Unit {
let size = 1 <<< N;
use (qs, target) = (Qubit[N], Qubit());
for k in 0 .. size - 1 {
// Prepare k-th bit vector
let binary = IntAsBoolArray(k, N);
//Message($"{k}/{N} = {binary}");
// binary is little-endian notation, so the second vector tried has qubit 0 in state 1 and the rest in state 0
ApplyPauliFromBitString(PauliX, true, binary, qs);
// Apply the operation and check that it doesn't use measurements
within {
AllowAtMostNCallsCA(0, Measure, "Your solution should not use measurements");
AllowAtMostNCallsCA(0, M, "Your solution should not use measurements");
} apply {
oracle(qs, target);
}
// Check that the result is what we'd expect to measure
let val = f(binary);
AssertQubit(val ? One | Zero, target);
Reset(target);
// Check that the query qubits are still in the same state
ApplyPauliFromBitString(PauliX, true, binary, qs);
AssertAllZero(qs);
}
}
// ------------------------------------------------------
function PalindromeF(args : Bool[]) : Bool {
let N = Length(args);
for i in 0 .. N / 2 - 1 {
if args[i] != args[N - i - 1] {
return false;
}
}
return true;
}
@Test("QuantumSimulator")
operation T01_PalindromeOracle () : Unit {
for N in 2 .. 6 {
AssertOracleImplementsFunction(N, PalindromeOracle, PalindromeF);
}
}
// ------------------------------------------------------
function PeriodicGivenPeriodF(args : Bool[], P : Int) : Bool {
let N = Length(args);
for i in 0 .. N - P - 1 {
if args[i] != args[i + P] {
return false;
}
}
return true;
}
@Test("QuantumSimulator")
operation T02_PeriodicGivenPeriodOracle () : Unit {
for N in 2 .. 6 {
for P in 2 .. N - 1 {
AssertOracleImplementsFunction(N, PeriodicGivenPeriodOracle(_, _, P), PeriodicGivenPeriodF(_, P));
}
}
}
// ------------------------------------------------------
function PeriodicF(args : Bool[]) : Bool {
let N = Length(args);
for P in 1 .. N - 1 {
if PeriodicGivenPeriodF(args, P) {
return true;
}
}
return false;
}
@Test("QuantumSimulator")
operation T03_PeriodicOracle () : Unit {
for N in 2 .. 6 {
AssertOracleImplementsFunction(N, PeriodicOracle, PeriodicF);
}
}
// ------------------------------------------------------
function ContainsSubstringAtPositionF(args : Bool[], pattern : Bool[], P : Int) : Bool {
for i in 0 .. Length(pattern) - 1 {
if pattern[i] != args[i + P] {
return false;
}
}
return true;
}
@Test("QuantumSimulator")
operation T04_ContainsSubstringAtPositionOracle () : Unit {
for (N, P, pattern) in [
(2, 1, [true]),
(3, 0, [false, true]),
(4, 1, [true, true, false]),
(5, 3, [false])
] {
AssertOracleImplementsFunction(N,
ContainsSubstringAtPositionOracle(_, _, pattern, P),
ContainsSubstringAtPositionF(_, pattern, P));
}
}
// ------------------------------------------------------
function PatternMatchingF(args : Bool[], indices : Int[], pattern : Bool[]) : Bool {
for i in 0 .. Length(indices) - 1 {
if args[indices[i]] != pattern[i] {
return false;
}
}
return true;
}
@Test("QuantumSimulator")
operation T05_PatternMatchingOracle () : Unit {
for (N, indices, pattern) in [
(2, [], []),
(2, [1], [true]),
(3, [0, 2], [false, true]),
(4, [1, 3], [true, false]),
(5, [0, 1, 4], [true, true, false])
] {
AssertOracleImplementsFunction(N, PatternMatchingOracle(_, _, indices, pattern), PatternMatchingF(_, indices, pattern));
}
}
// ------------------------------------------------------
function ContainsSubstringF(args : Bool[], pattern : Bool[]) : Bool {
let N = Length(args);
let K = Length(pattern);
for P in 0 .. N - K {
if ContainsSubstringAtPositionF(args, pattern, P) {
return true;
}
}
return false;
}
@Test("QuantumSimulator")
operation T06_ContainsSubstringOracle () : Unit {
for (N, pattern) in [
(2, [true]),
(3, [false, true]),
(4, [true, true, false]),
(5, [false, false])
] {
AssertOracleImplementsFunction(N,
ContainsSubstringOracle(_, _, pattern),
ContainsSubstringF(_, pattern));
}
}
// ------------------------------------------------------
function BalancedF(args : Bool[]) : Bool {
return Count(x -> x, args) == Length(args) / 2;
}
@Test("QuantumSimulator")
operation T07_BalancedOracle () : Unit {
for N in 2 .. 2 .. 6 {
AssertOracleImplementsFunction(N, BalancedOracle, BalancedF);
}
}
// ------------------------------------------------------
function MajorityF(args : Bool[]) : Bool {
let N = Length(args);
return Count(x -> x, args) > (N - 1) / 2;
}
@Test("QuantumSimulator")
operation T08_MajorityOracle () : Unit {
let N = 7;
AssertOracleImplementsFunction(N, MajorityOracle, MajorityF);
}
// ------------------------------------------------------
function BitSumDivisibleBy3F(args : Bool[]) : Bool {
return Count(x -> x, args) % 3 == 0;
}
@Test("QuantumSimulator")
operation T09_BitSumDivisibleBy3Oracle () : Unit {
for N in 3 .. 6 {
AssertOracleImplementsFunction(N, BitSumDivisibleBy3Oracle, BitSumDivisibleBy3F);
}
}
// ------------------------------------------------------
function DivisibleBy3F(args : Bool[]) : Bool {
return BoolArrayAsInt(args) % 3 == 0;
}
@Test("QuantumSimulator")
operation T10_DivisibleBy3Oracle () : Unit {
for N in 2 .. 7 {
AssertOracleImplementsFunction(N, DivisibleBy3Oracle, DivisibleBy3F);
}
}
}