Skip to content

Commit

Permalink
[Named Topologies] LineTopology nodes_as_linequbits (#4536)
Browse files Browse the repository at this point in the history
This little helper function makes explicit the conversion between topology graphs and line qubits. This is analogous to `TiltedSquareLattice.nodes_as_gridqubits()`
  • Loading branch information
mpharrigan committed Sep 27, 2021
1 parent 9591615 commit e256423
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cirq-core/cirq/devices/named_topologies.py
Expand Up @@ -19,7 +19,7 @@
from typing import Dict, List, Tuple, Any, Sequence, Union, Iterable, TYPE_CHECKING

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

Expand Down Expand Up @@ -119,6 +119,10 @@ def __post_init__(self):
)
object.__setattr__(self, 'graph', graph)

def nodes_as_linequbits(self) -> List['cirq.LineQubit']:
"""Get the graph nodes as cirq.LineQubit"""
return [LineQubit(x) for x in sorted(self.graph.nodes)]

def draw(self, ax=None, tilted: bool = True, **kwargs) -> Dict[Any, Tuple[int, int]]:
"""Draw this graph using Matplotlib.
Expand Down
5 changes: 5 additions & 0 deletions cirq-core/cirq/devices/named_topologies_test.py
Expand Up @@ -78,6 +78,11 @@ def test_line_topology():
assert LineTopology(2).graph.number_of_nodes() == 2


def test_line_topology_nodes_as_qubits():
for n in range(2, 10, 2):
assert LineTopology(n).nodes_as_linequbits() == cirq.LineQubit.range(n)


@pytest.mark.parametrize('tilted', [True, False])
def test_draw_gridlike(tilted):
graph = nx.grid_2d_graph(3, 3)
Expand Down

0 comments on commit e256423

Please sign in to comment.