-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathmatrix.h
More file actions
296 lines (251 loc) · 8.28 KB
/
matrix.h
File metadata and controls
296 lines (251 loc) · 8.28 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
// 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 MATRIX_H_
#define MATRIX_H_
#include <algorithm>
#include <utility>
#include <vector>
#include "bits.h"
namespace qsim {
/**
* Gate matrix type. Matrices are stored as vectors. The matrix elements are
* accessed as real(m[i][j]) <- vector[2 * (n * i + j)] and
* imag(m[i][j]) <- vector[2 * (n * i + j) + 1], where n is the number of rows
* or columns (n = 2^q, where q is the number of gate qubits).
*/
template <typename fp_type>
using Matrix = std::vector<fp_type>;
/**
* Sets all matrix elements to zero.
* @m Matrix to be cleared.
*/
template <typename fp_type>
inline void MatrixClear(Matrix<fp_type>& m) {
for (unsigned i = 0; i < m.size(); ++i) {
m[i] = 0;
}
}
/**
* Sets an identity matrix.
* @n Number of matrix rows (columns).
* @m Output identity matrix.
*/
template <typename fp_type>
inline void MatrixIdentity(unsigned n, Matrix<fp_type>& m) {
m.resize(2 * static_cast<typename Matrix<fp_type>::size_type>(n) * n);
MatrixClear(m);
for (unsigned i = 0; i < n; ++i) {
m[2 * (n * i + i)] = 1;
}
}
/**
* Multiplies two gate matrices of equal size: m2 = m1 m2.
* @q Number of gate qubits. The number of matrix rows (columns) is 2^q.
* @m1 Matrix m1.
* @m2 Input matrix m2. Output product of matrices m2 = m1 m2.
*/
template <typename fp_type1, typename fp_type2>
inline void MatrixMultiply(
unsigned q, const Matrix<fp_type1>& m1, Matrix<fp_type2>& m2) {
Matrix<fp_type2> mt = m2;
unsigned n = unsigned{1} << q;
for (unsigned i = 0; i < n; ++i) {
for (unsigned j = 0; j < n; ++j) {
fp_type2 re = 0;
fp_type2 im = 0;
for (unsigned k = 0; k < n; ++k) {
fp_type2 r1 = m1[2 * (n * i + k)];
fp_type2 i1 = m1[2 * (n * i + k) + 1];
fp_type2 r2 = mt[2 * (n * k + j)];
fp_type2 i2 = mt[2 * (n * k + j) + 1];
re += r1 * r2 - i1 * i2;
im += r1 * i2 + i1 * r2;
}
m2[2 * (n * i + j)] = re;
m2[2 * (n * i + j) + 1] = im;
}
}
}
/**
* Multiplies two gate matrices of equal size: m2 = m1^\dagger m2.
* @q Number of gate qubits. The number of matrix rows (columns) is 2^q.
* @m1 Matrix m1.
* @m2 Input matrix m2. Output product of matrices m2 = m1 m2.
*/
template <typename fp_type1, typename fp_type2>
inline void MatrixDaggerMultiply(
unsigned q, const Matrix<fp_type1>& m1, Matrix<fp_type2>& m2) {
Matrix<fp_type2> mt = m2;
unsigned n = unsigned{1} << q;
for (unsigned i = 0; i < n; ++i) {
for (unsigned j = 0; j < n; ++j) {
fp_type2 re = 0;
fp_type2 im = 0;
for (unsigned k = 0; k < n; ++k) {
fp_type2 r1 = m1[2 * (n * k + i)];
fp_type2 i1 = m1[2 * (n * k + i) + 1];
fp_type2 r2 = mt[2 * (n * k + j)];
fp_type2 i2 = mt[2 * (n * k + j) + 1];
re += r1 * r2 + i1 * i2;
im += r1 * i2 - i1 * r2;
}
m2[2 * (n * i + j)] = re;
m2[2 * (n * i + j) + 1] = im;
}
}
}
/**
* Multiplies two gate matrices: m2 = m1 m2. The size of m1 should not exceed
* the size of m2.
* @mask1 Qubit mask that specifies the subset of qubits m1 acts on.
* @q1 Number of gate qubits. The number of matrix rows (columns) is 2^q1.
* @m1 Matrix m1.
* @q2 Number of gate qubits. The number of matrix rows (columns) is 2^q2.
* @m2 Input matrix m2. Output product of matrices m2 = m1 m2.
*/
template <typename fp_type1, typename fp_type2>
inline void MatrixMultiply(unsigned mask1,
unsigned q1, const Matrix<fp_type1>& m1,
unsigned q2, Matrix<fp_type2>& m2) {
if (q1 == q2) {
MatrixMultiply(q1, m1, m2);
} else {
Matrix<fp_type2> mt = m2;
unsigned n1 = unsigned{1} << q1;
unsigned n2 = unsigned{1} << q2;
for (unsigned i = 0; i < n2; ++i) {
unsigned si = bits::CompressBits(i, q2, mask1);
for (unsigned j = 0; j < n2; ++j) {
fp_type2 re = 0;
fp_type2 im = 0;
for (unsigned k = 0; k < n1; ++k) {
unsigned ek = bits::ExpandBits(k, q2, mask1) + (i & ~mask1);
fp_type2 r1 = m1[2 * (n1 * si + k)];
fp_type2 i1 = m1[2 * (n1 * si + k) + 1];
fp_type2 r2 = mt[2 * (n2 * ek + j)];
fp_type2 i2 = mt[2 * (n2 * ek + j) + 1];
re += r1 * r2 - i1 * i2;
im += r1 * i2 + i1 * r2;
}
m2[2 * (n2 * i + j)] = re;
m2[2 * (n2 * i + j) + 1] = im;
}
}
}
}
/**
* Multiply a matrix by a real scalar value.
* @c Scalar value.
* @m Input matrix to be multiplied. Output matrix.
*/
template <typename fp_type1, typename fp_type2>
inline void MatrixScalarMultiply(fp_type1 c, Matrix<fp_type2>& m) {
for (unsigned i = 0; i < m.size(); ++i) {
m[i] *= c;
}
}
/**
* Multiply a matrix by a complex scalar value.
* @re Real part of scalar value.
* @im Imaginary part of scalar value.
* @m Input matrix to be multiplied. Output matrix.
*/
template <typename fp_type1, typename fp_type2>
inline void MatrixScalarMultiply(
fp_type1 re, fp_type1 im, Matrix<fp_type2>& m) {
for (unsigned i = 0; i < m.size() / 2; ++i) {
fp_type2 re0 = m[2 * i + 0];
fp_type2 im0 = m[2 * i + 1];
m[2 * i + 0] = re * re0 - im * im0;
m[2 * i + 1] = re * im0 + im * re0;
}
}
/**
* Daggers a matrix.
* @n Number of matrix rows (columns).
* @m Input matrix. Output matrix.
*/
template <typename fp_type>
inline void MatrixDagger(unsigned n, Matrix<fp_type>& m) {
for (unsigned i = 0; i < n; ++i) {
m[2 * (n * i + i) + 1] = -m[2 * (n * i + i) + 1];
for (unsigned j = i + 1; j < n; ++j) {
std::swap(m[2 * (n * i + j)], m[2 * (n * j + i)]);
fp_type t = m[2 * (n * i + j) + 1];
m[2 * (n * i + j) + 1] = -m[2 * (n * j + i) + 1];
m[2 * (n * j + i) + 1] = -t;
}
}
}
/**
* Gets a permutation to rearrange qubits from "normal" order to "gate"
* order. Qubits are ordered in increasing order for "normal" order.
* Qubits are ordered arbitrarily for "gate" order. Returns an empty vector
* if the qubits are in "normal" order.
* @qubits Qubit indices in "gate" order.
* @return Permutation as a vector.
*/
inline std::vector<unsigned> NormalToGateOrderPermutation(
const std::vector<unsigned>& qubits) {
std::vector<unsigned> perm;
bool normal_order = true;
for (std::size_t i = 1; i < qubits.size(); ++i) {
if (qubits[i] < qubits[i - 1]) {
normal_order = false;
break;
}
}
if (!normal_order) {
struct QI {
unsigned q;
unsigned index;
};
std::vector<QI> qis;
qis.reserve(qubits.size());
for (std::size_t i = 0; i < qubits.size(); ++i) {
qis.push_back({qubits[i], unsigned(i)});
}
std::sort(qis.begin(), qis.end(), [](const QI& l, const QI& r) {
return l.q < r.q;
});
perm.reserve(qubits.size());
for (std::size_t i = 0; i < qubits.size(); ++i) {
perm.push_back(qis[i].index);
}
}
return perm;
}
/**
* Shuffles the gate matrix elements to get the matrix that acts on qubits
* that are in "normal" order (in increasing orger).
* @perm Permutation to rearrange qubits from "normal" order to "gate" order.
* @q Number of gate qubits. The number of matrix rows (columns) is 2^q.
* @m Input matrix. Output shuffled matrix.
*/
template <typename fp_type>
inline void MatrixShuffle(const std::vector<unsigned>& perm,
unsigned q, Matrix<fp_type>& m) {
Matrix<fp_type> mt = m;
unsigned n = unsigned{1} << q;
for (unsigned i = 0; i < n; ++i) {
unsigned pi = bits::PermuteBits(i, q, perm);
for (unsigned j = 0; j < n; ++j) {
unsigned pj = bits::PermuteBits(j, q, perm);
m[2 * (n * i + j)] = mt[2 * (n * pi + pj)];
m[2 * (n * i + j) + 1] = mt[2 * (n * pi + pj) + 1];
}
}
}
} // namespace qsim
#endif // MATRIX_H_