-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Cache the hash of frozen circuits to avoid recomputing #5738
Conversation
This follows what we previously did for GridQubit, but reworked to use `_compat.cached_method` and to compute the hash lazily.
hash_cache = _compat._method_cache_name(self.__hash__) | ||
if hash_cache in state: | ||
state = state.copy() | ||
del state[hash_cache] |
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.
Optional suggestion:
state = self.__dict__.copy()
hash_cache = _compat._method_cache_name(self.__hash__)
state.pop(hash_cache, None)
return state
Rationale: This is consistent in always returning a copy of self.__dict__
. Also, it avoids branching.
(If you choose to do this, apply to GridQubit, too.)
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.
AFAICT from the docs on __getstate__
it's not necessary to return a copy, since the pickle infrastructure does not modify the returned object. In fact, pickle will use self.__dict__
directly if __getstate__
is not defined.
This follows what we previously did for GridQubit, but reworked to use `_compat.cached_method` and to compute the hash lazily.
This follows what we previously did for GridQubit, but reworked to use `_compat.cached_method` and to compute the hash lazily.
This follows what we previously did for GridQubit, but reworked to use
_compat.cached_method
and to compute the hash lazily.