forked from Michaelvll/myQShor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperation.qs
134 lines (113 loc) · 3.24 KB
/
Operation.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace myShorLib {
open Microsoft.Quantum.Primitive;
open Microsoft.Quantum.Canon;
operation PhaseEstimation(
x : Int,
N : Int,
precision : Int) : Int
{
body
{
let eigenLength = BitSize(N);
mutable res = 0;
using (eigenvector = Qubit[eigenLength]){
X(eigenvector[0]);
using (target = Qubit[precision]){
PhaseEstimationImpl(x, N, target, eigenvector);
for (i in 0..(precision - 1)) {
set res = res * 2;
if (M(target[i]) == One) {
set res = res + 1;
}
}
ResetAll(target);
ResetAll(eigenvector);
}
}
return res;
}
}
operation PhaseEstimationImpl (
x : Int,
N : Int,
target : Qubit[],
eigenvector : Qubit[]) : ()
{
body {
let targetLength = Length(target);
for (idx in 0..(targetLength - 1)) {
H(target[idx]);
}
mutable power = 1;
for (idx in 0..(targetLength - 1)) {
(Controlled ConstructU) ([target[targetLength - 1 -idx]], (x, N, power, eigenvector));
set power = power * 2;
}
(InverseFT)(target);
// (Adjoint QFT)(BigEndian(target));
}
}
operation ConstructU(
x : Int,
modulus : Int,
power : Int,
target : Qubit[]) : ()
{
body {
ModularMultiplyByConstantLE(
PowerMod(x, power, modulus),
modulus,
LittleEndian(target)
);
}
adjoint auto
controlled auto
adjoint controlled auto
}
operation InverseFT(qs : Qubit[]) : ()
{
body {
let qLength = Length(qs);
for (i in 0..(qLength - 1)) {
for (j in 0..(i-1)) {
(Controlled R1Frac) ([qs[j]], (1, i - j, qs[i]));
}
H(qs[i]);
}
for (i in 0..qLength/2-1) {
SWAP(qs[i],qs[qLength-1-i]);
}
}
adjoint auto
controlled auto
controlled adjoint auto
}
function PowerMod(
x : Int,
power : Int,
modulus : Int) : Int
{
mutable times = x;
mutable result = 1;
mutable powerBits = power;
// Fast Power
let powerLength = BitSize(power);
for (i in 0 .. powerLength-1) {
if (powerBits % 2 == 1) {
set result = result * times % modulus;
}
set powerBits = powerBits / 2;
set times = times * times % modulus;
}
return result % modulus;
}
operation MyTest() : Int
{
body {
let a = PhaseEstimation(3, 5, 7);
return a;
}
}
}