forked from cda-tum/mqt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQPE.cpp
149 lines (130 loc) · 4.22 KB
/
QPE.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "algorithms/QPE.hpp"
namespace qc {
QPE::QPE(const std::size_t nq, const bool exact, const bool iter)
: precision(nq), iterative(iter) {
if (exact) {
// if an exact solution is wanted, generate a random n-bit number and
// convert it to an appropriate phase
const std::uint64_t max = 1ULL << nq;
auto distribution =
std::uniform_int_distribution<std::uint64_t>(0, max - 1);
std::uint64_t theta = 0;
while (theta == 0) {
theta = distribution(mt);
}
lambda = 0.;
for (std::size_t i = 0; i < nq; ++i) {
if ((theta & (1ULL << (nq - i - 1))) != 0) {
lambda += 1. / static_cast<double>(1ULL << i);
}
}
} else {
// if an inexact solution is wanted, generate a random n+1-bit number (that
// has its last bit set) and convert it to an appropriate phase
const std::uint64_t max = 1ULL << (nq + 1);
auto distribution =
std::uniform_int_distribution<std::uint64_t>(0, max - 1);
std::uint64_t theta = 0;
while ((theta & 1) == 0) {
theta = distribution(mt);
}
lambda = 0.;
for (std::size_t i = 0; i <= nq; ++i) {
if ((theta & (1ULL << (nq - i))) != 0) {
lambda += 1. / static_cast<double>(1ULL << i);
}
}
}
createCircuit();
}
QPE::QPE(const fp l, const std::size_t prec, const bool iter)
: lambda(l), precision(prec), iterative(iter) {
createCircuit();
}
std::ostream& QPE::printStatistics(std::ostream& os) const {
os << "QPE Statistics:\n";
os << "\tn: " << nqubits + 1 << "\n";
os << "\tm: " << getNindividualOps() << "\n";
os << "\tlambda: " << lambda << "π" << "\n";
os << "\tprecision: " << precision << "\n";
os << "\titerative: " << iterative << "\n";
os << "--------------" << "\n";
return os;
}
void QPE::createCircuit() {
addQubitRegister(1, "psi");
if (iterative) {
addQubitRegister(1, "q");
} else {
addQubitRegister(precision, "q");
}
addClassicalRegister(precision, "c");
// prepare eigenvalue
x(0);
if (iterative) {
// set up initial layout
initialLayout[0] = 1;
initialLayout[1] = 0;
setLogicalQubitGarbage(1);
outputPermutation.erase(0);
outputPermutation[1] = 0;
for (std::size_t i = 0; i < precision; i++) {
// Hadamard
h(1);
// normalize angle
const auto angle = std::remainder(
static_cast<double>(1ULL << (precision - 1 - i)) * lambda, 2.0);
// controlled phase rotation
cp(angle * PI, 1, 0);
// hybrid quantum-classical inverse QFT
for (std::size_t j = 0; j < i; j++) {
auto iQFTLambda = -PI / static_cast<double>(1ULL << (i - j));
classicControlled(P, 1, {j, 1U}, 1U, {iQFTLambda});
}
h(1);
// measure result
measure(1, i);
// reset qubit if not finished
if (i < precision - 1) {
reset(1);
}
}
} else {
// set up initial layout
initialLayout[0] = static_cast<Qubit>(precision);
for (std::size_t i = 1; i <= precision; ++i) {
initialLayout[static_cast<Qubit>(i)] = static_cast<Qubit>(i - 1);
}
setLogicalQubitGarbage(static_cast<Qubit>(precision));
outputPermutation.erase(0);
// Hadamard Layer
for (std::size_t i = 1; i <= precision; i++) {
h(static_cast<Qubit>(i));
}
for (std::size_t i = 0; i < precision; i++) {
// normalize angle
const auto angle = std::remainder(
static_cast<double>(1ULL << (precision - 1 - i)) * lambda, 2.0);
// controlled phase rotation
cp(angle * PI, static_cast<Qubit>(1 + i), 0);
// inverse QFT
for (std::size_t j = 1; j < 1 + i; j++) {
const auto iQFTLambda = -PI / static_cast<double>(2ULL << (i - j));
if (j == i) {
csdg(static_cast<Qubit>(i), static_cast<Qubit>(1 + i));
} else if (j == (i - 1)) {
ctdg(static_cast<Qubit>(i - 1), static_cast<Qubit>(1 + i));
} else {
cp(iQFTLambda, static_cast<Qubit>(j), static_cast<Qubit>(1 + i));
}
}
h(static_cast<Qubit>(1 + i));
}
// measure results
for (std::size_t i = 0; i < nqubits - 1; i++) {
measure(static_cast<Qubit>(i + 1), i);
outputPermutation[static_cast<Qubit>(i + 1)] = static_cast<Qubit>(i);
}
}
}
} // namespace qc