Skip to content

Commit

Permalink
Leverage retworkx generators for all cmap constructors
Browse files Browse the repository at this point in the history
In Qiskit#5183 we switched the internal graph datastructures used for the
CouplingMap to be based on retworkx instead of networkx. That PR was
written against the 0.6.0 retworkx release and for the from_grid()
and from_full() constructors python had to be used since retworkx
was missing functions to generate graphs of those types. However, in the
recent 0.7.x retworkx release 2 new generators directed_grid_graph() [1]
and directed_mesh_graph() [2] were added. This commit switches the
implementation of from_grid() and from_full() to use these generators,
which significantly speeds up (and reduces the memory overhead) of the
constructor methods.

The only constructor which still relies on a Python loop is the
from_full() method when bidirection=False. This is because retworkx
doesn't offer a constructor that will build a graph like that. If in
a future release a generator is added we can remove that for loop too.

[1] https://retworkx.readthedocs.io/en/stable/stubs/retworkx.generators.directed_grid_graph.html
[2] https://retworkx.readthedocs.io/en/stable/stubs/retworkx.generators.directed_mesh_graph.html
  • Loading branch information
mtreinish committed Dec 4, 2020
1 parent 3fa3657 commit 1259055
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions qiskit/transpiler/coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,14 @@ def reduce(self, mapping):
def from_full(cls, num_qubits, bidirectional=True):
"""Return a fully connected coupling map on n qubits."""
cmap = cls(description='full')
edge_list = []
for i in range(num_qubits):
for j in range(i):
edge_list.append((j, i))
if bidirectional:
edge_list.append((i, j))
cmap.graph.extend_from_edge_list(edge_list)
if bidirectional:
cmap.graph = rx.generators.directed_mesh_graph(num_qubits)
else:
edge_list = []
for i in range(num_qubits):
for j in range(i):
edge_list.append((j, i))
cmap.graph.extend_from_edge_list(edge_list)
return cmap

@classmethod
Expand All @@ -303,25 +304,8 @@ def from_ring(cls, num_qubits, bidirectional=True):
def from_grid(cls, num_rows, num_columns, bidirectional=True):
"""Return qubits connected on a grid of num_rows x num_columns."""
cmap = cls(description='grid')
edge_list = []
for i in range(num_rows):
for j in range(num_columns):
node = i * num_columns + j

up = (node-num_columns) if i > 0 else None # pylint: disable=invalid-name
down = (node+num_columns) if i < num_rows-1 else None
left = (node-1) if j > 0 else None
right = (node+1) if j < num_columns-1 else None

if up is not None and bidirectional:
edge_list.append((node, up))
if left is not None and bidirectional:
edge_list.append((node, left))
if down is not None:
edge_list.append((node, down))
if right is not None:
edge_list.append((node, right))
cmap.graph.extend_from_edge_list(edge_list)
cmap.graph = rx.generators.directed_grid_graph(
num_rows, num_columns, bidirectional=bidirectional)
return cmap

def largest_connected_component(self):
Expand Down

0 comments on commit 1259055

Please sign in to comment.