-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathqmatrix.cpp
More file actions
255 lines (183 loc) · 4.82 KB
/
Copy pathqmatrix.cpp
File metadata and controls
255 lines (183 loc) · 4.82 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
/** @file
* Testing utilities which define 'qmatrix', used
* to perform reference complex matrix algebra, and
* as a reference proxy to a quantum density matrix.
*
* @author Tyson Jones
*/
#include "qmatrix.hpp"
#include "qvector.hpp"
#include "macros.hpp"
/*
* MATRIX CREATION
*/
qmatrix getZeroMatrix(size_t dim) {
DEMAND( dim >= 1 );
return qmatrix(dim, qvector(dim, 0));
}
qmatrix getConstantMatrix(size_t dim, qcomp elem) {
DEMAND( dim >= 1 );
return qmatrix(dim, qvector(dim, elem));
}
qmatrix getIdentityMatrix(size_t dim) {
DEMAND( dim >= 1 );
qmatrix out = getZeroMatrix(dim);
for (size_t i=0; i<dim; i++)
out[i][i] = 1;
return out;
}
qmatrix getDiagonalMatrix(qvector v) {
DEMAND( v.size() >= 1 );
qmatrix out = getZeroMatrix(v.size());
for (size_t i=0; i<v.size(); i++)
out[i][i] = v[i];
return out;
}
qmatrix getPauliMatrix(int id) {
DEMAND( id >= 0 );
DEMAND( id <= 3 );
if (id == 0)
return {{1 ,0}, {0, 1}};
if (id == 1)
return {{0, 1}, {1, 0}};
if (id == 2)
return {{0, -1_i}, {1_i, 0}};
if (id == 3)
return {{1, 0}, {0, -1}};
// unreachable
return {{-1}};
}
/*
* SCALAR MULTIPLICATION
*/
qmatrix operator * (const qcomp& a, const qmatrix& m) {
qmatrix out = m;
for (auto& row : out)
for (auto& elem : row)
elem *= a;
return out;
}
qmatrix operator * (const qmatrix& m, const qcomp& a) {
return a * m;
}
qmatrix operator *= (qmatrix& m, const qcomp& a) {
m = a * m;
return m;
}
qmatrix operator * (const qreal& a, const qmatrix& m) {
return qcomp(a,0) * m;
}
qmatrix operator * (const qmatrix& m, const qreal& a) {
return a * m;
}
qmatrix operator *= (qmatrix& m, const qreal& a) {
m = a * m;
return m;
}
/*
* SCALAR DIVISION
*/
qmatrix operator / (const qmatrix& m, const qcomp& a) {
DEMAND( std::abs(a) != 0 );
return (1/a) * m;
}
qmatrix operator /= (qmatrix& m, const qcomp& a) {
m = m / a;
return m;
}
qmatrix operator / (const qmatrix& m, const qreal& a) {
return m / qcomp(a,0);
}
qmatrix operator /= (qmatrix& m, const qreal& a) {
m = m / a;
return m;
}
/*
* MATRIX ADDITION
*/
qmatrix operator + (const qmatrix& m1, const qmatrix& m2) {
DEMAND( m1.size() == m2.size() );
qmatrix out = m1;
for (size_t r=0; r<m1.size(); r++)
for (size_t c=0; c<m1.size(); c++)
out[r][c] += m2[r][c];
return out;
}
qmatrix operator += (qmatrix& m1, const qmatrix& m2) {
m1 = m1 + m2;
return m1;
}
/*
* MATRIX SUBTRACTION
*/
qmatrix operator - (const qmatrix& m1, const qmatrix& m2) {
return m1 + (-1 * m2);
}
qmatrix operator -= (qmatrix& m1, const qmatrix& m2) {
m1 = m1 - m2;
return m1;
}
/*
* MATRIX MULTIPLICATION
*/
qmatrix operator * (const qmatrix& m1, const qmatrix& m2) {
DEMAND( m1.size() > 0 );
DEMAND( m2.size() > 0 );
DEMAND( m1[0].size() == m2.size() );
// unlike most functions which assume qmatrix is square,
// we cheekily permit m1 and m2 to be non-square (and
// ergo differing), necessary to calculate partial traces
qmatrix out = qmatrix(m1.size(), qvector(m2[0].size(),0));
for (size_t r=0; r<out.size(); r++)
for (size_t c=0; c<out[0].size(); c++)
for (size_t k=0; k<m2.size(); k++)
out[r][c] += m1[r][k] * m2[k][c];
return out;
}
qmatrix operator *= (qmatrix& m1, const qmatrix& m2) {
m1 = m1 * m2;
return m1;
}
/*
* SETTERS
*/
void setSubMatrix(qmatrix &dest, qmatrix sub, size_t r, size_t c) {
DEMAND( sub.size() > 0 );
DEMAND( sub .size() + r <= dest.size() );
DEMAND( sub[0].size() + c <= dest.size() );
// this function cheekily supports when 'sub' is non-square,
// which is inconsistent with the preconditions of most of
// the qmatrix functions, but is needed by setDensityQuregAmps()
for (size_t i=0; i<sub.size(); i++)
for (size_t j=0; j<sub[i].size(); j++)
dest[r+i][c+j] = sub[i][j];
}
void setSubMatrix(qmatrix &dest, qvector sub, size_t flatInd) {
DEMAND( sub.size() + flatInd <= dest.size()*dest.size() );
for (size_t i=0; i<sub.size(); i++) {
size_t r = (i + flatInd) / dest.size(); // floors
size_t c = (i + flatInd) % dest.size();
dest[r][c] = sub[i];
}
}
void setToDebugState(qmatrix &m) {
DEMAND( !m.empty() );
size_t i = 0;
// iterate column-wise
for (size_t c=0; c<m.size(); c++) {
for (size_t r=0; r<m.size(); r++) {
m[r][c] = qcomp(2*i/10., (2*i+1)/10.);
i++;
}
}
}
/*
* GETTERS
*/
qvector getDiagonals(qmatrix m) {
DEMAND( m.size() > 1 );
qvector out = getZeroVector(m.size());
for (size_t i=0; i<out.size(); i++)
out[i] = m[i][i];
return out;
}