-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathsimulator_basic.h
More file actions
349 lines (283 loc) · 9.24 KB
/
Copy pathsimulator_basic.h
File metadata and controls
349 lines (283 loc) · 9.24 KB
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
// Copyright 2019 Google LLC. All Rights Reserved.
//
// 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
//
// https://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.
#ifndef SIMULATOR_BASIC_H_
#define SIMULATOR_BASIC_H_
#include <complex>
#include <cstdint>
#include <functional>
#include <vector>
#include "simulator.h"
#include "statespace_basic.h"
namespace qsim {
/**
* Quantum circuit simulator without vectorization.
*/
template <typename For, typename FP = float>
class SimulatorBasic final : public SimulatorBase {
public:
using StateSpace = StateSpaceBasic<For, FP>;
using State = typename StateSpace::State;
using fp_type = typename StateSpace::fp_type;
template <typename... ForArgs>
explicit SimulatorBasic(ForArgs&&... args) : for_(args...) {}
/**
* Applies a gate using non-vectorized instructions.
* @param qs Indices of the qubits affected by this gate.
* @param matrix Matrix representation of the gate to be applied.
* @param state The state of the system, to be updated by this method.
*/
void ApplyGate(const std::vector<unsigned>& qs,
const fp_type* matrix, State& state) const {
// Assume qs[0] < qs[1] < qs[2] < ... .
switch (qs.size()) {
case 0:
ApplyGateH<0>(qs, matrix, state);
break;
case 1:
ApplyGateH<1>(qs, matrix, state);
break;
case 2:
ApplyGateH<2>(qs, matrix, state);
break;
case 3:
ApplyGateH<3>(qs, matrix, state);
break;
case 4:
ApplyGateH<4>(qs, matrix, state);
break;
case 5:
ApplyGateH<5>(qs, matrix, state);
break;
case 6:
ApplyGateH<6>(qs, matrix, state);
break;
default:
// Not implemented.
break;
}
}
/**
* Applies a controlled gate using non-vectorized instructions.
* @param qs Indices of the qubits affected by this gate.
* @param cqs Indices of control qubits.
* @param cvals Bit mask of control qubit values.
* @param matrix Matrix representation of the gate to be applied.
* @param state The state of the system, to be updated by this method.
*/
void ApplyControlledGate(const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals,
const fp_type* matrix, State& state) const {
// Assume qs[0] < qs[1] < qs[2] < ... .
if (cqs.size() == 0) {
ApplyGate(qs, matrix, state);
return;
}
switch (qs.size()) {
case 0:
ApplyControlledGateH<0>(qs, cqs, cvals, matrix, state);
break;
case 1:
ApplyControlledGateH<1>(qs, cqs, cvals, matrix, state);
break;
case 2:
ApplyControlledGateH<2>(qs, cqs, cvals, matrix, state);
break;
case 3:
ApplyControlledGateH<3>(qs, cqs, cvals, matrix, state);
break;
case 4:
ApplyControlledGateH<4>(qs, cqs, cvals, matrix, state);
break;
default:
// Not implemented.
break;
}
}
/**
* Computes the expectation value of an operator using non-vectorized
* instructions.
* @param qs Indices of the qubits the operator acts on.
* @param matrix The operator matrix.
* @param state The state of the system.
* @return The computed expectation value.
*/
std::complex<double> ExpectationValue(const std::vector<unsigned>& qs,
const fp_type* matrix,
const State& state) const {
// Assume qs[0] < qs[1] < qs[2] < ... .
switch (qs.size()) {
case 1:
return ExpectationValueH<1>(qs, matrix, state);
break;
case 2:
return ExpectationValueH<2>(qs, matrix, state);
break;
case 3:
return ExpectationValueH<3>(qs, matrix, state);
break;
case 4:
return ExpectationValueH<4>(qs, matrix, state);
break;
case 5:
return ExpectationValueH<5>(qs, matrix, state);
break;
case 6:
return ExpectationValueH<6>(qs, matrix, state);
break;
default:
// Not implemented.
break;
}
return 0;
}
/**
* @return The size of SIMD register if applicable.
*/
static unsigned SIMDRegisterSize() {
return 1;
}
private:
template <unsigned H>
void ApplyGateH(const std::vector<unsigned>& qs,
const fp_type* matrix, State& state) const {
auto f = [](unsigned n, unsigned m, uint64_t i, const fp_type* v,
const uint64_t* ms, const uint64_t* xss, fp_type* rstate) {
constexpr unsigned hsize = 1 << H;
fp_type rn, in;
fp_type rs[hsize], is[hsize];
uint64_t ii = i & ms[0];
for (unsigned j = 1; j <= H; ++j) {
i *= 2;
ii |= i & ms[j];
}
auto p0 = rstate + 2 * ii;
for (unsigned k = 0; k < hsize; ++k) {
rs[k] = *(p0 + xss[k]);
is[k] = *(p0 + xss[k] + 1);
}
uint64_t j = 0;
for (unsigned k = 0; k < hsize; ++k) {
rn = rs[0] * v[j] - is[0] * v[j + 1];
in = rs[0] * v[j + 1] + is[0] * v[j];
j += 2;
for (unsigned l = 1; l < hsize; ++l) {
rn += rs[l] * v[j] - is[l] * v[j + 1];
in += rs[l] * v[j + 1] + is[l] * v[j];
j += 2;
}
*(p0 + xss[k]) = rn;
*(p0 + xss[k] + 1) = in;
}
};
uint64_t ms[H + 1];
uint64_t xss[1 << H];
FillIndices<H>(state.num_qubits(), qs, ms, xss);
unsigned n = state.num_qubits() > H ? state.num_qubits() - H : 0;
uint64_t size = uint64_t{1} << n;
for_.Run(size, f, matrix, ms, xss, state.get());
}
template <unsigned H>
void ApplyControlledGateH(const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs,
uint64_t cvals, const fp_type* matrix,
State& state) const {
auto f = [](unsigned n, unsigned m, uint64_t i, const fp_type* v,
const uint64_t* ms, const uint64_t* xss,
uint64_t cvalsh, uint64_t cmaskh, fp_type* rstate) {
constexpr unsigned hsize = 1 << H;
fp_type rn, in;
fp_type rs[hsize], is[hsize];
uint64_t ii = i & ms[0];
for (unsigned j = 1; j <= H; ++j) {
i *= 2;
ii |= i & ms[j];
}
if ((ii & cmaskh) == cvalsh) {
auto p0 = rstate + 2 * ii;
for (unsigned k = 0; k < hsize; ++k) {
rs[k] = *(p0 + xss[k]);
is[k] = *(p0 + xss[k] + 1);
}
uint64_t j = 0;
for (unsigned k = 0; k < hsize; ++k) {
rn = rs[0] * v[j] - is[0] * v[j + 1];
in = rs[0] * v[j + 1] + is[0] * v[j];
j += 2;
for (unsigned l = 1; l < hsize; ++l) {
rn += rs[l] * v[j] - is[l] * v[j + 1];
in += rs[l] * v[j + 1] + is[l] * v[j];
j += 2;
}
*(p0 + xss[k]) = rn;
*(p0 + xss[k] + 1) = in;
}
}
};
uint64_t ms[H + 1];
uint64_t xss[1 << H];
FillIndices<H>(state.num_qubits(), qs, ms, xss);
auto m = GetMasks7(state.num_qubits(), qs, cqs, cvals);
unsigned n = state.num_qubits() > H ? state.num_qubits() - H : 0;
uint64_t size = uint64_t{1} << n;
for_.Run(size, f, matrix, ms, xss, m.cvalsh, m.cmaskh, state.get());
}
template <unsigned H>
std::complex<double> ExpectationValueH(const std::vector<unsigned>& qs,
const fp_type* matrix,
const State& state) const {
auto f = [](unsigned n, unsigned m, uint64_t i, const fp_type* v,
const uint64_t* ms, const uint64_t* xss,
const fp_type* rstate) {
constexpr unsigned hsize = 1 << H;
fp_type rn, in;
fp_type rs[hsize], is[hsize];
uint64_t ii = i & ms[0];
for (unsigned j = 1; j <= H; ++j) {
i *= 2;
ii |= i & ms[j];
}
auto p0 = rstate + 2 * ii;
for (unsigned k = 0; k < hsize; ++k) {
rs[k] = *(p0 + xss[k]);
is[k] = *(p0 + xss[k] + 1);
}
double re = 0;
double im = 0;
uint64_t j = 0;
for (unsigned k = 0; k < hsize; ++k) {
rn = rs[0] * v[j] - is[0] * v[j + 1];
in = rs[0] * v[j + 1] + is[0] * v[j];
j += 2;
for (unsigned l = 1; l < hsize; ++l) {
rn += rs[l] * v[j] - is[l] * v[j + 1];
in += rs[l] * v[j + 1] + is[l] * v[j];
j += 2;
}
re += rs[k] * rn + is[k] * in;
im += rs[k] * in - is[k] * rn;
}
return std::complex<double>{re, im};
};
uint64_t ms[H + 1];
uint64_t xss[1 << H];
FillIndices<H>(state.num_qubits(), qs, ms, xss);
unsigned n = state.num_qubits() > H ? state.num_qubits() - H : 0;
uint64_t size = uint64_t{1} << n;
using Op = std::plus<std::complex<double>>;
return for_.RunReduce(size, f, Op(), matrix, ms, xss, state.get());
}
For for_;
};
} // namespace qsim
#endif // SIMULATOR_BASIC_H_