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

Fix input normalisation of transpile(initial_layout=...) #11031

Merged
merged 6 commits into from
Oct 19, 2023
Merged

Fix input normalisation of transpile(initial_layout=...) #11031

merged 6 commits into from
Oct 19, 2023

Conversation

henryzou50
Copy link
Contributor

…ormalize input of range to a list

Summary

Fixes issue10554.

Details and comments

In transpile, when we are parsing through the initial layout, it misses the case of range inputs, which caused things like transpile(qc, initial_layout=range(2)) to raise the error TranspilerError: "SetLayout was intialized with the layout type: <class 'range'>". This PR changes the _parsee_initial_layout in transpile to also check for case where the initial_layout is an instance of range, and changes it to a list.

Note, this essentially makes transpile(qc, initial_layout=range(2)) equivalent to transpile(qc, initial_layout=[0,1])

@henryzou50 henryzou50 requested a review from a team as a code owner October 17, 2023 21:03
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Oct 17, 2023
@qiskit-bot
Copy link
Collaborator

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:

  • @Qiskit/terra-core

@CLAassistant
Copy link

CLAassistant commented Oct 17, 2023

CLA assistant check
All committers have signed the CLA.

@mtreinish mtreinish added this to the 0.25.3 milestone Oct 17, 2023
@mtreinish mtreinish removed the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Oct 17, 2023
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.

Thanks for looking at this, Henry. Could you add:

  • a brief test in test/python/compiler/test_transpile.py that a transpile call with initial_layout=range(...) returns the same result as a call with initial_layout=list(range(...)) (for a fixed transpilation seed)
  • a bugfix release note about this?

Comment on lines 515 to 518
if isinstance(initial_layout, range):
return list(initial_layout)
if all(phys is None or isinstance(phys, Qubit) for phys in initial_layout):
return Layout.from_qubit_list(initial_layout)
Copy link
Member

Choose a reason for hiding this comment

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

I think at this point we could just do initial_layout = list(initial_layout) to handle any iterable? At worst it'll make a copy of an existing list, but the list isn't going to be large, so it's not a big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing! I've added the test and release note, and I agree that we can just use initial_layout = list(initial_layout). I made that adjustment.

@jakelishman jakelishman changed the title Fixed issue10554 by modifying _parse_initial_layout in transpile to n… Fix input normalisation of transpile(initial_layout=...) Oct 19, 2023
@jakelishman jakelishman added Changelog: Bugfix Include in the "Fixed" section of the changelog mod: transpiler Issues and PRs related to Transpiler stable backport potential The bug might be minimal and/or import enough to be port to stable labels Oct 19, 2023
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.

Thanks for the PR Henry! The test looks good.

I pushed a couple of minor tweaks, one to add a couple of cross-references to the release note, and one to let us handle consumable iterables (like generators). Sorry for stepping on your toes, I'm just trying to get this in the merge queue asap.

@jakelishman jakelishman added this pull request to the merge queue Oct 19, 2023
Merged via the queue into Qiskit:main with commit 6148f90 Oct 19, 2023
14 checks passed
mergify bot pushed a commit that referenced this pull request Oct 19, 2023
* Fixed issue10554 by modifying _parse_initial_layout in transpile to normalize input of range to a list

* Added test and bugfix note

* Handle consumable iterables

* Fix release-note cross references

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 6148f90)

# Conflicts:
#	test/python/transpiler/test_transpile_layout.py
github-merge-queue bot pushed a commit that referenced this pull request Oct 25, 2023
…11031) (#11058)

* Fix input normalisation of `transpile(initial_layout=...)` (#11031)

* Fixed issue10554 by modifying _parse_initial_layout in transpile to normalize input of range to a list

* Added test and bugfix note

* Handle consumable iterables

* Fix release-note cross references

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 6148f90)

# Conflicts:
#	test/python/transpiler/test_transpile_layout.py

* Fix backport merge conflicts

The backport of this commit from main failed because the test for this
bug fix was added to an existing file on the main branch but that didn't
exist on the stable/0.25 branch. To fix this the test is moved to a
separate standalone file. Arguably, this should be done on main and
stable/0.45 too as the file the test was added to was for a different
component.

---------

Co-authored-by: Henry Zou <87874865+henryzou50@users.noreply.github.com>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
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 mod: transpiler Issues and PRs related to Transpiler stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

transpile initial layout and SetLayout pass don't work with range in 0.25
5 participants