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

Unroll3qOrMore before layout allocation stage in the levels 0, 1, and 2 #7251

Merged
merged 12 commits into from
Nov 30, 2021

Conversation

1ucian0
Copy link
Member

@1ucian0 1ucian0 commented Nov 10, 2021

Fixes #7156

Summary

Many layout methods ignore 3-or-more qubit gates resulting in expected layout allocation decisions. This PR runs Unroll3qOrMore before any layout pass.

@1ucian0 1ucian0 added this to the 0.19 milestone Nov 10, 2021
@1ucian0 1ucian0 changed the title Un Unroll3qOrMore before layout allocation stage in the levels 0, 1, and 2 Nov 10, 2021
@1ucian0 1ucian0 marked this pull request as ready for review November 11, 2021 13:55
@1ucian0 1ucian0 requested a review from a team as a code owner November 11, 2021 13:55
Comment on lines 97 to 103
# 1. Decompose so only 1-qubit and 2-qubit gates remain
_unroll3q = [
# Use unitary synthesis for basis aware decomposition of UnitaryGates
UnitarySynthesis(
basis_gates,
approximation_degree=approximation_degree,
coupling_map=coupling_map,
Copy link
Member

Choose a reason for hiding this comment

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

If we haven't laid out the qubits yet, what does the coupling map mean for the plugin? We could omit it, but then it starts to feel like we'll not following the calling conventions we laid out in the definition of the synthesis plugin.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this is a good point. If the circuit doesn't have a layout set this will be doing the wrong thing, because the synthesis pass assumes it's going to have the bits in physical order in the dag. I think we should set this to None if run prior to layout. None is still a valid value here to indicate there is no target coupling map set.

Copy link
Member

Choose a reason for hiding this comment

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

Oh yeah, I forgot we still passed through explicit None on that one. Yeah, that seems like the best solution for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

If the circuit doesn't have a layout set this will be doing the wrong thing,

Any suggested test to add about this?

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 a bit tricky to test for because it doesn't come up with the default/built-in unitary synthesis mechanism since for >=3q it uses isometry which isn't coupling map aware. For the default plugin this only comes up with <=2q unitaries. It's only for potential plugins like AQC or BQSKit where this would come up, since the synthesis pass sends bit indices to the plugin assuming their in physical order: https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/transpiler/passes/synthesis/unitary_synthesis.py#L272-L276 (ie post routing)

Copy link
Member

Choose a reason for hiding this comment

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

You could set up a unittest.mock.MagicMock wrapper around the default synthesis plugin to catch all calls, then run the transpiler pass and assert that the first call has min_qubits=3 and coupling_map=None, perhaps? There's some examples of this ~~sort of test in test_unitary_synthesis_plugin.py, like here: https://github.com/Qiskit/qiskit-terra/blob/7e118e81344cb05aada5082e3cc71c1c239baaa5/test/python/transpiler/test_unitary_synthesis_plugin.py#L220-L235

Copy link
Member Author

Choose a reason for hiding this comment

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

The testing is a bit artificial. Is it worthy?

@coveralls
Copy link

coveralls commented Nov 11, 2021

Pull Request Test Coverage Report for Build 1521847541

  • 6 of 6 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 82.857%

Totals Coverage Status
Change from base Build 1517139552: 0.0%
Covered Lines: 50147
Relevant Lines: 60522

💛 - Coveralls

@mtreinish mtreinish added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Nov 11, 2021
@kdk kdk assigned 1ucian0 and unassigned mtreinish Nov 30, 2021
@mtreinish mtreinish assigned mtreinish and unassigned 1ucian0 Nov 30, 2021
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, we can skip a test since yeah we'd be just validating that we're passing None before layout and a coupling map after layout to the UnitarySynthesis pass and we already have test coverage that validates from inside the pass propagates to plugins so we're probably fine on coverage

@mergify mergify bot merged commit fe0abaa into Qiskit:main Nov 30, 2021
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 priority: high
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unroll3qOrMore should run before the layout allocation stage in the preset passmanagers (0, 1, and 2)
4 participants