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

Add nodes_to_linequbits method to LineTopology #5821

Merged
merged 5 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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:
Caffetaria marked this conversation as resolved.
Show resolved Hide resolved
offset: Offset integer position of the resultant LineQubits by this amount.
Caffetaria marked this conversation as resolved.
Show resolved Hide resolved
"""
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
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 @@ -85,6 +85,10 @@ 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)
Caffetaria marked this conversation as resolved.
Show resolved Hide resolved
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