-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Modify preset pass managers for control-flow support #8830
Conversation
This adds control-flow support to the level 0 and level 1 pass managers directly, including neatly erroring out at runtime if the pass manager was constructed with options that are not yet compatible with control flow. In general, we assume that plugins from outside of Terra are safe for control flow - it's highly likely that this is not in fact the case, but it's better to do things this way so an external plugin _can_ support control flow, as opposed to forbidding them from doing so. Level 2 and level 3 both contain optimisations that are incompatible with control flow right now, and so immediately error if an input circuit has control flow. The failing optimisations are ones that involve a two-pass process of "block collection" followed by "block replacement"; we have no updated any of the "block collection" analysis passes to be safe for control flow, since we would also likely need to communicate the control-flow structure through the property set, which is not a path we're going down just yet. In order to construct pass managers with nested flow control (e.g. the routing-pass stages contain conditionals, but the entire routing stage needs to be conditioned on the presence of control flow), there is a new `PassManager` method `to_flow_controller`. This produces a `FlowControllerLinear` with the same content as the pass manager, such that it can then itself be conditioned as a complete block, and embedded into other pass managers.
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review this:
|
So I haven't looked at this in too much detail but we did add support for nested #6962. I was able to use that to execute a series of passes with a nested controller to enable |
The change I made to That's only possible because of #6962 - I had to go through the old git history to find that PR, because the documented types are wrong (and control flow through The only change is these lines: https://github.com/Qiskit/qiskit-terra/blob/921139eb39c3ffa984c76949fe22a0b1d0afa72f/qiskit/transpiler/passmanager.py#L325-L336 |
Qiskitgh-6962 added support for nested `FlowController` instances. It did not, however, propagate any `DAGCircuit` output by its internal passes into the transpilation loop, which meant that if a pass returned a new DAG rather than mutating the input, the changes would be thrown away. Similar behaviour was occuring with the pass "normalisation" step; incorrect classes would raise an error, but if the normalisation needed to raise a single pass to a list, the result would not be re-bound into the nested controller.
The majority of the failing tests here transpired to be because of two reasons:
|
Pull Request Test Coverage Report for Build 3204437657
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall approach LGTM and matches what we discussed offline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall I'm happy with this direction it makes sense to me, I left a few inline comments from quick things that caught my eye on this first pass. After #8418 merges and this is rebased with tests verifying transpile()
with control I'll circle back and do a more thorough review.
("optimization", optimization_method), | ||
("scheduling", scheduling_method), | ||
]: | ||
option = stage + "_method" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we just use "layout_method"
etc for L99-L103? Besides the _CONTROL_FLOW_STATES
lookup this is just used for the error message and *_method
works fine for the error message too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I did this just because one of the possible error messages is "the entire {stage} stage is not supported", and that's weird with scheduling_method
.
init = common.generate_error_on_control_flow( | ||
"The optimizations in optimization_level=2 do not yet support control flow." | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We technically could get a level2 pm that given the assumptions made in common.py
is runnable with control flow. But it'd basically be layout_method=dense
, routing_method=stochastic
, optimization=plugin
. It seems to be fairly unlikely in practice so this is fine I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was treating us as "supporting" an optimisation level if the defaults will work - for O2, you have to override everything that actually makes it O2 back to what's in O1, so I think it's better to reject optimization_level=2
out-of-hand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess at O1, now that routing and layout are both Sabre by default there's an argument that we're kind of just doing that internally too, I suppose. I was still thinking in terms of the old O1 that had dense/stochastic as its default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with this for now, to a certain degree if people want to get more complicated maybe they should just be using custom pass managers. We just will have to document clearly in the release note that optimization_level
2 and 3 will error regardless of options with a control flow circuit.
I've added the tests for the new functionality. They won't pass yet until this PR has #8418 as well, but they should pass immediately after that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this LGTM, as you predicted the only comments I had were on the release notes, just a few small ideas and suggestions inline. But not worth blocking over either, we can always work on these in the 0.22.0 final release prep when we edit the release notes too. So don't feel you have to update the release notes now.
releasenotes/notes/transpiler-control-flow-708896bfdb51961d.yaml
Outdated
Show resolved
Hide resolved
releasenotes/notes/transpiler-control-flow-708896bfdb51961d.yaml
Outdated
Show resolved
Hide resolved
releasenotes/notes/transpiler-control-flow-708896bfdb51961d.yaml
Outdated
Show resolved
Hide resolved
Merged with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the updates to the release notes. Looking through the test changes all of those make sense to me. TBH, in my earlier reviews I didn't look too closely at the test code as we were waiting for #8418 still
Summary
This adds control-flow support to the level 0 and level 1 pass managers directly, including neatly erroring out at runtime if the pass manager was constructed with options that are not yet compatible with control flow. In general, we assume that plugins from outside of Terra are safe for control flow - it's highly likely that this is not in fact the case, but it's better to do things this way so an external plugin can support control flow, as opposed to forbidding them from doing so.
Level 2 and level 3 both contain optimisations that are incompatible with control flow right now, and so immediately error if an input circuit has control flow. The failing optimisations are ones that involve a two-pass process of "block collection" followed by "block replacement"; we have no updated any of the "block collection" analysis passes to be safe for control flow, since we would also likely need to communicate the control-flow structure through the property set, which is not a path we're going down just yet.
In order to construct pass managers with nested flow control (e.g. the routing-pass stages contain conditionals, but the entire routing stage needs to be conditioned on the presence of control flow), there is a new
PassManager
methodto_flow_controller
. This produces aFlowControllerLinear
with the same content as the pass manager, such that it can then itself be conditioned as a complete block, and embedded into other pass managers.Details and comments
Depends (effectively) on #8418 - the modifications to level 1 assume that
StochasticSwap
will work with control flow.I haven't yet written tests for this, as I'm not 100% certain this is the right direction and I don't want to waste time. Most of them likely wouldn't pass without #8418 anyway. I'll write tests once we've got a bit of an agreement in spirit.
I anticipate this PR being the one that will add all the release notes for the new control-flow support through the transpiler - I will also add those before merge.
Fix #8630. (This will be the last PR of that epic.)
Fix #8214.