Skip to content

Commit 36fa7cb

Browse files
mpharriganrht
authored andcommitted
Named topology reprs (quantumlib#4701)
Adds repr to NamedTopology.
1 parent 8e31ed8 commit 36fa7cb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

cirq-core/cirq/devices/named_topologies.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
from typing import Dict, List, Tuple, Any, Sequence, Union, Iterable, TYPE_CHECKING
2020

2121
import networkx as nx
22+
from matplotlib import pyplot as plt
23+
24+
from cirq import _compat
2225
from cirq.devices import GridQubit, LineQubit
2326
from cirq.protocols.json_serialization import obj_to_dict_helper
24-
from matplotlib import pyplot as plt
2527

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

142+
def __repr__(self) -> str:
143+
return _compat.dataclass_repr(self)
144+
140145

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

245+
def __repr__(self) -> str:
246+
return _compat.dataclass_repr(self)
247+
240248

241249
def get_placements(
242250
big_graph: nx.Graph, small_graph: nx.Graph, max_placements=100_000

cirq-core/cirq/devices/named_topologies_test.py

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def test_tilted_square_lattice(width, height):
3636
assert nx.is_connected(topo.graph)
3737
assert nx.algorithms.planarity.check_planarity(topo.graph)
3838

39+
cirq.testing.assert_equivalent_repr(topo)
40+
3941

4042
def test_bad_tilted_square_lattice():
4143
with pytest.raises(ValueError):
@@ -82,6 +84,8 @@ def test_line_topology():
8284
assert LineTopology(2).n_nodes == 2
8385
assert LineTopology(2).graph.number_of_nodes() == 2
8486

87+
cirq.testing.assert_equivalent_repr(topo)
88+
8589

8690
def test_line_topology_nodes_as_qubits():
8791
for n in range(2, 10, 2):

0 commit comments

Comments
 (0)