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

Make TwoQubitWeylDecomposition pickle-able #7333

Merged
merged 8 commits into from
Nov 2, 2022
Merged

Conversation

levbishop
Copy link
Member

@levbishop levbishop commented Dec 1, 2021

Partial fix for #7312

Probably I should have used a factory function to specialize to the appropriate subclass rather than this trickery with __new__ that is only strictly required when subclassing builtins such as int, but its an easy fix to tweak __getnewargs_ex__ to pass an argument to __new__ so it can recognize if it is being called in unpickle context where there is no need to redo the decomposition.

@levbishop levbishop requested review from chriseclectic and a team as code owners December 1, 2021 17:01
@coveralls
Copy link

coveralls commented Dec 1, 2021

Pull Request Test Coverage Report for Build 3376943375

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.008%) to 84.533%

Totals Coverage Status
Change from base Build 3373509944: 0.008%
Covered Lines: 62442
Relevant Lines: 73867

💛 - Coveralls

@levbishop levbishop closed this Dec 4, 2021
@levbishop levbishop deleted the weylpickle branch December 4, 2021 16:07
@levbishop levbishop restored the weylpickle branch December 4, 2021 16:08
@levbishop levbishop reopened this Dec 4, 2021
jakelishman
jakelishman previously approved these changes Dec 23, 2021
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.

The entire __new__ of this class terrifies me, but given that, I think this change is a sensible way to do things.

One comment about potentially testing copy.copy and copy.deepcopy as pickle-adjacent, but I'm not super bothered about them, so I've approved as well in case they're not worth doing.

Comment on lines +177 to +178
def assertRoundTripPickle(self, weyl1: TwoQubitWeylDecomposition):
"""Fail if loads(dumps(weyl1)) not equal to weyl1"""
Copy link
Member

Choose a reason for hiding this comment

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

It may also be worth adding a very similar test that copy.deepcopy and copy.copy work correctly as well/ deepcopy presumably will because it's a full pickle/unpickle by default, but copy might do something funny; I think it basically calls __getstate__, then restores it with an immediate call to __setstate__ without recursing the pickle through the state. I would guess it does the right thing and calls __getnewargs_ex__ if defined, but I don't really know, and it might be worth a test, since Qiskit uses the copy module pretty extensively.


pkl = pickle.dumps(weyl1, protocol=max(4, pickle.DEFAULT_PROTOCOL))
weyl2 = pickle.loads(pkl)
msg_base = f"weyl1:\n{weyl1}\nweyl2:\n{repr(weyl2)}"
Copy link
Member

@jakelishman jakelishman Dec 23, 2021

Choose a reason for hiding this comment

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

I learned recently that f"{repr(x)}" can also be written as f"{x!r}" (and !s for str), which seems like a fun way to introduce a million more sigils to the strings and make them completely unreadable. When we get to Python 3.8+ only, there's an even more arcane one: f"{weyl1=}", which is equivalent to f"weyl1={weyl1!r}".

@andrenmelo
Copy link

Seems this PR was forgotten -- any plans to merge?

jakelishman
jakelishman previously approved these changes Nov 1, 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 fixed the merge conflict. I'll just merge this now (thanks for the reminder!) and if any copy/deepcopy shenanigans ever rear their heads we can fix them. The pickle test alone should be fine.

@jakelishman jakelishman added Changelog: Bugfix Include in the "Fixed" section of the changelog automerge labels Nov 1, 2022
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants