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 Expr support to the control-flow builders #10400

Merged
merged 2 commits into from
Jul 19, 2023

Conversation

jakelishman
Copy link
Member

Summary

This is generally relatively straightforwards; anywhere where we examine the resources used by an operation, we need to update to account for classical resources potentially being tied up in ControlFlowOp fields in Expr nodes as well.

This also has the side effect of fixing a bug where a nested SwitchCaseOp.target wasn't considered in the scope building for all control-flow operations, which could incorrectly cause some registers to be missed in outer scopes.

Details and comments

Fix #10398.
Close #10228.
Depends on #10367.

Additional feature changelog in #10331.

@jakelishman jakelishman added Changelog: New Feature Include in the "Added" section of the changelog Changelog: Bugfix Include in the "Fixed" section of the changelog labels Jul 6, 2023
@jakelishman jakelishman added this to the 0.25.0 milestone Jul 6, 2023
@jakelishman jakelishman requested a review from a team as a code owner July 6, 2023 17:44
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

@coveralls
Copy link

coveralls commented Jul 6, 2023

Pull Request Test Coverage Report for Build 5602359644

  • 32 of 32 (100.0%) changed or added relevant lines in 3 files are covered.
  • 99 unchanged lines in 8 files lost coverage.
  • Overall coverage increased (+0.02%) to 86.073%

Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/instruction_durations.py 1 92.96%
qiskit/transpiler/passmanager_config.py 1 97.26%
qiskit/extensions/quantum_initializer/squ.py 2 80.0%
crates/qasm2/src/lex.rs 3 90.89%
qiskit/providers/fake_provider/fake_backend.py 4 91.12%
crates/qasm2/src/parse.rs 6 97.58%
qiskit/compiler/transpiler.py 13 92.89%
qiskit/circuit/quantumcircuit.py 69 94.18%
Totals Coverage Status
Change from base Build 5597428209: 0.02%
Covered Lines: 72637
Relevant Lines: 84390

💛 - Coveralls

This is generally relatively straightforwards; anywhere where we examine
the resources used by an operation, we need to update to account for
classical resources potentially being tied up in `ControlFlowOp` fields
in `Expr` nodes as well.

This also has the side effect of fixing a bug where a nested
`SwitchCaseOp.target` wasn't considered in the scope building for _all_
control-flow operations, which could incorrectly cause some registers to
be missed in outer scopes.
@jakelishman jakelishman removed the on hold Can not fix yet label Jul 19, 2023
@jakelishman
Copy link
Member Author

Now rebased over main.

@kevinhartman kevinhartman self-assigned this Jul 19, 2023
Copy link
Contributor

@kevinhartman kevinhartman left a comment

Choose a reason for hiding this comment

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

LGTM, just a few small comments.

qiskit/circuit/controlflow/builder.py Outdated Show resolved Hide resolved
*,
in_loop: bool,
label: Optional[str] = None,
):
self.circuit = circuit
self.target = target
self._target = target
if isinstance(target, Clbit):
Copy link
Contributor

Choose a reason for hiding this comment

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

Not really a comment to be addressed in this PR, but did we consider just always lifting the conditions and targets to Expr? I suppose that'd be a breaking change since then self.{target,condition} would be an Expr rather than what the user provided. But it'd at least remove this series of checks which we currently require all over the place!

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 did consider it, but wanted to minimise potential API breakage for the old paths, since the intent is likely to deprecate/remove the bare form.

expected = QuantumCircuit(qr, clbits, cr1, cr2, cr3, cr4)
expected.for_loop(range(3), None, for_body, [qr[0]], clbits + list(cr1))

self.assertEqual(canonicalize_control_flow(test), canonicalize_control_flow(expected))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need the canonicalize_control_flow-ing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably not, but I didn't want to make that change in the same PR, since there's a whole bunch of places where it could be removed.

@kevinhartman kevinhartman added this pull request to the merge queue Jul 19, 2023
Merged via the queue into Qiskit:main with commit 9a46c5a Jul 19, 2023
13 checks passed
@jakelishman jakelishman deleted the expr/control-flow-builders branch July 19, 2023 21:18
to24toro pushed a commit to to24toro/qiskit-terra that referenced this pull request Aug 3, 2023
* Add `Expr` support to the control-flow builders

This is generally relatively straightforwards; anywhere where we examine
the resources used by an operation, we need to update to account for
classical resources potentially being tied up in `ControlFlowOp` fields
in `Expr` nodes as well.

This also has the side effect of fixing a bug where a nested
`SwitchCaseOp.target` wasn't considered in the scope building for _all_
control-flow operations, which could incorrectly cause some registers to
be missed in outer scopes.

* Remove out-of-date comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
5 participants