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

Use frozensets for key protocols #5560

Merged
merged 8 commits into from
Jun 24, 2022
Merged

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Jun 19, 2022

Fixes #5557, using frozenset in key protocol dunder methods, thus allowing the top-level protocol method to avoid defensive copies. Provide a temporary warning until v0.16 for third-party dunders to do the same. This speeds up tight loops about 15% in my testing. @95-martin-orion

Note this is not breaking, as FrozenSet is a subclass of AbstractSet. (AbstractSet is a readonly interface of a set, and is superclass of both FrozenSet and plain Set). Changed the return types to FrozenSet to be more explicit and make chaining easier.

@daxfohl daxfohl requested review from a team, vtomole and cduck as code owners June 19, 2022 06:11
@daxfohl daxfohl requested a review from pavoljuhas June 19, 2022 06:11
@CirqBot CirqBot added the size: M 50< lines changed <250 label Jun 19, 2022
Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

LGTM with fixing a few cosmetic issues.

Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

FrozenSet being a subclass of AbstractSet works in the case of (1) old external code calling internal magic, but may cause trouble when (2) internal code calls old external magic. Does Cirq at any point rely on the frozen-ness of these sets (e.g. for hashing)?

cirq-core/cirq/ops/gate_operation.py Outdated Show resolved Hide resolved
cirq-core/cirq/protocols/control_key_protocol.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

LGTM modulo protocol type fixes.

@daxfohl daxfohl requested a review from 95-martin-orion June 24, 2022 05:41
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

LGTM

@95-martin-orion 95-martin-orion added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 24, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jun 24, 2022
@CirqBot CirqBot merged commit 90c45d2 into quantumlib:master Jun 24, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jun 24, 2022
@daxfohl daxfohl deleted the frozenset branch June 24, 2022 23:42
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Fixes quantumlib#5557, using frozenset in key protocol dunder methods, thus allowing the top-level protocol method to avoid defensive copies. Provide a temporary warning until v0.16 for third-party dunders to do the same. This speeds up tight loops about 15% in my testing. @95-martin-orion 

Note this is not breaking, as FrozenSet is a subclass of AbstractSet. (AbstractSet is a readonly interface of a set, and is superclass of both FrozenSet and plain Set). Changed the return types to FrozenSet to be more explicit and make chaining easier.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Fixes quantumlib#5557, using frozenset in key protocol dunder methods, thus allowing the top-level protocol method to avoid defensive copies. Provide a temporary warning until v0.16 for third-party dunders to do the same. This speeds up tight loops about 15% in my testing. @95-martin-orion 

Note this is not breaking, as FrozenSet is a subclass of AbstractSet. (AbstractSet is a readonly interface of a set, and is superclass of both FrozenSet and plain Set). Changed the return types to FrozenSet to be more explicit and make chaining easier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid defensive copying in keys protocols
4 participants