Skip to content

Fix TypeError when checking unhashable gates in Gateset.__contains__ - #7963

Merged
pavoljuhas merged 5 commits into
quantumlib:mainfrom
Vivek1106-04:fix-unhashable-gate-gateset-contains
Mar 20, 2026
Merged

pavoljuhas merged 5 commits into
quantumlib:mainfrom
Vivek1106-04:fix-unhashable-gate-gateset-contains

Conversation

@Vivek1106-04

Copy link
Copy Markdown
Contributor

Fix TypeError: unhashable type when checking if an unhashable gate is contained in a Gateset

Fixes #7941

@Vivek1106-04
Vivek1106-04 requested review from a team and vtomole as code owners March 19, 2026 16:21
@Vivek1106-04
Vivek1106-04 requested a review from maffoo March 19, 2026 16:21
@github-actions github-actions Bot added the size: S 10< lines changed <50 label Mar 19, 2026
@codecov

codecov Bot commented Mar 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.63%. Comparing base (0c987fc) to head (a3f0ac9).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7963      +/-   ##
==========================================
- Coverage   99.63%   99.63%   -0.01%     
==========================================
  Files        1110     1110              
  Lines       99662    99678      +16     
==========================================
+ Hits        99296    99311      +15     
- Misses        366      367       +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread cirq-core/cirq/ops/gateset.py Outdated
Comment on lines +443 to +444
try:
if g in self._instance_gate_families:

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.

The try-except block may mask TypeError-s unrelated to g being hashable.

Please replace with if isinstance(g, Hashable) and g in ....

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done.

@Vivek1106-04
Vivek1106-04 force-pushed the fix-unhashable-gate-gateset-contains branch from 08a8981 to 9dd93ea Compare March 20, 2026 18:27

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

Please add test for the failing example in #7941 to convert_to_neutral_atom_gates_test.py.

Also, see more suggestions inline.

Comment thread cirq-core/cirq/ops/gateset_test.py Outdated
gate = cirq.UniformSuperpositionGate(m_value=3, num_qubits=2)
gs = cirq.Gateset(cirq.X, cirq.Y, cirq.Z)

# Should return False without raising TypeError

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.

Nit - please avoid unnecessary comments which are apparent from the code. These are likely to go out of date and become confusing later.

Comment thread cirq-core/cirq/ops/gateset_test.py Outdated
def test_gateset_contains_unhashable_gate() -> None:
"""Tests that unhashable gates can be checked for containment without raising TypeError."""
# UniformSuperpositionGate is unhashable (has __eq__ but no __hash__)
gate = cirq.UniformSuperpositionGate(m_value=3, num_qubits=2)

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.

This would not cover the changed code if we add hash support to UniformSuperpositionGate later. Please use a local test-only gate class (see raw_types_test.py for such pattern), for example,

class NonHashableGate(cirq.Gate):

    def _num_qubits_(self):
        return 1

    __hash__ = None

    def __eq__(self, other):
        return type(other) is NonHashableGate

Comment thread cirq-core/cirq/ops/gateset_test.py Outdated

# Should return False without raising TypeError
assert gate not in gs
assert gate.on(*cirq.LineQubit.range(2)) not in gs

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.

Also add a positive contains-test for type-created cirq.Gateset(NonHashableGate).

(instance created Gateset(NonHashableGate()) fails due to unhashable object, but supporting that is out-of-scope.)

@Vivek1106-04
Vivek1106-04 requested a review from pavoljuhas March 20, 2026 20:29

@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 a couple of tweaks. Thank you for taking this on!

@pavoljuhas
pavoljuhas enabled auto-merge March 20, 2026 21:41
@pavoljuhas
pavoljuhas disabled auto-merge March 20, 2026 21:43
@pavoljuhas
pavoljuhas enabled auto-merge March 20, 2026 21:44
@pavoljuhas
pavoljuhas added this pull request to the merge queue Mar 20, 2026
Merged via the queue into quantumlib:main with commit 353a928 Mar 20, 2026
41 checks passed
@Vivek1106-04
Vivek1106-04 deleted the fix-unhashable-gate-gateset-contains branch March 27, 2026 03:42
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.

is_native_neutral_atom_gate crashes with TypeError when checking unhashable gates

2 participants