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

Remove deepcopy usage from circuit_to_dag() #1530

Merged
merged 8 commits into from
Dec 18, 2018

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented Dec 17, 2018

Summary

The circuit_to_dag() function was previously using a deepcopy to make
copies of all input circuits. This could get quite expensive as it has
to update all the references. However, this deepcopy was only being used
to create a fresh copy of the instructions objects that were part of the
circuit. This instructions are passed directly to the dag in the output
so we can't pass by reference otherwise changes to the dag will break
the circuit. To avoid the performance hit with a deepcopy this commit
changes the function to instantiate new instruction objects right before
we add them to the dag.

Details and comments

Fixes #1528

The circuit_to_dag() function was previously using a deepcopy to make
copies of all input circuits. This could get quite expensive as it has
to update all the references. However, this deepcopy was only being used
to create a fresh copy of the instructions objects that were part of the
circuit. This instructions are passed directly to the dag in the output
so we can't pass by reference otherwise changes to the dag will break
the circuit. To avoid the performance hit with a deepcopy this commit
changes the function to instatiate new instruction objects right before
we add them to the dag.

Fixes Qiskit#1528
jaygambetta
jaygambetta previously approved these changes Dec 17, 2018
@jaygambetta
Copy link
Member

do we have speed improvement numbers

@mtreinish
Copy link
Member Author

mtreinish commented Dec 17, 2018

I've just done a small synthetic test running this function on a freshly loaded large circuit object (loaded from a 3500 line qasm file) and converting that to a dag. It was ~1.6x as fast for that quick conversion going from ~0.21 secs to ~0.13 secs. The raw data is:

With Patch:
0.128338
0.129884
0.125828
0.125982
0.126041
0.129518
0.126339
0.125583
0.126122
0.127914
0.125035
0.127082
0.126778
0.125588
0.130447

Without Patch:
0.205295
0.206874
0.207197
0.208566
0.214064
0.208503
0.208941
0.207808
0.210187
0.205799
0.207779
0.213378
0.209816
0.211099
0.205749

If you've have any specific benchmarks you'd like me to run I can quickly throw it together.

@mtreinish
Copy link
Member Author

mtreinish commented Dec 17, 2018

I ran transpile and compile on the same circuit from qasm and ran benchmark runs on those. For transpile() its a ~1.4x speed up going from ~0.27 secs to ~0.19 secs and for compile() it's a ~1.2x speed up going from ~1.33 secs to ~1.12 secs. The raw data from that testing is here: compile_transpile.txt

Heh, not quite as drastic as the >1000x speedup from the last time we did a patch like this in #1431

@jaygambetta jaygambetta merged commit b186529 into Qiskit:master Dec 18, 2018
@mtreinish mtreinish deleted the no-deepcopy-circuit-to-dag branch December 18, 2018 14:14
lia-approves pushed a commit to edasgupta/qiskit-terra that referenced this pull request Jul 30, 2019
* Remove deepcopy usage from circuit_to_dag()

The circuit_to_dag() function was previously using a deepcopy to make
copies of all input circuits. This could get quite expensive as it has
to update all the references. However, this deepcopy was only being used
to create a fresh copy of the instructions objects that were part of the
circuit. This instructions are passed directly to the dag in the output
so we can't pass by reference otherwise changes to the dag will break
the circuit. To avoid the performance hit with a deepcopy this commit
changes the function to instatiate new instruction objects right before
we add them to the dag.

Fixes Qiskit#1528

* Fix Lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove deepcopy from circuit_to_dag
3 participants