This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrovers.qs
64 lines (52 loc) · 1.81 KB
/
Grovers.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
namespace Final_Project {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Extensions.Convert;
open Microsoft.Quantum.Extensions.Math;
/// # Summary
/// GroverIteration is a single step in Grover Algorithm.
/// For more insightful description look at Grovers Kata.
///
/// # Example
/// ```Q#
/// using ((x, y, t) = (Qubit[4], Qubit[4], Qubit())) {
/// GroverIteration(x, y, t, distance_cmp);
/// }
/// ```
operation GroverIteration (x : Qubit[], y : Qubit[], target: Qubit, oracle : ((Qubit[], Qubit[], Qubit) => Unit : Adjoint)) : Unit {
body (...) {
oracle(x, y, target);
ApplyToEachA(H, x);
ApplyToEachA(X, x);
ApplyToEachA(H, y);
ApplyToEachA(X, y);
Controlled Z(Most(x), Tail(x));
Controlled Z(Most(y), Tail(y));
ApplyToEachA(X, y);
ApplyToEachA(H, y);
ApplyToEachA(X, x);
ApplyToEachA(H, x);
}
adjoint invert;
}
/// # Summary
/// GroverSearch is a performing iterations steps in Grover Algorithm.
/// For more insightful description look at Grovers Kata.
///
/// # Example
/// ```Q#
/// using ((x, y, t) = (Qubit[4], Qubit[4], Qubit())) {
/// GroverSearch(x, y, t, distance_cmp, 4);
/// }
/// ```
operation GroversSearch (x : Qubit[], y : Qubit[], target : Qubit, oracle : ((Qubit[], Qubit[], Qubit) => Unit : Adjoint), iterations : Int) : Unit {
body (...) {
ApplyToEachA(H, x);
ApplyToEachA(H, y);
for (i in 1 .. iterations) {
GroverIteration(x, y, target, oracle);
}
}
adjoint invert;
}
}