1111// that they have been altered from the originals.
1212
1313use pyo3:: prelude:: * ;
14+ use pyo3:: IntoPyObjectExt ;
1415
15- #[ pyclass( eq, module = "qiskit._accelerate.circuit" ) ]
16- #[ derive( PartialEq , Clone , Copy , Debug ) ]
17- #[ allow( non_camel_case_types) ]
1816/// A length of time used to express circuit timing.
1917///
2018/// It defines a group of classes which are all subclasses of itself (functionally, an
@@ -30,23 +28,54 @@ use pyo3::prelude::*;
3028/// case _:
3129/// raise ValueError("expected dt or seconds")
3230///
33- /// And in Python 3.9, you can use ``isinstance` ` to determine which variant
31+ /// And in Python 3.9, you can use :meth:`Duration.unit ` to determine which variant
3432/// is populated::
3533///
36- /// if isinstance( duration, Duration.dt) :
37- /// return duration[0]
38- /// elif isinstance( duration, Duration.s) :
39- /// return duration[0] / 5e-7
34+ /// if duration.unit() == "dt" :
35+ /// return duration.value()
36+ /// elif duration.unit() == "s" :
37+ /// return duration.value() / 5e-7
4038/// else:
4139/// 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) ]
4243pub enum Duration {
43- dt( u64 ) ,
44+ dt( i64 ) ,
4445 ns( f64 ) ,
4546 us( f64 ) ,
4647 ms( f64 ) ,
4748 s( f64 ) ,
4849}
4950
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+
5079impl Duration {
5180 fn __repr__ ( & self ) -> String {
5281 match self {
0 commit comments