quantrs2_core/
error.rs

1use thiserror::Error;
2
3/// Common error types for quantum operations
4#[derive(Error, Debug, Clone, PartialEq, Eq)]
5pub enum QuantRS2Error {
6    /// Error when a qubit is not in a valid range
7    #[error("Invalid qubit ID {0}, must be within the valid range for this operation")]
8    InvalidQubitId(u32),
9
10    /// Error when an operation is not supported
11    #[error("Unsupported operation: {0}")]
12    UnsupportedOperation(String),
13
14    /// Error when a gate application fails
15    #[error("Failed to apply gate: {0}")]
16    GateApplicationFailed(String),
17
18    /// Error when circuit validation fails
19    #[error("Circuit validation failed: {0}")]
20    CircuitValidationFailed(String),
21
22    /// Error when backend execution fails
23    #[error("Backend execution failed: {0}")]
24    BackendExecutionFailed(String),
25
26    /// Error when unsupported qubit count is requested
27    #[error("Unsupported qubit count {0}: {1}")]
28    UnsupportedQubits(usize, String),
29
30    /// Error when invalid input is provided
31    #[error("Invalid input: {0}")]
32    InvalidInput(String),
33
34    /// Error during computation
35    #[error("Computation error: {0}")]
36    ComputationError(String),
37
38    /// Linear algebra error
39    #[error("Linear algebra error: {0}")]
40    LinalgError(String),
41
42    /// Routing error
43    #[error("Routing error: {0}")]
44    RoutingError(String),
45
46    /// Matrix construction error
47    #[error("Matrix construction error: {0}")]
48    MatrixConstruction(String),
49
50    /// Matrix inversion error
51    #[error("Matrix inversion error: {0}")]
52    MatrixInversion(String),
53
54    /// Optimization failed error
55    #[error("Optimization failed: {0}")]
56    OptimizationFailed(String),
57
58    /// Tensor network error
59    #[error("Tensor network error: {0}")]
60    TensorNetwork(String),
61
62    /// Runtime error
63    #[error("Runtime error: {0}")]
64    RuntimeError(String),
65
66    /// Execution error
67    #[error("Execution error: {0}")]
68    ExecutionError(String),
69
70    /// Invalid gate operation
71    #[error("Invalid gate operation: {0}")]
72    InvalidGateOp(String),
73
74    /// UltraThink mode error variants
75    #[error("Invalid parameter: {0}")]
76    InvalidParameter(String),
77
78    #[error("Quantum decoherence: {0}")]
79    QuantumDecoherence(String),
80
81    #[error("No storage available: {0}")]
82    NoStorageAvailable(String),
83
84    #[error("Calibration not found: {0}")]
85    CalibrationNotFound(String),
86
87    #[error("Access denied: {0}")]
88    AccessDenied(String),
89
90    #[error("Storage capacity exceeded: {0}")]
91    StorageCapacityExceeded(String),
92
93    #[error("Hardware target not found: {0}")]
94    HardwareTargetNotFound(String),
95
96    #[error("Gate fusion error: {0}")]
97    GateFusionError(String),
98
99    #[error("Unsupported gate: {0}")]
100    UnsupportedGate(String),
101
102    #[error("Compilation timeout: {0}")]
103    CompilationTimeout(String),
104
105    #[error("Node not found: {0}")]
106    NodeNotFound(String),
107
108    #[error("Node unavailable: {0}")]
109    NodeUnavailable(String),
110
111    #[error("Network error: {0}")]
112    NetworkError(String),
113
114    #[error("No hardware available: {0}")]
115    NoHardwareAvailable(String),
116
117    #[error("State not found: {0}")]
118    StateNotFound(String),
119
120    #[error("Invalid operation: {0}")]
121    InvalidOperation(String),
122
123    #[error("QKD failure: {0}")]
124    QKDFailure(String),
125
126    #[error("Division by zero")]
127    DivisionByZero,
128}
129
130/// Result type for quantum operations
131pub type QuantRS2Result<T> = Result<T, QuantRS2Error>;
132
133impl From<ndarray::ShapeError> for QuantRS2Error {
134    fn from(err: ndarray::ShapeError) -> Self {
135        QuantRS2Error::InvalidInput(format!("Shape error: {}", err))
136    }
137}
138
139#[cfg(feature = "mps")]
140impl From<ndarray_linalg::error::LinalgError> for QuantRS2Error {
141    fn from(err: ndarray_linalg::error::LinalgError) -> Self {
142        QuantRS2Error::LinalgError(format!("Linear algebra error: {}", err))
143    }
144}
145
146impl From<std::io::Error> for QuantRS2Error {
147    fn from(err: std::io::Error) -> Self {
148        QuantRS2Error::RuntimeError(format!("I/O error: {}", err))
149    }
150}
151
152impl From<Box<bincode::ErrorKind>> for QuantRS2Error {
153    fn from(err: Box<bincode::ErrorKind>) -> Self {
154        QuantRS2Error::RuntimeError(format!("Serialization error: {:?}", err))
155    }
156}
157
158impl From<serde_json::Error> for QuantRS2Error {
159    fn from(err: serde_json::Error) -> Self {
160        QuantRS2Error::RuntimeError(format!("JSON error: {}", err))
161    }
162}