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
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
use crate::{Error, Result};
use serde::{Deserialize, Serialize};

/// An independent binary fault coordinate with up to two detector endpoints.
/// Endpoint `detectors` denotes a virtual spatial boundary (no parity constraint).
/// `logical` is its flip mask on the declared logical observables.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DetectorEdge {
    /// First detector endpoint, or the virtual boundary index.
    pub a: usize,
    /// Second detector endpoint, or the virtual boundary index.
    pub b: usize,
    /// XOR flip mask on the declared logical observables.
    pub logical: u64,
}
/// Allocation limits checked before graph construction and decoder allocations.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct GraphLimits {
    /// Maximum constrained detector vertices, excluding the virtual boundary.
    pub max_detectors: usize,
    /// Maximum independent binary fault coordinates per graph.
    pub max_edges: usize,
}
impl Default for GraphLimits {
    fn default() -> Self {
        Self {
            max_detectors: 1 << 20,
            max_edges: 1 << 24,
        }
    }
}
/// Work limits are outcomes, not silently discarded trials.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DecoderBudget {
    /// Maximum graph-edge inspections, including initialization and peeling.
    pub max_edge_visits: u64,
    /// Maximum synchronous half-edge growth rounds for the mixed decoder.
    pub max_growth_rounds: usize,
}
impl Default for DecoderBudget {
    fn default() -> Self {
        Self {
            max_edge_visits: 1 << 28,
            max_growth_rounds: 1 << 16,
        }
    }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
/// Reproducible decoder effort, independent of host wall-clock timing.
pub struct DecoderWork {
    /// Number of edge inspections actually completed.
    pub edge_visits: u64,
    /// Number of synchronous half-edge growth steps.
    pub growth_rounds: usize,
}
impl DecoderWork {
    fn visit(&mut self, budget: DecoderBudget) -> bool {
        if self.edge_visits >= budget.max_edge_visits {
            return false;
        }
        self.edge_visits += 1;
        true
    }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
/// Decoder completion class; a returned correction can still have an unknown
/// logical error under a Pauli channel. Only the simulator can check that truth.
pub enum GraphStatus {
    /// Syndrome-consistent correction, with no intrinsic erasure ambiguity.
    Corrected,
    /// True erased coordinates support distinct logical classes.
    Ambiguous,
    /// The supplied syndrome cannot be explained by the allowed support.
    Inconsistent,
    /// A declared work bound prevented completion; do not use partial correction.
    WorkLimit,
}
/// Exact logical image of the erased cycle space. A rank-r erased component
/// makes 2^r logical classes equiprobable under the uniform erasure channel.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ErasureAnalysis {
    /// Completion class, including explicit work exhaustion.
    pub status: GraphStatus,
    /// Independent XOR basis, with distinct highest set bits.
    pub logical_basis: Vec<u64>,
    /// Completed work; never estimated from wall time.
    pub work: DecoderWork,
}
impl ErasureAnalysis {
    /// Dimension of the erased logical image; incomplete results have no basis.
    pub fn rank(&self) -> usize {
        self.logical_basis.len()
    }
    /// Logical observables whose values are not fixed by the available records.
    pub fn ambiguous_mask(&self) -> u64 {
        self.logical_basis.iter().fold(0, |a, b| a | b)
    }
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
/// Correction and erasure evidence. Correction is valid only for Corrected or
/// Ambiguous status; a work-limited or inconsistent result may contain partial work.
pub struct GraphDecode {
    /// Completion class, including explicit work exhaustion.
    pub status: GraphStatus,
    /// Physical fault coordinates in the selected recovery chain.
    pub correction: Vec<bool>,
    /// XOR flip mask on the declared logical observables.
    pub logical: u64,
    /// Logical image of genuinely erased edges, excluding grown Pauli support.
    pub erased_logical_basis: Vec<u64>,
    /// Completed work; never estimated from wall time.
    pub work: DecoderWork,
}
/// Sparse binary incidence graph, including parallel edges and relative cycles
/// ending on a spatial boundary. Memory is linear in vertices plus edges.
#[derive(Clone, Debug)]
pub struct DecoderGraph {
    detectors: usize,
    logicals: usize,
    edges: Vec<DetectorEdge>,
}
impl DecoderGraph {
    /// Validate sparse graph incidence, logical bit width and allocation limits.
    pub fn new(
        detectors: usize,
        logicals: usize,
        edges: Vec<DetectorEdge>,
        limits: GraphLimits,
    ) -> Result<Self> {
        if detectors == 0 || logicals > 64 {
            return Err(Error::InvalidDimensions("detector graph dimensions".into()));
        }
        for (resource, required, limit) in [
            ("detector graph vertices", detectors, limits.max_detectors),
            ("detector graph edges", edges.len(), limits.max_edges),
        ] {
            if required > limit {
                return Err(Error::ResourceLimit {
                    resource,
                    required,
                    limit,
                });
            }
        }
        detectors.checked_add(1).ok_or(Error::Overflow)?;
        let mask = if logicals == 64 {
            u64::MAX
        } else {
            (1u64 << logicals) - 1
        };
        if edges
            .iter()
            .any(|e| e.a > detectors || e.b > detectors || e.logical & !mask != 0)
        {
            return Err(Error::Shape("detector graph edge or logical mask".into()));
        }
        Ok(Self {
            detectors,
            logicals,
            edges,
        })
    }
    /// Number of constrained detector vertices; also the virtual boundary index.
    pub fn detectors(&self) -> usize {
        self.detectors
    }
    /// Number of declared logical observables (at most 64).
    pub fn logicals(&self) -> usize {
        self.logicals
    }
    /// Ordered independent fault coordinates and their incidence/labels.
    pub fn edges(&self) -> &[DetectorEdge] {
        &self.edges
    }
    fn validate_mask(&self, values: &[bool]) -> Result<()> {
        if values.len() != self.edges.len() {
            Err(Error::Shape("detector graph edge mask".into()))
        } else {
            Ok(())
        }
    }
    /// Detector parities generated by a complete binary fault vector.
    pub fn syndrome(&self, errors: &[bool]) -> Result<Vec<bool>> {
        self.validate_mask(errors)?;
        let mut out = vec![false; self.detectors];
        for (&fault, edge) in errors.iter().zip(&self.edges) {
            if fault {
                if edge.a < self.detectors {
                    out[edge.a] ^= true;
                }
                if edge.b < self.detectors {
                    out[edge.b] ^= true;
                }
            }
        }
        Ok(out)
    }
    /// Logical-observable flips generated by a binary fault vector.
    pub fn logical_flips(&self, errors: &[bool]) -> Result<u64> {
        self.validate_mask(errors)?;
        Ok(errors
            .iter()
            .zip(&self.edges)
            .fold(0, |value, (&fault, edge)| {
                value ^ if fault { edge.logical } else { 0 }
            }))
    }
    /// Exact cycle-space homology using union by size, path compression and XOR
    /// potentials. Boundary-to-boundary paths are cycles through the virtual node.
    /// No dense check matrix, quantum state, or measurement reference is constructed.
    pub fn analyze_erasures(
        &self,
        erased: &[bool],
        budget: DecoderBudget,
    ) -> Result<ErasureAnalysis> {
        self.validate_mask(erased)?;
        let mut dsu = Dsu::new(self.detectors + 1);
        let mut basis = [0u64; 64];
        let mut work = DecoderWork::default();
        for (&lost, edge) in erased.iter().zip(&self.edges) {
            if !work.visit(budget) {
                return Ok(ErasureAnalysis {
                    status: GraphStatus::WorkLimit,
                    logical_basis: vec![],
                    work,
                });
            }
            if lost && let Some(cycle) = dsu.join(edge.a, edge.b, edge.logical) {
                insert_basis(&mut basis, cycle);
            }
        }
        let logical_basis: Vec<_> = basis.into_iter().filter(|&v| v != 0).collect();
        Ok(ErasureAnalysis {
            status: if logical_basis.is_empty() {
                GraphStatus::Corrected
            } else {
                GraphStatus::Ambiguous
            },
            logical_basis,
            work,
        })
    }
}
fn insert_basis(basis: &mut [u64; 64], mut value: u64) {
    while value != 0 {
        let pivot = 63 - value.leading_zeros() as usize;
        if basis[pivot] == 0 {
            basis[pivot] = value;
            return;
        }
        value ^= basis[pivot];
    }
}
struct Dsu {
    parent: Vec<usize>,
    size: Vec<usize>,
    potential: Vec<u64>,
}
impl Dsu {
    fn new(n: usize) -> Self {
        Self {
            parent: (0..n).collect(),
            size: vec![1; n],
            potential: vec![0; n],
        }
    }
    fn find(&mut self, q: usize) -> (usize, u64) {
        let mut root = q;
        let mut total = 0;
        while self.parent[root] != root {
            total ^= self.potential[root];
            root = self.parent[root];
        }
        let mut at = q;
        let mut prefix = 0;
        while self.parent[at] != at {
            let next = self.parent[at];
            let old = self.potential[at];
            self.parent[at] = root;
            self.potential[at] = total ^ prefix;
            prefix ^= old;
            at = next;
        }
        (root, total)
    }
    /// Returns the logical label of a newly closed cycle, or None for a tree edge.
    fn join(&mut self, a: usize, b: usize, label: u64) -> Option<u64> {
        let (mut ra, pa) = self.find(a);
        let (mut rb, pb) = self.find(b);
        let delta = pa ^ pb ^ label;
        if ra == rb {
            return Some(delta);
        }
        if self.size[ra] > self.size[rb] {
            std::mem::swap(&mut ra, &mut rb);
        }
        self.parent[ra] = rb;
        self.potential[ra] = delta;
        self.size[rb] += self.size[ra];
        None
    }
}

impl DecoderGraph {
    /// Exact erasure-only correction by spanning-forest peeling. `Ambiguous`
    /// still returns a syndrome-consistent correction and the full logical image;
    /// it must not be interpreted as reliable recovery of erased logical data.
    pub fn decode_erasures(
        &self,
        syndrome: &[bool],
        erased: &[bool],
        budget: DecoderBudget,
    ) -> Result<GraphDecode> {
        self.decode(syndrome, erased, budget, false)
    }
    /// Unweighted union-find decoding: simultaneously grow all odd, non-boundary
    /// clusters by half an edge, fuse completed edges, then peel a spanning forest.
    /// This implements Algorithm1 of Delfosse–Nickerson (1709.06218v3) with path
    /// compression/union by size. Global edge sweeps give O((V+E)*rounds*alpha(V))
    /// work and O(V+E) storage, not the optimized boundary-list complexity of their
    /// Algorithm2. Unequal edge probabilities are not weighted by this decoder.
    pub fn decode_union_find(
        &self,
        syndrome: &[bool],
        erased: &[bool],
        budget: DecoderBudget,
    ) -> Result<GraphDecode> {
        self.decode(syndrome, erased, budget, true)
    }
    fn decode(
        &self,
        syndrome: &[bool],
        erased: &[bool],
        budget: DecoderBudget,
        grow: bool,
    ) -> Result<GraphDecode> {
        self.validate_mask(erased)?;
        if syndrome.len() != self.detectors {
            return Err(Error::Shape("detector syndrome width".into()));
        }
        let mut result = GraphDecode {
            status: GraphStatus::Corrected,
            correction: vec![false; self.edges.len()],
            logical: 0,
            erased_logical_basis: vec![],
            work: DecoderWork::default(),
        };
        let n = self.detectors + 1;
        let mut dsu = Dsu::new(n);
        let mut basis = [0; 64];
        let mut support = vec![0u8; self.edges.len()];
        let mut forest = Vec::with_capacity(n - 1);
        for (i, (&lost, e)) in erased.iter().zip(&self.edges).enumerate() {
            if !result.work.visit(budget) {
                result.status = GraphStatus::WorkLimit;
                return Ok(result);
            }
            if lost {
                support[i] = 2;
                if let Some(cycle) = dsu.join(e.a, e.b, e.logical) {
                    insert_basis(&mut basis, cycle);
                } else {
                    forest.push(i);
                }
            }
        }
        result.erased_logical_basis = basis.into_iter().filter(|&v| v != 0).collect();
        let mut parity = vec![false; n];
        let mut complete = Vec::new();
        loop {
            parity.fill(false);
            for (v, &bit) in syndrome.iter().enumerate() {
                if bit {
                    let root = dsu.find(v).0;
                    parity[root] ^= true;
                }
            }
            parity[dsu.find(self.detectors).0] = false;
            if !parity.iter().any(|&v| v) {
                break;
            }
            if !grow {
                result.status = GraphStatus::Inconsistent;
                return Ok(result);
            }
            if result.work.growth_rounds >= budget.max_growth_rounds {
                result.status = GraphStatus::WorkLimit;
                return Ok(result);
            }
            result.work.growth_rounds += 1;
            complete.clear();
            let mut progress = false;
            // Freeze parity for this entire synchronous growth round. Joining
            // clusters during this pass would introduce edge-order growth bias.
            for (i, e) in self.edges.iter().enumerate() {
                if !result.work.visit(budget) {
                    result.status = GraphStatus::WorkLimit;
                    return Ok(result);
                }
                if support[i] == 2 {
                    continue;
                }
                let a = dsu.find(e.a).0;
                let b = dsu.find(e.b).0;
                if a == b {
                    continue;
                }
                let increment = u8::from(parity[a]) + u8::from(parity[b]);
                if increment != 0 {
                    progress = true;
                    support[i] = (support[i] + increment).min(2);
                    if support[i] == 2 {
                        complete.push(i);
                    }
                }
            }
            if !progress {
                result.status = GraphStatus::Inconsistent;
                return Ok(result);
            }
            for &i in &complete {
                let e = self.edges[i];
                if dsu.join(e.a, e.b, e.logical).is_none() {
                    forest.push(i);
                }
            }
        }
        let mut adjacency = vec![Vec::new(); n];
        for i in forest {
            if !result.work.visit(budget) {
                result.status = GraphStatus::WorkLimit;
                return Ok(result);
            }
            let e = self.edges[i];
            adjacency[e.a].push((e.b, i));
            adjacency[e.b].push((e.a, i));
        }
        let mut visited = vec![false; n];
        let mut pending = syndrome.to_vec();
        pending.push(false);
        let mut order = Vec::with_capacity(n);
        let mut parent = vec![None; n];
        // Root the component touching a boundary at that boundary. Its parity is
        // unconstrained; all other roots must have even total detector syndrome.
        for root in std::iter::once(self.detectors).chain(0..self.detectors) {
            if visited[root] {
                continue;
            }
            visited[root] = true;
            let begin = order.len();
            order.push(root);
            let mut position = begin;
            while position < order.len() {
                let v = order[position];
                position += 1;
                for &(w, e) in &adjacency[v] {
                    if !result.work.visit(budget) {
                        result.status = GraphStatus::WorkLimit;
                        return Ok(result);
                    }
                    if !visited[w] {
                        visited[w] = true;
                        parent[w] = Some((v, e));
                        order.push(w);
                    }
                }
            }
            for &v in order[begin..].iter().rev() {
                if let Some((p, e)) = parent[v]
                    && pending[v]
                {
                    result.correction[e] = true;
                    result.logical ^= self.edges[e].logical;
                    pending[p] ^= true;
                }
            }
            if root != self.detectors && pending[root] {
                result.status = GraphStatus::Inconsistent;
                return Ok(result);
            }
        }
        if !result.erased_logical_basis.is_empty() {
            result.status = GraphStatus::Ambiguous;
        }
        Ok(result)
    }
}