Skip to main content

tensorlogic_quantrs_hooks/
error.rs

1//! Error types for PGM operations.
2
3use std::fmt;
4
5/// Errors that can occur in PGM operations.
6#[derive(Debug, Clone, PartialEq)]
7pub enum PgmError {
8    /// Variable not found in factor graph
9    VariableNotFound(String),
10    /// Factor not found
11    FactorNotFound(String),
12    /// Dimension mismatch in tensor operations
13    DimensionMismatch {
14        expected: Vec<usize>,
15        got: Vec<usize>,
16    },
17    /// Invalid probability distribution
18    InvalidDistribution(String),
19    /// Message passing convergence failure
20    ConvergenceFailure(String),
21    /// Invalid factor graph structure
22    InvalidGraph(String),
23}
24
25impl fmt::Display for PgmError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::VariableNotFound(name) => write!(f, "Variable not found: {}", name),
29            Self::FactorNotFound(name) => write!(f, "Factor not found: {}", name),
30            Self::DimensionMismatch { expected, got } => {
31                write!(
32                    f,
33                    "Dimension mismatch: expected {:?}, got {:?}",
34                    expected, got
35                )
36            }
37            Self::InvalidDistribution(msg) => write!(f, "Invalid distribution: {}", msg),
38            Self::ConvergenceFailure(msg) => write!(f, "Convergence failure: {}", msg),
39            Self::InvalidGraph(msg) => write!(f, "Invalid graph: {}", msg),
40        }
41    }
42}
43
44impl std::error::Error for PgmError {}
45
46/// Result type for PGM operations.
47pub type Result<T> = std::result::Result<T, PgmError>;