-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdistributed_densitymatrix.hpp
More file actions
409 lines (300 loc) · 13.6 KB
/
distributed_densitymatrix.hpp
File metadata and controls
409 lines (300 loc) · 13.6 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
#ifndef DISTRIBUTED_DENSITYMATRIX_HPP
#define DISTRIBUTED_DENSITYMATRIX_HPP
#include "types.hpp"
#include "states.hpp"
#include "bit_maths.hpp"
#include "misc.hpp"
#include "communication.hpp"
#include "local_densitymatrix.hpp"
#include "distributed_statevector.hpp"
static void distributed_densitymatrix_manyTargGate(DensityMatrix &rho, NatArray targets, AmpMatrix gate) {
assert( 2*targets.size() <= rho.logNumAmpsPerNode );
distributed_statevector_manyTargGate(rho, targets, gate);
for (Nat &t : targets)
t += rho.numQubits;
gate = getConjugateMatrix(gate);
distributed_statevector_manyTargGate(rho, targets, gate);
}
static void distributed_densitymatrix_swapGate(DensityMatrix &rho, Nat qb1, Nat qb2) {
Nat n = rho.numQubits;
distributed_statevector_swapGate(rho, qb1, qb2);
distributed_statevector_swapGate(rho, qb1 + n, qb2 + n);
}
static void distributed_densitymatrix_pauliTensor(DensityMatrix &rho, NatArray targets, NatArray paulis) {
distributed_statevector_pauliTensor(rho, targets, paulis);
for (Nat &t : targets)
t += rho.numQubits;
distributed_statevector_pauliTensor(rho, targets, paulis);
if (containsOddNumY(paulis)) {
#pragma omp parallel for
for (Amp & : rho.amps)
amp *= -1;
}
}
static void distributed_densitymatrix_pauliGadget(DensityMatrix &rho, NatArray targets, NatArray paulis, Real theta) {
distributed_statevector_pauliGadget(rho, targets, paulis, theta);
for (Nat &t : targets)
t += rho.numQubits;
if (!containsOddNumY(paulis))
theta *= -1;
distributed_statevector_pauliGadget(rho, targets, paulis, theta);
}
static void distributed_densitymatrix_phaseGadget(DensityMatrix &rho, NatArray targets, Real theta) {
distributed_statevector_phaseGadget(rho, targets, theta);
for (Nat &t : targets)
t += rho.numQubits;
theta *= -1;
distributed_statevector_phaseGadget(rho, targets, theta);
}
static void distributed_densitymatrix_krausMap(DensityMatrix &rho, MatrixArray krausOps, NatArray targets) {
assert( 2*targets.size() <= rho.logNumAmpsPerNode );
AmpMatrix superOp = getSuperoperator(krausOps);
NatArray extendedTargets = targets;
for (Nat t : targets)
extendedTargets.push_back(t + rho.numQubits);
distributed_statevector_manyTargGate(rho, extendedTargets, superOp);
}
static void distributed_densitymatrix_oneQubitDephasing(DensityMatrix &rho, Nat qb, Real prob) {
local_densitymatrix_oneQubitDephasing(rho, qb, prob);
}
static void distributed_densitymatrix_twoQubitDephasing(DensityMatrix &rho, Nat qb1, Nat qb2, Real prob) {
local_densitymatrix_twoQubitDephasing(rho, qb1, qb2, prob);
}
static void distributed_densitymatrix_oneQubitDepolarising(DensityMatrix &rho, Nat qb, Real prob) {
Amp c1 = 2*prob/3;
Amp c2 = 1 - 2*prob/3;
Amp c3 = 1 - 4*prob/3;
if (qb < rho.numQubits - rho.logNumNodes)
local_densitymatrix_oneQubitDepolarising(rho, qb, c1, c2, c3);
else {
Nat qbShift = qb - (rho.numQubits - rho.logNumNodes);
Nat bit = getBit(rho.rank, qbShift);
Index numIts = rho.numAmpsPerNode / 2;
// pack half of local amps into buffer
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertBit(k, qb, bit);
rho.buffer[k] = rho.amps[j];
}
// swap half-buffers, by sending buffer[0...], receiving in buffer[offset...]
Nat pairRank = flipBit(rho.rank, qbShift);
Index bufferOffset = numIts;
comm_exchangeArrays(rho.buffer, 0, rho.buffer, bufferOffset, numIts, pairRank);
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertBit(k, qb, ! bit);
rho.amps[j] *= c3;
}
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertBit(k, qb, bit);
Index l = k + bufferOffset;
rho.amps[j] = c2*rho.amps[j] + c1*rho.buffer[l];
}
}
}
static void distributed_densitymatrix_twoQubitDepolarising_subroutine_pair(DensityMatrix &rho, Nat q0, Nat q1, Nat q2, Amp c1, Amp c2, Amp c3) {
Nat alt1 = q1 - (rho.numQubits - rho.logNumNodes);
Nat bit = getBit(rho.rank, alt1);
// scale all amplitudes
#pragma omp parallel for
for (Index j=0; j<rho.numAmpsPerNode; j++) {
Nat flag1 = getBit(j, q0) == getBit(j, q2);
Nat flag2 = getBit(j, q1) == bit;
Amp fac = 1. + c3 * Amp(!(flag1 & flag2), 0);
rho.amps[j] *= fac;
}
// pack eighth of buffer with pre-summed amp pairs
Index numIts = rho.numAmpsPerNode / 8;
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j000 = insertThreeZeroBits(k, q2, q1, q0);
Index j0b0 = setBit(j000, q1, bit);
Index j1b1 = flipTwoBits(j0b0, q2, q0);
rho.buffer[k] = rho.amps[j0b0] + rho.amps[j1b1];
}
// swap eighth-buffers (send buffer[0...], receive in buffer[offset...])
Nat pairRank = flipBit(rho.rank, alt1);
Index bufferOffset = numIts;
comm_exchangeArrays(rho.buffer, 0, rho.buffer, bufferOffset, numIts, pairRank);
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j000 = insertThreeZeroBits(k, q2, q1, q0);
Index j0b0 = setBit(j000, q1, bit);
Index j1b1 = flipTwoBits(j0b0, q2, q0);
Index l = k + bufferOffset;
rho.amps[j0b0] = c1*rho.amps[j0b0] + c2*(rho.amps[j1b1] + rho.buffer[l]);
rho.amps[j1b1] = c1*rho.amps[j1b1] + c2*(rho.amps[j0b0] + rho.buffer[l]);
}
}
static void distributed_densitymatrix_twoQubitDepolarising_subroutine_quad(DensityMatrix &rho, Nat q0, Nat q1, Amp c1, Amp c2, Amp c3) {
Nat alt0 = q0 - (rho.numQubits - rho.logNumNodes);
Nat alt1 = q1 - (rho.numQubits - rho.logNumNodes);
Nat bit0 = getBit(rho.rank, alt0);
Nat bit1 = getBit(rho.rank, alt1);
// scale all amplitudes
#pragma omp parallel for
for (Index j=0; j<rho.numAmpsPerNode; j++) {
Nat flag1 = getBit(j, q0) == bit0;
Nat flag2 = getBit(j, q1) == bit1;
Amp fac = 1. + c3 * Amp(!(flag1 & flag2), 0);
rho.amps[j] *= fac;
}
// pack fourth of buffer
Index numIts = rho.numAmpsPerNode / 4;
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertTwoBits(k, q1, bit1, q0, bit0);
rho.buffer[k] = rho.amps[j];
}
// swap fourth-buffer with first pair node (send buffer[0...], receive in buffer[offset...])
Nat pairRank0 = flipBit(rho.rank, alt0);
Index bufferOffset = numIts;
comm_exchangeArrays(rho.buffer, 0, rho.buffer, bufferOffset, numIts, pairRank0);
// update local amps and buffer
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertTwoBits(k, q1, bit1, q0, bit0);
Index l = k + bufferOffset;
Amp amp = c1*rho.amps[j] + c2*rho.buffer[l];
rho.amps[j] = amp;
rho.buffer[k] = amp;
}
// swap fourth-buffer with second pair node (send buffer[0...], receive in buffer[offset...])
Nat pairRank1 = flipBit(rho.rank, alt1);
comm_exchangeArrays(rho.buffer, 0, rho.buffer, bufferOffset, numIts, pairRank1);
// update local amps
Amp c4 = c2/c1;
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertTwoBits(k, q1, bit1, q0, bit0);
Index l = k + bufferOffset;
rho.amps[j] = c4 * rho.buffer[l];
}
}
static void distributed_densitymatrix_twoQubitDepolarising(DensityMatrix &rho, Nat qb1, Nat qb2, Real prob) {
// ensure qb2 is larger
if (qb1 > qb2)
std::swap(qb1, qb2);
Amp c1 = 1 - 4*prob/5;
Amp c2 = 4*prob/15;
Amp c3 = -16*prob/15;
Nat shift1 = qb1 + rho.numQubits;
Nat shift2 = qb2 + rho.numQubits;
Nat threshold = rho.numQubits - rho.logNumNodes;
if (qb2 < threshold)
local_densitymatrix_twoQubitDepolarising(rho, qb1, qb2, shift1, shift2, c1, c2, c3);
else if (qb2 >= threshold && qb1 < threshold)
distributed_densitymatrix_twoQubitDepolarising_subroutine_pair(rho, qb1, qb2, shift1, c1, c2, c3);
else
distributed_densitymatrix_twoQubitDepolarising_subroutine_quad(rho, qb1, qb2, c1, c2, c3);
}
static void distributed_densitymatrix_damping(DensityMatrix &rho, Nat qb, Real prob) {
Nat threshold = rho.numQubits - rho.logNumNodes;
if (qb < threshold)
local_densitymatrix_damping(rho, qb, prob);
else {
Index numIts = rho.numAmpsPerNode / 2;
Nat pairRank = flipBit(rho.rank, qb - threshold);
Nat bit = getBit(rho.rank, qb - threshold);
Amp c1 = sqrt(1 - prob);
Amp c2 = 1 - prob;
// half of all nodes...
if (bit == 1) {
// pack half buffer and scale half their amps
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertBit(k, qb, 1);
rho.buffer[k] = rho.amps[j];
rho.amps[j] *= c2;
}
// asynchronously send half-buffer, instantly proceeding
comm_asynchSendArray(rho.buffer, numIts, pairRank);
}
// all nodes proceed to scale half their (remaining) local amps
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertBit(k, qb, !bit);
rho.amps[j] *= c1;
}
// other half of all nodes...
if (bit == 0) {
// receive half-buffer (waiting for full receive)
comm_receiveArray(rho.buffer, numIts, pairRank);
// and combine it with their remaining local amps
#pragma omp parallel for
for (Index k=0; k<numIts; k++) {
Index j = insertBit(k, qb, 0);
rho.amps[j] += prob * rho.buffer[k];
}
}
// stop asynch senders from proceeding, so their buffers aren't subsequently prematurely modified
comm_synch();
}
}
static Amp distributed_densitymatrix_expecPauliString(DensityMatrix &rho, RealArray coeffs, NatArray allPaulis) {
Amp value = 0;
Index numCoeffs = coeffs.size();
#pragma omp parallel for reduction(+:value)
for (Index j=0; j<rho.numAmpsPerNode; j++) {
Index i = (rho.rank << rho.logNumAmpsPerNode) | j;
Amp term = 0;
for (Index t=0; t<numCoeffs; t++) {
Nat* termPaulis = &allPaulis[t*rho.numQubits];
Amp elem = getPauliTensorElem(termPaulis, rho.numQubits, i);
term += elem * coeffs[t];
}
value += term * rho.amps[j];
}
comm_reduceAmp(value);
return value;
}
static DensityMatrix distributed_densitymatrix_partialTrace(DensityMatrix &inRho, NatArray targets) {
// require that the reduced density matrix has more (or equal) columns than nodes, so
// that it is compatible with this project's other algorithms
Nat outRhoNumQubits = inRho.numQubits - targets.size();
assert( outRhoNumQubits >= inRho.logNumNodes );
/* Note this algorithm imposes a looser constraint; that
* targets.size() <= inRho.numQubits - (inRho.logNumNodes/2)
* and ergo permits tracing out more qubits than the above restriction
* permits. However, the reduced density matrices violate the precondition
* of the DensityMatrix constructor
*/
// targets must be sorted for bitwise insertions
std::sort(targets.begin(), targets.end());
// if all targets are in suffix, invoke embarrassingly parallel trace on {t, t+N}, and return
if (targets.back() + inRho.numQubits < inRho.logNumAmpsPerNode) {
NatArray pairs = targets;
for (Nat &t : pairs)
t += inRho.numQubits;
return local_densitymatrix_partialTrace(inRho, targets, pairs);
}
// otherwise, treating outRho as a statevector, collect all involved qubits...
NatArray extendedTargets = targets;
for (Nat t : targets)
extendedTargets.push_back(t + inRho.numQubits);
// and find which qubits they should be swapped with, so that all effective targets lie in the suffix substate
NatArray reorderedTargets = getReorderedAllSuffixTargets(extendedTargets, inRho.logNumAmpsPerNode);
// effect those swaps, enabling subsequent (disordered) partial trace to be embarrassingly parallel.
// we iterate in reverse (swap leftmost qubits first), to minimise violating relative order of non-targeted bits
for (Nat q = reorderedTargets.size(); q-- != 0; )
if (reorderedTargets[q] != extendedTargets[q])
distributed_statevector_swapGate(inRho, reorderedTargets[q], extendedTargets[q]);
// embarassingly parallel reduce the density matrix
NatArray pairTargets(reorderedTargets.begin() + targets.size(), reorderedTargets.end());
DensityMatrix outRho = local_densitymatrix_partialTrace(inRho, targets, pairTargets);
// determine the relative ordering of the remaining non-targered qubits
NatArray remainingQubits = getNonTargetedQubitOrder(2*inRho.numQubits, extendedTargets, reorderedTargets);
// perform additional swaps to re-order the remaining qubits
for (Nat q=remainingQubits.size(); q-- != 0; ) {
if (remainingQubits[q] == q)
continue;
Nat p = 0;
while (remainingQubits[p] != q)
p++;
distributed_statevector_swapGate(outRho, q, p);
std::swap(remainingQubits[q], remainingQubits[p]);
}
return outRho;
}
#endif // DISTRIBUTED_DENSITYMATRIX_HPP