Skip to content

Commit

Permalink
Remove list searches in CouplingMap.distance()
Browse files Browse the repository at this point in the history
Previously, the CouplingMap.distance() method was checking whether the
qubit number was in the coupling graph by searching over a list for the
coupling map indices. This search becomes a bottleneck at large qubit
numbers because in the worst case it has to traverse the entire list to
find a match. This also is unecessary because the coupling map indices
are always an ordered range from 0 to n for n qubits. This commit fixes
this bottleneck by updating the check to just error if the index is out
of bounds for the size of the graph.

Related to Qiskit#5197
  • Loading branch information
mtreinish committed Oct 27, 2020
1 parent 662f1fa commit 12c2de5
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions qiskit/transpiler/coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ def distance(self, physical_qubit1, physical_qubit2):
Raises:
CouplingError: if the qubits do not exist in the CouplingMap
"""
if physical_qubit1 not in self.physical_qubits:
raise CouplingError("%s not in coupling graph" % (physical_qubit1,))
if physical_qubit2 not in self.physical_qubits:
raise CouplingError("%s not in coupling graph" % (physical_qubit2,))
size = self.size()
if physical_qubit1 >= size:
raise CouplingError("%s not in coupling graph" % physical_qubit1)
if physical_qubit2 >= size:
raise CouplingError("%s not in coupling graph" % physical_qubit2)
if self._dist_matrix is None:
self._compute_distance_matrix()
return int(self._dist_matrix[physical_qubit1, physical_qubit2])
Expand Down

0 comments on commit 12c2de5

Please sign in to comment.