Skip to main content

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