forked from microsoft/QuantumKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.qs
More file actions
115 lines (87 loc) · 4.25 KB
/
Tests.qs
File metadata and controls
115 lines (87 loc) · 4.25 KB
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
// 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.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. Quantum oracles
//////////////////////////////////////////////////////////////////
// Exercise 3.
@Test("QuantumSimulator")
operation T3_QuantumOracle () : Unit {
for N in 1 .. 5 {
AssertOperationsEqualReferenced(N, PhaseOracle_MostSignificantBit, PhaseOracle_MostSignificantBit_Reference);
}
}
//////////////////////////////////////////////////////////////////
// Part III. Quantum algorithm
//////////////////////////////////////////////////////////////////
// Exercise 4.
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 T4_QuantumAlgorithm () : 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)");
}
}