-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeutschJozsa.qs
79 lines (64 loc) · 2.56 KB
/
DeutschJozsa.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
// Copyright (c) Sarah Kaiser. All rights reserved.
// Licensed under the MIT License.
namespace OSD.DeutschJozsa {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Diagnostics;
operation ApplyZeroOracle(control : Qubit, target : Qubit) : Unit {
}
operation ApplyOneOracle(control : Qubit, target : Qubit) : Unit {
X(target);
}
operation ApplyIdOracle(control : Qubit, target : Qubit) : Unit {
CNOT(control, target);
}
operation ApplyNotOracle(control : Qubit, target : Qubit) : Unit {
X(control);
CNOT(control, target);
X(control);
}
operation CheckIfOracleIsBalanced(
verbose : Bool,
oracle : ((Qubit, Qubit) => Unit)
) : Bool {
use (control, target) = (Qubit(), Qubit());
// Prepare superposition on the control register.
H(control);
// Use the phase kickback technique from Chapter 7
// to learn a global property of our oracle.
within {
X(target);
H(target);
} apply {
if (verbose) {
Message("Before oracle call:");
DumpMachine();
}
oracle(control, target);
if (verbose) {
Message("\n\nAfter oracle call:");
DumpMachine();
}
}
return MResetX(control) == One;
}
operation RunDeutschJozsaAlgorithm(verbose : Bool) : Unit {
Fact(not CheckIfOracleIsBalanced(false, ApplyZeroOracle), "Test failed for zero oracle.");
if (verbose) {
Message($"The ZeroOracle is Balanced: {CheckIfOracleIsBalanced(false, ApplyZeroOracle)}");
}
Fact(not CheckIfOracleIsBalanced(false, ApplyOneOracle), "Test failed for one oracle.");
if (verbose) {
Message($"The OneOracle is Balanced: {CheckIfOracleIsBalanced(false, ApplyOneOracle)}");
}
Fact(CheckIfOracleIsBalanced(false, ApplyIdOracle), "Test failed for id oracle.");
if (verbose) {
Message($"The IdOracle is Balanced: {CheckIfOracleIsBalanced(false, ApplyIdOracle)}");
}
Fact(CheckIfOracleIsBalanced(false, ApplyNotOracle), "Test failed for not oracle.");
if (verbose) {
Message($"The NotOracle is Balanced: {CheckIfOracleIsBalanced(false, ApplyNotOracle)}");
}
Message("All tests passed!");
}
}