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
//! Sampling a circuit with a Pauli frame, independently of any error model.
//!
//! The frame records how this shot differs from a noiseless run. For a Clifford
//! circuit under Pauli noise that is all a detector needs: it reads a parity the
//! noiseless circuit fixes, so it fires exactly when the frame flips an odd
//! number of the records it reads. Noise is sampled with each channel's true
//! (disjoint) semantics, not the error model's independent approximation, and
//! nothing here looks at the model, so a model that is wrong about the circuit
//! shows up as disagreement between the two rather than agreeing with itself.
//!
//! Resets and measurements randomise the component of the frame that the state
//! is insensitive to, as Stim's frame simulator does. It costs a draw and changes
//! nothing for a deterministic detector; for a non-deterministic one it makes the
//! failure visible as a coin flip instead of hiding it.
use crate::circuit::{Basis, Circuit, Instr};
use crate::nonpauli::NonPauli;
use crate::surface_code::Xorshift;
pub struct Shot {
pub detectors: Vec<bool>,
pub observables: u64,
/// For a circuit with leakage, whether each measurement found its qubit leaked; empty
/// otherwise.
pub heralds: Vec<bool>,
}
pub struct FrameSampler {
/// Whether the circuit has leakage (`I_ERROR[LEAK(p=…)]` and the rest): a leaked flag per
/// qubit, set by LEAK and LEAK_TRANSPORT, cleared by SEEP and by a reset. A two-qubit gate
/// with a leaked partner leaves the other qubit a uniformly random Pauli (Google's model),
/// and measuring a leaked qubit reads 1 (or a coin flip, `leaked_reads_one = false`).
leaky: bool,
pub leaked_reads_one: bool,
/// The noiseless reference record, which a leaked qubit's 1 is a flip from.
reference: Vec<bool>,
instrs: Vec<Instr>,
num_qubits: usize,
num_measurements: usize,
detectors: Vec<Vec<usize>>,
observables: Vec<Vec<usize>>,
}
/// After a two-qubit gate, a leaked partner leaves the other qubit a uniformly random Pauli.
fn leaked_partner(leaked: &[bool], a: usize, b: usize, x: &mut [bool], z: &mut [bool], rng: &mut Xorshift) {
for (l, o) in [(a, b), (b, a)] {
if leaked[l] && !leaked[o] {
apply(x, z, o, [0, 1, 3, 2][(rng.next_u64() % 4) as usize]);
}
}
}
fn apply(x: &mut [bool], z: &mut [bool], q: usize, pauli: u8) {
if pauli & 1 != 0 {
x[q] ^= true;
}
if pauli & 2 != 0 {
z[q] ^= true;
}
}
impl FrameSampler {
pub fn new(circuit: &Circuit) -> Result<FrameSampler, String> {
let res = circuit.resolve()?;
let leaky = res.instrs.iter().any(|i| matches!(i, Instr::NonPauli(ops) if ops.iter().any(|o| o.is_leakage())));
let reference = if leaky { crate::m2d::run(circuit, crate::batch_sampler::Counts::of(&circuit.instrs)?.qubits, &[], 1) } else { Vec::new() };
Ok(FrameSampler {
leaky,
leaked_reads_one: true,
reference,
instrs: res.instrs,
num_qubits: res.num_qubits,
num_measurements: res.num_measurements,
detectors: res.detectors,
observables: res.observables,
})
}
pub fn num_detectors(&self) -> usize {
self.detectors.len()
}
pub fn num_observables(&self) -> usize {
self.observables.len()
}
pub fn sample(&self, rng: &mut Xorshift) -> Shot {
let nq = self.num_qubits;
let mut x = vec![false; nq];
let mut z: Vec<bool> = (0..nq).map(|_| rng.next_u64() & 1 == 1).collect();
let mut rec = Vec::with_capacity(self.num_measurements);
// Whether the current chain of correlated errors has fired.
let mut chain = false;
let mut pauli_obs = 0u64;
let mut leaked = vec![false; if self.leaky { nq } else { 0 }];
let mut heralds = Vec::new();
for ins in &self.instrs {
match ins {
Instr::Reset { basis, qubits } => {
for &q in qubits {
let q = q as usize;
if self.leaky {
leaked[q] = false;
}
match basis {
Basis::Z => {
x[q] = false;
z[q] = rng.next_u64() & 1 == 1;
}
Basis::X => {
z[q] = false;
x[q] = rng.next_u64() & 1 == 1;
}
}
}
}
Instr::H(qubits) => {
for &q in qubits {
let q = q as usize;
std::mem::swap(&mut x[q], &mut z[q]);
}
}
Instr::Cx(pairs) => {
for &(c, t) in pairs {
let (c, t) = (c as usize, t as usize);
if x[c] {
x[t] ^= true;
}
if z[t] {
z[c] ^= true;
}
if self.leaky {
leaked_partner(&leaked, c, t, &mut x, &mut z, rng);
}
}
}
Instr::Cz(pairs) => {
for &(a, b) in pairs {
let (a, b) = (a as usize, b as usize);
if x[a] {
z[b] ^= true;
}
if x[b] {
z[a] ^= true;
}
if self.leaky {
leaked_partner(&leaked, a, b, &mut x, &mut z, rng);
}
}
}
Instr::Measure { basis, reset, flip, qubits } => {
for &q in qubits {
let q = q as usize;
let mut r = match basis {
Basis::Z => x[q],
Basis::X => z[q],
};
if self.leaky {
heralds.push(leaked[q]);
if leaked[q] {
// A leaked qubit reads 1: a flip from the reference's value.
r = if self.leaked_reads_one { !self.reference[rec.len()] } else { rng.next_u64() & 1 == 1 };
}
if *reset {
leaked[q] = false;
}
}
if *flip > 0.0 && rng.next_f64() < *flip {
r = !r;
}
rec.push(r);
match basis {
Basis::Z => z[q] = rng.next_u64() & 1 == 1,
Basis::X => x[q] = rng.next_u64() & 1 == 1,
}
if *reset {
match basis {
Basis::Z => {
x[q] = false;
z[q] = rng.next_u64() & 1 == 1;
}
Basis::X => {
z[q] = false;
x[q] = rng.next_u64() & 1 == 1;
}
}
}
}
}
Instr::PauliError { pauli, p, qubits } => {
for &q in qubits {
if rng.next_f64() < *p {
apply(&mut x, &mut z, q as usize, *pauli);
}
}
}
Instr::Depolarize1 { p, qubits } => {
for &q in qubits {
if rng.next_f64() < *p {
apply(&mut x, &mut z, q as usize, 1 + (rng.next_u64() % 3) as u8);
}
}
}
Instr::PauliChannel1 { px, py, pz, qubits } => {
for &q in qubits {
let u = rng.next_f64();
let pauli = if u < *px {
1
} else if u < px + py {
3
} else if u < px + py + pz {
2
} else {
0
};
apply(&mut x, &mut z, q as usize, pauli);
}
}
Instr::Depolarize2 { p, pairs } => {
for &(a, b) in pairs {
if rng.next_f64() < *p {
let r = 1 + rng.next_u64() % 15;
apply(&mut x, &mut z, a as usize, (r & 3) as u8);
apply(&mut x, &mut z, b as usize, (r >> 2) as u8);
}
}
}
Instr::S(qubits) => {
for &q in qubits {
let q = q as usize;
z[q] ^= x[q];
}
}
Instr::Correlated { p, paulis, chained } => {
let fires = rng.next_f64() < *p && !(*chained && chain);
chain = if *chained { chain || fires } else { fires };
if fires {
for &(q, pauli) in paulis {
apply(&mut x, &mut z, q as usize, pauli);
}
}
}
Instr::PauliChannel2 { probs, pairs } => {
for &(a, b) in pairs {
let u = rng.next_f64();
let mut acc = 0.0;
if let Some(k) = probs.iter().position(|p| {
acc += p;
u < acc
}) {
const CODE: [u8; 4] = [0, 1, 3, 2];
apply(&mut x, &mut z, a as usize, CODE[(k + 1) / 4]);
apply(&mut x, &mut z, b as usize, CODE[(k + 1) % 4]);
}
}
}
// Relative to the noiseless run, a record's flip flips its Pauli; a sweep bit
// moves only the reference.
Instr::Feedback { pauli, control, qubit } => {
if let crate::circuit::Control::Rec(k) = control {
if rec[rec.len() - *k as usize] {
apply(&mut x, &mut z, *qubit as usize, *pauli);
}
}
}
Instr::Heralded { probs, qubits, .. } => {
for &q in qubits {
let u = rng.next_f64();
let mut acc = 0.0;
let case = probs.iter().position(|p| {
acc += p;
u < acc
});
rec.push(case.is_some());
if let Some(k) = case {
apply(&mut x, &mut z, q as usize, [0, 1, 3, 2][k]);
}
}
}
Instr::Pad { flip, values } => {
for _ in values {
rec.push(*flip > 0.0 && rng.next_f64() < *flip);
}
}
// Signs, annotations and ticks leave a frame alone; loops and gates are
// flattened away.
// A Pauli target reads the frame: X anticommutes with its Z part, Z with its X.
Instr::Observable { index, paulis, .. } => {
for &(q, p, _) in paulis {
let q = q as usize;
if (p & 1 != 0 && z[q]) != (p & 2 != 0 && x[q]) {
pauli_obs ^= 1u64 << index;
}
}
}
Instr::NonPauli(ops) if self.leaky => {
for op in ops {
match *op {
NonPauli::Leak { p, qubit } => {
if rng.next_f64() < p {
leaked[qubit as usize] = true;
}
}
NonPauli::Seep { p, qubit } => {
let q = qubit as usize;
if rng.next_f64() < p && leaked[q] {
leaked[q] = false;
// Back in |0⟩ or |1⟩ at random.
x[q] ^= rng.next_u64() & 1 == 1;
z[q] = rng.next_u64() & 1 == 1;
}
}
NonPauli::LeakTransport { p, a, b } => {
let (a, b) = (a as usize, b as usize);
if rng.next_f64() < p && leaked[a] != leaked[b] {
let back = if leaked[a] { a } else { b };
leaked.swap(a, b);
x[back] ^= rng.next_u64() & 1 == 1;
z[back] = rng.next_u64() & 1 == 1;
}
}
_ => {}
}
}
}
Instr::Pauli { .. }
| Instr::SweepX(_)
| Instr::Detector { .. }
| Instr::QubitCoords { .. }
| Instr::ShiftCoords(_)
| Instr::Tick
| Instr::NonPauli(_)
| Instr::Repeat { .. }
| Instr::Gate { .. } => {}
}
}
let parity = |recs: &[usize]| recs.iter().fold(false, |acc, &m| acc ^ rec[m]);
let detectors = self.detectors.iter().map(|r| parity(r)).collect();
let mut observables = pauli_obs;
for (k, r) in self.observables.iter().enumerate() {
if parity(r) {
observables |= 1u64 << k;
}
}
Shot { detectors, observables, heralds }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dem::Dem;
use crate::fixtures::REP3;
/// Marginals follow from the model exactly: a detector fires when an odd
/// number of its independent mechanisms do, (1 − Π(1 − 2p))/2. The sampler
/// never reads the model, so agreement is a check on both.
#[test]
fn detector_marginals_match_the_error_model() {
let circuit = Circuit::parse(&REP3.replace("0.01", "0.05")).unwrap();
let dem = Dem::from_circuit(&circuit).unwrap();
let sampler = FrameSampler::new(&circuit).unwrap();
let n = 200_000usize;
let mut counts = vec![0usize; sampler.num_detectors()];
let mut obs = 0usize;
let mut rng = Xorshift::new(11);
for _ in 0..n {
let shot = sampler.sample(&mut rng);
for (i, &b) in shot.detectors.iter().enumerate() {
counts[i] += b as usize;
}
obs += (shot.observables & 1) as usize;
}
let predict = |pick: &dyn Fn(&crate::dem::Mechanism) -> bool| -> f64 {
let prod: f64 = dem.mechanisms.iter().filter(|m| pick(m)).map(|m| 1.0 - 2.0 * m.p).product();
(1.0 - prod) / 2.0
};
for (d, &c) in counts.iter().enumerate() {
let q = predict(&|m| m.detectors.contains(&(d as u32)));
let sigma = (q * (1.0 - q) / n as f64).sqrt();
let rate = c as f64 / n as f64;
assert!((rate - q).abs() < 5.0 * sigma, "D{d}: sampled {rate}, model {q}");
}
let q = predict(&|m| m.observables & 1 == 1);
let rate = obs as f64 / n as f64;
assert!((rate - q).abs() < 5.0 * (q * (1.0 - q) / n as f64).sqrt(), "L0: sampled {rate}, model {q}");
}
#[test]
fn noiseless_circuits_never_fire() {
let circuit = Circuit::parse(&REP3.replace("0.01", "0")).unwrap();
let sampler = FrameSampler::new(&circuit).unwrap();
let mut rng = Xorshift::new(3);
for _ in 0..2000 {
let shot = sampler.sample(&mut rng);
assert!(shot.detectors.iter().all(|&b| !b));
assert_eq!(shot.observables, 0);
}
}
}