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
/
Copy pathTests.qs
164 lines (124 loc) · 6.05 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
// 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.
//////////////////////////////////////////////////////////////////////
namespace Quantum.Kata.DeutschJozsaAlgorithm {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;
open Quantum.Kata.Utils;
function ConstantOrBalanced (value : Bool) : String {
return (value ? "constant" | "balanced");
}
//////////////////////////////////////////////////////////////////
// Part I. Classical algorithm
//////////////////////////////////////////////////////////////////
// Exercise 1.
@Test("QuantumSimulator")
operation T1_ClassicalFunction () : Unit {
for N in 1 .. 5 {
for x in 0 .. (1 <<< (N - 1)) - 1 {
let ret = Function_MostSignificantBit(x, N);
Fact(ret == 0, $"Unexpected return for x = {x}, N = {N}: expected 0, got {ret}");
}
for x in (1 <<< (N - 1))..(1 <<< N) - 1 {
let ret = Function_MostSignificantBit(x, N);
Fact(ret == 1, $"Unexpected return for x = {x}, N = {N}: expected 1, got {ret}");
}
}
}
// Exercise 2.
operation CheckClassicalAlgorithm (N : Int, f : (Int -> Int), expected : Bool, functionName : String) : Unit {
Message($"Testing {functionName}...");
let actual = IsFunctionConstant_Classical(N, f);
// check that the return value is correct
if actual != expected {
let actualStr = ConstantOrBalanced(actual);
let expectedStr = ConstantOrBalanced(expected);
fail $" identified as {actualStr} but it is {expectedStr}.";
}
Message(" correct!");
}
@Test("QuantumSimulator")
operation T2_ClassicalAlgorithm () : Unit {
CheckClassicalAlgorithm(4, Function_Zero_Reference, true, "f(x) = 0");
CheckClassicalAlgorithm(4, Function_One_Reference, true, "f(x) = 1");
CheckClassicalAlgorithm(4, Function_Xmod2_Reference, false, "f(x) = x mod 2");
CheckClassicalAlgorithm(4, Function_OddNumberOfOnes_Reference, false, "f(x) = (1 if x has odd number of 1s, and 0 otherwise)");
}
//////////////////////////////////////////////////////////////////
// Part II. Single-bit problem
//////////////////////////////////////////////////////////////////
// Exercise 3.
@Test("QuantumSimulator")
operation T3_PhaseOracle_OneMinusX () : Unit {
// We need to compare phase oracles as their controlled versions to account for the global phase.
AssertOperationsEqualReferenced(2,
qs => Controlled PhaseOracle_OneMinusX([qs[0]], qs[1]),
qs => Controlled PhaseOracle_OneMinusX_Reference([qs[0]], qs[1]));
}
// Exercise 4.
operation CheckDeutschAlgorithm (oracle : (Qubit => Unit), expected : Bool, functionName : String) : Unit {
Message($"Testing {functionName}...");
let actual = DeutschAlgorithm(oracle);
// check that the return value is correct
if actual != expected {
let actualStr = ConstantOrBalanced(actual);
let expectedStr = ConstantOrBalanced(expected);
fail $" identified as {actualStr} but it is {expectedStr}.";
}
let nu = GetOracleCallsCount(oracle);
if nu > 1 {
fail $" took {nu} oracle calls to decide; you are only allowed to call the oracle once";
}
Message(" correct!");
}
@Test("Microsoft.Quantum.Katas.CounterSimulator")
operation T4_DeutschAlgorithm () : Unit {
ResetOracleCallsCount();
for (oracle, expectedVerdict, name) in [(I, true, "f(x) = 0"),
(R(PauliI, 2.0 * PI(), _), true, "f(x) = 1"),
(Z, false, "f(x) = x"),
(BoundCA([X, Z, X]), false, "f(x) = 1 - x")] {
CheckDeutschAlgorithm(oracle, expectedVerdict, name);
}
}
//////////////////////////////////////////////////////////////////
// Part III. Multi-bit problem
//////////////////////////////////////////////////////////////////
// TODO: switch exercise 5 to a controlled wrapper as well
// Exercise 5.
@Test("QuantumSimulator")
operation T5_MostSignificantBitOracle () : Unit {
for N in 1 .. 5 {
AssertOperationsEqualReferenced(N, PhaseOracle_MostSignificantBit, PhaseOracle_MostSignificantBit_Reference);
}
}
// Exercise 6.
operation CheckQuantumAlgorithm (N : Int, oracle : (Qubit[] => Unit), expected : Bool, functionName : String) : Unit {
Message($"Testing {functionName}...");
let actual = DeutschJozsaAlgorithm(N, oracle);
// check that the return value is correct
if actual != expected {
let actualStr = ConstantOrBalanced(actual);
let expectedStr = ConstantOrBalanced(expected);
fail $" identified as {actualStr} but it is {expectedStr}.";
}
let nu = GetOracleCallsCount(oracle);
if nu > 1 {
fail $" took {nu} oracle calls to decide; you are only allowed to call the oracle once";
}
Message(" correct!");
}
@Test("Microsoft.Quantum.Katas.CounterSimulator")
operation T6_DeutschJozsaAlgorithm () : Unit {
ResetOracleCallsCount();
CheckQuantumAlgorithm(4, PhaseOracle_Zero_Reference, true, "f(x) = 0");
CheckQuantumAlgorithm(4, PhaseOracle_One_Reference, true, "f(x) = 1");
CheckQuantumAlgorithm(4, PhaseOracle_Xmod2_Reference, false, "f(x) = x mod 2");
CheckQuantumAlgorithm(4, PhaseOracle_OddNumberOfOnes_Reference, false, "f(x) = (1 if x has odd number of 1s, and 0 otherwise)");
}
}