Skip to main content

prism_q/
error.rs

1//! Error types for PRISM-Q.
2//!
3//! Parsing and simulation entry points return [`PrismError`] for invalid input.
4//! Construction and accessor APIs panic on API misuse (out-of-bounds indices,
5//! wrong-variant accessors); each such method documents the condition under
6//! `# Panics`. Internal invariants use `debug_assert!` and fire in debug builds.
7
8use thiserror::Error;
9
10/// Top-level error type for PRISM-Q operations.
11#[derive(Debug, Error, Clone, PartialEq)]
12pub enum PrismError {
13    /// OpenQASM parse error with source line number.
14    #[error("parse error at line {line}: {message}")]
15    Parse { line: usize, message: String },
16
17    /// Encountered a valid OpenQASM construct that PRISM-Q v0 does not support.
18    #[error("unsupported construct at line {line}: `{construct}`")]
19    UnsupportedConstruct { construct: String, line: usize },
20
21    /// Qubit index exceeds register size.
22    #[error("invalid qubit index {index} (register size: {register_size})")]
23    InvalidQubit { index: usize, register_size: usize },
24
25    /// Classical bit index exceeds register size.
26    #[error("invalid classical bit index {index} (register size: {register_size})")]
27    InvalidClassicalBit { index: usize, register_size: usize },
28
29    /// Gate applied to wrong number of qubits.
30    #[error("gate `{gate}`: expected {expected} qubit(s), got {got}")]
31    GateArity {
32        gate: String,
33        expected: usize,
34        got: usize,
35    },
36
37    /// Backend does not support the requested operation.
38    #[error("backend `{backend}` does not support: {operation}")]
39    BackendUnsupported { backend: String, operation: String },
40
41    /// Invalid gate parameter (e.g., NaN rotation angle).
42    #[error("invalid parameter: {message}")]
43    InvalidParameter { message: String },
44
45    /// Reference to a register name that was never declared.
46    #[error("undefined register `{name}` at line {line}")]
47    UndefinedRegister { name: String, line: usize },
48
49    /// Circuit holds an instruction with no OpenQASM 3.0 spelling.
50    #[error("cannot export instruction {index} to OpenQASM 3.0: {reason}")]
51    ExportUnsupported { index: usize, reason: String },
52
53    /// Incompatible backend for the given circuit.
54    #[error("backend `{backend}` is incompatible: {reason}")]
55    IncompatibleBackend { backend: String, reason: String },
56}
57
58pub type Result<T> = std::result::Result<T, PrismError>;