Skip to content

Commit

Permalink
Improve efficiency of CouplingMap.make_symmetric (#9571) (#9618)
Browse files Browse the repository at this point in the history
* Improve efficiency of CouplingMap.make_symmetric

Right now the CouplingMap.make_symmetric has a quadratic overhead. It
was iterating over a list of each edgess' endpoints in the graph
and for each edge it was iterating over the same list again to check
whether the reverse edge is present or not. The overhead for this for
large coupling maps can be quite large, for example with a 10497 qubit
heavy hex coupling map the time it took to run this method as part of
SabreLayout's initialization was 10x slower than actually running the
pass (a equally sized Bernstein-Vazirani circuit). Instead of doing an
O(n) contains check inside the loop this commit updates it to use a
set which will be an O(1) lookup. This should address the performance
issue with this method. In the future we should leverage the native
rustworkx method to do this, which is being added in:
Qiskit/rustworkx#814

* Further reduce the overhead by avoiding CouplingMap.add_edge()

The CouplingMap add_edge() method has similar overhead to check whether
nodes need to be added. However, in the case of make_symmetric we don't
need this checking because both endpoints are already in the graph. This
commit further improves the performance of the method by just adding the
reverse edge directly to the graph via the rustworkx api instead.

(cherry picked from commit 5afbb0b)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
  • Loading branch information
mergify[bot] and mtreinish committed Feb 17, 2023
1 parent b5c5761 commit d7ca395
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions qiskit/transpiler/coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,13 @@ def make_symmetric(self):
"""
Convert uni-directional edges into bi-directional.
"""
# TODO: replace with PyDiGraph.make_symmetric() after rustworkx
# 0.13.0 is released.
edges = self.get_edges()
edge_set = set(edges)
for src, dest in edges:
if (dest, src) not in edges:
self.add_edge(dest, src)
if (dest, src) not in edge_set:
self.graph.add_edge(dest, src, None)
self._dist_matrix = None # invalidate
self._is_symmetric = None # invalidate

Expand Down

0 comments on commit d7ca395

Please sign in to comment.