-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Use pickle __reduce__
for TwoQubitWeylDecomposition
#12003
Conversation
One or more of the the following people are requested to review this:
|
Pull Request Test Coverage Report for Build 8283866556Details
💛 - Coveralls |
Looks like a better way to do things for pickle. Not sure that I agree for exposing this to from_bytes: although a discrepancy between the original and reconstructed state does indicate a floating point error, it may equally likely be an error in the initial decomposition giving a chance to fix it at the re-decomposition? Neither seems obviously better to me, and at least with the current re-decomposition the numerical stability gets checked in the test suite. |
We can check numerical stability by just trying to do the same decomposition more than once, right? |
Since this class does complicated calculation within its `__new__` method (as many extension classes do), we want to use an alternative extension-module constructor for the object on unpickle, to avoid leaking the "pickle" / "no pickle" state through the default constructor. This implements a private Python-space method that directly constructs the object from components, which is then used as the `__reduce__` target, along with the pickle state.
3deea32
to
d86b391
Compare
Now rebased over |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, this is a cleaner solution to handle pickling without having to worry about duplicate calculations.
Summary
Since this class does complicated calculation within its
__new__
method (as many extension classes do), we want to use an alternative extension-module constructor for the object on unpickle, to avoid leaking the "pickle" / "no pickle" state through the default constructor.This implements a private Python-space method that directly constructs the object from components, which is then used as the
__reduce__
target, along with the pickle state.Details and comments
Built on top of #11946, since that might be on the verge of landing. See #11946 (comment) for context.
It's plausible we might want to expose
_from_state
as a public constructor, or otherwise provide a convenient debugging method that produces a copy/paste codeblock in terms of it, since unlikefrom_bytes
, it completely skips all the recalculation of the decomposition and simply builds the state in place; there's zero risk of floating-point errors from the re-decomposition, because it simply doesn't happen.