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
47/// Result type for quantum operations
48pub type QuantRS2Result<T> = Result<T, QuantRS2Error>;
49
50impl From<ndarray::ShapeError> for QuantRS2Error {
51    fn from(err: ndarray::ShapeError) -> Self {
52        QuantRS2Error::InvalidInput(format!("Shape error: {}", err))
53    }
54}
55
56#[cfg(feature = "mps")]
57#[allow(unexpected_cfgs)]
58impl From<ndarray_linalg::error::LinalgError> for QuantRS2Error {
59    fn from(err: ndarray_linalg::error::LinalgError) -> Self {
60        QuantRS2Error::LinalgError(format!("Linear algebra error: {}", err))
61    }
62}