-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[Stretch] Support Duration type in classical expressions.
#13844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
af4ba52
e423bf9
0a5917b
a97434d
1afc965
86655f1
aaeae9b
a2a444b
8ac2dc3
9f8313c
db9d9cb
71b7e7a
2091557
7307be9
9b30284
ce1faf1
4fee48f
88ab046
c5a230f
7c88d88
c58a7b8
d9e9a8c
4a56150
23b5961
8bf2e4f
111eb32
a839d51
a19b39a
7f02e56
55af327
86b7af9
9b3c821
69eab91
971cc26
25508bf
2c8ce43
ccf9441
c6eab02
edd7806
8afa92e
4e0f2df
15ba943
81c5833
50c31d3
7e43322
2449ee6
e55e189
1d51022
eb8f150
415f62e
d38f3e9
fd66c1d
e9b6d97
52da3cc
a3be688
07771f1
9332ed3
17241c6
ca91bfd
ad87e78
d3bca5f
50deb93
f1dc1a1
1f439a0
166d7b2
10b2c8d
9d6cf39
8021e00
a15141b
ca2785d
e902009
16475c3
6b5930d
be73180
25f1693
ecc343b
b32e6f0
864506d
6bc728a
2d38178
42258b8
481700a
e09762f
cbd55db
f12afb4
8d059cd
e05d9ff
0f14cca
7db8222
34e615b
bbe67a6
0acc450
7fea216
78b2951
97bbeba
49f2af8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // This code is part of Qiskit. | ||
| // | ||
| // (C) Copyright IBM 2025 | ||
| // | ||
| // This code is licensed under the Apache License, Version 2.0. You may | ||
| // obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| // of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| // | ||
| // Any modifications or derivative works of this code must retain this | ||
| // copyright notice, and modified files need to carry a notice indicating | ||
| // that they have been altered from the originals. | ||
|
|
||
| use pyo3::prelude::*; | ||
| use pyo3::IntoPyObjectExt; | ||
|
|
||
| /// A length of time used to express circuit timing. | ||
| /// | ||
| /// It defines a group of classes which are all subclasses of itself (functionally, an | ||
| /// enumeration carrying data). | ||
| /// | ||
| /// In Python 3.10+, you can use it in a match statement:: | ||
| /// | ||
| /// match duration: | ||
| /// case Duration.dt(dt): | ||
| /// return dt | ||
| /// case Duration.s(seconds): | ||
| /// return seconds / 5e-7 | ||
| /// case _: | ||
| /// raise ValueError("expected dt or seconds") | ||
| /// | ||
| /// And in Python 3.9, you can use :meth:`Duration.unit` to determine which variant | ||
| /// is populated:: | ||
| /// | ||
| /// if duration.unit() == "dt": | ||
| /// return duration.value() | ||
| /// elif duration.unit() == "s": | ||
| /// return duration.value() / 5e-7 | ||
| /// else: | ||
| /// raise ValueError("expected dt or seconds") | ||
| #[pyclass(eq, module = "qiskit._accelerate.circuit")] | ||
| #[derive(PartialEq, Clone, Copy, Debug)] | ||
| #[allow(non_camel_case_types)] | ||
| pub enum Duration { | ||
| dt(i64), | ||
| ns(f64), | ||
| us(f64), | ||
| ms(f64), | ||
| s(f64), | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl Duration { | ||
| /// The corresponding ``unit`` of the duration. | ||
| fn unit(&self) -> &'static str { | ||
| match self { | ||
| Duration::dt(_) => "dt", | ||
| Duration::us(_) => "us", | ||
| Duration::ns(_) => "ns", | ||
| Duration::ms(_) => "ms", | ||
| Duration::s(_) => "s", | ||
| } | ||
| } | ||
|
|
||
| /// The ``value`` of the duration. | ||
| /// | ||
| /// This will be a Python ``int`` if the :meth:`~Duration.unit` is ``"dt"``, | ||
| /// else a ``float``. | ||
| #[pyo3(name = "value")] | ||
| fn py_value(&self, py: Python) -> PyResult<PyObject> { | ||
| match self { | ||
| Duration::dt(v) => v.into_py_any(py), | ||
| Duration::us(v) | Duration::ns(v) | Duration::ms(v) | Duration::s(v) => { | ||
| v.into_py_any(py) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Duration { | ||
| fn __repr__(&self) -> String { | ||
| match self { | ||
| Duration::ns(t) => format!("Duration.ns({})", t), | ||
| Duration::us(t) => format!("Duration.us({})", t), | ||
| Duration::ms(t) => format!("Duration.ms({})", t), | ||
| Duration::s(t) => format!("Duration.s({})", t), | ||
| Duration::dt(t) => format!("Duration.dt({})", t), | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,6 +173,18 @@ class BitType(ClassicalType): | |
| __slots__ = () | ||
|
|
||
|
|
||
| class DurationType(ClassicalType): | ||
| """Type information for a duration.""" | ||
|
|
||
| __slots__ = () | ||
|
|
||
|
|
||
| class StretchType(ClassicalType): | ||
| """Type information for a stretch.""" | ||
|
|
||
| __slots__ = () | ||
|
|
||
|
|
||
|
Comment on lines
+182
to
+187
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just semantics: my reading was we give-or-take decided to interpret the OQ3 spec as saying that class StretchDeclaration(Statement):
identifier: Identifier
bound: Optional[Expr]rather than a type?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah, that makes a ton more sense. We don't use this in this PR anyway, so if it's alright, I'll leave it for now and make the switch in the PR that actually does. |
||
| class BitArrayType(ClassicalType): | ||
| """Type information for a sized number of classical bits.""" | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.