Skip to content

Conversation

mtreinish
Copy link
Member

Summary

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 unnecessary 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.

Details and comments

Related to #5197

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
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Nov 16, 2020
In sabre_swap the comprehension in _score_heuristic() with the basic
heuristic starts to become a bottleneck as the size of the coupling map
increases. This probably becomes the top bottleneck in a transpilation
after Qiskit#5316, Qiskit#5183, Qiskit#5294, Qiskit#5267, and Qiskit#5272 are merged. This commit is
an attempt to try and mitigate that a bit by using numpy native
operations instead of a python comprehension in sum(). If this is
insufficient we'll likely have to either avoid doing a sum like this or
drop down to a lower level with cython or rust and operate on the array
directly to remove this bottleneck.
Copy link
Member

@kdk kdk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the docstring to point out that qubit indices need to be consecutive, non-negative integers (e.g. that holes aren't supported, and indices omitted from the couplinglist will be implicitly added)?

Comment on lines +178 to +181
if physical_qubit1 >= self.size():
raise CouplingError("%s not in coupling graph" % physical_qubit1)
if physical_qubit2 >= self.size():
raise CouplingError("%s not in coupling graph" % physical_qubit2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to likewise check if0 > physical_qubit1 here?

ajavadia
ajavadia previously approved these changes Nov 25, 2020
mergify bot added a commit that referenced this pull request Nov 25, 2020
* Use numpy sum instead of comprehension in _score_heuristic

In sabre_swap the comprehension in _score_heuristic() with the basic
heuristic starts to become a bottleneck as the size of the coupling map
increases. This probably becomes the top bottleneck in a transpilation
after #5316, #5183, #5294, #5267, and #5272 are merged. This commit is
an attempt to try and mitigate that a bit by using numpy native
operations instead of a python comprehension in sum(). If this is
insufficient we'll likely have to either avoid doing a sum like this or
drop down to a lower level with cython or rust and operate on the array
directly to remove this bottleneck.

* Make distance matrix a coupling map property

* Fix tests

* Fix lint

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@mergify mergify bot merged commit 3fa3657 into Qiskit:master Dec 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants