-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathgate_decomposition.cc
More file actions
503 lines (457 loc) · 17.2 KB
/
gate_decomposition.cc
File metadata and controls
503 lines (457 loc) · 17.2 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
/*
* Copyright 2021 Google LLC
*
* 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
*
* http://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.
*/
#include "stim/circuit/gate_decomposition.h"
#include <span>
#include "stim/stabilizers/pauli_string.h"
using namespace stim;
struct ConjugateBySelfInverse {
CircuitInstruction inst;
const std::function<void(const CircuitInstruction &inst)> &do_instruction_callback;
ConjugateBySelfInverse(
CircuitInstruction inst, const std::function<void(const CircuitInstruction &inst)> &do_instruction_callback)
: inst(inst), do_instruction_callback(do_instruction_callback) {
if (!inst.targets.empty()) {
do_instruction_callback(inst);
}
}
~ConjugateBySelfInverse() {
if (!inst.targets.empty()) {
do_instruction_callback(inst);
}
}
};
bool stim::accumulate_next_obs_terms_to_pauli_string_helper(
CircuitInstruction instruction,
size_t *start,
PauliString<64> *obs,
std::vector<GateTarget> *bits,
bool allow_imaginary) {
if (*start >= instruction.targets.size()) {
return false;
}
if (bits != nullptr) {
bits->clear();
}
obs->xs.clear();
obs->zs.clear();
obs->sign = false;
bool imag = false;
// Find end of current product.
size_t end = *start + 1;
while (end < instruction.targets.size() && instruction.targets[end].is_combiner()) {
end += 2;
}
// Accumulate terms.
for (size_t k = *start; k < end; k += 2) {
GateTarget t = instruction.targets[k];
if (t.is_pauli_target()) {
obs->left_mul_pauli(t, &imag);
} else if (t.is_classical_bit_target() && bits != nullptr) {
bits->push_back(t);
} else {
throw std::invalid_argument("Found an unsupported target `" + t.str() + "` in " + instruction.str());
}
}
if (imag && !allow_imaginary) {
throw std::invalid_argument(
"Acted on an anti-Hermitian operator (e.g. X0*Z0 instead of Y0) in " + instruction.str());
}
*start = end;
return true;
}
void stim::decompose_mpp_operation(
const CircuitInstruction &mpp_op,
size_t num_qubits,
const std::function<void(const CircuitInstruction &inst)> &do_instruction_callback) {
PauliString<64> current(num_qubits);
simd_bits<64> merged(num_qubits);
std::vector<GateTarget> h_xz;
std::vector<GateTarget> h_yz;
std::vector<GateTarget> cnot;
std::vector<GateTarget> meas;
auto flush = [&]() {
if (meas.empty()) {
return;
}
{
ConjugateBySelfInverse c1(CircuitInstruction(GateType::H, {}, h_xz, mpp_op.tag), do_instruction_callback);
ConjugateBySelfInverse c2(
CircuitInstruction(GateType::H_YZ, {}, h_yz, mpp_op.tag), do_instruction_callback);
ConjugateBySelfInverse c3(CircuitInstruction(GateType::CX, {}, cnot, mpp_op.tag), do_instruction_callback);
do_instruction_callback(CircuitInstruction(GateType::M, mpp_op.args, meas, mpp_op.tag));
}
h_xz.clear();
h_yz.clear();
cnot.clear();
meas.clear();
merged.clear();
};
size_t start = 0;
while (accumulate_next_obs_terms_to_pauli_string_helper(mpp_op, &start, ¤t, nullptr)) {
// Products equal to +-I become MPAD instructions.
if (current.ref().has_no_pauli_terms()) {
flush();
GateTarget t = GateTarget::qubit((uint32_t)current.sign);
do_instruction_callback(CircuitInstruction{GateType::MPAD, mpp_op.args, &t, mpp_op.tag});
continue;
}
// If there's overlap with previous groups, the previous groups need to be flushed.
if (current.xs.intersects(merged) || current.zs.intersects(merged)) {
flush();
}
merged |= current.xs;
merged |= current.zs;
// Buffer operations to perform the desired measurement.
bool first = true;
current.ref().for_each_active_pauli([&](uint32_t q) {
bool x = current.xs[q];
bool z = current.zs[q];
// Include single qubit gates transforming the Pauli into a Z.
if (x) {
if (z) {
h_yz.push_back({q});
} else {
h_xz.push_back({q});
}
}
// Include CNOT gates folding onto a single measured qubit.
if (first) {
meas.push_back(GateTarget::qubit(q, current.sign));
first = false;
} else {
cnot.push_back({q});
cnot.push_back({meas.back().qubit_value()});
}
});
assert(!first);
}
// Flush remaining groups.
flush();
}
static void decompose_spp_or_spp_dag_operation_helper(
PauliStringRef<64> observable,
std::span<const GateTarget> classical_bits,
bool invert_sign,
const std::function<void(const CircuitInstruction &inst)> &do_instruction_callback,
std::vector<GateTarget> *h_xz_buf,
std::vector<GateTarget> *h_yz_buf,
std::vector<GateTarget> *cnot_buf,
std::string_view tag) {
h_xz_buf->clear();
h_yz_buf->clear();
cnot_buf->clear();
// Assemble quantum terms from the observable.
uint64_t focus_qubit = UINT64_MAX;
observable.for_each_active_pauli([&](uint32_t q) {
bool x = observable.xs[q];
bool z = observable.zs[q];
// Include single qubit gates transforming the Pauli into a Z.
if (x) {
if (z) {
h_yz_buf->push_back({q});
} else {
h_xz_buf->push_back({q});
}
}
// Include CNOT gates folding onto a single measured qubit.
if (focus_qubit == UINT64_MAX) {
focus_qubit = q;
} else {
cnot_buf->push_back({q});
cnot_buf->push_back({(uint32_t)focus_qubit});
}
});
// Products need a quantum part to have an observable effect.
if (focus_qubit == UINT64_MAX) {
return;
}
for (const auto &t : classical_bits) {
cnot_buf->push_back({t});
cnot_buf->push_back({(uint32_t)focus_qubit});
}
GateTarget t = GateTarget::qubit(focus_qubit);
bool sign = invert_sign ^ observable.sign;
GateType g = sign ? GateType::S_DAG : GateType::S;
{
ConjugateBySelfInverse c1(CircuitInstruction(GateType::H, {}, *h_xz_buf, tag), do_instruction_callback);
ConjugateBySelfInverse c2(CircuitInstruction(GateType::H_YZ, {}, *h_yz_buf, tag), do_instruction_callback);
ConjugateBySelfInverse c3(CircuitInstruction(GateType::CX, {}, *cnot_buf, tag), do_instruction_callback);
do_instruction_callback(CircuitInstruction(g, {}, &t, tag));
}
}
void stim::decompose_spp_or_spp_dag_operation(
const CircuitInstruction &spp_op,
size_t num_qubits,
bool invert_sign,
const std::function<void(const CircuitInstruction &inst)> &do_instruction_callback) {
PauliString<64> obs(num_qubits);
std::vector<GateTarget> h_xz_buf;
std::vector<GateTarget> h_yz_buf;
std::vector<GateTarget> cnot_buf;
std::vector<GateTarget> bits;
if (spp_op.gate_type == GateType::SPP) {
// No sign inversion needed.
} else if (spp_op.gate_type == GateType::SPP_DAG) {
invert_sign ^= true;
} else {
throw std::invalid_argument("Not an SPP or SPP_DAG instruction: " + spp_op.str());
}
size_t start = 0;
while (accumulate_next_obs_terms_to_pauli_string_helper(spp_op, &start, &obs, &bits)) {
decompose_spp_or_spp_dag_operation_helper(
obs, bits, invert_sign, do_instruction_callback, &h_xz_buf, &h_yz_buf, &cnot_buf, spp_op.tag);
}
}
static void decompose_cpp_operation_with_reverse_independence_helper(
CircuitInstruction cpp_op,
PauliStringRef<64> obs1,
PauliStringRef<64> obs2,
std::span<const GateTarget> classical_bits1,
std::span<const GateTarget> classical_bits2,
const std::function<void(const CircuitInstruction &inst)> &do_instruction_callback,
Circuit *workspace,
std::vector<GateTarget> *buf) {
assert(obs1.num_qubits == obs2.num_qubits);
if (!obs1.commutes(obs2)) {
std::stringstream ss;
ss << "Attempted to CPP two anticommuting observables.\n";
ss << " obs1: " << obs1 << "\n";
ss << " obs2: " << obs2 << "\n";
ss << " instruction: " << cpp_op;
throw std::invalid_argument(ss.str());
}
workspace->clear();
auto apply_fixup = [&](CircuitInstruction inst) {
workspace->safe_append(inst);
obs1.do_instruction(inst);
obs2.do_instruction(inst);
};
auto reduce = [&](PauliStringRef<64> target_obs) {
// Turn all non-identity terms into Z terms.
target_obs.xs.for_each_set_bit([&](uint32_t q) {
GateTarget t = GateTarget::qubit(q);
apply_fixup(CircuitInstruction{target_obs.zs[q] ? GateType::H_YZ : GateType::H, {}, &t, {}});
});
// Cancel any extra Z terms.
uint64_t pivot = UINT64_MAX;
target_obs.for_each_active_pauli([&](uint32_t q) {
if (pivot == UINT64_MAX) {
pivot = q;
} else {
std::array<GateTarget, 2> ts{GateTarget::qubit(q), GateTarget::qubit(pivot)};
apply_fixup({GateType::CX, {}, ts, {}});
}
});
return pivot;
};
uint64_t pivot1 = reduce(obs1);
uint64_t pivot2 = reduce(obs2);
if (pivot1 == pivot2 && pivot1 != UINT64_MAX) {
// Both observables had identical quantum parts (up to sign).
// If their sign differed, we should do nothing.
// If their sign matched, we should apply Z to obs1.
assert(obs1.xs == obs2.xs);
assert(obs1.zs == obs2.zs);
obs2.zs[pivot2] = false;
obs2.sign ^= obs1.sign;
obs2.sign ^= true;
pivot2 = UINT64_MAX;
}
assert(obs1.weight() <= 1);
assert(obs2.weight() <= 1);
assert((pivot1 == UINT64_MAX) == (obs1.weight() == 0));
assert((pivot2 == UINT64_MAX) == (obs2.weight() == 0));
assert(pivot1 == UINT64_MAX || obs1.xs[pivot1] + 2 * obs1.zs[pivot1] == 2);
assert(pivot1 == UINT64_MAX || obs2.xs[pivot1] + 2 * obs2.zs[pivot1] == 0);
assert(pivot2 == UINT64_MAX || obs1.xs[pivot2] + 2 * obs1.zs[pivot2] == 0);
assert(pivot2 == UINT64_MAX || obs2.xs[pivot2] + 2 * obs2.zs[pivot2] == 2);
// Apply rewrites.
workspace->for_each_operation(do_instruction_callback);
// Handle the quantum-quantum interaction.
if (pivot1 != UINT64_MAX && pivot2 != UINT64_MAX) {
assert(pivot1 != pivot2);
std::array<GateTarget, 2> ts{GateTarget::qubit(pivot1), GateTarget::qubit(pivot2)};
do_instruction_callback({GateType::CZ, {}, ts, cpp_op.tag});
}
// Handle sign and classical feedback into obs1.
if (pivot1 != UINT64_MAX) {
for (const auto &t : classical_bits2) {
std::array<GateTarget, 2> ts{t, GateTarget::qubit(pivot1)};
do_instruction_callback({GateType::CZ, {}, ts, cpp_op.tag});
}
if (obs2.sign) {
GateTarget t = GateTarget::qubit(pivot1);
do_instruction_callback({GateType::Z, {}, &t, cpp_op.tag});
}
}
// Handle sign and classical feedback into obs2.
if (pivot2 != UINT64_MAX) {
for (const auto &t : classical_bits1) {
std::array<GateTarget, 2> ts{t, GateTarget::qubit(pivot2)};
do_instruction_callback({GateType::CZ, {}, ts, cpp_op.tag});
}
if (obs1.sign) {
GateTarget t = GateTarget::qubit(pivot2);
do_instruction_callback({GateType::Z, {}, &t, cpp_op.tag});
}
}
// Undo rewrites.
workspace->for_each_operation_reverse([&](CircuitInstruction inst) {
assert(inst.args.empty());
if (inst.gate_type == GateType::CX) {
buf->clear();
for (size_t k = inst.targets.size(); k;) {
k -= 2;
buf->push_back(inst.targets[k]);
buf->push_back(inst.targets[k + 1]);
}
do_instruction_callback({GateType::CX, {}, *buf, cpp_op.tag});
} else {
assert(inst.gate_type == GATE_DATA[inst.gate_type].inverse().id);
do_instruction_callback(inst);
}
});
}
void stim::decompose_cpp_operation_with_reverse_independence(
const CircuitInstruction &cpp_op,
size_t num_qubits,
const std::function<void(const CircuitInstruction &inst)> &do_instruction_callback) {
PauliString<64> obs1(num_qubits);
PauliString<64> obs2(num_qubits);
std::vector<GateTarget> bits1;
std::vector<GateTarget> bits2;
Circuit circuit_workspace;
std::vector<GateTarget> target_buf;
size_t start = 0;
while (true) {
bool b1 = accumulate_next_obs_terms_to_pauli_string_helper(cpp_op, &start, &obs1, &bits1);
bool b2 = accumulate_next_obs_terms_to_pauli_string_helper(cpp_op, &start, &obs2, &bits2);
if (!b2) {
break;
}
if (!b1) {
throw std::invalid_argument("Odd number of products.");
}
decompose_cpp_operation_with_reverse_independence_helper(
cpp_op, obs1, obs2, bits1, bits2, do_instruction_callback, &circuit_workspace, &target_buf);
}
}
void stim::decompose_pair_instruction_into_disjoint_segments(
const CircuitInstruction &inst, size_t num_qubits, const std::function<void(CircuitInstruction)> &callback) {
simd_bits<64> used_as_control(num_qubits);
size_t num_flushed = 0;
size_t cur_index = 0;
auto flush = [&]() {
callback(
CircuitInstruction{
inst.gate_type,
inst.args,
inst.targets.sub(num_flushed, cur_index),
inst.tag,
});
used_as_control.clear();
num_flushed = cur_index;
};
while (cur_index < inst.targets.size()) {
size_t q0 = inst.targets[cur_index].qubit_value();
size_t q1 = inst.targets[cur_index + 1].qubit_value();
if (used_as_control[q0] || used_as_control[q1]) {
flush();
}
used_as_control[q0] = true;
used_as_control[q1] = true;
cur_index += 2;
}
if (num_flushed < inst.targets.size()) {
flush();
}
}
void stim::for_each_disjoint_target_segment_in_instruction_reversed(
const CircuitInstruction &inst,
simd_bits_range_ref<64> workspace,
const std::function<void(CircuitInstruction)> &callback) {
workspace.clear();
size_t cur_end = inst.targets.size();
size_t cur_start = inst.targets.size();
auto flush = [&]() {
callback(CircuitInstruction(inst.gate_type, inst.args, inst.targets.sub(cur_start, cur_end), inst.tag));
workspace.clear();
cur_end = cur_start;
};
while (cur_start > 0) {
auto t = inst.targets[cur_start - 1];
if (t.has_qubit_value()) {
if (workspace[t.qubit_value()]) {
flush();
}
workspace[t.qubit_value()] = true;
}
cur_start--;
}
if (cur_end > 0) {
flush();
}
}
void stim::for_each_combined_targets_group(
const CircuitInstruction &inst, const std::function<void(CircuitInstruction)> &callback) {
if (inst.targets.empty()) {
return;
}
size_t start = 0;
size_t next_start = 1;
while (true) {
if (next_start >= inst.targets.size() || !inst.targets[next_start].is_combiner()) {
callback(CircuitInstruction(inst.gate_type, inst.args, inst.targets.sub(start, next_start), inst.tag));
start = next_start;
next_start = start + 1;
if (next_start > inst.targets.size()) {
return;
}
} else {
next_start += 2;
}
}
}
void stim::for_each_pair_combined_targets_group(
const CircuitInstruction &inst, const std::function<void(CircuitInstruction)> &callback) {
if (inst.targets.empty()) {
return;
}
size_t start = 0;
size_t next_start = 1;
bool parity = false;
while (true) {
if (next_start >= inst.targets.size() || !inst.targets[next_start].is_combiner()) {
if (parity) {
callback(CircuitInstruction(inst.gate_type, inst.args, inst.targets.sub(start, next_start), inst.tag));
start = next_start;
next_start = start + 1;
parity = false;
if (next_start > inst.targets.size()) {
return;
}
} else {
if (next_start >= inst.targets.size()) {
throw std::invalid_argument("Missing combined target partner: " + inst.str());
}
parity = true;
next_start += 1;
}
} else {
next_start += 2;
}
}
}