Skip to content

Use frozensets for key protocols#5560

Merged
CirqBot merged 8 commits into
quantumlib:masterfrom
daxfohl:frozenset
Jun 24, 2022
Merged

Use frozensets for key protocols#5560
CirqBot merged 8 commits into
quantumlib:masterfrom
daxfohl:frozenset

Conversation

@daxfohl

@daxfohl daxfohl commented Jun 19, 2022

Copy link
Copy Markdown
Collaborator

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, cduck and vtomole 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
Comment thread cirq-core/cirq/protocols/measurement_key_protocol_test.py Outdated
Comment thread cirq-core/cirq/protocols/measurement_key_protocol_test.py Outdated
Comment thread cirq-core/cirq/circuits/circuit.py Outdated

@pavoljuhas pavoljuhas left a comment

Copy link
Copy Markdown
Collaborator

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.

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

Copy link
Copy Markdown
Collaborator

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

Comment thread cirq-core/cirq/ops/gate_operation.py Outdated
Comment thread cirq-core/cirq/protocols/control_key_protocol.py Outdated

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

Copy link
Copy Markdown
Collaborator

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

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

Copy link
Copy Markdown
Collaborator

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