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 Qid instances for common types #6371

Merged
merged 8 commits into from
Dec 8, 2023
Merged

Cache Qid instances for common types #6371

merged 8 commits into from
Dec 8, 2023

Conversation

maffoo
Copy link
Contributor

@maffoo maffoo commented Dec 5, 2023

Here we add caches of common qubit types GridQubit, LineQubit, and NamedQubit (and their associated Qid types) so that we can reuse instances of these objects. Operations on these objects are extremely common, so reusing instances can have significant performance benefits, in particular because hashes do not need to be recomputed and equality comparisons can be short-circuited with instance equality in common cases.

@maffoo maffoo force-pushed the u/maffoo/gq-cache branch from 5b9b5f8 to ae62991 Compare December 5, 2023 19:40
Copy link

codecov bot commented Dec 5, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6d437c4) 97.80% compared to head (88018d7) 97.81%.

❗ Current head 88018d7 differs from pull request most recent head a181472. Consider uploading reports for the commit a181472 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6371   +/-   ##
=======================================
  Coverage   97.80%   97.81%           
=======================================
  Files        1111     1111           
  Lines       96877    96948   +71     
=======================================
+ Hits        94754    94825   +71     
  Misses       2123     2123           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mpharrigan
Copy link
Collaborator

  • Is there anything in the standard library for doing this automatically?
  • should we limit the cache size? What If I make, run/simulate, and garbage-collect a whole lot of circuits on a whole lot of different qubits?

@maffoo
Copy link
Contributor Author

maffoo commented Dec 7, 2023

  • Is there anything in the standard library for doing this automatically?

I don't know of anything to do this automatically. Which is unfortunate because it seems like a rather natural thing to do, and in fact I think we could improve performance by doing this for other heavily-used cirq classes as well.

  • should we limit the cache size? What If I make, run/simulate, and garbage-collect a whole lot of circuits on a whole lot of different qubits?

We could do that, though I suspect it's not common to create so many different qubits that this would matter.

@maffoo
Copy link
Contributor Author

maffoo commented Dec 7, 2023

Changed to use a WeakValueDictionary for caching.

@maffoo maffoo force-pushed the u/maffoo/gq-cache branch from 9e26a02 to b8a4a62 Compare December 7, 2023 22:34
@maffoo maffoo force-pushed the u/maffoo/gq-cache branch from b8a4a62 to 74ff0df Compare December 7, 2023 23:22
@maffoo maffoo requested a review from mpharrigan December 7, 2023 23:43
Copy link
Collaborator

@dstrain115 dstrain115 left a comment

Choose a reason for hiding this comment

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

Seems fine to me modulo some extra comments to help future devs try to modify it.

@@ -178,22 +171,33 @@ class GridQid(_BaseGridQid):
cirq.GridQid(5, 4, dimension=2)
"""

def __init__(self, row: int, col: int, *, dimension: int) -> None:
"""Initializes a grid qid at the given row, col coordinate
_cache = weakref.WeakValueDictionary[Tuple[int, int, int], 'cirq.GridQid']()
Copy link
Collaborator

Choose a reason for hiding this comment

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

A line comment explaining this would be appreciated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

cls._cache[key] = inst
return inst

def __getnewargs_ex__(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like a pretty obscure dunder function. Maybe a docstring to explain what this is would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

@maffoo maffoo enabled auto-merge (squash) December 8, 2023 17:23
@maffoo maffoo merged commit 33c2573 into main Dec 8, 2023
@maffoo maffoo deleted the u/maffoo/gq-cache branch December 8, 2023 17:53
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
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.

3 participants