1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! The crate-wide error type.
use std::fmt;
use crate::qubit::{ClassicalBit, QubitId};
/// Errors returned when building or running a circuit.
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Error {
/// A qubit index referenced a wire outside the register.
QubitOutOfRange {
/// The offending qubit index.
qubit: QubitId,
/// The number of qubits in the register.
num_qubits: usize,
},
/// A classical-bit index referenced a bit outside the classical register.
ClassicalBitOutOfRange {
/// The offending classical-bit index.
bit: ClassicalBit,
/// The number of classical bits in the register.
num_classical: usize,
},
/// A gate was given the same qubit for two distinct operands.
DuplicateQubit {
/// The repeated qubit index.
qubit: QubitId,
},
/// An amplitude buffer length was not a power of two, or did not match the
/// declared qubit count.
DimensionMismatch {
/// The number of amplitudes provided.
len: usize,
/// The number of amplitudes expected (`2^n`).
expected: usize,
},
/// A circuit run on the stabilizer backend used a gate outside the Clifford
/// group, which that backend cannot simulate.
NonClifford {
/// A human-readable name for the offending gate.
gate: &'static str,
},
/// A parse or emit error from the `OpenQASM` 3 interface.
Qasm {
/// Source line (1-indexed; 0 if not applicable).
line: usize,
/// Source column (1-indexed; 0 if not applicable).
col: usize,
/// Human-readable description.
message: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::QubitOutOfRange { qubit, num_qubits } => {
write!(
f,
"qubit {qubit} out of range for {num_qubits}-qubit register"
)
}
Self::ClassicalBitOutOfRange { bit, num_classical } => {
write!(
f,
"classical bit {bit} out of range for {num_classical}-bit register"
)
}
Self::DuplicateQubit { qubit } => {
write!(f, "qubit {qubit} used more than once in a single gate")
}
Self::DimensionMismatch { len, expected } => {
write!(f, "amplitude buffer has length {len}, expected {expected}")
}
Self::NonClifford { gate } => {
write!(
f,
"{gate} is not in the Clifford group; the stabilizer backend cannot simulate it"
)
}
Self::Qasm { line, col, message } => {
write!(f, "OpenQASM 3 error at {line}:{col}: {message}")
}
}
}
}
impl std::error::Error for Error {}
/// A `Result` whose error is this crate's [`Error`].
pub type Result<T> = std::result::Result<T, Error>;