-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathsimulator.h
More file actions
516 lines (412 loc) · 13.9 KB
/
simulator.h
File metadata and controls
516 lines (412 loc) · 13.9 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// 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_H_
#define SIMULATOR_H_
#include <cstdint>
#include "bits.h"
namespace qsim {
/**
* Base class for simulator classes.
*/
class SimulatorBase {
protected:
// The follwoing template parameters are used for functions below.
// H - the number of high (target) qubits.
// L - the number of low (target) qubits.
// R - SIMD register width in floats.
// Fills the table of masks (ms) that is used to calculate base state indices
// and the table of offset indices (xss) that is used to access the state
// vector entries in matrix-vector multiplication functions. This function is
// used in simulator_basic.h, simulator_sse.h and simulator_avx.h (no bmi2
// version).
template <unsigned H, unsigned L = 0>
static void FillIndices(unsigned num_qubits, const std::vector<unsigned>& qs,
uint64_t* ms, uint64_t* xss) {
constexpr unsigned hsize = 1 << H;
if (H == 0) {
ms[0] = uint64_t(-1);
xss[0] = 0;
} else {
uint64_t xs[H + 1];
xs[0] = uint64_t{1} << (qs[L] + 1);
ms[0] = (uint64_t{1} << qs[L]) - 1;
for (unsigned i = 1; i < H; ++i) {
xs[i] = uint64_t{1} << (qs[L + i] + 1);
ms[i] = ((uint64_t{1} << qs[L + i]) - 1) ^ (xs[i - 1] - 1);
}
ms[H] = ((uint64_t{1} << num_qubits) - 1) ^ (xs[H - 1] - 1);
for (unsigned i = 0; i < hsize; ++i) {
uint64_t a = 0;
for (uint64_t k = 0; k < H; ++k) {
a += xs[k] * ((i >> k) & 1);
}
xss[i] = a;
}
}
}
// Fills gate matrix entries for gates with low qubits.
template <unsigned H, unsigned L, unsigned R, typename fp_type>
static void FillMatrix(unsigned qmaskl, const fp_type* matrix, fp_type* w) {
constexpr unsigned gsize = 1 << (H + L);
constexpr unsigned hsize = 1 << H;
constexpr unsigned lsize = 1 << L;
constexpr unsigned rsize = 1 << R;
unsigned s = 0;
for (unsigned i = 0; i < hsize; ++i) {
for (unsigned j = 0; j < gsize; ++j) {
unsigned p0 = 2 * i * lsize * gsize + 2 * lsize * (j / lsize);
for (unsigned k = 0; k < rsize; ++k) {
unsigned l = bits::CompressBits(k, R, qmaskl);
unsigned p = p0 + 2 * (gsize * l + (j + l) % lsize);
w[s + 0] = matrix[p];
w[s + rsize] = matrix[p + 1];
++s;
}
s += rsize;
}
}
}
// Fills gate matrix entries for controlled gates with high target qubits
// and low control qubits.
template <unsigned H, unsigned R, typename fp_type>
static void FillControlledMatrixH(uint64_t cvalsl, uint64_t cmaskl,
const fp_type* matrix, fp_type* w) {
constexpr unsigned hsize = 1 << H;
constexpr unsigned rsize = 1 << R;
unsigned s = 0;
for (unsigned i = 0; i < hsize; ++i) {
for (unsigned j = 0; j < hsize; ++j) {
unsigned p = hsize * i + j;
fp_type v = i == j ? 1 : 0;
for (unsigned k = 0; k < rsize; ++k) {
w[s] = cvalsl == (k & cmaskl) ? matrix[2 * p] : v;
w[s + rsize] = cvalsl == (k & cmaskl) ? matrix[2 * p + 1] : 0;
++s;
}
s += rsize;
}
}
}
// Fills gate matrix entries for controlled gates with low target qubits
// and low control qubits.
template <unsigned H, unsigned L, unsigned R, typename fp_type>
static void FillControlledMatrixL(uint64_t cvalsl, uint64_t cmaskl,
unsigned qmaskl, const fp_type* matrix,
fp_type* w) {
constexpr unsigned gsize = 1 << (H + L);
constexpr unsigned hsize = 1 << H;
constexpr unsigned lsize = 1 << L;
constexpr unsigned rsize = 1 << R;
unsigned s = 0;
for (unsigned i = 0; i < hsize; ++i) {
for (unsigned j = 0; j < gsize; ++j) {
unsigned p0 = i * lsize * gsize + lsize * (j / lsize);
for (unsigned k = 0; k < rsize; ++k) {
unsigned l = bits::CompressBits(k, R, qmaskl);
unsigned p = p0 + gsize * l + (j + l) % lsize;
fp_type v = p / gsize == p % gsize ? 1 : 0;
w[s] = cvalsl == (k & cmaskl) ? matrix[2 * p] : v;
w[s + rsize] = cvalsl == (k & cmaskl) ? matrix[2 * p + 1] : 0;
++s;
}
s += rsize;
}
}
}
/*
The GetMasks* functions below provide various masks and related values.
GetMasks1, GetMasks2, GetMasks3, GetMasks4, GetMasks5 and GetMasks6 are
used in simulator_avx.h (BMI2 version) and in simulator_avx512.h. GetMasks7,
GetMasks8, GetMasks9, GetMasks10 and GetMasks11 are used in simulator_avx.h
(no BMI2 version) and in simulator_sse.h.
imaskh - inverted mask of high qubits (high control and target qubits).
qmaskh - mask of high qubits (high target qubits).
cvalsh - control bit values of high control qubits placed in correct
positions.
cvalsl - control bit values of low control qubits placed in correct positions.
cmaskh - mask of high control qubits.
cmaskl - mask of low control qubits.
qmaskl - mask of low qubits (low target qubits).
cl - the number of low control qubits.
Note that imaskh, qmaskh and cvalsh are multiplied by two in GetMasks1,
GetMasks2, GetMasks3, GetMasks4, GetMasks5 and GetMasks6.
*/
struct Masks1 {
uint64_t imaskh;
uint64_t qmaskh;
};
template <unsigned H, unsigned R>
static Masks1 GetMasks1(const std::vector<unsigned>& qs) {
uint64_t qmaskh = 0;
for (unsigned i = 0; i < H; ++i) {
qmaskh |= uint64_t{1} << qs[i];
}
return {2 * (~qmaskh ^ ((1 << R) - 1)), 2 * qmaskh};
}
struct Masks2 {
uint64_t imaskh;
uint64_t qmaskh;
unsigned qmaskl;
};
template <unsigned H, unsigned L, unsigned R>
static Masks2 GetMasks2(const std::vector<unsigned>& qs) {
uint64_t qmaskh = 0;
unsigned qmaskl = 0;
for (unsigned i = 0; i < L; ++i) {
qmaskl |= 1 << qs[i];
}
for (unsigned i = L; i < H + L; ++i) {
qmaskh |= uint64_t{1} << qs[i];
}
return {2 * (~qmaskh ^ ((1 << R) - 1)), 2 * qmaskh, qmaskl};
}
struct Masks3 {
uint64_t imaskh;
uint64_t qmaskh;
uint64_t cvalsh;
};
template <unsigned H, unsigned R>
static Masks3 GetMasks3(unsigned num_qubits, const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals) {
uint64_t qmaskh = 0;
uint64_t cmaskh = 0;
for (unsigned i = 0; i < H; ++i) {
qmaskh |= uint64_t{1} << qs[i];
}
for (auto q : cqs) {
cmaskh |= uint64_t{1} << q;
}
uint64_t cvalsh = bits::ExpandBits(cvals, num_qubits, cmaskh);
uint64_t maskh = ~(qmaskh | cmaskh) ^ ((1 << R) - 1);
return {2 * maskh, 2 * qmaskh, 2 * cvalsh};
}
struct Masks4 {
uint64_t imaskh;
uint64_t qmaskh;
uint64_t cvalsh;
uint64_t cvalsl;
uint64_t cmaskl;
unsigned cl;
};
template <unsigned H, unsigned R>
static Masks4 GetMasks4(unsigned num_qubits, const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals) {
unsigned cl = 0;
uint64_t qmaskh = 0;
uint64_t cmaskh = 0;
uint64_t cmaskl = 0;
for (unsigned i = 0; i < H; ++i) {
qmaskh |= uint64_t{1} << qs[i];
}
for (auto q : cqs) {
if (q >= R) {
cmaskh |= uint64_t{1} << q;
} else {
++cl;
cmaskl |= uint64_t{1} << q;
}
}
uint64_t cvalsh = bits::ExpandBits(cvals >> cl, num_qubits, cmaskh);
uint64_t cvalsl = bits::ExpandBits(cvals & ((1 << cl) - 1), R, cmaskl);
uint64_t maskh = ~(qmaskh | cmaskh) ^ ((1 << R) - 1);
return {2 * maskh, 2 * qmaskh, 2 * cvalsh, cvalsl, cmaskl, cl};
}
struct Masks5 {
uint64_t imaskh;
uint64_t qmaskh;
uint64_t cvalsh;
unsigned qmaskl;
};
template <unsigned H, unsigned L, unsigned R>
static Masks5 GetMasks5(unsigned num_qubits, const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals) {
uint64_t qmaskh = 0;
uint64_t cmaskh = 0;
unsigned qmaskl = 0;
for (unsigned i = 0; i < L; ++i) {
qmaskl |= 1 << qs[i];
}
for (unsigned i = L; i < H + L; ++i) {
qmaskh |= uint64_t{1} << qs[i];
}
for (auto q : cqs) {
cmaskh |= uint64_t{1} << q;
}
uint64_t cvalsh = bits::ExpandBits(cvals, num_qubits, cmaskh);
uint64_t maskh = ~(qmaskh | cmaskh) ^ ((1 << R) - 1);
return {2 * maskh, 2 * qmaskh, 2 * cvalsh, qmaskl};
}
struct Masks6 {
uint64_t imaskh;
uint64_t qmaskh;
uint64_t cvalsh;
uint64_t cvalsl;
uint64_t cmaskl;
unsigned qmaskl;
unsigned cl;
};
template <unsigned H, unsigned L, unsigned R>
static Masks6 GetMasks6(unsigned num_qubits, const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals) {
unsigned cl = 0;
uint64_t qmaskh = 0;
uint64_t cmaskh = 0;
uint64_t cmaskl = 0;
unsigned qmaskl = 0;
for (unsigned i = 0; i < L; ++i) {
qmaskl |= 1 << qs[i];
}
for (unsigned i = L; i < H + L; ++i) {
qmaskh |= uint64_t{1} << qs[i];
}
for (auto q : cqs) {
if (q >= R) {
cmaskh |= uint64_t{1} << q;
} else {
++cl;
cmaskl |= uint64_t{1} << q;
}
}
uint64_t cvalsh = bits::ExpandBits(cvals >> cl, num_qubits, cmaskh);
uint64_t cvalsl = bits::ExpandBits(cvals & ((1 << cl) - 1), R, cmaskl);
uint64_t maskh = ~(qmaskh | cmaskh) ^ ((1 << R) - 1);
return {2 * maskh, 2 * qmaskh, 2 * cvalsh, cvalsl, cmaskl, qmaskl, cl};
}
struct Masks7 {
uint64_t cvalsh;
uint64_t cmaskh;
};
static Masks7 GetMasks7(unsigned num_qubits, const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals) {
uint64_t cmaskh = 0;
for (auto q : cqs) {
cmaskh |= uint64_t{1} << q;
}
uint64_t cvalsh = bits::ExpandBits(cvals, num_qubits, cmaskh);
return {cvalsh, cmaskh};
}
struct Masks8 {
uint64_t cvalsh;
uint64_t cmaskh;
uint64_t cvalsl;
uint64_t cmaskl;
};
template <unsigned R>
static Masks8 GetMasks8(unsigned num_qubits, const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals) {
unsigned cl = 0;
uint64_t cmaskh = 0;
uint64_t cmaskl = 0;
for (auto q : cqs) {
if (q >= R) {
cmaskh |= uint64_t{1} << q;
} else {
++cl;
cmaskl |= uint64_t{1} << q;
}
}
uint64_t cvalsh = bits::ExpandBits(cvals >> cl, num_qubits, cmaskh);
uint64_t cvalsl = bits::ExpandBits(cvals & ((1 << cl) - 1), R, cmaskl);
return {cvalsh, cmaskh, cvalsl, cmaskl};
}
struct Masks9 {
uint64_t cvalsh;
uint64_t cmaskh;
unsigned qmaskl;
};
template <unsigned L>
static Masks9 GetMasks9(unsigned num_qubits, const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals) {
uint64_t cmaskh = 0;
unsigned qmaskl = 0;
for (unsigned i = 0; i < L; ++i) {
qmaskl |= 1 << qs[i];
}
for (auto q : cqs) {
cmaskh |= uint64_t{1} << q;
}
uint64_t cvalsh = bits::ExpandBits(cvals, num_qubits, cmaskh);
return {cvalsh, cmaskh, qmaskl};
}
struct Masks10 {
uint64_t cvalsh;
uint64_t cmaskh;
uint64_t cvalsl;
uint64_t cmaskl;
unsigned qmaskl;
};
template <unsigned L, unsigned R>
static Masks10 GetMasks10(unsigned num_qubits,
const std::vector<unsigned>& qs,
const std::vector<unsigned>& cqs, uint64_t cvals) {
unsigned cl = 0;
uint64_t cmaskh = 0;
uint64_t cmaskl = 0;
unsigned qmaskl = 0;
for (unsigned i = 0; i < L; ++i) {
qmaskl |= 1 << qs[i];
}
for (auto q : cqs) {
if (q >= R) {
cmaskh |= uint64_t{1} << q;
} else {
++cl;
cmaskl |= uint64_t{1} << q;
}
}
uint64_t cvalsh = bits::ExpandBits(cvals >> cl, num_qubits, cmaskh);
uint64_t cvalsl = bits::ExpandBits(cvals & ((1 << cl) - 1), R, cmaskl);
return {cvalsh, cmaskh, cvalsl, cmaskl, qmaskl};
}
struct Masks11 {
unsigned qmaskl;
};
template <unsigned L>
static Masks11 GetMasks11(const std::vector<unsigned>& qs) {
unsigned qmaskl = 0;
for (unsigned i = 0; i < L; ++i) {
qmaskl |= 1 << qs[i];
}
return {qmaskl};
}
template <unsigned R>
static unsigned MaskedAdd(
unsigned a, unsigned b, unsigned mask, unsigned lsize) {
unsigned c = bits::CompressBits(a, R, mask);
return bits::ExpandBits((c + b) % lsize, R, mask);
}
};
template <>
inline void SimulatorBase::FillIndices<0, 1>(unsigned num_qubits,
const std::vector<unsigned>& qs,
uint64_t* ms, uint64_t* xss) {
ms[0] = -1;
xss[0] = 0;
}
template <>
inline void SimulatorBase::FillIndices<0, 2>(unsigned num_qubits,
const std::vector<unsigned>& qs,
uint64_t* ms, uint64_t* xss) {
ms[0] = -1;
xss[0] = 0;
}
template <>
inline void SimulatorBase::FillIndices<0, 3>(unsigned num_qubits,
const std::vector<unsigned>& qs,
uint64_t* ms, uint64_t* xss) {
ms[0] = -1;
xss[0] = 0;
}
} // namespace qsim
#endif // SIMULATOR_H_