Skip to content
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

Avoid deepcopy in sabre_swap #5316

Merged
merged 2 commits into from
Nov 2, 2020
Merged

Conversation

mtreinish
Copy link
Member

Summary

This commit changes the deepcopy usage in the sabreswap pass to a
shallow copy. In sabre_swap the deepcopy was run on the DAGCircuit node
when remapping it, however this was not necessary because it's not
actually used where shared references matter. The output from the
remapper is not used directly and instead a copy of the DAGNode object
is just used as a container for the required args in
apply_operation_back() where a new DAGNode is always created from the
op, qargs, etc. The remapping just replaces the qargs parameter with the
remapped one. So this commit changes the deepcopy to a shallow copy
which won't have the performance overhead.

Details and comments

Fixes #5197

This commit changes the deepcopy usage in the sabreswap pass to a
shallow copy. In sabre_swap the deepcopy was run on the DAGCircuit node
when remapping it, however this was not necessary because it's not
actually used where shared references matter. The output from the
remapper is not used directly and instead a copy of the DAGNode object
is just used as a container for the required args in
apply_operation_back() where a new DAGNode is always created from the
op, qargs, etc. The remapping just replaces the qargs parameter with the
remapped one. So this commit changes the deepcopy to a shallow copy
which won't have the performance overhead.

Fixes Qiskit#5197
@mtreinish mtreinish requested a review from a team as a code owner October 29, 2020 16:52
@mtreinish mtreinish added performance stable backport potential The bug might be minimal and/or import enough to be port to stable labels Oct 29, 2020
@mergify mergify bot merged commit a506ba9 into Qiskit:master Nov 2, 2020
mergify bot pushed a commit that referenced this pull request Nov 2, 2020
This commit changes the deepcopy usage in the sabreswap pass to a
shallow copy. In sabre_swap the deepcopy was run on the DAGCircuit node
when remapping it, however this was not necessary because it's not
actually used where shared references matter. The output from the
remapper is not used directly and instead a copy of the DAGNode object
is just used as a container for the required args in
apply_operation_back() where a new DAGNode is always created from the
op, qargs, etc. The remapping just replaces the qargs parameter with the
remapped one. So this commit changes the deepcopy to a shallow copy
which won't have the performance overhead.

Fixes #5197

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit a506ba9)
mergify bot added a commit that referenced this pull request Nov 3, 2020
This commit changes the deepcopy usage in the sabreswap pass to a
shallow copy. In sabre_swap the deepcopy was run on the DAGCircuit node
when remapping it, however this was not necessary because it's not
actually used where shared references matter. The output from the
remapper is not used directly and instead a copy of the DAGNode object
is just used as a container for the required args in
apply_operation_back() where a new DAGNode is always created from the
op, qargs, etc. The remapping just replaces the qargs parameter with the
remapped one. So this commit changes the deepcopy to a shallow copy
which won't have the performance overhead.

Fixes #5197

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit a506ba9)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Luciano Bello <luciano.bello@ibm.com>
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.
@mtreinish mtreinish deleted the fix-copy-sabre branch November 23, 2020 18:19
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>
@kdk kdk added the Changelog: None Do not include in changelog label Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog performance stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sabre swap mapper dominated by data copy
3 participants