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

Only run translation stage in optimization loop if necessary #6940

Merged
merged 44 commits into from
Sep 2, 2022

Conversation

irajput
Copy link
Contributor

@irajput irajput commented Aug 24, 2021

Summary

This commit updates the preset pass managers to only run the translation stage as part of the optimization loop if
there are gates outside the target in the circuit. This leverages the GatesInBasis analysis pass to the default
optimization loops for preset passmanagers level 1, 2, and 3 and conditions the execution of the basis translation passes
on whether any gates are detected outside the target basis. This should also address the potential issue reported in
#6677 where additional translation is resulting in unoptimized output in some cases.

Details and comments

Fixes #6677

mtreinish and others added 4 commits July 29, 2021 10:33
This commit adds a new analysis pass, GatesInBasis, which is used to
check if all the gates in the circuit are currently in the basis set or
not. The intent for this pass is to use this to conditionally trigger
the execution of basis translation (normally the BasisTranslator) in the
main optimization loop of the preset pass managers, however this is
blocked on Qiskit#6830. So for now this just adds the pass so we can use it in
the future.
@irajput irajput requested a review from a team as a code owner August 24, 2021 22:02
@irajput irajput changed the title WIP/RFC: Allowing for nested flow controllers for more PassManager functionality WIP/RFC: Allowing for nested flow controllers for PassManager nested conditionals Aug 24, 2021
@jlapeyre
Copy link
Contributor

As we discussed elsewhere, at first glance, it looks like #6938 solves the problem of nesting conditionals in a more general way. I mean, you can do more with PassManager than with FlowController. However, there is some possibility that this PR, #6940, is lighter weight and more efficient in some cases.

@irajput irajput changed the title WIP/RFC: Allowing for nested flow controllers for PassManager nested conditionals WIP: Making Sequences produced by Synthesis routine and opt. 3 efficient Aug 30, 2021
@jlapeyre jlapeyre added the on hold Can not fix yet label Dec 3, 2021
@jlapeyre
Copy link
Contributor

The blocker #6962 has been merged. But, I am leaving the "hold" tag because there is a new error, probably because of an incompatible change in one of the recent merges of the main branch.

Copy link
Member

@kdk kdk left a comment

Choose a reason for hiding this comment

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

The CI failures are intest.python.providers.test_backend_v2.TestBackendV2 ( https://dev.azure.com/qiskit-ci/qiskit-terra/_build/results?buildId=33780&view=logs&j=5ed7f5be-2954-510c-f69f-57064fd91e48&t=42b17b83-3671-53ec-2b04-ff846dcbcf0e&l=16157 ).

I'm not 100% sure, but it looks like this is preventing a run of the BasisTranslator (which is needed here for the BackendV2 case where not all gates are present on all qubits/pairs of qubits). I think this can be resolved by making GatesInBasis "target-aware" but maybe @mtreinish could say more definitively (or if this would be better handled in a seperate issue).

qiskit/transpiler/preset_passmanagers/level3.py Outdated Show resolved Hide resolved
@mtreinish
Copy link
Member

I'm not 100% sure, but it looks like this is preventing a run of the BasisTranslator (which is needed here for the BackendV2 case where not all gates are present on all qubits/pairs of qubits). I think this can be resolved by making GatesInBasis "target-aware" but maybe @mtreinish could say more definitively (or if this would be better handled in a seperate issue).

Yeah, we'll need to update the GatesinBasis pass to be target aware to make this work I think. From a quick glance at the test failure it looks to me like gates in basis is setting the property set to True because globally all gates are in the global basis set in the target but that's ignoring the local per qubit restrictions and causing the failure. I'll push up a quick PR to make gates in basis target aware it should be straightforward.

@HuangJunye HuangJunye added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 21, 2022
This commit expands the conditional unrolling so that it occurs in every
optimization level that has an optimization loop. Since this PR branch
was originally created we've expanded the use of the unroller stage in
the optimization loop to account for other issues caused by new
developments in the transpiler since this was originally developed. This
commit updates level 1 and level 2 to avoid unecessary basis translation
if the circuit is already in the target basis.
For the gates in basis pass it previously was raising an error if you
initialized the pass without a target or basis gates constraint set.
This was because the pass didn't have any constraints to enforce so
there wasn't anything to validate. However, with the integration of the
pass into the preset pass managers this behavior wasn't desireable
because people run transpile() without a backend, target, or basis_gates
set. To account for this the pass logic is changed to just return True
if there are no instruction contstraints because all the gates are in
the basis since there is no basis.
@mtreinish mtreinish changed the title Making Sequences produced by Synthesis routine and opt. 3 efficient Only run translation stage in optimization loop if necessary Aug 30, 2022
@mtreinish
Copy link
Member

I've updated this PR so I think it's ready to merge now, but since I basically rewrote it as part of updating the PR, I'd like to get another set of eyes to review it before we merge.

@mtreinish mtreinish removed the on hold Can not fix yet label Aug 30, 2022
@mtreinish mtreinish added this to the 0.22 milestone Aug 30, 2022
@mtreinish mtreinish added performance mod: transpiler Issues and PRs related to Transpiler labels Aug 30, 2022
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

I pushed a commit to early return instead of increasing the nesting level (which also shows that the changeset to GatesInBasis is minimal), but otherwise this looks fine.

Comment on lines +221 to +222
nonlocal gates_in_basis_true_count
nonlocal collect_2q_blocks_count
Copy link
Member

Choose a reason for hiding this comment

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

It's not everyday there's a ~valid usage for nonlocal.

@jakelishman jakelishman added automerge Changelog: None Do not include in changelog labels Sep 2, 2022
@mergify mergify bot merged commit 8a3e760 into Qiskit:main Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: transpiler Issues and PRs related to Transpiler performance
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Inefficient 1Q sequences in -O3
7 participants