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

Qiskit / qiskit / 18901832664

29 Oct 2025 08:36AM UTC coverage: 88.189% (+0.002%) from 88.187%
18901832664

Pull #15264

github

web-flow
Merge da0ae41ba into 046aba2ff
Pull Request #15264: Docker-based QPY compatibility tests

93673 of 106219 relevant lines covered (88.19%)

1155761.55 hits per line

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

97.87
/crates/circuit/src/classical/expr/value.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 http://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 crate::classical::expr::{ExprKind, PyExpr};
14
use crate::classical::types::Type;
15
use crate::duration::Duration;
16
use pyo3::prelude::*;
17
use pyo3::types::PyTuple;
18
use pyo3::{IntoPyObjectExt, intern};
19

20
/// A single scalar value expression.
21
#[derive(Clone, Debug, PartialEq)]
22
pub enum Value {
23
    Duration(Duration),
24
    Float { raw: f64, ty: Type },
25
    Uint { raw: u64, ty: Type },
26
}
27

28
impl<'py> IntoPyObject<'py> for Value {
29
    type Target = PyAny;
30
    type Output = Bound<'py, PyAny>;
31
    type Error = PyErr;
32

33
    fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
3,634✔
34
        Ok(Bound::new(py, (PyValue(self), PyExpr(ExprKind::Value)))?.into_any())
3,634✔
35
    }
3,634✔
36
}
37

38
impl<'a, 'py> FromPyObject<'a, 'py> for Value {
39
    type Error = <PyValue as FromPyObject<'a, 'py>>::Error;
40

41
    fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
5,030✔
42
        let PyValue(v) = ob.extract()?;
5,030✔
43
        Ok(v)
5,030✔
44
    }
5,030✔
45
}
46

47
/// A single scalar value.
48
#[pyclass(eq, extends = PyExpr, name = "Value", module = "qiskit._accelerate.circuit.classical.expr")]
49
#[derive(PartialEq, Clone, Debug)]
50
pub struct PyValue(Value);
51

52
#[pymethods]
×
53
impl PyValue {
54
    #[new]
55
    #[pyo3(text_signature = "(value, type)")]
56
    fn new(py: Python, value: Bound<PyAny>, ty: Type) -> PyResult<Py<Self>> {
6,468✔
57
        let value = if let Ok(raw) = value.extract::<u64>() {
6,468✔
58
            Value::Uint { raw, ty }
4,656✔
59
        } else if let Ok(raw) = value.extract::<f64>() {
1,812✔
60
            Value::Float { raw, ty }
832✔
61
        } else {
62
            Value::Duration(value.extract()?)
980✔
63
        };
64
        Py::new(py, (PyValue(value), PyExpr(ExprKind::Value)))
6,468✔
65
    }
6,468✔
66

67
    #[getter]
68
    fn get_value(&self, py: Python) -> PyResult<Py<PyAny>> {
6,256✔
69
        match self.0 {
6,256✔
70
            Value::Duration(d) => d.into_py_any(py),
1,628✔
71
            Value::Float { raw, .. } => raw.into_py_any(py),
816✔
72
            Value::Uint { raw, .. } => raw.into_py_any(py),
3,812✔
73
        }
74
    }
6,256✔
75

76
    #[getter]
77
    fn get_const(&self) -> bool {
164✔
78
        true
164✔
79
    }
164✔
80

81
    #[getter]
82
    fn get_type(&self, py: Python) -> PyResult<Py<PyAny>> {
13,342✔
83
        match self.0 {
13,342✔
84
            Value::Duration(_) => Type::Duration.into_py_any(py),
3,170✔
85
            Value::Float { ty, .. } | Value::Uint { ty, .. } => ty.into_py_any(py),
10,172✔
86
        }
87
    }
13,342✔
88

89
    fn accept<'py>(
3,630✔
90
        slf: PyRef<'py, Self>,
3,630✔
91
        visitor: &Bound<'py, PyAny>,
3,630✔
92
    ) -> PyResult<Bound<'py, PyAny>> {
3,630✔
93
        visitor.call_method1(intern!(visitor.py(), "visit_value"), (slf,))
3,630✔
94
    }
3,630✔
95

96
    fn __reduce__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
2,582✔
97
        (
98
            py.get_type::<Self>(),
2,582✔
99
            (self.get_value(py)?, self.get_type(py)?),
2,582✔
100
        )
101
            .into_pyobject(py)
2,582✔
102
    }
2,582✔
103

104
    fn __repr__(&self, py: Python) -> PyResult<String> {
8✔
105
        Ok(format!(
8✔
106
            "Value({}, {})",
8✔
107
            self.get_value(py)?.bind(py).repr()?,
8✔
108
            self.get_type(py)?.bind(py).repr()?,
8✔
109
        ))
110
    }
8✔
111
}
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