1use thiserror::Error;
2
3#[derive(Error, Debug, Clone, PartialEq, Eq)]
5pub enum QuantRS2Error {
6 #[error("Invalid qubit ID {0}, must be within the valid range for this operation")]
8 InvalidQubitId(u32),
9
10 #[error("Unsupported operation: {0}")]
12 UnsupportedOperation(String),
13
14 #[error("Failed to apply gate: {0}")]
16 GateApplicationFailed(String),
17
18 #[error("Circuit validation failed: {0}")]
20 CircuitValidationFailed(String),
21
22 #[error("Backend execution failed: {0}")]
24 BackendExecutionFailed(String),
25
26 #[error("Unsupported qubit count {0}: {1}")]
28 UnsupportedQubits(usize, String),
29
30 #[error("Invalid input: {0}")]
32 InvalidInput(String),
33
34 #[error("Computation error: {0}")]
36 ComputationError(String),
37
38 #[error("Linear algebra error: {0}")]
40 LinalgError(String),
41
42 #[error("Routing error: {0}")]
44 RoutingError(String),
45
46 #[error("Matrix construction error: {0}")]
48 MatrixConstruction(String),
49
50 #[error("Matrix inversion error: {0}")]
52 MatrixInversion(String),
53
54 #[error("Optimization failed: {0}")]
56 OptimizationFailed(String),
57
58 #[error("Tensor network error: {0}")]
60 TensorNetwork(String),
61
62 #[error("Runtime error: {0}")]
64 RuntimeError(String),
65
66 #[error("Execution error: {0}")]
68 ExecutionError(String),
69
70 #[error("Invalid gate operation: {0}")]
72 InvalidGateOp(String),
73
74 #[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 #[error("Lock poisoned: {0}")]
130 LockPoisoned(String),
131}
132
133pub type QuantRS2Result<T> = Result<T, QuantRS2Error>;
135
136impl From<scirs2_core::ndarray::ShapeError> for QuantRS2Error {
137 fn from(err: scirs2_core::ndarray::ShapeError) -> Self {
138 Self::InvalidInput(format!("Shape error: {err}"))
139 }
140}
141
142#[cfg(feature = "mps")]
143impl From<scirs2_linalg::error::LinalgError> for QuantRS2Error {
144 fn from(err: scirs2_linalg::error::LinalgError) -> Self {
145 Self::LinalgError(format!("Linear algebra error: {err}"))
146 }
147}
148
149impl From<std::io::Error> for QuantRS2Error {
150 fn from(err: std::io::Error) -> Self {
151 Self::RuntimeError(format!("I/O error: {err}"))
152 }
153}
154
155impl From<bincode::error::EncodeError> for QuantRS2Error {
156 fn from(err: bincode::error::EncodeError) -> Self {
157 Self::RuntimeError(format!("Serialization encode error: {err:?}"))
158 }
159}
160
161impl From<bincode::error::DecodeError> for QuantRS2Error {
162 fn from(err: bincode::error::DecodeError) -> Self {
163 Self::RuntimeError(format!("Serialization decode error: {err:?}"))
164 }
165}
166
167impl From<serde_json::Error> for QuantRS2Error {
168 fn from(err: serde_json::Error) -> Self {
169 Self::RuntimeError(format!("JSON error: {err}"))
170 }
171}