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

Add representation of box #13869

Merged
merged 4 commits into from
Mar 6, 2025
Merged

Add representation of box #13869

merged 4 commits into from
Mar 6, 2025

Conversation

jakelishman
Copy link
Member

@jakelishman jakelishman commented Feb 18, 2025

This adds in the base box control-flow construction, with support for containing instructions and having a literal delay, like the Delay instruction.

This supports basic output to OpenQASM 3, QPY and some rudimentary support in the text and mpl drawers. The transpiler largely handles things already, since control flow is handled generically in most places.

Known issues:

  • We expect this to be able to accept stretches in its duration, just as Delay can, which will need a follow-up.
  • We expect Box to support "annotations" in a future release of Qiskit.
  • There is currently no way in OpenQASM 3 to represent a qubit that is idle during a box without inserting a magic instruction on it.
  • IBM backends don't claim support for box yet, so transpile against a backend will fail, though you can modify the Target to add the instruction manually.

Summary

Details and comments

No tests yet, but I tested various things casually - this is just the WIP to show what's going on.

Close #13772.

@jakelishman jakelishman added Changelog: New Feature Include in the "Added" section of the changelog mod: transpiler Issues and PRs related to Transpiler labels Feb 18, 2025
@jakelishman jakelishman added this to the 2.0.0 milestone Feb 18, 2025
@jakelishman jakelishman requested review from nonhermitian and a team as code owners February 18, 2025 15:28
@qiskit-bot
Copy link
Collaborator

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

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

@jakelishman jakelishman self-assigned this Feb 18, 2025
@coveralls
Copy link

coveralls commented Feb 18, 2025

Pull Request Test Coverage Report for Build 13687126255

Details

  • 111 of 118 (94.07%) changed or added relevant lines in 11 files are covered.
  • 6 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.01%) to 87.023%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/controlflow/box.py 50 51 98.04%
qiskit/visualization/circuit/matplotlib.py 0 6 0.0%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 94.49%
crates/qasm2/src/lex.rs 5 92.23%
Totals Coverage Status
Change from base Build 13683975981: 0.01%
Covered Lines: 76192
Relevant Lines: 87554

💛 - Coveralls

This adds in the base `box` control-flow construction, with support for
containing instructions and having a literal delay, like the `Delay`
instruction.

This supports basic output to OpenQASM 3, QPY and some rudimentary
support in the text and mpl drawers.  The transpiler largely handles
things already, since control flow is handled generically in most
places.

Known issues:

- We expect this to be able to accept stretches in its duration, just as
  `Delay` can, which will need a follow-up.
- We expect `Box` to support "annotations" in a future release of
  Qiskit.
- There is currently no way in OpenQASM 3 to represent a qubit that is
  idle during a `box` without inserting a magic instruction on it.
- IBM backends don't claim support for `box` yet, so `transpile` against
  a backend will fail, though you can modify the `Target` to add the
  instruction manually.

Add tests of box
@jakelishman jakelishman marked this pull request as ready for review February 27, 2025 23:06
@qiskit-bot
Copy link
Collaborator

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

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

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lgtm thanks for doing this. It's all pretty straightforward. I just had a small comment/question inline about having a setter for params. But other than that I think this is good to merge.

Comment on lines +64 to +85
@params.setter
def params(self, parameters):
# pylint: disable=cyclic-import
from qiskit.circuit import QuantumCircuit

(body,) = parameters

if not isinstance(body, QuantumCircuit):
raise CircuitError(
"BoxOp expects a body parameter of type "
f"QuantumCircuit, but received {type(body)}."
)

if body.num_qubits != self.num_qubits or body.num_clbits != self.num_clbits:
raise CircuitError(
"Attempted to assign a body parameter with a num_qubits or "
"num_clbits different than that of the BoxOp. "
f"BoxOp num_qubits/clbits: {self.num_qubits}/{self.num_clbits} "
f"Supplied body num_qubits/clbits: {body.num_qubits}/{body.num_clbits}."
)

self._params = [body]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to allow inplace mutation like this? I guess it's a required part of the data model, it just seems like playing with fire to support it given how much trouble we had with this on gates.

But this also potentially problematic when we migrate control flow to rust.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, unfortunately I just copied it from the other control-flow ops - we're already in trouble.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was thinking we don't need to continue the trend here. Or raise a NotImplementedError if it is required. But this is a reminder that we need to add the docs about not supporting mutating Instruction subclasses after inserting them into a circuit. We've done it piecemeal as we moved things into rust, but we meant to document it in 2.0 as not allowed for anything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather be consistent with all control-flow ops, and not add new restrictions now - I wouldn't want to bet it's not used by the Instruction init or even some other library code.

Copy link
Member Author

@jakelishman jakelishman Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In retrospect: this needs to be settable so QuantumCircuit.assign_parameters works (or maybe we only mutate the list in-place? Can't remember.)

super().__init__("box", body.num_qubits, body.num_clbits, [body], label=label)
self.duration = duration
self.unit = unit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be opposed to yet another alias to the blocks? But specifically, to get the only element.

@property
def content(self):
    return self.params[0]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1367570. I called it body, though, if not only to match the variable terminology used in the class.

@mtreinish mtreinish enabled auto-merge March 5, 2025 23:10
@mtreinish mtreinish added this pull request to the merge queue Mar 6, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 6, 2025
@mtreinish mtreinish added this pull request to the merge queue Mar 6, 2025
Merged via the queue into Qiskit:main with commit b77a822 Mar 6, 2025
20 checks passed
@jakelishman jakelishman deleted the box/repr branch March 6, 2025 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: transpiler Issues and PRs related to Transpiler priority: high
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add box scope
6 participants