quantrs2_device/qec/
traits.rs

1//! Quantum Error Correction Trait Definitions
2//!
3//! This module defines the core traits for quantum error correction:
4//! - `SyndromeDetector`: For detecting error syndromes from measurements
5//! - `ErrorCorrector`: For applying error corrections based on syndromes
6//! - `QuantumErrorCode`: For defining quantum error correction codes
7
8use std::collections::HashMap;
9
10use scirs2_core::ndarray::Array1;
11use scirs2_core::Complex64;
12
13use crate::DeviceError;
14
15use super::{CorrectionOperation, LogicalOperator, StabilizerGroup, SyndromePattern};
16
17/// Result type for QEC operations
18pub type QECResult<T> = Result<T, DeviceError>;
19
20/// Trait for syndrome detection in quantum error correction
21pub trait SyndromeDetector {
22    /// Detect error syndromes from measurement results
23    fn detect_syndromes(
24        &self,
25        measurements: &HashMap<String, Vec<i32>>,
26        stabilizers: &[StabilizerGroup],
27    ) -> QECResult<Vec<SyndromePattern>>;
28
29    /// Validate a detected syndrome against historical patterns
30    fn validate_syndrome(
31        &self,
32        syndrome: &SyndromePattern,
33        history: &[SyndromePattern],
34    ) -> QECResult<bool>;
35}
36
37/// Trait for error correction in quantum systems
38pub trait ErrorCorrector {
39    /// Apply error corrections based on detected syndromes
40    fn correct_errors(
41        &self,
42        syndromes: &[SyndromePattern],
43        code: &dyn QuantumErrorCode,
44    ) -> QECResult<Vec<CorrectionOperation>>;
45
46    /// Estimate the fidelity of a proposed correction operation
47    fn estimate_correction_fidelity(
48        &self,
49        correction: &CorrectionOperation,
50        current_state: Option<&Array1<Complex64>>,
51    ) -> QECResult<f64>;
52}
53
54/// Trait defining a quantum error correction code
55pub trait QuantumErrorCode {
56    /// Get the stabilizer generators for this code
57    fn get_stabilizers(&self) -> Vec<StabilizerGroup>;
58
59    /// Get the logical operators for this code
60    fn get_logical_operators(&self) -> Vec<LogicalOperator>;
61
62    /// Get the code distance (minimum weight of logical operators)
63    fn distance(&self) -> usize;
64
65    /// Get the number of physical data qubits
66    fn num_data_qubits(&self) -> usize;
67
68    /// Get the number of ancilla qubits for syndrome measurement
69    fn num_ancilla_qubits(&self) -> usize;
70
71    /// Get the number of logical qubits encoded
72    fn logical_qubit_count(&self) -> usize;
73
74    /// Encode a logical state into the code space
75    fn encode_logical_state(
76        &self,
77        logical_state: &Array1<Complex64>,
78    ) -> QECResult<Array1<Complex64>>;
79}