Skip to content

Commit

Permalink
Add nodes_to_linequbits method to LineTopology (#5821)
Browse files Browse the repository at this point in the history
Resolves #4710

Co-authored-by: Pavol Juhas <juhas@google.com>
  • Loading branch information
Caffetaria and pavoljuhas authored Aug 11, 2022
1 parent c25d74f commit f39bb8a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cirq-core/cirq/devices/named_topologies.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ def draw(self, ax=None, tilted: bool = True, **kwargs) -> Dict[Any, Tuple[int, i
g2 = nx.relabel_nodes(self.graph, {n: (n, 1) for n in self.graph.nodes})
return draw_gridlike(g2, ax=ax, tilted=tilted, **kwargs)

def nodes_to_linequbits(self, offset: int = 0) -> Dict[int, 'cirq.LineQubit']:
"""Return a mapping from graph nodes to `cirq.LineQubit`
Args:
offset: Offset integer positions of the resultant LineQubits by this amount.
"""
return dict(enumerate(LineQubit.range(offset, offset + self.n_nodes)))

def _json_dict_(self) -> Dict[str, Any]:
return dataclass_json_dict(self)

Expand Down Expand Up @@ -240,8 +248,8 @@ def nodes_to_gridqubits(self, offset=(0, 0)) -> Dict[Tuple[int, int], 'cirq.Grid
"""Return a mapping from graph nodes to `cirq.GridQubit`
Args:
offset: Offest row and column indices of the resultant GridQubits by this amount.
The offest positions the top-left node in the `draw(tilted=False)` frame.
offset: Offset row and column indices of the resultant GridQubits by this amount.
The offset positions the top-left node in the `draw(tilted=False)` frame.
"""
return {(r, c): GridQubit(r, c) + offset for r, c in self.graph.nodes}

Expand Down
5 changes: 5 additions & 0 deletions cirq-core/cirq/devices/named_topologies_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def test_line_topology():
assert LineTopology(2).n_nodes == 2
assert LineTopology(2).graph.number_of_nodes() == 2

mapping = topo.nodes_to_linequbits(offset=3)
assert sorted(mapping.keys()) == list(range(n))
assert all(isinstance(q, cirq.LineQubit) for q in mapping.values())
assert all(mapping[x] == cirq.LineQubit(x + 3) for x in mapping)

cirq.testing.assert_equivalent_repr(topo)


Expand Down

0 comments on commit f39bb8a

Please sign in to comment.