quantrs2_sim/
error.rs

1//! Error types for the quantum simulator module.
2
3use quantrs2_core::error::QuantRS2Error;
4use thiserror::Error;
5
6/// Error types for quantum simulation
7#[derive(Debug, Clone, Error)]
8pub enum SimulatorError {
9    /// Invalid number of qubits
10    #[error("Invalid number of qubits: {0}")]
11    InvalidQubits(usize),
12
13    /// Invalid qubit index
14    #[error("Invalid qubit count: {0}")]
15    InvalidQubitCount(String),
16
17    /// Qubit index out of range
18    #[error("Qubit index {index} out of range for {num_qubits} qubits")]
19    InvalidQubitIndex { index: usize, num_qubits: usize },
20
21    /// Invalid gate specification
22    #[error("Invalid gate: {0}")]
23    InvalidGate(String),
24
25    /// Computation error during simulation
26    #[error("Computation error: {0}")]
27    ComputationError(String),
28
29    /// Gate target out of bounds
30    #[error("Gate target out of bounds: {0}")]
31    IndexOutOfBounds(usize),
32
33    /// Invalid operation
34    #[error("Invalid operation: {0}")]
35    InvalidOperation(String),
36
37    /// GPU not available
38    #[error("GPU not available")]
39    GPUNotAvailable,
40
41    /// Shader compilation failed
42    #[error("Shader compilation failed: {0}")]
43    ShaderCompilationError(String),
44
45    /// GPU execution error
46    #[error("GPU execution error: {0}")]
47    GPUExecutionError(String),
48
49    /// General GPU error
50    #[error("GPU error: {0}")]
51    GpuError(String),
52
53    /// Dimension mismatch
54    #[error("Dimension mismatch: {0}")]
55    DimensionMismatch(String),
56
57    /// Invalid input
58    #[error("Invalid input: {0}")]
59    InvalidInput(String),
60
61    /// Invalid configuration
62    #[error("Invalid configuration: {0}")]
63    InvalidConfiguration(String),
64
65    /// Numerical error
66    #[error("Numerical error: {0}")]
67    NumericalError(String),
68
69    /// Core error
70    #[error("Core error: {0}")]
71    CoreError(#[from] QuantRS2Error),
72
73    /// Unsupported operation
74    #[error("Unsupported operation: {0}")]
75    UnsupportedOperation(String),
76
77    /// Linear algebra error
78    #[error("Linear algebra error: {0}")]
79    LinalgError(String),
80
81    /// Initialization failed
82    #[error("Initialization failed: {0}")]
83    InitializationFailed(String),
84
85    /// Memory allocation error
86    #[error("Memory error: {0}")]
87    MemoryError(String),
88
89    /// Initialization error
90    #[error("Initialization error: {0}")]
91    InitializationError(String),
92
93    /// Operation not supported
94    #[error("Operation not supported: {0}")]
95    OperationNotSupported(String),
96
97    /// Invalid parameter
98    #[error("Invalid parameter: {0}")]
99    InvalidParameter(String),
100
101    /// Not implemented
102    #[error("Not implemented: {0}")]
103    NotImplemented(String),
104
105    /// Memory allocation failed
106    #[error("Memory allocation failed: {0}")]
107    MemoryAllocationFailed(String),
108
109    /// Resource exhausted
110    #[error("Resource exhausted: {0}")]
111    ResourceExhausted(String),
112
113    /// Invalid state
114    #[error("Invalid state: {0}")]
115    InvalidState(String),
116
117    /// Backend error
118    #[error("Backend error: {0}")]
119    BackendError(String),
120
121    /// Invalid observable
122    #[error("Invalid observable: {0}")]
123    InvalidObservable(String),
124}
125
126/// Result type for simulator operations
127pub type Result<T> = std::result::Result<T, SimulatorError>;
128
129impl From<scirs2_core::ndarray::ShapeError> for SimulatorError {
130    fn from(err: scirs2_core::ndarray::ShapeError) -> Self {
131        Self::DimensionMismatch(err.to_string())
132    }
133}
134
135impl From<scirs2_core::linalg::LapackError> for SimulatorError {
136    fn from(err: scirs2_core::linalg::LapackError) -> Self {
137        Self::LinalgError(format!("LAPACK error: {err}"))
138    }
139}