Skip to main content

quantrs2_ml/
error.rs

1use quantrs2_core::error::QuantRS2Error;
2use std::io;
3use thiserror::Error;
4
5/// Type alias for Result with MLError as error type
6pub type Result<T> = std::result::Result<T, MLError>;
7
8/// Error type for Machine Learning operations
9#[derive(Error, Debug)]
10#[non_exhaustive]
11pub enum MLError {
12    /// Error during training or inference
13    #[error("Machine learning error: {0}")]
14    MLOperationError(String),
15
16    /// Error during model creation
17    #[error("Model creation error: {0}")]
18    ModelCreationError(String),
19
20    /// Error in optimization process
21    #[error("Optimization error: {0}")]
22    OptimizationError(String),
23
24    /// Error in data handling
25    #[error("Data error: {0}")]
26    DataError(String),
27
28    /// Error in quantum circuit execution
29    #[error("Circuit execution error: {0}")]
30    CircuitExecutionError(String),
31
32    /// Error during feature extraction
33    #[error("Feature extraction error: {0}")]
34    FeatureExtractionError(String),
35
36    /// Invalid parameter
37    #[error("Invalid parameter: {0}")]
38    InvalidParameter(String),
39
40    /// Invalid input
41    #[error("Invalid input: {0}")]
42    InvalidInput(String),
43
44    /// Invalid configuration
45    #[error("Invalid configuration: {0}")]
46    InvalidConfiguration(String),
47
48    /// Configuration error
49    #[error("Configuration error: {0}")]
50    ConfigurationError(String),
51
52    /// Dimension mismatch error
53    #[error("Dimension mismatch: {0}")]
54    DimensionMismatch(String),
55
56    /// Not implemented
57    #[error("Not implemented: {0}")]
58    NotImplemented(String),
59
60    /// Not supported
61    #[error("Not supported: {0}")]
62    NotSupported(String),
63
64    /// Validation error
65    #[error("Validation error: {0}")]
66    ValidationError(String),
67
68    /// Model not trained
69    #[error("Model not trained: {0}")]
70    ModelNotTrained(String),
71
72    /// Computation error
73    #[error("Computation error: {0}")]
74    ComputationError(String),
75
76    /// I/O error
77    #[error("I/O error: {0}")]
78    IOError(#[from] io::Error),
79
80    /// Quantum error
81    #[error("Quantum error: {0}")]
82    QuantumError(#[from] QuantRS2Error),
83
84    /// Shape error from ndarray
85    #[error("Shape error: {0}")]
86    ShapeError(#[from] scirs2_core::ndarray::ShapeError),
87
88    /// JSON serialization error
89    #[error("JSON error: {0}")]
90    JsonError(#[from] serde_json::Error),
91
92    /// Numerical error during computation
93    #[error("Numerical error: {0}")]
94    NumericalError(String),
95
96    /// Backend error
97    #[error("Backend error: {0}")]
98    BackendError(String),
99}
100
101impl From<String> for MLError {
102    fn from(s: String) -> Self {
103        MLError::ComputationError(s)
104    }
105}