-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGroversSearch.qs
More file actions
66 lines (59 loc) · 2.41 KB
/
Copy pathGroversSearch.qs
File metadata and controls
66 lines (59 loc) · 2.41 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
namespace Quantum.DecoratingTheTree
{
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
operation OracleConverterImpl (markingOracle : ((Qubit[], Qubit) => Unit : Adjoint), register : Qubit[]) : Unit {
body (...) {
using (target = Qubit()) {
// Put the target into the |-⟩ state
X(target);
H(target);
// Apply the marking oracle; since the target is in the |-⟩ state,
// flipping the target if the register satisfies the oracle condition will apply a -1 factor to the state
markingOracle(register, target);
// Put the target back into |0⟩ so we can return it
H(target);
X(target);
}
}
adjoint invert;
}
function OracleConverter (markingOracle : ((Qubit[], Qubit) => Unit : Adjoint)) : (Qubit[] => Unit : Adjoint) {
return OracleConverterImpl(markingOracle, _);
}
operation GroversSearch_Loop (register : Qubit[], oracle : ((Qubit[], Qubit) => Unit : Adjoint), iterations : Int) : Unit {
let phaseOracle = OracleConverter(oracle);
ApplyToEach(H, register);
for (i in 1 .. iterations) {
phaseOracle(register);
ApplyToEach(H, register);
ApplyToEach(X, register);
Controlled Z(Most(register), Tail(register));
ApplyToEach(X, register);
ApplyToEach(H, register);
}
}
operation GroversSearch_Main () : Bool[] {
let n = 9; // number of qubits
let oracle = Oracle_IsValidTreeDecoration;
let iter = 6;
mutable answer = new Bool[n];
using ((register, output) = (Qubit[n], Qubit())) {
mutable correct = false;
repeat {
GroversSearch_Loop(register, oracle, iter);
let res = MultiM(register);
// to check whether the result is correct, apply the oracle to the register plus ancilla after measurement
oracle(register, output);
if (MResetZ(output) == One) {
set correct = true;
set answer = BoolArrFromResultArr(res);
}
ResetAll(register);
} until (correct)
fixup {
}
}
return answer;
}
}