ruqu-neural-decoder 0.1.32

Neural Quantum Error Decoder (NQED) - GNN-based decoder with O(d^2) Mamba state-space architecture
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Syndrome Graph Construction
//!
//! This module provides functionality to construct graphs from syndrome bitmaps
//! for quantum error correction codes, particularly surface codes.
//!
//! ## Graph Structure
//!
//! Each detector in the syndrome becomes a node in the graph, with edges
//! connecting neighboring detectors. Edge weights are derived from the
//! correlation structure of the error model.

use crate::error::{NeuralDecoderError, Result};
use ndarray::{Array1, Array2};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A node in the detector graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Node {
    /// Unique node identifier
    pub id: usize,
    /// Row position in the surface code lattice
    pub row: usize,
    /// Column position in the surface code lattice
    pub col: usize,
    /// Whether this detector is fired (syndrome bit is 1)
    pub fired: bool,
    /// Node type (X-type or Z-type stabilizer)
    pub node_type: NodeType,
    /// Feature vector for this node
    pub features: Vec<f32>,
}

/// Type of stabilizer node
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NodeType {
    /// X-type stabilizer (measures bit flips)
    XStabilizer,
    /// Z-type stabilizer (measures phase flips)
    ZStabilizer,
    /// Boundary node (virtual)
    Boundary,
}

/// An edge in the detector graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Edge {
    /// Source node index
    pub from: usize,
    /// Target node index
    pub to: usize,
    /// Edge weight (derived from error probability)
    pub weight: f32,
    /// Edge type
    pub edge_type: EdgeType,
}

/// Type of edge
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EdgeType {
    /// Horizontal edge in the lattice
    Horizontal,
    /// Vertical edge in the lattice
    Vertical,
    /// Temporal edge (between measurement rounds)
    Temporal,
    /// Boundary edge (to virtual boundary node)
    Boundary,
}

/// The detector graph representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectorGraph {
    /// All nodes in the graph
    pub nodes: Vec<Node>,
    /// All edges in the graph
    pub edges: Vec<Edge>,
    /// Adjacency list representation
    adjacency: HashMap<usize, Vec<usize>>,
    /// Code distance
    pub distance: usize,
    /// Number of fired detectors
    pub num_fired: usize,
}

impl DetectorGraph {
    /// Create an empty detector graph
    pub fn new(distance: usize) -> Self {
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
            adjacency: HashMap::new(),
            distance,
            num_fired: 0,
        }
    }

    /// Add a node to the graph
    pub fn add_node(&mut self, node: Node) {
        let id = node.id;
        if node.fired {
            self.num_fired += 1;
        }
        self.nodes.push(node);
        self.adjacency.entry(id).or_default();
    }

    /// Add an edge to the graph
    pub fn add_edge(&mut self, edge: Edge) {
        self.adjacency.entry(edge.from).or_default().push(edge.to);
        self.adjacency.entry(edge.to).or_default().push(edge.from);
        self.edges.push(edge);
    }

    /// Get neighbors of a node
    pub fn neighbors(&self, node_id: usize) -> Option<&Vec<usize>> {
        self.adjacency.get(&node_id)
    }

    /// Get the node features as a matrix
    pub fn node_features(&self) -> Array2<f32> {
        if self.nodes.is_empty() {
            return Array2::zeros((0, 1));
        }

        let feature_dim = self.nodes[0].features.len();
        let mut features = Array2::zeros((self.nodes.len(), feature_dim));

        for (i, node) in self.nodes.iter().enumerate() {
            for (j, &f) in node.features.iter().enumerate() {
                features[[i, j]] = f;
            }
        }

        features
    }

    /// Get the adjacency matrix
    pub fn adjacency_matrix(&self) -> Array2<f32> {
        let n = self.nodes.len();
        let mut adj = Array2::zeros((n, n));

        for edge in &self.edges {
            adj[[edge.from, edge.to]] = edge.weight;
            adj[[edge.to, edge.from]] = edge.weight;
        }

        adj
    }

    /// Get edge weights as a vector
    pub fn edge_weights(&self) -> Array1<f32> {
        Array1::from_iter(self.edges.iter().map(|e| e.weight))
    }

    /// Get fired detector indices
    pub fn fired_indices(&self) -> Vec<usize> {
        self.nodes
            .iter()
            .filter(|n| n.fired)
            .map(|n| n.id)
            .collect()
    }

    /// Check if the graph is valid
    pub fn validate(&self) -> Result<()> {
        if self.nodes.is_empty() {
            return Err(NeuralDecoderError::EmptyGraph);
        }

        // Check all edge endpoints are valid
        for edge in &self.edges {
            if edge.from >= self.nodes.len() || edge.to >= self.nodes.len() {
                return Err(NeuralDecoderError::InvalidDetector(
                    edge.from.max(edge.to)
                ));
            }
        }

        Ok(())
    }

    /// Get the number of nodes
    pub fn num_nodes(&self) -> usize {
        self.nodes.len()
    }

    /// Get the number of edges
    pub fn num_edges(&self) -> usize {
        self.edges.len()
    }
}

