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

Implement a from_backend method for PassManagerConfig #7163

Merged
merged 17 commits into from
Oct 26, 2021

Conversation

yjt98765
Copy link
Contributor

Summary

The task is proposed by issue 7023. It proposes a from_backend method to ease the access of preset pass managers.

Details and comments

Currently, the user needs to manually build a PassManagerConfig, which contains copying some backend options, e.g.

from qiskit.transpiler import PassManagerConfig, CouplingMap
from qiskit.transpiler.preset_passmanagerrs import *

pass_manager_config = qk.transpiler.PassManagerConfig(
    basis_gates = backend.configuration().basis_gates,
    coupling_map = CouplingMap(backend.configuration().coupling_map),
    backend_properties = backend.properties(),
)

pm = level_3_pass_manager(pass_manager_config)

This work can be alleviated if some method is provided. Issue #7023 proposes a method from_backend(backend, **pass_manager_options) that takes a backend and user options as arguments, and returns a PassManagerConfig object that can be ready to use. In this way, the above snippet can be simplified to level_1_pass_manager(from_backend(backend, **pass_manager_options)). As to which class to add this method, Issue #7023 provides two options: PassManagerConfig or FullPassManager. Since the latter is still under review, this PR adds the from_backend method to PassManagerConfig. If later we find it is better to put it under FullPassManager, the migration should be straightforward.

This PR includes the following parts:

  1. The implementation of the from_backend() method (qiskit/transpiler/passmanager_config.py);
  2. The testing of the implemented method (test/python/transpiler/test_passmanager_config.py);
  3. The modification of test/python/transpiler/test_passmanager_run.py to demonstrate the usage of the implemented method;
  4. Adding a changelog (releasenotes/notes/add-passmanager-config-from-backend-af5dd7d99ec053ef.yaml) since this method can be used by the user.

@yjt98765 yjt98765 requested a review from a team as a code owner October 19, 2021 07:45
@CLAassistant
Copy link

CLAassistant commented Oct 19, 2021

CLA assistant check
All committers have signed the CLA.

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.

Thanks for pushing this up, this is a really good idea for the class to simplify it's creation slightly. We can probably leverage this in the transpile() function logic to simplify that web of code. While the PassManagerConfig will be obsolete with the introduction of the the Target class in #5885 it will still continue to exist for a long time until we can update everything to use the next backend interface version and then deprecate and remove it (which will probably be at least a year from the next release before we can deprecate it).

I left a few comments inline mostly on how this can be simplified. Most of the options in a PassManagerConfig do not relate to the backend in use so we can just rely on __init__ to do most of the work here. Then for the 5 options that do potentially come from a backend we should handle them one by one because each has unique processing that needs to be done individually.

qiskit/transpiler/passmanager_config.py Outdated Show resolved Hide resolved
qiskit/transpiler/passmanager_config.py Show resolved Hide resolved
or the configuration does not support a `to_dict()` method.
AttributeError: If the field passed in is not part of the options.
"""
res = PassManagerConfig()
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason to do this instead of something like:

res = PassManagerConfig(**pass_manager_options)

and then you can change the rest of the logic to be something like:

config = backend.configuration()
if res.basis is None:
    res.basis_gates = getattr(config, 'basis_gates', None)

for each of the backend attributes and then explicitly handle the construction for them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. I followed you suggestion, but with a slightly different style: res.basis_gates = res.basis_gates or getattr(config, "basis_gates", None). I think using or makes the code a little more concise.

Copy link
Member

Choose a reason for hiding this comment

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

The issue with using or like this is that it will also treat empty containers as not being user specified which is why I used is None in my example. For example, with the code like this if a user calls something like: PassManagerConfig.from_backend(FakeMelbourne(), basis_gates=[]) the output PassManagerConfig object will have the basis gates of fake melbourne and not [], while the user expectation would be that basis gates is [] because it was manually specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. Now I see the problem with or. The code is updated.

qiskit/transpiler/passmanager_config.py Outdated Show resolved Hide resolved
qiskit/transpiler/passmanager_config.py Outdated Show resolved Hide resolved
qiskit/transpiler/passmanager_config.py Outdated Show resolved Hide resolved
qiskit/transpiler/passmanager_config.py Outdated Show resolved Hide resolved
qiskit/transpiler/passmanager_config.py Outdated Show resolved Hide resolved
qiskit/transpiler/passmanager_config.py Outdated Show resolved Hide resolved
qiskit/transpiler/passmanager_config.py Outdated Show resolved Hide resolved
@yjt98765
Copy link
Contributor Author

yjt98765 commented Oct 25, 2021

@mtreinish Thank you so much for the detailed review. I learned a lot from it. As a newcomer, I am not familiar with the data structure used in Qiskit. Without your patient instructions, this task cannot be done. I am looking forward to continuing to participate in this project and learning from it!

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.

The code looks much improved to me now, thanks for the fast updates. I think the only issue I see is the one I mentioned before where false evaluation for more than just the default None which is probably undesirable behavior. I also suggested some change to the release notes which aren't critical.

yjt98765 and others added 5 commits October 26, 2021 08:46
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
…99ec053ef.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
…99ec053ef.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
…99ec053ef.yaml

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@yjt98765
Copy link
Contributor Author

The code looks much improved to me now, thanks for the fast updates. I think the only issue I see is the one I mentioned before where false evaluation for more than just the default None which is probably undesirable behavior. I also suggested some change to the release notes which aren't critical.

Thank you. I have updated the code.

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 now, thanks for all the updates and sticking with it

@mtreinish mtreinish added automerge Changelog: New Feature Include in the "Added" section of the changelog labels Oct 26, 2021
@mergify mergify bot merged commit f27a180 into Qiskit:main Oct 26, 2021
@kdk kdk added this to the 0.19 milestone Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants