Skip to main content

quantrs2_circuit/qasm/
error.rs

1//! Error types for OpenQASM 2.0 import/export
2
3use thiserror::Error;
4
5/// Errors that can occur during QASM 2.0 operations
6#[derive(Debug, Error)]
7#[non_exhaustive]
8pub enum QasmError {
9    /// The gate is not supported in QASM 2.0
10    #[error("Unsupported gate: {0}")]
11    UnsupportedGate(String),
12
13    /// The QASM input is syntactically invalid
14    #[error("Parse error at line {line}: {message}")]
15    ParseError {
16        /// Line number where the error occurred (1-indexed)
17        line: usize,
18        /// Human-readable error description
19        message: String,
20    },
21
22    /// A register referenced in the QASM was not declared
23    #[error("Undefined register: {0}")]
24    UndefinedRegister(String),
25
26    /// A qubit index is out of range for its register
27    #[error("Qubit index {index} out of range for register '{register}' (size {size})")]
28    QubitIndexOutOfRange {
29        /// Register name
30        register: String,
31        /// Attempted index
32        index: usize,
33        /// Register size
34        size: usize,
35    },
36
37    /// An invalid or unsupported gate parameter was encountered
38    #[error("Invalid parameter in gate '{gate}': {message}")]
39    InvalidParameter {
40        /// Gate name
41        gate: String,
42        /// Error description
43        message: String,
44    },
45
46    /// Wrong number of parameters for a gate
47    #[error("Gate '{gate}' expects {expected} parameter(s), got {actual}")]
48    WrongParameterCount {
49        /// Gate name
50        gate: String,
51        /// Expected count
52        expected: usize,
53        /// Actual count
54        actual: usize,
55    },
56
57    /// Wrong number of qubits for a gate
58    #[error("Gate '{gate}' expects {expected} qubit(s), got {actual}")]
59    WrongQubitCount {
60        /// Gate name
61        gate: String,
62        /// Expected count
63        expected: usize,
64        /// Actual count
65        actual: usize,
66    },
67
68    /// A string formatting error during export
69    #[error("Format error: {0}")]
70    FormatError(#[from] std::fmt::Error),
71
72    /// No qubits in circuit (cannot determine register size)
73    #[error("Circuit has no qubits")]
74    EmptyCircuit,
75
76    /// An expression in the QASM could not be evaluated
77    #[error("Cannot evaluate expression '{0}'")]
78    ExpressionError(String),
79}
80
81impl QasmError {
82    /// Create a parse error with position information
83    pub fn parse(line: usize, message: impl Into<String>) -> Self {
84        Self::ParseError {
85            line,
86            message: message.into(),
87        }
88    }
89}