ket/
error.rs

1// SPDX-FileCopyrightText: 2024 Evandro Chagas Ribeiro da Rosa <evandro@quantuloop.com>
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#[derive(thiserror::Error, Debug, Clone, Copy)]
6#[repr(i32)]
7pub enum KetError {
8    #[error("No error occurred")]
9    Success,
10
11    #[error("The maximum number of qubits has been reached")]
12    MaxQubitsReached,
13
14    #[error("Cannot append instruction to a terminated process")]
15    TerminatedProcess,
16
17    #[error("Cannot append non-gate instructions within an inverse scope")]
18    InverseScope,
19
20    #[error("Cannot append non-gate instructions within a controlled scope")]
21    ControlledScope,
22
23    #[error("Qubit is not available for measurement or gate application")]
24    QubitUnavailable,
25
26    #[error("A qubit cannot be both control and target in the same instruction")]
27    ControlTargetOverlap,
28
29    #[error("A qubit cannot be used as a control qubit more than once")]
30    ControlTwice,
31
32    #[error("A measurement feature (measure, sample, exp_value, or dump) is disabled")]
33    MeasurementDisabled,
34
35    #[error("No qubits are available in the current control stack to pop")]
36    ControlStackEmpty,
37
38    #[error("No inverse scope is available to end")]
39    InverseScopeEmpty,
40
41    #[error("The provided data does not match the expected number of results")]
42    ResultDataMismatch,
43
44    #[error("Ending a non-empty control stack is not allowed")]
45    ControlStackNotEmpty,
46
47    #[error("Cannot end the primary control stack")]
48    ControlStackRemovePrimary,
49
50    #[error("No auxiliary qubit available to free")]
51    AuxQubitNotAvailable,
52
53    #[error("Operation not allowed in auxiliary qubits")]
54    AuxQubitNotAllowed,
55}
56
57pub type Result<T> = std::result::Result<T, KetError>;
58
59impl KetError {
60    pub fn error_code(&self) -> i32 {
61        *self as i32
62    }
63
64    /// # Safety
65    pub unsafe fn from_error_code(error_code: i32) -> KetError {
66        unsafe { std::mem::transmute(error_code) }
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::KetError;
73
74    #[test]
75    fn success_is_zero() {
76        assert!(KetError::Success.error_code() == 0)
77    }
78}