Skip to content

Commit

Permalink
Named topology reprs (#4701)
Browse files Browse the repository at this point in the history
Adds repr to NamedTopology.
  • Loading branch information
mpharrigan authored Nov 23, 2021
1 parent 42ac9da commit d42e058
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cirq-core/cirq/devices/named_topologies.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
from typing import Dict, List, Tuple, Any, Sequence, Union, Iterable, TYPE_CHECKING

import networkx as nx
from matplotlib import pyplot as plt

from cirq import _compat
from cirq.devices import GridQubit, LineQubit
from cirq.protocols.json_serialization import obj_to_dict_helper
from matplotlib import pyplot as plt

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -137,6 +139,9 @@ def draw(self, ax=None, tilted: bool = True, **kwargs) -> Dict[Any, Tuple[int, i
def _json_dict_(self) -> Dict[str, Any]:
return dataclass_json_dict(self)

def __repr__(self) -> str:
return _compat.dataclass_repr(self)


@dataclass(frozen=True)
class TiltedSquareLattice(NamedTopology):
Expand Down Expand Up @@ -237,6 +242,9 @@ def nodes_to_gridqubits(self, offset=(0, 0)) -> Dict[Tuple[int, int], 'cirq.Grid
def _json_dict_(self) -> Dict[str, Any]:
return dataclass_json_dict(self)

def __repr__(self) -> str:
return _compat.dataclass_repr(self)


def get_placements(
big_graph: nx.Graph, small_graph: nx.Graph, max_placements=100_000
Expand Down
4 changes: 4 additions & 0 deletions cirq-core/cirq/devices/named_topologies_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def test_tilted_square_lattice(width, height):
assert nx.is_connected(topo.graph)
assert nx.algorithms.planarity.check_planarity(topo.graph)

cirq.testing.assert_equivalent_repr(topo)


def test_bad_tilted_square_lattice():
with pytest.raises(ValueError):
Expand Down Expand Up @@ -82,6 +84,8 @@ def test_line_topology():
assert LineTopology(2).n_nodes == 2
assert LineTopology(2).graph.number_of_nodes() == 2

cirq.testing.assert_equivalent_repr(topo)


def test_line_topology_nodes_as_qubits():
for n in range(2, 10, 2):
Expand Down

0 comments on commit d42e058

Please sign in to comment.