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

Order of keys shouldn't matter when comparing cirq.google.KeyValueExecutableSpec #5073

Merged
merged 7 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cirq-google/cirq_google/workflow/quantum_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class KeyValueExecutableSpec(ExecutableSpec):
Args:
executable_family: A unique name to group executables.
key_value_pairs: A tuple of key-value pairs. The keys should be strings but the values
can be any immutable object.
can be any immutable object. Note that the order of the key-value pairs does NOT matter
when comparing two objects.
"""

executable_family: str
Expand Down Expand Up @@ -82,6 +83,12 @@ def _from_json_dict_(
def __repr__(self) -> str:
return cirq._compat.dataclass_repr(self, namespace='cirq_google')

def __eq__(self, other):
# The conversion to a dict object is required so that the order of the keys doesn't matter.
return (self.executable_family == other.executable_family) and (
dict(self.key_value_pairs) == dict(other.key_value_pairs)
)


@dataclass(frozen=True)
class BitstringsMeasurement:
Expand Down
27 changes: 27 additions & 0 deletions cirq-google/cirq_google/workflow/quantum_executable_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,30 @@ def test_quantum_executable_group_serialization(tmpdir):
cirq.to_json(eg, f'{tmpdir}/eg.json')
eg_reconstructed = cirq.read_json(f'{tmpdir}/eg.json')
assert eg == eg_reconstructed


def test_equality():
k1 = KeyValueExecutableSpec(
executable_family='test',
key_value_pairs=(
('a', 1),
('b', 2),
),
)
k2 = KeyValueExecutableSpec(
executable_family='test',
key_value_pairs=(
('b', 2),
('a', 1),
),
)
assert k1 == k2


def test_equality_from_dictionaries():
d1 = {'a': 1, 'b': 2}
d2 = {'b': 2, 'a': 1}
assert d1 == d2
k1 = KeyValueExecutableSpec.from_dict(d1, executable_family='test')
k2 = KeyValueExecutableSpec.from_dict(d2, executable_family='test')
assert k1 == k2