/// Builder for constructing detector graphs
pub struct GraphBuilder {
    distance: usize,
    syndrome: Option<Vec<bool>>,
    node_type_pattern: NodeTypePattern,
    error_rate: f64,
}

/// Pattern for determining node types
#[derive(Debug, Clone, Copy)]
pub enum NodeTypePattern {
    /// Checkerboard pattern (standard surface code)
    Checkerboard,
    /// All X-type
    AllX,
    /// All Z-type
    AllZ,
}

impl GraphBuilder {
    /// Create a builder for a surface code of given distance
    pub fn from_surface_code(distance: usize) -> Self {
        Self {
            distance,
            syndrome: None,
            node_type_pattern: NodeTypePattern::Checkerboard,
            error_rate: 0.001,
        }
    }

    /// Set the syndrome bitmap
    pub fn with_syndrome(mut self, syndrome: &[bool]) -> Result<Self> {
        let expected = self.distance * self.distance;
        if syndrome.len() != expected {
            return Err(NeuralDecoderError::syndrome_dim(
                self.distance,
                syndrome.len(),
                1,
            ));
        }
        self.syndrome = Some(syndrome.to_vec());
        Ok(self)
    }

    /// Set the node type pattern
    pub fn with_node_pattern(mut self, pattern: NodeTypePattern) -> Self {
        self.node_type_pattern = pattern;
        self
    }

    /// Set the error rate (for edge weights)
    pub fn with_error_rate(mut self, rate: f64) -> Self {
        self.error_rate = rate;
        self
    }

