|
| 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 pyo3::prelude::*; |
| 14 | +use pyo3::IntoPyObjectExt; |
| 15 | + |
| 16 | +/// A length of time used to express circuit timing. |
| 17 | +/// |
| 18 | +/// It defines a group of classes which are all subclasses of itself (functionally, an |
| 19 | +/// enumeration carrying data). |
| 20 | +/// |
| 21 | +/// In Python 3.10+, you can use it in a match statement:: |
| 22 | +/// |
| 23 | +/// match duration: |
| 24 | +/// case Duration.dt(dt): |
| 25 | +/// return dt |
| 26 | +/// case Duration.s(seconds): |
| 27 | +/// return seconds / 5e-7 |
| 28 | +/// case _: |
| 29 | +/// raise ValueError("expected dt or seconds") |
| 30 | +/// |
| 31 | +/// And in Python 3.9, you can use :meth:`Duration.unit` to determine which variant |
| 32 | +/// is populated:: |
| 33 | +/// |
| 34 | +/// if duration.unit() == "dt": |
| 35 | +/// return duration.value() |
| 36 | +/// elif duration.unit() == "s": |
| 37 | +/// return duration.value() / 5e-7 |
| 38 | +/// else: |
| 39 | +/// raise ValueError("expected dt or seconds") |
| 40 | +#[pyclass(eq, module = "qiskit._accelerate.circuit")] |
| 41 | +#[derive(PartialEq, Clone, Copy, Debug)] |
| 42 | +#[allow(non_camel_case_types)] |
| 43 | +pub enum Duration { |
| 44 | + dt(i64), |
| 45 | + ns(f64), |
| 46 | + us(f64), |
| 47 | + ms(f64), |
| 48 | + s(f64), |
| 49 | +} |
| 50 | + |
| 51 | +#[pymethods] |
| 52 | +impl Duration { |
| 53 | + /// The corresponding ``unit`` of the duration. |
| 54 | + fn unit(&self) -> &'static str { |
| 55 | + match self { |
| 56 | + Duration::dt(_) => "dt", |
| 57 | + Duration::us(_) => "us", |
| 58 | + Duration::ns(_) => "ns", |
| 59 | + Duration::ms(_) => "ms", |
| 60 | + Duration::s(_) => "s", |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// The ``value`` of the duration. |
| 65 | + /// |
| 66 | + /// This will be a Python ``int`` if the :meth:`~Duration.unit` is ``"dt"``, |
| 67 | + /// else a ``float``. |
| 68 | + #[pyo3(name = "value")] |
| 69 | + fn py_value(&self, py: Python) -> PyResult<PyObject> { |
| 70 | + match self { |
| 71 | + Duration::dt(v) => v.into_py_any(py), |
| 72 | + Duration::us(v) | Duration::ns(v) | Duration::ms(v) | Duration::s(v) => { |
| 73 | + v.into_py_any(py) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Duration { |
| 80 | + fn __repr__(&self) -> String { |
| 81 | + match self { |
| 82 | + Duration::ns(t) => format!("Duration.ns({})", t), |
| 83 | + Duration::us(t) => format!("Duration.us({})", t), |
| 84 | + Duration::ms(t) => format!("Duration.ms({})", t), |
| 85 | + Duration::s(t) => format!("Duration.s({})", t), |
| 86 | + Duration::dt(t) => format!("Duration.dt({})", t), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments