• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Qiskit / qiskit / 23304525254

19 Mar 2026 04:09PM UTC coverage: 87.346% (+0.04%) from 87.309%
23304525254

Pull #15361

github

web-flow
Merge 52d8c7053 into 13b23a355
Pull Request #15361: Add `qk_circuit_draw` function to the C API

15 of 17 new or added lines in 2 files covered. (88.24%)

102 existing lines in 7 files now uncovered.

103313 of 118280 relevant lines covered (87.35%)

1137052.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.83
/crates/qpy/src/error.rs
1
// This code is part of Qiskit.
2
//
3
// (C) Copyright IBM 2025
4
//
5
// This code is licensed under the Apache License, Version 2.0. You may
6
// obtain a copy of this license in the LICENSE.txt file in the root directory
7
// of this source tree or at https://www.apache.org/licenses/LICENSE-2.0.
8
//
9
// Any modifications or derivative works of this code must retain this
10
// copyright notice, and modified files need to carry a notice indicating
11
// that they have been altered from the originals.
12

13
use pyo3::PyErr;
14
use thiserror::Error;
15

16
mod py_exceptions {
17
    use pyo3::import_exception;
18
    import_exception!(qiskit.qpy.exceptions, UnsupportedFeatureForVersion);
19
    import_exception!(qiskit.qpy.exceptions, QpyError);
20
}
21

22
use py_exceptions::QpyError as PyQpyError;
23
use py_exceptions::UnsupportedFeatureForVersion as PyUnsupportedFeatureForVersion;
24

25
// The initial version of the QpyError enum and its helper functions were created with Bob 0.14 (probably using Claude Sonnet 3.5)
26

27
/// Errors that can occur during QPY serialization and deserialization operations.
28
///
29
/// This error type is used internally within the QPY module. It is converted to
30
/// Python exceptions only at the boundary when returning to Python space.
31
#[derive(Error, Debug)]
32
pub enum QpyError {
33
    /// An unsupported feature was encountered for the target QPY version
34
    #[error(
35
        "'{feature}' is not supported in QPY version {version}. Minimum required version is {min_version}"
36
    )]
37
    UnsupportedFeatureForVersion {
38
        feature: String,
39
        version: u32,
40
        min_version: u32,
41
    },
42

43
    /// Failure to cast a python object into a specific type
44
    #[error("could not cast {name} into {python_type}")]
45
    InvalidPythonType { python_type: String, name: String },
46

47
    /// Invalid value type encountered during serialization/deserialization
48
    #[error("invalid value type: expected {expected}, got {actual}")]
49
    InvalidValueType { expected: String, actual: String },
50

51
    /// Failed to serialize data
52
    #[error("serialization failed: {0}")]
53
    SerializationError(String),
54

55
    /// Failed to deserialize data
56
    #[error("deserialization failed: {0}")]
57
    DeserializationError(String),
58

59
    /// Invalid QPY format or corrupted data
60
    #[error("invalid QPY format: {0}")]
61
    InvalidFormat(String),
62

63
    /// Missing required data
64
    #[error("missing required data: {0}")]
65
    MissingData(String),
66

67
    /// Invalid instruction or operation
68
    #[error("invalid instruction: {0}")]
69
    InvalidInstruction(String),
70

71
    /// Invalid parameter
72
    #[error("invalid parameter: {0}")]
73
    InvalidParameter(String),
74

75
    /// Invalid register reference
76
    #[error("invalid register: {0}")]
77
    InvalidRegister(String),
78

79
    /// Invalid bit reference
80
    #[error("invalid bit reference: {0}")]
81
    InvalidBit(String),
82

83
    /// Type conversion error
84
    #[error("type conversion failed: {0}")]
85
    ConversionError(String),
86

87
    /// Circuit data error
88
    #[error("circuit data error: {0}")]
89
    CircuitError(String),
90

91
    /// Custom instruction not found
92
    #[error("custom instruction '{0}' not found")]
93
    CustomInstructionNotFound(String),
94

95
    /// Invalid annotation
96
    #[error("invalid annotation: {0}")]
97
    InvalidAnnotation(String),
98

99
    /// Invalid expression
100
    #[error("invalid expression: {0}")]
101
    InvalidExpression(String),
102

103
    /// Invalid layout
104
    #[error("invalid layout: {0}")]
105
    InvalidLayout(String),
106

107
    /// Metadata serialization/deserialization error
108
    #[error("metadata error: {0}")]
109
    MetadataError(String),
110

111
    /// I/O error during file operations
112
    #[error(transparent)]
113
    IoError(#[from] std::io::Error),
114

115
    /// UTF-8 conversion error
116
    #[error(transparent)]
117
    Utf8Error(#[from] std::str::Utf8Error),
118

119
    /// Integer conversion error
120
    #[error(transparent)]
121
    IntConversionError(#[from] std::num::TryFromIntError),
122

123
    /// Circuit data error from the circuit module
124
    #[error(transparent)]
125
    CircuitDataError(#[from] qiskit_circuit::circuit_data::CircuitDataError),
126

127
    /// Parameter error from the circuit module
128
    #[error(transparent)]
129
    ParameterError(#[from] qiskit_circuit::parameter::parameter_expression::ParameterError),
130

131
    /// BinRW parsing error
132
    #[error("binary parsing error: {0}")]
133
    BinRwError(String),
134

135
    /// Something went wrong in annotation handling.
136
    #[error("annotation-handling error: {0}")]
137
    AnnotationError(String),
138

139
    /// Python error that occurred during a Python call
140
    #[error("Python error: {0}")]
141
    PythonError(#[from] PyErr),
142
}
143

144
impl From<QpyError> for PyErr {
145
    fn from(error: QpyError) -> Self {
4✔
146
        match error {
4✔
147
            QpyError::UnsupportedFeatureForVersion {
148
                feature,
×
149
                version,
×
UNCOV
150
                min_version,
×
151
            } => PyUnsupportedFeatureForVersion::new_err((feature, min_version, version)),
×
152
            QpyError::PythonError(py_err) => py_err,
2✔
153
            _ => PyQpyError::new_err(error.to_string()),
2✔
154
        }
155
    }
4✔
156
}
157

158
impl From<binrw::Error> for QpyError {
UNCOV
159
    fn from(err: binrw::Error) -> Self {
×
UNCOV
160
        QpyError::BinRwError(err.to_string())
×
161
    }
×
162
}
163

164
/// Helper function to convert a binrw error to QpyError for use in binrw parsing
165
pub fn to_binrw_error<W: std::io::Seek, E: std::error::Error + Send + Sync + 'static>(
×
166
    writer: &mut W,
×
167
    err: E,
×
168
) -> binrw::Error {
×
169
    binrw::Error::Custom {
×
UNCOV
170
        pos: writer.stream_position().unwrap_or(0),
×
UNCOV
171
        err: Box::new(err),
×
172
    }
×
173
}
×
174

175
/// Helper function to convert binrw errors back to QpyError
UNCOV
176
pub fn from_binrw_error(err: binrw::Error) -> QpyError {
×
UNCOV
177
    QpyError::BinRwError(err.to_string())
×
UNCOV
178
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc