Skip to content

Conversation

@ACE07-Sev
Copy link
Contributor

closes #6376 .

@ACE07-Sev ACE07-Sev requested review from a team and vtomole as code owners December 3, 2025 20:00
@ACE07-Sev ACE07-Sev requested a review from senecameeks December 3, 2025 20:00
@github-actions github-actions bot added the size: M 50< lines changed <250 label Dec 3, 2025
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.

Please fix for unsortable sets and make sure the tests exercise some equal, but different-order sets; see inline comments for more details.

types.
"""

if isinstance(val, set):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if isinstance(val, set):
if isinstance(val, (frozenset, set)):

And the same below.

"""

if isinstance(val, set):
val = sorted(val)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will throw TypeError for set sa0 = {"a", 0}, but such set should be approx_eq to itself - approx_eq(sa0, sa0) is True.

Please add a test for unsortable sets, ie, that asserts cirq.approx_eq({"a", 0}, {"a", 0}) is true as expected.

Suggested change
val = sorted(val)
try:
val = sorted(val)
except TypeError:
return NotImplemented

and the same for other below.

You will also need to change the caller to correctly handle a NotImplemented value here -

# If the values are iterable, try comparing recursively on items.
if isinstance(val, Iterable) and isinstance(other, Iterable):
return _approx_eq_iterables(val, other, atol=atol)

Comment on lines +156 to +159
for n in range(10, 20):
assert cirq.approx_eq(
frozenset(cirq.LineQubit.range(n)), frozenset({*cirq.LineQubit.range(n)})
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This does not test the new code as shown by coverage error. set and frozenset use the same hash and are constructed from the same sequence so this test works without sorting too:

for n in range(10, 20):
    assert list(frozenset(cirq.LineQubit.range(n))) == list(frozenset({*cirq.LineQubit.range(n)}))

To test the new code, you need to find sets for which the iteration sequence changes with insertion order:

found_differently_ordered_sets = False
for i in range(20):
    for j in range(i + 1, 20):
        sij = {cirq.q(i), cirq.q(j)}
        sji = {cirq.q(j), cirq.q(i)}
        if list(sij) != list(sji):
            found_differently_ordered_sets = True
            assert cirq.approx_eq(sij, sji)
            assert cirq.approx_eq(frozenset(sij), sji)

assert found_differently_ordered_sets

Comment on lines +36 to +40
assert cirq.approx_eq("ab", "ab", atol=1e-3)
assert not cirq.approx_eq("ab", "ac", atol=1e-3)
assert not cirq.approx_eq("1", "2", atol=999)
assert not cirq.approx_eq("test", 1, atol=1e-3)
assert not cirq.approx_eq("1", 1, atol=1e-3)
Copy link
Collaborator

@pavoljuhas pavoljuhas Dec 3, 2025

Choose a reason for hiding this comment

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

Please revert all formatting changes that convert single to double quotes. They make it harder to review the code and also make git-blame less useful.

We may decide to standardize double quotes later, but that would need its own PR (which can be added to .git-blame-ignore-revs).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing, I accidentally called ruff on it. I'll revert them in my commit.

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.

cirq.approx_eq is inconsistent for sets

2 participants