-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Remove list searches in CouplingMap.distance() #5294
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
Conversation
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
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.
There was a problem hiding this 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)?
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) |
There was a problem hiding this comment.
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?
* 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>
Summary
Previously, the
CouplingMap.distance()
method was checking whether thequbit 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