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    #[error("Gradient calculation is not enabled in the process")]
57    GradientDisabled,
58
59    #[error("Cannot add control qubits to a gate with gradient calculation")]
60    ControlledParameter,
61
62    #[error("Fail to uncompute auxiliary qubit")]
63    UncomputeFaill,
64
65    #[error("The qubit is not part of the interaction group of the auxiliary qubit")]
66    NoInInteractionGroup,
67}
68
69pub type Result<T> = std::result::Result<T, KetError>;
70
71impl KetError {
72    pub fn error_code(&self) -> i32 {
73        *self as i32
74    }
75
76    /// # Safety
77    pub unsafe fn from_error_code(error_code: i32) -> KetError {
78        unsafe { std::mem::transmute(error_code) }
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::KetError;
85
86    #[test]
87    fn success_is_zero() {
88        assert!(KetError::Success.error_code() == 0)
89    }
90}