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

Add memoization to StagedPassmanager #8277

Closed
wants to merge 7 commits into from

Conversation

pedrorrivero
Copy link
Member

@pedrorrivero pedrorrivero commented Jun 30, 2022

Summary

Simplifies code and improves readability by introducing memoization for expanded_stages in StagedPassmanager through the cached_property decorator in functools. @mtreinish @jakelishman

Details and comments

  1. Removes need to call parent class __setattr__.
  2. Removes pylint ignores.
  3. Removes need to set self._expanded_stages attribute.
  4. Maintains efficiency and functionality thanks to self.expanded_stages immutability.

@pedrorrivero pedrorrivero requested a review from a team as a code owner June 30, 2022 02:25
@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

@coveralls
Copy link

coveralls commented Jun 30, 2022

Pull Request Test Coverage Report for Build 2590459825

  • 11 of 14 (78.57%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 83.961%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/transpiler/passmanager.py 11 14 78.57%
Totals Coverage Status
Change from base Build 2585369189: 0.002%
Covered Lines: 55787
Relevant Lines: 66444

💛 - Coveralls

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.

This feels like a lot of complexity - _validate_init_kwargs is always called during __init__, so the expanded_stages object is always created anyway. If the issue is purely about __setattr__, then it would be better to just handle that in __setattr__: something like having both self.expanded_stages accesses be getattr(self, "_expanded_stages", ()) would sidestep the issue - if _expanded_stages hasn't yet been set, then it'll just default to an empty set, and __setattr__ will always do the right thing.

cached_property is effectively just a different way of sidestepping the underlying __setattr__ issue - it directly gets the __dict__ from the object it's attached to, and then does its gets and sets into that rather than using the regular structures.

@jakelishman
Copy link
Member

This has merge conflicts due to the rewriting of the pass-manager structures in #10124 and #10127. Given my above comment, I'm not super clear that the additional complexity added by this PR is worthwhile, but if this is something you feel strongly should be included in the StagedPassManager, feel free to re-open this PR / open a new one targetting the new structure (as appropriate).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants