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

Cache the hash of frozen circuits to avoid recomputing #5738

Merged
merged 3 commits into from
Jul 12, 2022

Conversation

maffoo
Copy link
Contributor

@maffoo maffoo commented Jul 12, 2022

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.
@maffoo maffoo requested review from a team, vtomole and cduck as code owners July 12, 2022 19:11
@maffoo maffoo requested a review from mpharrigan July 12, 2022 19:11
@CirqBot CirqBot added the size: S 10< lines changed <50 label Jul 12, 2022
hash_cache = _compat._method_cache_name(self.__hash__)
if hash_cache in state:
state = state.copy()
del state[hash_cache]
Copy link
Collaborator

@viathor viathor Jul 12, 2022

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.)

Copy link
Contributor Author

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.

@maffoo maffoo added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jul 12, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jul 12, 2022
@CirqBot CirqBot merged commit 506012a into master Jul 12, 2022
@CirqBot CirqBot removed the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jul 12, 2022
@CirqBot CirqBot deleted the u/maffoo/frozen-circuit-hash branch July 12, 2022 23:11
@CirqBot CirqBot removed the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jul 12, 2022
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
This follows what we previously did for GridQubit, but reworked to use `_compat.cached_method` and to compute the hash lazily.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
This follows what we previously did for GridQubit, but reworked to use `_compat.cached_method` and to compute the hash lazily.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants