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] Add stretch variable support to QuantumCircuit. #13852

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

Conversation

kevinhartman
Copy link
Contributor

@kevinhartman kevinhartman commented Feb 16, 2025

Summary

Adds support for adding new uninitialized stretch variables to a circuit via QuantumCircuit::add_stretch. The returned variable follows the same scoping rules as other classical circuit variables. Stretch variables can be used to initialize other stretch variables as well in an expression via the existing QuantumCircuit::add_var method. EDIT: we decided to punt on this, since it'd likely require inline initializers when writing to QASM, which isn't something we've done before, so the functionality has been removed.

Details and comments

Based on #13850. See the readable diff here.

  • Uninitialized stretch variables (the only kind we're supporting for now) are tracked the same way as other circuit variables. We make a special case for them in QuantumCircuit.add_uninitialized_var to be allowed even though types.Stretch is always const (...which is normally blocked to prevent users from creating uninitialized constants).
  • Stretches and durations are not allowed as QuantumCircuit "input" variables, since we don't allow const input variables (this can be relaxed in the future, if needed).
  • We can now declare stretch variables and have those be emitted to QASM, but we don't be able to do anything with them until Delay and Box start using them in their duration expressions.

To-do

  • 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.
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).
We decided to punt on the idea of assigning const-typed variables,
so we don't support declaring stretch expressions via add_var or
the 'declarations' argument to QuantumCircuit. We also don't
allow const 'inputs'.
At the moment, we track stretches as variables, but these
will eventually make their way here during circuit copy
etc., so we need to support them even though they're
const.
@kevinhartman kevinhartman marked this pull request as ready for review February 19, 2025 22:19
@kevinhartman kevinhartman requested a review from a team as a code owner February 19, 2025 22:19
@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