forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.qs
63 lines (51 loc) · 1.75 KB
/
Program.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Samples.StateVisualizer {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Measurement;
operation QsMain () : Unit {
Teleport();
GroverSearch();
}
// Teleportation
operation Teleport () : Unit {
using ((msg, here, there) = (Qubit(), Qubit(), Qubit())) {
H(msg);
H(here);
CNOT(here, there);
CNOT(msg, here);
H(msg);
if (MResetZ(msg) == One) { Z(there); }
if (MResetZ(here) == One) { X(there); }
H(there);
}
}
// Grover's search
// Based on the Grover's algorithm kata
// https://github.com/microsoft/QuantumKatas/tree/master/GroversAlgorithm
operation AllOnesPhaseOracle (register : Qubit[]) : Unit {
Controlled Z(register[1...], register[0]);
}
operation AllZeroesPhaseOracle (register : Qubit[]) : Unit {
ApplyWith(ApplyToEachA(X, _), AllOnesPhaseOracle, register);
}
operation GroverIteration (register : Qubit[], oracle : (Qubit[] => Unit)) : Unit {
oracle(register);
ApplyToEach(H, register);
AllZeroesPhaseOracle(register);
ApplyToEach(H, register);
}
operation GroverSearch () : Unit {
let n = 3;
using (register = Qubit[n]) {
ApplyToEach(H, register);
for (i in 1 .. Floor(Sqrt(PowD(2.0, IntAsDouble(n))))) {
GroverIteration(register, AllOnesPhaseOracle);
}
ResetAll(register);
}
}
}