-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathoperations.hpp
400 lines (341 loc) · 9.81 KB
/
operations.hpp
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2020 Xanadu Quantum Technologies Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file
* \rst
* Contains tensor representations of supported gates in ``lightning.qubit``.
* \endrst
*/
#pragma once
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include "typedefs.hpp"
const double SQRT_2 = sqrt(2);
const std::complex<double> IMAG(0, 1);
const std::complex<double> NEGATIVE_IMAG(0, -1);
/**
* Generates the identity gate.
*
* @return the identity tensor
*/
Gate_Xq<1> Identity() {
Gate_Xq<1> X(2, 2);
X.setValues({{1, 0}, {0, 1}});
return X;
}
/**
* Generates the X gate.
*
* @return the X tensor
*/
Gate_Xq<1> X() {
Gate_Xq<1> X(2, 2);
X.setValues({{0, 1}, {1, 0}});
return X;
}
/**
* Generates the Y gate.
*
* @return the Y tensor
*/
Gate_Xq<1> Y() {
Gate_Xq<1> Y(2, 2);
Y.setValues({{0, NEGATIVE_IMAG}, {IMAG, 0}});
return Y;
}
/**
* Generates the Z gate.
*
* @return the Z tensor
*/
Gate_Xq<1> Z() {
Gate_Xq<1> Z(2, 2);
Z.setValues({{1, 0}, {0, -1}});
return Z;
}
/**
* Generates the H gate.
*
* @return the H tensor
*/
Gate_Xq<1> H() {
Gate_Xq<1> H(2, 2);
H.setValues({{1/SQRT_2, 1/SQRT_2}, {1/SQRT_2, -1/SQRT_2}});
return H;
}
/**
* Generates the S gate.
*
* @return the S tensor
*/
Gate_Xq<1> S() {
Gate_Xq<1> S(2, 2);
S.setValues({{1, 0}, {0, IMAG}});
return S;
}
/**
* Generates the T gate.
*
* @return the T tensor
*/
Gate_Xq<1> T() {
Gate_Xq<1> T(2, 2);
const std::complex<double> exponent(0, M_PI/4);
T.setValues({{1, 0}, {0, std::pow(M_E, exponent)}});
return T;
}
/**
* Generates the X rotation gate.
*
* @param parameter the rotation angle
* @return the RX tensor
*/
Gate_Xq<1> RX(const double& parameter) {
Gate_Xq<1> RX(2, 2);
const std::complex<double> c (std::cos(parameter / 2), 0);
const std::complex<double> js (0, std::sin(-parameter / 2));
RX.setValues({{c, js}, {js, c}});
return RX;
}
/**
* Generates the Y rotation gate.
*
* @param parameter the rotation angle
* @return the RY tensor
*/
Gate_Xq<1> RY(const double& parameter) {
Gate_Xq<1> RY(2, 2);
const double c = std::cos(parameter / 2);
const double s = std::sin(parameter / 2);
RY.setValues({{c, -s}, {s, c}});
return RY;
}
/**
* Generates the Z rotation gate.
*
* @param parameter the rotation angle
* @return the RZ tensor
*/
Gate_Xq<1> RZ(const double& parameter) {
Gate_Xq<1> RZ(2, 2);
const std::complex<double> exponent(0, -parameter/2);
const std::complex<double> exponent_second(0, parameter/2);
const std::complex<double> first = std::pow(M_E, exponent);
const std::complex<double> second = std::pow(M_E, exponent_second);
RZ.setValues({{first, 0}, {0, second}});
return RZ;
}
/**
* Generates the phase-shift gate.
*
* @param parameter the phase shift
* @return the phase-shift tensor
*/
Gate_Xq<1> PhaseShift(const double& parameter) {
Gate_Xq<1> PhaseShift(2, 2);
const std::complex<double> exponent(0, parameter);
const std::complex<double> shift = std::pow(M_E, exponent);
PhaseShift.setValues({{1, 0}, {0, shift}});
return PhaseShift;
}
/**
* Generates the arbitrary single qubit rotation gate.
*
* The rotation is achieved through three separate rotations:
* \f$R(\phi, \theta, \omega)= RZ(\omega)RY(\theta)RZ(\phi)\f$.
*
* @param phi the first rotation angle
* @param theta the second rotation angle
* @param omega the third rotation angle
* @return the rotation tensor
*/
Gate_Xq<1> Rot(const double& phi, const double& theta, const double& omega) {
Gate_Xq<1> Rot(2, 2);
const std::complex<double> e00(0, (-phi - omega)/2);
const std::complex<double> e10(0, (-phi + omega)/2);
const std::complex<double> e01(0, (phi - omega)/2);
const std::complex<double> e11(0, (phi + omega)/2);
const std::complex<double> exp00 = std::pow(M_E, e00);
const std::complex<double> exp10 = std::pow(M_E, e10);
const std::complex<double> exp01 = std::pow(M_E, e01);
const std::complex<double> exp11 = std::pow(M_E, e11);
const double c = std::cos(theta / 2);
const double s = std::sin(theta / 2);
Rot.setValues({{exp00 * c, -exp01 * s}, {exp10 * s, exp11 * c}});
return Rot;
}
/**
* Generates the CNOT gate.
*
* @return the CNOT tensor
*/
Gate_Xq<2> CNOT() {
Gate_Xq<2> CNOT(2,2,2,2);
CNOT.setValues({{{{1, 0},{0, 0}},{{0, 1},{0, 0}}},{{{0, 0},{0, 1}},{{0, 0},{1, 0}}}});
return CNOT;
}
/**
* Generates the SWAP gate.
*
* @return the SWAP tensor
*/
Gate_Xq<2> SWAP() {
Gate_Xq<2> SWAP(2,2,2,2);
SWAP.setValues({{{{1, 0},{0, 0}},{{0, 0},{1, 0}}},{{{0, 1},{0, 0}},{{0, 0},{0, 1}}}});
return SWAP;
}
/**
* Generates the CZ gate.
*
* @return the CZ tensor
*/
Gate_Xq<2> CZ() {
Gate_Xq<2> CZ(2,2,2,2);
CZ.setValues({{{{1, 0},{0, 0}},{{0, 1},{0, 0}}},{{{0, 0},{1, 0}},{{0, 0},{0, -1}}}});
return CZ;
}
/**
* Generates the Toffoli gate.
*
* @return the Toffoli tensor
*/
Gate_Xq<3> Toffoli() {
Gate_Xq<3> Toffoli(2,2,2,2,2,2);
Toffoli.setValues({{{{{{1, 0},{0, 0}},{{0, 0},{0, 0}}},{{{0, 1},{0, 0}},{{0, 0},{0, 0}}}},
{{{{0, 0},{1, 0}},{{0, 0},{0, 0}}},{{{0, 0},{0, 1}},{{0, 0},{0, 0}}}}
},
{ {{{{0, 0},{0, 0}},{{1, 0},{0, 0}}},{{{0, 0},{0, 0}},{{0, 1},{0, 0}}}},
{{{{0, 0},{0, 0}},{{0, 0},{0, 1}}},{{{0, 0},{0, 0}},{{0, 0},{1, 0}}}}
}});
return Toffoli;
}
/**
* Generates the CSWAP gate.
*
* @return the CSWAP tensor
*/
Gate_Xq<3> CSWAP() {
Gate_Xq<3> CSWAP(2,2,2,2,2,2);
CSWAP.setValues({{{{{{1, 0},{0, 0}},{{0, 0},{0, 0}}},{{{0, 1},{0, 0}},{{0, 0},{0, 0}}}},
{{{{0, 0},{1, 0}},{{0, 0},{0, 0}}},{{{0, 0},{0, 1}},{{0, 0},{0, 0}}}}
},
{ {{{{0, 0},{0, 0}},{{1, 0},{0, 0}}},{{{0, 0},{0, 0}},{{0, 0},{1, 0}}}},
{{{{0, 0},{0, 0}},{{0, 1},{0, 0}}},{{{0, 0},{0, 0}},{{0, 0},{0, 1}}}}
}});
return CSWAP;
}
/**
* Generates the controlled-X rotation gate.
*
* @param parameter the rotation angle
* @return the CRX tensor
*/
Gate_Xq<2> CRX(const double& parameter) {
Gate_Xq<2> CRX(2, 2, 2, 2);
const std::complex<double> c (std::cos(parameter / 2), 0);
const std::complex<double> js (0, std::sin(-parameter / 2));
CRX.setValues({{{{1, 0},{0, 0}},{{0, 1},{0, 0}}},{{{0, 0},{c, js}},{{0, 0},{js, c}}}});
return CRX;
}
/**
* Generates the controlled-Y rotation gate.
*
* @param parameter the rotation angle
* @return the CRY tensor
*/
Gate_Xq<2> CRY(const double& parameter) {
Gate_Xq<2> CRY(2, 2, 2, 2);
const double c = std::cos(parameter / 2);
const double s = std::sin(parameter / 2);
CRY.setValues({{{{1, 0},{0, 0}},{{0, 1},{0, 0}}},{{{0, 0},{c, -s}},{{0, 0},{s, c}}}});
return CRY;
}
/**
* Generates the controlled-Z rotation gate.
*
* @param parameter the rotation angle
* @return the CRZ tensor
*/
Gate_Xq<2> CRZ(const double& parameter) {
Gate_Xq<2> CRZ(2, 2, 2, 2);
const std::complex<double> exponent(0, -parameter/2);
const std::complex<double> exponent_second(0, parameter/2);
const std::complex<double> first = std::pow(M_E, exponent);
const std::complex<double> second = std::pow(M_E, exponent_second);
CRZ.setValues({{{{1, 0},{0, 0}},{{0, 1},{0, 0}}},{{{0, 0},{first, 0}},{{0, 0},{0, second}}}});
return CRZ;
}
/**
* Generates the controlled rotation gate.
*
* This gate implements a rotation on a target qubit depending on a control qubit. The rotation
* on the target qubit is achieved through three separate rotations:
* \f$R(\phi, \theta, \omega)= RZ(\omega)RY(\theta)RZ(\phi)\f$.
*
* @param phi the first rotation angle
* @param theta the second rotation angle
* @param omega the third rotation angle
* @return the controlled rotation tensor
*/
Gate_Xq<2> CRot(const double& phi, const double& theta, const double& omega) {
Gate_Xq<2> CRot(2,2,2,2);
const std::complex<double> e00(0, (-phi - omega)/2);
const std::complex<double> e10(0, (-phi + omega)/2);
const std::complex<double> e01(0, (phi - omega)/2);
const std::complex<double> e11(0, (phi + omega)/2);
const std::complex<double> exp00 = std::pow(M_E, e00);
const std::complex<double> exp10 = std::pow(M_E, e10);
const std::complex<double> exp01 = std::pow(M_E, e01);
const std::complex<double> exp11 = std::pow(M_E, e11);
const double c = std::cos(theta / 2);
const double s = std::sin(theta / 2);
CRot.setValues({{{{1, 0},{0, 0}},{{0, 1},{0, 0}}},{{{0, 0},{exp00 * c, -exp01 * s}},
{{0, 0},{exp10 * s, exp11 * c}}
}});
return CRot;
}
// Defining the operation maps
const std::map<std::string, pfunc_Xq<1>> OneQubitOps = {
{"Identity", Identity},
{"PauliX", X},
{"PauliY", Y},
{"PauliZ", Z},
{"Hadamard", H},
{"S", S},
{"T", T}
};
const std::map<std::string, pfunc_Xq_one_param<1>> OneQubitOpsOneParam = {
{"RX", RX},
{"RY", RY},
{"RZ", RZ},
{"PhaseShift", PhaseShift}
};
const std::map<std::string, pfunc_Xq_three_params<1>> OneQubitOpsThreeParams = {
{"Rot", Rot}
};
const std::map<std::string, pfunc_Xq<2>> TwoQubitOps = {
{"CNOT", CNOT},
{"SWAP", SWAP},
{"CZ", CZ}
};
const std::map<std::string, pfunc_Xq_one_param<2>> TwoQubitOpsOneParam = {
{"CRX", CRX},
{"CRY", CRY},
{"CRZ", CRZ}
};
const std::map<std::string, pfunc_Xq_three_params<2>> TwoQubitOpsThreeParams = {
{"CRot", CRot}
};
const std::map<std::string, pfunc_Xq<3>> ThreeQubitOps = {
{"Toffoli", Toffoli},
{"CSWAP", CSWAP}
};