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

Update equalstester in DeviceMetadata. #4840

Merged
merged 4 commits into from
Jan 14, 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
13 changes: 7 additions & 6 deletions cirq-core/cirq/devices/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def __init__(

self._nx_graph = nx_graph

@property
def qubit_set(self) -> Optional[FrozenSet['cirq.Qid']]:
"""Returns a set of qubits on the device, if possible.

Expand All @@ -215,6 +216,7 @@ def qubit_set(self) -> Optional[FrozenSet['cirq.Qid']]:
"""
return self._qubits_set

@property
def nx_graph(self) -> Optional['nx.Graph']:
"""Returns a nx.Graph where nodes are qubits and edges are couple-able qubits.

Expand All @@ -226,13 +228,12 @@ def nx_graph(self) -> Optional['nx.Graph']:
def _value_equality_values_(self):
graph_equality = None
if self._nx_graph is not None:
graph_equality = (sorted(self._nx_graph.nodes()), sorted(self._nx_graph.edges()))

qubit_equality = None
if self._qubits_set is not None:
qubit_equality = sorted(list(self._qubits_set))
graph_equality = (
tuple(sorted(self._nx_graph.nodes())),
tuple(sorted(self._nx_graph.edges(data='directed'))),
)

return qubit_equality, graph_equality
return self._qubits_set, graph_equality

def _json_dict_(self):
graph_payload = ''
Expand Down
24 changes: 20 additions & 4 deletions cirq-core/cirq/devices/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ def test_metadata():
qubits = cirq.LineQubit.range(4)
graph = nx.star_graph(3)
metadata = cirq.DeviceMetadata(qubits, graph)
assert metadata.qubit_set() == frozenset(qubits)
assert metadata.nx_graph() == graph
assert metadata.qubit_set == frozenset(qubits)
assert metadata.nx_graph == graph

metadata = cirq.DeviceMetadata()
assert metadata.qubit_set() is None
assert metadata.nx_graph() is None
assert metadata.qubit_set is None
assert metadata.nx_graph is None


def test_metadata_json_load_logic():
Expand All @@ -103,3 +103,19 @@ def test_metadata_json_load_logic():
str_rep = cirq.to_json(metadata)
output = cirq.read_json(json_text=str_rep)
assert metadata == output


def test_metadata_equality():
qubits = cirq.LineQubit.range(4)
graph = nx.star_graph(3)
graph2 = nx.star_graph(3)
graph.add_edge(1, 2, directed=False)
graph2.add_edge(1, 2, directed=True)

eq = cirq.testing.EqualsTester()
eq.add_equality_group(cirq.DeviceMetadata(qubits, graph))
eq.add_equality_group(cirq.DeviceMetadata(None, graph))
eq.add_equality_group(cirq.DeviceMetadata(qubits, None))
eq.add_equality_group(cirq.DeviceMetadata(None, None))

assert cirq.DeviceMetadata(None, graph) != cirq.DeviceMetadata(None, graph2)