forked from cda-tum/mqt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatePreparation.cpp
277 lines (248 loc) · 9.41 KB
/
StatePreparation.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (c) 2025 Chair for Design Automation, TUM
* All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* Licensed under the MIT License
*/
#include "algorithms/StatePreparation.hpp"
#include "CircuitOptimizer.hpp"
#include "ir/operations/StandardOperation.hpp"
#include <cmath>
#include <complex>
#include <utility>
static const double EPS = 1e-10;
namespace qc {
using Matrix = std::vector<std::vector<double>>;
auto createStatePreparationCircuit(
const std::vector<std::complex<double>>& amplitudes) -> QuantumComputation {
if (!isNormalized(amplitudes)) {
throw std::invalid_argument{
"Using State Preparation with Amplitudes that are not normalized"};
}
// get number of qubits needed
double const numQubits = std::log2(amplitudes.size());
if (numQubits == 0 || std::floor(numQubits) != numQubits) {
throw std::invalid_argument{
"Using State Preparation with vector size that is not a power of 2"};
}
QuantumComputation toZeroCircuit =
gatesToUncompute(amplitudes, static_cast<size_t>(numQubits));
// invert circuit
CircuitOptimizer::flattenOperations(toZeroCircuit);
toZeroCircuit.invert();
return toZeroCircuit;
}
template <typename T>
[[noexcept]] auto isNormalized(std::vector<T> vec) -> bool {
return std::abs(1 - twoNorm(vec)) < EPS;
}
template <typename T>[[noexcept]] auto twoNorm(std::vector<T> vec) -> double {
double norm = 0;
for (auto elem : vec) {
norm += std::norm(elem);
}
return sqrt(norm);
}
[[noexcept]] auto kroneckerProduct(Matrix matrixA, Matrix matrixB) -> Matrix {
size_t const rowA = matrixA.size();
size_t const rowB = matrixB.size();
size_t const colA = matrixA[0].size();
size_t const colB = matrixB[0].size();
// initialize size
Matrix newMatrix{(rowA * rowB), std::vector<double>(colA * colB, 0)};
// code taken from RosettaCode slightly adapted
for (size_t i = 0; i < rowA; ++i) {
// k loops till rowB
for (size_t j = 0; j < colA; ++j) {
// j loops till colA
for (size_t k = 0; k < rowB; ++k) {
// l loops till colB
for (size_t l = 0; l < colB; ++l) {
// Each element of matrix A is
// multiplied by whole Matrix B
// resp and stored as Matrix C
newMatrix[i * rowB + k][j * colB + l] = matrixA[i][j] * matrixB[k][l];
}
}
}
}
return newMatrix;
}
[[noexcept]] auto createIdentity(size_t size) -> Matrix {
Matrix identity{
std::vector<std::vector<double>>(size, std::vector<double>(size, 0))};
for (size_t i = 0; i < size; ++i) {
identity[i][i] = 1;
}
return identity;
}
[[noexcept]] auto
matrixVectorProd(const Matrix& matrix,
std::vector<double> vector) -> std::vector<double> {
std::vector<double> result;
for (const auto& matrixVec : matrix) {
double sum{0};
for (size_t i = 0; i < matrixVec.size(); ++i) {
sum += matrixVec[i] * vector[i];
}
result.push_back(sum);
}
return result;
}
// creates circuit that takes desired vector to zero
[[noexcept]] auto gatesToUncompute(std::vector<std::complex<double>> amplitudes,
size_t numQubits) -> QuantumComputation {
QuantumComputation disentangler{numQubits};
for (size_t i = 0; i < numQubits; ++i) {
// rotations to disentangle LSB
auto [remainingParams, thetas, phis] = rotationsToDisentangle(amplitudes);
amplitudes = remainingParams;
// perform required rotations
bool addLastCnot = true;
double const phisNorm = twoNorm(phis);
double const thetasNorm = twoNorm(thetas);
if (phisNorm != 0 && thetasNorm != 0) {
addLastCnot = false;
}
if (phisNorm != 0) {
// call multiplex with RZGate
QuantumComputation rzMultiplexer =
multiplex(OpType{RZ}, phis, addLastCnot);
// append rzMultiplexer to disentangler, but it should only attach on
// qubits i-numQubits, thus "i" is added to the local qubit indices
for (auto& op : rzMultiplexer) {
for (auto& target : op->getTargets()) {
target += static_cast<unsigned int>(i);
}
for (auto control : op->getControls()) {
// there were some errors when accessing the qubit directly and
// adding to it
op->setControls(
Controls{Control{control.qubit + static_cast<unsigned int>(i)}});
}
}
disentangler.emplace_back<Operation>(rzMultiplexer.asOperation());
}
if (thetasNorm != 0) {
// call multiplex with RYGate
QuantumComputation ryMultiplexer =
multiplex(OpType{RY}, thetas, addLastCnot);
// append reversed ry_multiplexer to disentangler, but it should only
// attach on qubits i-numQubits, thus "i" is added to the local qubit
// indices
std::reverse(ryMultiplexer.begin(), ryMultiplexer.end());
for (auto& op : ryMultiplexer) {
for (auto& target : op->getTargets()) {
target += static_cast<unsigned int>(i);
}
for (auto control : op->getControls()) {
// there were some errors when accessing the qubit directly and
// adding to it
op->setControls(
Controls{Control{control.qubit + static_cast<unsigned int>(i)}});
}
}
disentangler.emplace_back<Operation>(ryMultiplexer.asOperation());
}
}
// adjust global phase according to the last e^(it)
double const arg = -std::arg(std::accumulate(
amplitudes.begin(), amplitudes.end(), std::complex<double>(0, 0)));
if (arg != 0) {
disentangler.gphase(arg);
}
return disentangler;
}
// works out Ry and Rz rotation angles used to disentangle LSB qubit
// rotations make up block diagonal matrix U
[[noexcept]] auto
rotationsToDisentangle(std::vector<std::complex<double>> amplitudes)
-> std::tuple<std::vector<std::complex<double>>, std::vector<double>,
std::vector<double>> {
std::vector<std::complex<double>> remainingVector;
std::vector<double> thetas;
std::vector<double> phis;
for (size_t i = 0; i < (amplitudes.size() / 2); ++i) {
auto [remains, theta, phi] =
blochAngles(amplitudes[2 * i], amplitudes[2 * i + 1]);
remainingVector.push_back(remains);
// minus sign because we move it to zero
thetas.push_back(-theta);
phis.push_back(-phi);
}
return {remainingVector, thetas, phis};
}
[[noexcept]] auto blochAngles(std::complex<double> const complexA,
std::complex<double> const complexB)
-> std::tuple<std::complex<double>, double, double> {
double theta{0};
double phi{0};
double finalT{0};
double const magA = std::abs(complexA);
double const magB = std::abs(complexB);
double const finalR = sqrt(pow(magA, 2) + pow(magB, 2));
if (finalR > EPS) {
theta = 2 * acos(magA / finalR);
double const aAngle = std::arg(complexA);
double const bAngle = std::arg(complexB);
finalT = aAngle + bAngle;
phi = bAngle - aAngle;
}
return {finalR * exp(std::complex<double>{0, 1} * finalT / 2.), theta, phi};
}
// recursive implementation that returns multiplexer circuit
/**
* @param target_gate : Ry or Rz gate to apply to target qubit, multiplexed
* over all other "select" qubits
* @param angles : list of rotation angles to apply Ry and Rz
* @param lastCnot : add last cnot if true
* @return multiplexer circuit as QuantumComputation
*/
[[noexcept]] auto multiplex(OpType targetGate, std::vector<double> angles,
bool lastCnot) -> QuantumComputation {
size_t const listLen = angles.size();
double const localNumQubits =
std::floor(std::log2(static_cast<double>(listLen))) + 1;
QuantumComputation multiplexer{static_cast<size_t>(localNumQubits)};
// recursion base case
if (localNumQubits == 1) {
multiplexer.emplace_back<StandardOperation>(multiplexer.getNqubits(),
Controls{}, 0, targetGate,
std::vector{angles[0]});
return multiplexer;
}
Matrix const matrix{std::vector<double>{0.5, 0.5},
std::vector<double>{0.5, -0.5}};
Matrix const identity =
createIdentity(static_cast<size_t>(pow(2., localNumQubits - 2.)));
Matrix const angleWeights = kroneckerProduct(matrix, identity);
angles = matrixVectorProd(angleWeights, angles);
std::vector<double> const angles1{
std::make_move_iterator(angles.begin()),
std::make_move_iterator(angles.begin() +
static_cast<int64_t>(listLen) / 2)};
QuantumComputation multiplex1 = multiplex(targetGate, angles1, false);
// append multiplex1 to multiplexer
multiplexer.emplace_back<Operation>(multiplex1.asOperation());
// flips the LSB qubit, control on MSB
multiplexer.cx(0, static_cast<Qubit>(localNumQubits - 1));
std::vector<double> const angles2{std::make_move_iterator(angles.begin()) +
static_cast<int64_t>(listLen) / 2,
std::make_move_iterator(angles.end())};
QuantumComputation multiplex2 = multiplex(targetGate, angles2, false);
// extra efficiency by reversing (!= inverting) second multiplex
if (listLen > 1) {
multiplex2.reverse();
multiplexer.emplace_back<Operation>(multiplex2.asOperation());
} else {
multiplexer.emplace_back<Operation>(multiplex2.asOperation());
}
if (lastCnot) {
multiplexer.cx(0, static_cast<Qubit>(localNumQubits - 1));
}
CircuitOptimizer::flattenOperations(multiplexer);
return multiplexer;
}
} // namespace qc