Skip to content
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

[Stretch] Support Duration and Stretch types in classical expressions. #13844

Open
wants to merge 60 commits into
base: main
Choose a base branch
from

Conversation

kevinhartman
Copy link
Contributor

@kevinhartman kevinhartman commented Feb 14, 2025

Summary

Adds two new types Duration and Stretch to the classical expression type system, as well as a new (and separate) core Duration type to qiskit.circuit.

Details and comments

Based on #13832. Here's a readable diff in the meantime.

Core type

The new core type Duration is a pyclass implemented in Rust as an enum type. For now, it's got variants for each of the supported timing kinds defined in OpenQASM (ns, us, ms, s, dt). All of these are f64s, except for dt which is a u64.

Equality of Duration currently follows the same choices made by #13816, in that durations are not first brought to the same unit and compared for magnitude. Perhaps in the future, we can consider equality between walltimes in different units if that feels useful.

Expression types

Like in QASM, the Duration type represents a known length of time, possibly negative. It is separate from Stretch to support things like Duration / Duration => Float. It is always const. These are implicitly convertible to Stretch, and just simply won't have an unknown component to them.

A Stretch type represents a not-yet-known length of time. The plan is to use these in Var.new expressions. They are always const as well, so they introduce the first use of a const-typed Var expression in Qiskit (which will be made not to emit a Store instruction when added to a circuit).

To-do

  • Decide on Duration representation and expression type (Value or Var).
  • Decide on Duration equality.
  • Support QPY and QASM.
  • QPY testing.
  • QASM testing.
  • Update QPY description.
  • Release note.

Types that have some natural order no longer have an ordering
when one of them is strictly greater but has an incompatible
const-ness (i.e. when the greater type is const but the other
type is not).
We need to reject types with const=True in QPY until it supports them.

For now, I've also made the Index and shift operator constructors
lift their RHS to the same const-ness as the target to make it
less likely that existing users of expr run into issues when
serializing to older QPY versions.
This is probably a better default in general, since we
don't really have much use for const except for timing
stuff.
Since we're going for using a Cast node when const-ness
differs, this will be fine.
I wasn't going to have this, but since we have DANGEROUS
Float => Int, and we have Int => Bool, I think this makes the
most sense.
A Stretch can always represent a Duration (it's just an expression
without any unresolved stretch variables, in this case), so we
allow implicit conversion from Duration => Stretch.

The reason for a separate Duration type is to support things like
Duration / Duration => Float. This is not valid for stretches in
OpenQASM (to my knowledge).
Also adds support to expr.lift to create a value expression of
type types.Duration from an instance of qiskit.circuit.Duration.
Also removes the assumption that a const-type can never be an l-value
in favor of just restricting l-values with const types from being
added to circuits for now.

We will (in a separate PR) add support for adding stretch variables
to circuits, which are const. However, we may track those
differently, or at least not report them as variable when users
query the circuit for variables.
This one I'd added thinking I ought to block store from using
a const var target. But since I figured it's better to just
restrict adding vars to the circuit that are const (and leave
the decision of whether or not a const var can be an l-value
till later), this test no longer makes sense.
I can't really test Stretch yet since we can't add them to circuits
until a later PR.
The best I can do to test these right now (before Delay
is made to accept timing expressions) is to use them in
comparison operations (will be in a follow up commit).
@kevinhartman kevinhartman marked this pull request as ready for review February 19, 2025 05:48
@kevinhartman kevinhartman requested a review from a team as a code owner February 19, 2025 05:48
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @mtreinish
  • @nkanazawa1989

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on hold Can not fix yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants