burn_quantum 0.4.0

Differentiable qubit and qudit simulation with Burn's native tensor and module APIs
Documentation
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
//! Combined merge/split phenomenological detector model with random seam boundaries.
use super::*;
use crate::photonic::qec::threshold::{DecoderGraph, DetectorEdge, GraphLimits};
use crate::photonic::stabilizer::{Pauli, PauliProduct};
use crate::{Error, Result};
use std::collections::{BTreeSet, VecDeque};
/// Declared independent fault locations; no ideal encoder/check-gate hook errors are implied.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SurgeryFault {
    /// X or Z just before a merged/split round, numbered0..2d. Final checks are ideal.
    Data {
        /// Before this zero-based merge/split schedule round.
        round: usize,
        /// Relative site in the two-patch data register.
        site: usize,
        /// Independent X or Z fault; coincident coordinates compose to Y.
        pauli: Pauli,
    },
    /// Classical flipped sign in one nonfinal local-check measurement.
    Readout {
        /// Index in the complete record list, excluding noiseless final checks.
        record: usize,
    },
}
/// One recorded check; final=true is noiseless closure of the actual final data syndrome.
#[derive(Clone, Debug)]
pub struct SurgeryCheckRecord {
    /// Round in the complete schedule; 0..d merged,d..2d split,2d final.
    pub round: usize,
    /// Local check within its phase.
    pub check: usize,
    /// Measured relative data Pauli.
    pub observable: PauliProduct,
    /// Excludes this record from classical measurement-fault locations.
    pub final_check: bool,
}
/// Compilation budgets for the sparse transition model and distance certification.
#[derive(Clone, Copy, Debug)]
pub struct SurgeryDetectorLimits {
    /// Number of measured check signs, including ideal closure.
    pub max_records: usize,
    /// Number of data/readout fault locations.
    pub max_faults: usize,
    /// Sparse record/detector incidences visited while deriving all fault signatures.
    pub max_signature_work: usize,
    /// Edge visits in the four logical-distance searches.
    pub max_distance_work: usize,
}
impl Default for SurgeryDetectorLimits {
    fn default() -> Self {
        Self {
            max_records: 1 << 20,
            max_faults: 1 << 20,
            max_signature_work: 1 << 27,
            max_distance_work: 1 << 27,
        }
    }
}
/// Complete phase-transition detector model for ideal checks plus independent
/// round-boundary data X/Z and check-readout faults. Split gauge corrections are
/// deferred until after the ideal closing checks. All first-round seam gauges
/// and first split single-patch signs remain unknown; only justified relations
/// become detectors. This is a phenomenological model, not a circuit-noise model.
#[derive(Clone, Debug)]
pub struct SurgeryDetectorModel {
    plan: PlanarSurgery,
    records: Vec<SurgeryCheckRecord>,
    detectors: Vec<Vec<usize>>,
    faults: Vec<SurgeryFault>,
    graph: DecoderGraph,
    logical: Vec<PauliProduct>,
    distances: [usize; 4],
    witnesses: Vec<Vec<usize>>,
    work: usize,
}
impl SurgeryDetectorModel {
    /// Derive each physical fault signature, reject nongraphlike models, and
    /// certify shortest undetected paths for all four output-Choi observables.
    pub fn new(plan: PlanarSurgery, limits: SurgeryDetectorLimits) -> Result<Self> {
        let d = plan.code.distance();
        let n = 2 * plan.code.qubits();
        let m = plan.merged.len();
        let s = plan.split.len();
        let count = plan.measurements().checked_add(s).ok_or(Error::Overflow)?;
        let faults_count = (2 * d)
            .checked_mul(n)
            .and_then(|v| v.checked_mul(2))
            .and_then(|v| v.checked_add(plan.measurements()))
            .ok_or(Error::Overflow)?;
        for (resource, required, limit) in [
            ("surgery detector records", count, limits.max_records),
            ("surgery fault locations", faults_count, limits.max_faults),
        ] {
            if required > limit {
                return Err(Error::ResourceLimit {
                    resource,
                    required,
                    limit,
                });
            }
        }
        let mut records = Vec::with_capacity(count);
        for round in 0..=2 * d {
            let checks = if round < d { &plan.merged } else { &plan.split };
            for (check, p) in checks.iter().enumerate() {
                records.push(SurgeryCheckRecord {
                    round,
                    check,
                    observable: p.clone(),
                    final_check: round == 2 * d,
                });
            }
        }
        let mut detectors = vec![];
        // First seam values are random. Only unchanged bulk/composite generators
        // have known initial values in the two input code spaces.
        for check in d..m {
            detectors.push(vec![check]);
        }
        for round in 1..d {
            for check in 0..m {
                detectors.push(vec![(round - 1) * m + check, round * m + check]);
            }
        }
        let split_start = d * m;
        for (check, p) in plan.split.iter().enumerate() {
            if plan.boundary.iter().any(|pair| pair.contains(&check)) {
                continue;
            }
            let prior = plan.merged.iter().position(|q| q == p).ok_or_else(|| {
                Error::InvalidValue("unchanged split check missing from merge".into())
            })?;
            detectors.push(vec![(d - 1) * m + prior, split_start + check]);
        }
        for &[a, b] in &plan.boundary {
            let composite = plan.split[a].multiply_commuting(&plan.split[b])?.1;
            let prior = plan
                .merged
                .iter()
                .position(|q| q == &composite)
                .ok_or_else(|| Error::InvalidValue("split composite missing from merge".into()))?;
            detectors.push(vec![(d - 1) * m + prior, split_start + a, split_start + b]);
        }
        // Includes ideal final closure, but never a known-zero assumption on
        // the individually random restored-boundary generators.
        for round in 1..=d {
            for check in 0..s {
                detectors.push(vec![
                    split_start + (round - 1) * s + check,
                    split_start + round * s + check,
                ]);
            }
        }
        // Every relation has a distinct greatest record index, so these rows
        // are independent. Their nullity equals the d random merge gauges and
        // d-1 random split gauges; no unproved known-zero boundary is inserted.
        let pivots: BTreeSet<_> = detectors
            .iter()
            .map(|row| *row.iter().max().unwrap())
            .collect();
        if pivots.len() != detectors.len() || records.len() - detectors.len() != 2 * d - 1 {
            return Err(Error::InvalidValue(
                "surgery detector relations are incomplete or dependent".into(),
            ));
        }
        let mut inverse = vec![vec![]; records.len()];
        for (i, row) in detectors.iter().enumerate() {
            for &r in row {
                inverse[r].push(i);
            }
        }
        let mut incidence = vec![vec![vec![]; 2]; n];
        for (i, record) in records.iter().enumerate() {
            for &(q, p) in record.observable.terms() {
                let color = match p {
                    Pauli::Z => 0,
                    Pauli::X => 1,
                    Pauli::Y => {
                        return Err(Error::Unsupported(
                            "surgery detector model must be CSS".into(),
                        ));
                    }
                };
                incidence[q][color].push(i);
            }
        }
        let (a, b, c) = match plan.basis {
            MergeBasis::ZZ => (
                plan.code.logical_z(0)?,
                plan.code.logical_z(plan.code.qubits())?,
                plan.code
                    .logical_x(0)?
                    .multiply_commuting(&plan.code.logical_x(plan.code.qubits())?)?
                    .1,
            ),
            MergeBasis::XX => (
                plan.code.logical_x(0)?,
                plan.code.logical_x(plan.code.qubits())?,
                plan.code
                    .logical_z(0)?
                    .multiply_commuting(&plan.code.logical_z(plan.code.qubits())?)?
                    .1,
            ),
        };
        let parity = a.multiply_commuting(&b)?.1;
        let logical = vec![a, b, c, parity];
        let mut faults = vec![];
        let mut edges = vec![];
        let mut work = 0usize;
        let mut add = |fault: SurgeryFault,
                       flips: Vec<usize>,
                       physical: Option<(usize, Pauli)>|
         -> Result<()> {
            let mut touched = BTreeSet::new();
            let mut label = 0u64;
            for &r in &flips {
                work = work
                    .checked_add(inverse[r].len() + 1)
                    .ok_or(Error::Overflow)?;
                if work > limits.max_signature_work {
                    return Err(Error::ResourceLimit {
                        resource: "surgery detector signature work",
                        required: work,
                        limit: limits.max_signature_work,
                    });
                }
                if r < d {
                    label ^= 1 << 3;
                }
                for &detector in &inverse[r] {
                    if !touched.insert(detector) {
                        touched.remove(&detector);
                    }
                }
            }
            if let Some((site, pauli)) = physical {
                let p = PauliProduct::new(vec![(site, pauli)])?;
                for (i, l) in logical.iter().enumerate() {
                    if p.anticommutes(l) {
                        label ^= 1 << i;
                    }
                }
            }
            let endpoints: Vec<_> = touched.into_iter().collect();
            let boundary = detectors.len();
            let (a, b) = match endpoints.as_slice() {
                [] => (boundary, boundary),
                [a] => (*a, boundary),
                [a, b] => (*a, *b),
                _ => {
                    return Err(Error::Unsupported(
                        "surgery fault has more than two detector endpoints".into(),
                    ));
                }
            };
            edges.push(DetectorEdge {
                a,
                b,
                logical: label,
            });
            faults.push(fault);
            Ok(())
        };
        for round in 0..2 * d {
            for (site, by_pauli) in incidence.iter().enumerate() {
                for (color, &pauli) in [Pauli::X, Pauli::Z].iter().enumerate() {
                    let flips = by_pauli[color]
                        .iter()
                        .copied()
                        .filter(|&r| records[r].round >= round)
                        .collect();
                    add(
                        SurgeryFault::Data { round, site, pauli },
                        flips,
                        Some((site, pauli)),
                    )?;
                }
            }
        }
        for (record, r) in records.iter().enumerate() {
            if !r.final_check {
                add(SurgeryFault::Readout { record }, vec![record], None)?;
            }
        }
        let graph = DecoderGraph::new(
            detectors.len(),
            4,
            edges,
            GraphLimits {
                max_detectors: limits.max_records,
                max_edges: limits.max_faults,
            },
        )?;
        let (mut distances, mut witnesses) = ([0; 4], vec![]);
        let mut distance_work = 0;
        for (i, distance) in distances.iter_mut().enumerate() {
            let witness =
                shortest_logical(&graph, i, &mut distance_work, limits.max_distance_work)?;
            *distance = witness.len();
            witnesses.push(witness);
        }
        if distances.iter().any(|&value| value < d) {
            return Err(Error::InvalidValue(format!(
                "surgery transition distance below d: {distances:?}"
            )));
        }
        Ok(Self {
            plan,
            records,
            detectors,
            faults,
            graph,
            logical,
            distances,
            witnesses,
            work,
        })
    }
    /// Checked doubled-seam schedule.
    pub fn plan(&self) -> &PlanarSurgery {
        &self.plan
    }
    /// Complete observed-record order, including ideal final checks.
    pub fn records(&self) -> &[SurgeryCheckRecord] {
        &self.records
    }
    /// Detector XOR supports; every ideal random branch yields zero for all rows.
    pub fn detectors(&self) -> &[Vec<usize>] {
        &self.detectors
    }
    /// Complete physical fault catalog in graph-edge order.
    pub fn faults(&self) -> &[SurgeryFault] {
        &self.faults
    }
    /// Graph with four output-Choi/record logical labels.
    pub fn graph(&self) -> &DecoderGraph {
        &self.graph
    }
    /// Data factors of the four Choi stabilizers. Label3 also XORs the recorded
    /// first seam parity; the untouched reference factors are implicit.
    pub fn logical_data_observables(&self) -> &[PauliProduct] {
        &self.logical
    }
    /// Minimum undetected physical/readout fault count for each logical label.
    pub fn logical_distances(&self) -> [usize; 4] {
        self.distances
    }
    /// Edge indices proving each reported space-time distance upper bound.
    pub fn distance_witnesses(&self) -> &[Vec<usize>] {
        &self.witnesses
    }
    /// Sparse signature incidences visited during model derivation.
    pub fn signature_work(&self) -> usize {
        self.work
    }
    /// Project raw signs, without substituting zero for unknown/erased records.
    pub fn detector_values(&self, records: &[bool]) -> Result<Vec<bool>> {
        if records.len() != self.records.len() {
            return Err(Error::Shape("surgery record count".into()));
        }
        Ok(self
            .detectors
            .iter()
            .map(|row| row.iter().fold(false, |v, &r| v ^ records[r]))
            .collect())
    }
    /// Exact raw-record flip mask for one declared fault, under reference-Pauli
    /// coupling and deferred split feed-forward. Useful for independent oracles.
    pub fn fault_records(&self, index: usize) -> Result<Vec<bool>> {
        let fault = self
            .faults
            .get(index)
            .ok_or_else(|| Error::InvalidValue("surgery fault index".into()))?;
        match fault {
            SurgeryFault::Readout { record } => {
                Ok((0..self.records.len()).map(|r| r == *record).collect())
            }
            SurgeryFault::Data { round, site, pauli } => {
                let p = PauliProduct::new(vec![(*site, *pauli)])?;
                Ok(self
                    .records
                    .iter()
                    .map(|r| r.round >= *round && p.anticommutes(&r.observable))
                    .collect())
            }
        }
    }
}
fn shortest_logical(
    graph: &DecoderGraph,
    logical: usize,
    work: &mut usize,
    budget: usize,
) -> Result<Vec<usize>> {
    let nodes = graph.detectors() + 1;
    let mut adjacency = vec![vec![]; nodes];
    let mut starts = BTreeSet::new();
    for (i, e) in graph.edges().iter().enumerate() {
        let flip = (e.logical >> logical) & 1 != 0;
        adjacency[e.a].push((e.b, flip, i));
        adjacency[e.b].push((e.a, flip, i));
        if flip {
            starts.insert(e.a);
            starts.insert(e.b);
        }
    }
    let mut best: Option<Vec<usize>> = None;
    for start in starts {
        let root = 2 * start;
        let target = root + 1;
        let mut parent = vec![None; 2 * nodes];
        let mut depth = vec![0; 2 * nodes];
        parent[root] = Some((root, usize::MAX));
        let mut queue = VecDeque::from([root]);
        while let Some(node) = queue.pop_front() {
            if node == target {
                break;
            }
            if best.as_ref().is_some_and(|p| depth[node] + 1 >= p.len()) {
                continue;
            }
            for &(next, flip, edge) in &adjacency[node / 2] {
                *work = work.checked_add(1).ok_or(Error::Overflow)?;
                if *work > budget {
                    return Err(Error::ResourceLimit {
                        resource: "surgery spacetime distance work",
                        required: *work,
                        limit: budget,
                    });
                }
                let next = 2 * next + ((node % 2) ^ usize::from(flip));
                if parent[next].is_none() {
                    parent[next] = Some((node, edge));
                    depth[next] = depth[node] + 1;
                    queue.push_back(next);
                }
            }
        }
        if parent[target].is_some() {
            let mut path = vec![];
            let mut node = target;
            while node != root {
                let (p, e) = parent[node].unwrap();
                path.push(e);
                node = p;
            }
            if best.as_ref().is_none_or(|p| path.len() < p.len()) {
                best = Some(path);
            }
        }
    }
    best.ok_or_else(|| Error::InvalidValue("surgery logical has no spacetime witness".into()))
}