pymatching 0.1.0

Safe Rust bindings for the PyMatching quantum error-correction decoder
Documentation
use std::error::Error;
use std::fmt::{self, Display, Formatter};

/// Error returned by safe matching-graph and decoder operations.
#[derive(Debug, Clone, PartialEq)]
pub enum MatchingError {
    /// An edge or check-matrix probability was outside `0.0..=1.0`.
    InvalidProbability {
        /// The rejected probability.
        probability: f64,
    },
    /// The requested ordinary edge is not in the graph.
    MissingEdge {
        /// First requested endpoint.
        node1: usize,
        /// Second requested endpoint.
        node2: usize,
    },
    /// The requested virtual-boundary edge is not in the graph.
    MissingBoundaryEdge {
        /// Requested detector endpoint.
        node: usize,
    },
    /// A Rust adapter or helper received invalid input.
    InvalidInput(&'static str),
    /// The native PyMatching bridge rejected an operation.
    Upstream(String),
}

impl Display for MatchingError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidProbability { probability } => {
                write!(
                    f,
                    "error probability must be within 0.0..=1.0, got {probability}"
                )
            }
            Self::MissingEdge { node1, node2 } => {
                write!(f, "edge ({node1}, {node2}) not in graph")
            }
            Self::MissingBoundaryEdge { node } => {
                write!(f, "boundary edge ({node},) not in graph")
            }
            Self::InvalidInput(message) => f.write_str(message),
            Self::Upstream(message) => f.write_str(message),
        }
    }
}

impl Error for MatchingError {}

/// Result type used by the safe `pymatching` API.
pub type Result<T> = std::result::Result<T, MatchingError>;