Skip to content

Commit 4005bec

Browse files
committed
Add comments.
1 parent 4cb6217 commit 4005bec

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

lib/simulator.h

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ namespace qsim {
2626
*/
2727
class SimulatorBase {
2828
protected:
29+
// The follwoing template parameters are used for functions below.
30+
// H - the number of high (target) qubits.
31+
// L - the number of low (target) qubits.
32+
// R - SIMD register width in floats.
33+
34+
// Fills the table of masks (ms) that is used to calculate base state indices
35+
// and the table of offset indices (xss) that is used to access the state
36+
// vector entries in matrix-vector multiplication functions. This function is
37+
// used in simulator_basic.h, simulator_sse.h and simulator_avx.h (no bmi2
38+
// version).
2939
template <unsigned H, unsigned L = 0>
3040
static void FillIndices(unsigned num_qubits, const std::vector<unsigned>& qs,
3141
uint64_t* ms, uint64_t* xss) {
@@ -50,6 +60,7 @@ class SimulatorBase {
5060
}
5161
}
5262

63+
// Fills gate matrix entries for gates with low qubits.
5364
template <unsigned H, unsigned L, unsigned R, typename fp_type>
5465
static void FillMatrix(unsigned qmaskl, const fp_type* matrix, fp_type* w) {
5566
constexpr unsigned gsize = 1 << (H + L);
@@ -78,6 +89,8 @@ class SimulatorBase {
7889
}
7990
}
8091

92+
// Fills gate matrix entries for controlled gates with high target qubits
93+
// and low control qubits.
8194
template <unsigned H, unsigned R, typename fp_type>
8295
static void FillControlledMatrixH(uint64_t cvalsl, uint64_t cmaskl,
8396
const fp_type* matrix, fp_type* w) {
@@ -103,6 +116,8 @@ class SimulatorBase {
103116
}
104117
}
105118

119+
// Fills gate matrix entries for controlled gates with low target qubits
120+
// and low control qubits.
106121
template <unsigned H, unsigned L, unsigned R, typename fp_type>
107122
static void FillControlledMatrixL(uint64_t cvalsl, uint64_t cmaskl,
108123
unsigned qmaskl, const fp_type* matrix,
@@ -135,6 +150,27 @@ class SimulatorBase {
135150
}
136151
}
137152

153+
/*
154+
The GetMasks* functions below provide various masks and related values.
155+
GetMasks1, GetMasks2, GetMasks3, GetMasks4, GetMasks5 and GetMasks6 are
156+
used in simulator_avx.h (BMI2 version) and in simulator_avx512.h. GetMasks7,
157+
GetMasks8, GetMasks9, GetMasks10 and GetMasks11 are used in simulator_avx.h
158+
(no BMI2 version) and in simulator_sse.h.
159+
160+
imaskh - inverted mask of high qubits (high control and target qubits).
161+
qmaskh - mask of high qubits (high target qubits).
162+
cvalsh - control bit values of high control qubits placed in correct
163+
positions.
164+
cvalsl - control bit values of low control qubits placed in correct positions.
165+
cmaskh - mask of high control qubits.
166+
cmaskl - mask of low control qubits.
167+
qmaskl - mask of low qubits (low target qubits).
168+
cl - the number of low control qubits.
169+
170+
Note that imaskh, qmaskh and cvalsh are multiplied by two in GetMasks1,
171+
GetMasks2, GetMasks3, GetMasks4, GetMasks5 and GetMasks6.
172+
*/
173+
138174
struct Masks1 {
139175
uint64_t imaskh;
140176
uint64_t qmaskh;
@@ -423,15 +459,19 @@ class SimulatorBase {
423459
return {cvalsh, cmaskh, cvalsl, cmaskl, qmaskl};
424460
}
425461

462+
struct Masks11 {
463+
unsigned qmaskl;
464+
};
465+
426466
template <unsigned L>
427-
static unsigned GetQMask(const std::vector<unsigned>& qs) {
467+
static Masks11 GetMasks11(const std::vector<unsigned>& qs) {
428468
unsigned qmaskl = 0;
429469

430470
for (unsigned i = 0; i < L; ++i) {
431471
qmaskl |= 1 << qs[i];
432472
}
433473

434-
return qmaskl;
474+
return {qmaskl};
435475
}
436476

437477
template <unsigned R>

lib/simulator_avx.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -926,11 +926,11 @@ class SimulatorAVX final : public SimulatorBase {
926926
__m256i idx[1 << L];
927927
__m256 w[1 << (1 + 2 * H + L)];
928928

929-
unsigned qmaskl = GetQMask<L>(qs);
929+
auto m = GetMasks11<L>(qs);
930930

931931
FillIndices<H, L>(state.num_qubits(), qs, ms, xss);
932-
FillPermutationIndices<L>(qmaskl, idx);
933-
FillMatrix<H, L, 3>(qmaskl, matrix, (fp_type*) w);
932+
FillPermutationIndices<L>(m.qmaskl, idx);
933+
FillMatrix<H, L, 3>(m.qmaskl, matrix, (fp_type*) w);
934934

935935
unsigned r = 3 + H;
936936
unsigned n = state.num_qubits() > r ? state.num_qubits() - r : 0;
@@ -1315,11 +1315,11 @@ class SimulatorAVX final : public SimulatorBase {
13151315
__m256i idx[1 << L];
13161316
__m256 w[1 << (1 + 2 * H + L)];
13171317

1318-
unsigned qmaskl = GetQMask<L>(qs);
1318+
auto m = GetMasks11<L>(qs);
13191319

13201320
FillIndices<H, L>(state.num_qubits(), qs, ms, xss);
1321-
FillPermutationIndices<L>(qmaskl, idx);
1322-
FillMatrix<H, L, 3>(qmaskl, matrix, (fp_type*) w);
1321+
FillPermutationIndices<L>(m.qmaskl, idx);
1322+
FillMatrix<H, L, 3>(m.qmaskl, matrix, (fp_type*) w);
13231323

13241324
unsigned r = 3 + H;
13251325
unsigned n = state.num_qubits() > r ? state.num_qubits() - r : 0;

lib/simulator_sse.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,10 @@ class SimulatorSSE final : public SimulatorBase {
431431
uint64_t xss[1 << H];
432432
__m128 w[1 << (1 + 2 * H + L)];
433433

434-
unsigned qmaskl = GetQMask<L>(qs);
434+
auto m = GetMasks11<L>(qs);
435435

436436
FillIndices<H, L>(state.num_qubits(), qs, ms, xss);
437-
FillMatrix<H, L, 2>(qmaskl, matrix, (fp_type*) w);
437+
FillMatrix<H, L, 2>(m.qmaskl, matrix, (fp_type*) w);
438438

439439
unsigned k = 2 + H;
440440
unsigned n = state.num_qubits() > k ? state.num_qubits() - k : 0;
@@ -833,10 +833,10 @@ class SimulatorSSE final : public SimulatorBase {
833833
uint64_t xss[1 << H];
834834
__m128 w[1 << (1 + 2 * H + L)];
835835

836-
unsigned qmaskl = GetQMask<L>(qs);
836+
auto m = GetMasks11<L>(qs);
837837

838838
FillIndices<H, L>(state.num_qubits(), qs, ms, xss);
839-
FillMatrix<H, L, 2>(qmaskl, matrix, (fp_type*) w);
839+
FillMatrix<H, L, 2>(m.qmaskl, matrix, (fp_type*) w);
840840

841841
unsigned k = 2 + H;
842842
unsigned n = state.num_qubits() > k ? state.num_qubits() - k : 0;

0 commit comments

Comments
 (0)