    /// Build the detector graph
    pub fn build(self) -> Result<DetectorGraph> {
        let d = self.distance;
        let mut graph = DetectorGraph::new(d);

        // Default syndrome: all zeros
        let syndrome = self.syndrome.unwrap_or_else(|| vec![false; d * d]);

        // Create nodes
        for row in 0..d {
            for col in 0..d {
                let id = row * d + col;
                let fired = syndrome.get(id).copied().unwrap_or(false);

                let node_type = match self.node_type_pattern {
                    NodeTypePattern::Checkerboard => {
                        if (row + col) % 2 == 0 {
                            NodeType::XStabilizer
                        } else {
                            NodeType::ZStabilizer
                        }
                    }
                    NodeTypePattern::AllX => NodeType::XStabilizer,
                    NodeTypePattern::AllZ => NodeType::ZStabilizer,
                };

                // Feature vector: [fired, row_norm, col_norm, node_type_x, node_type_z]
                let features = vec![
                    if fired { 1.0 } else { 0.0 },
                    row as f32 / d as f32,
                    col as f32 / d as f32,
                    if node_type == NodeType::XStabilizer { 1.0 } else { 0.0 },
                    if node_type == NodeType::ZStabilizer { 1.0 } else { 0.0 },
                ];

                graph.add_node(Node {
                    id,
                    row,
                    col,
                    fired,
                    node_type,
                    features,
                });
            }
        }

        // Create edges (grid connectivity)
        let weight = (-self.error_rate.ln()) as f32;

        for row in 0..d {
            for col in 0..d {
                let id = row * d + col;

                // Horizontal edge
                if col + 1 < d {
                    let neighbor = row * d + (col + 1);
                    graph.add_edge(Edge {
                        from: id,
                        to: neighbor,
                        weight,
                        edge_type: EdgeType::Horizontal,
                    });
                }

                // Vertical edge
                if row + 1 < d {
                    let neighbor = (row + 1) * d + col;
                    graph.add_edge(Edge {
                        from: id,
                        to: neighbor,
                        weight,
                        edge_type: EdgeType::Vertical,
                    });
                }
            }
        }

        graph.validate()?;
        Ok(graph)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_node_creation() {
        let node = Node {
            id: 0,
            row: 0,
            col: 0,
            fired: true,
            node_type: NodeType::XStabilizer,
            features: vec![1.0],
        };
        assert_eq!(node.id, 0);
        assert!(node.fired);
    }

    #[test]
    fn test_edge_creation() {
        let edge = Edge {
            from: 0,
            to: 1,
            weight: 1.5,
            edge_type: EdgeType::Horizontal,
        };
        assert_eq!(edge.from, 0);
        assert_eq!(edge.to, 1);
    }

    #[test]
    fn test_graph_construction_d3() {
        let graph = GraphBuilder::from_surface_code(3)
            .build()
            .unwrap();

        // 3x3 = 9 nodes
        assert_eq!(graph.num_nodes(), 9);

        // Grid edges: 2*3 horizontal + 3*2 vertical = 12 edges
        assert_eq!(graph.num_edges(), 12);
    }

    #[test]
    fn test_graph_construction_d5() {
        let graph = GraphBuilder::from_surface_code(5)
            .build()
            .unwrap();

        // 5x5 = 25 nodes
        assert_eq!(graph.num_nodes(), 25);

        // Grid edges: 4*5 horizontal + 5*4 vertical = 40 edges
        assert_eq!(graph.num_edges(), 40);
    }

    #[test]
    fn test_graph_with_syndrome() {
        let syndrome = vec![true, false, true, false, true, false, false, false, true];
        let graph = GraphBuilder::from_surface_code(3)
            .with_syndrome(&syndrome)
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(graph.num_fired, 4);
        assert_eq!(graph.fired_indices(), vec![0, 2, 4, 8]);
    }

    #[test]
    fn test_graph_syndrome_dimension_mismatch() {
        let syndrome = vec![true, false, true]; // Wrong size
        let result = GraphBuilder::from_surface_code(3)
            .with_syndrome(&syndrome);

        assert!(result.is_err());
    }

    #[test]
    fn test_graph_adjacency() {
        let graph = GraphBuilder::from_surface_code(3)
            .build()
            .unwrap();

        // Corner node (0) should have 2 neighbors
        let neighbors = graph.neighbors(0).unwrap();
        assert_eq!(neighbors.len(), 2);

        // Center node (4) should have 4 neighbors
        let neighbors = graph.neighbors(4).unwrap();
        assert_eq!(neighbors.len(), 4);
    }

    #[test]
    fn test_node_features_matrix() {
        let graph = GraphBuilder::from_surface_code(3)
            .build()
            .unwrap();

        let features = graph.node_features();
        assert_eq!(features.shape(), &[9, 5]);
    }

    #[test]
    fn test_adjacency_matrix() {
        let graph = GraphBuilder::from_surface_code(3)
            .build()
            .unwrap();

        let adj = graph.adjacency_matrix();
        assert_eq!(adj.shape(), &[9, 9]);

        // Matrix should be symmetric
        for i in 0..9 {
            for j in 0..9 {
                assert_eq!(adj[[i, j]], adj[[j, i]]);
            }
        }
    }

    #[test]
    fn test_edge_weights() {
        let graph = GraphBuilder::from_surface_code(3)
            .with_error_rate(0.01)
            .build()
            .unwrap();

        let weights = graph.edge_weights();
        assert_eq!(weights.len(), 12);

        // All weights should be positive
        for w in weights.iter() {
            assert!(*w > 0.0);
        }
    }

    #[test]
    fn test_node_type_pattern_checkerboard() {
        let graph = GraphBuilder::from_surface_code(3)
            .with_node_pattern(NodeTypePattern::Checkerboard)
            .build()
            .unwrap();

        // Check checkerboard pattern
        for node in &graph.nodes {
            let expected = if (node.row + node.col) % 2 == 0 {
                NodeType::XStabilizer
            } else {
                NodeType::ZStabilizer
            };
            assert_eq!(node.node_type, expected);
        }
    }

    #[test]
    fn test_node_type_pattern_all_x() {
        let graph = GraphBuilder::from_surface_code(3)
            .with_node_pattern(NodeTypePattern::AllX)
            .build()
            .unwrap();

        for node in &graph.nodes {
            assert_eq!(node.node_type, NodeType::XStabilizer);
        }
    }

    #[test]
    fn test_empty_syndrome() {
        let syndrome = vec![false; 9];
        let graph = GraphBuilder::from_surface_code(3)
            .with_syndrome(&syndrome)
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(graph.num_fired, 0);
        assert!(graph.fired_indices().is_empty());
    }

    #[test]
    fn test_all_fired_syndrome() {
        let syndrome = vec![true; 9];
        let graph = GraphBuilder::from_surface_code(3)
            .with_syndrome(&syndrome)
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(graph.num_fired, 9);
        assert_eq!(graph.fired_indices().len(), 9);
    }

    #[test]
    fn test_graph_validation() {
        let graph = GraphBuilder::from_surface_code(3)
            .build()
            .unwrap();

        assert!(graph.validate().is_ok());
    }

    #[test]
    fn test_empty_graph_validation() {
        let graph = DetectorGraph::new(3);
        assert!(graph.validate().is_err());
    }
}