Skip to main content

prism_q/
error.rs

1//! Error types for PRISM-Q.
2//!
3//! All public APIs return structured errors. User-facing operations never panic.
4//! Internal debug_assert! may fire in debug builds for invariant violations.
5
6use thiserror::Error;
7
8/// Top-level error type for PRISM-Q operations.
9#[derive(Debug, Error, Clone, PartialEq)]
10pub enum PrismError {
11    /// OpenQASM parse error with source line number.
12    #[error("parse error at line {line}: {message}")]
13    Parse { line: usize, message: String },
14
15    /// Encountered a valid OpenQASM construct that PRISM-Q v0 does not support.
16    #[error("unsupported construct at line {line}: `{construct}`")]
17    UnsupportedConstruct { construct: String, line: usize },
18
19    /// Qubit index exceeds register size.
20    #[error("invalid qubit index {index} (register size: {register_size})")]
21    InvalidQubit { index: usize, register_size: usize },
22
23    /// Classical bit index exceeds register size.
24    #[error("invalid classical bit index {index} (register size: {register_size})")]
25    InvalidClassicalBit { index: usize, register_size: usize },
26
27    /// Gate applied to wrong number of qubits.
28    #[error("gate `{gate}`: expected {expected} qubit(s), got {got}")]
29    GateArity {
30        gate: String,
31        expected: usize,
32        got: usize,
33    },
34
35    /// Backend does not support the requested operation.
36    #[error("backend `{backend}` does not support: {operation}")]
37    BackendUnsupported { backend: String, operation: String },
38
39    /// Invalid gate parameter (e.g., NaN rotation angle).
40    #[error("invalid parameter: {message}")]
41    InvalidParameter { message: String },
42
43    /// Reference to a register name that was never declared.
44    #[error("undefined register `{name}` at line {line}")]
45    UndefinedRegister { name: String, line: usize },
46
47    /// Incompatible backend for the given circuit.
48    #[error("backend `{backend}` is incompatible: {reason}")]
49    IncompatibleBackend { backend: String, reason: String },
50}
51
52/// Convenience alias used throughout PRISM-Q.
53pub type Result<T> = std::result::Result<T, PrismError>;