Skip to content

Conversation

mtreinish
Copy link
Member

Summary

Just as has been demonstrated many times in the past we should not be
importing sympy at the module level. In #5358 another import was added
and was immediately flagged as an import speed regression [1]. Sympy is
a very heavy dependency and is slow to import (and slow to run too).
Adding a sympy import to the module level makes and is honestly a
dependency we should be reluctant to add anywhere because it becomes
the primary bottleneck whenever it is used. This commit removes the top
level sympy import and moves to be a runtime import in the single method
that uses sympy. Just as it was done in #5416, #5149, #4385, and #3156
before.

Details and comments

Fixes #5575

[1] https://qiskit.github.io/qiskit/#import.QiskitImport.time_qiskit_import?commits=300e9507

Just as has been demonstrated many times in the past we should not be
importing sympy at the module level. In Qiskit#5358 another import was added
and was immediately flagged as an import speed regression [1]. Sympy is
a very heavy dependency and is slow to import (and slow to run too).
Adding a sympy import to the module level makes and is honestly a
dependency we should be reluctant to add anywhere because it becomes
the primary bottleneck whenever it is used. This commit removes the top
level sympy import and moves to be a runtime import in the single method
that uses sympy. Just as it was done in Qiskit#5416, Qiskit#5149, Qiskit#4385, and Qiskit#3156
before.

Fixes Qiskit#5575

[1] https://qiskit.github.io/qiskit/#import.QiskitImport.time_qiskit_import?commits=300e9507
@mtreinish
Copy link
Member Author

I haven't dug into it but I feel like using sympy.srepr here is probably overkill/unnecessary especially since I expect it will be slower than an alternative solution. But in the short term this is at least a quick fix for the import performance regression.

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.

Given all the work on improving import time, maybe its the right time to add a test that checks that common large dependencies (sympy, matplotlib, networkx, ...) aren't pulled in by import qiskit.

@kdk kdk added this to the 0.17 milestone Jan 4, 2021
@kdk kdk merged commit 5989a31 into Qiskit:master Jan 4, 2021
@mtreinish mtreinish deleted the remove-sympy-module-import branch January 4, 2021 22:44
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Jan 5, 2021
This commit adds a new script that is run in CI to find whether slow
optional imports are in the default qiskit import path. We've recently
had several instances of PRs being pushed that added sympy to the
default import path which makes the overall qiskit import significantly
slower (see Qiskit#5576 for the most recent occurrence), this script will
catch those siutations and report the error.
mergify bot added a commit that referenced this pull request Feb 4, 2021
#5582)

* Add script to verify slow optional imports aren't used in default path

This commit adds a new script that is run in CI to find whether slow
optional imports are in the default qiskit import path. We've recently
had several instances of PRs being pushed that added sympy to the
default import path which makes the overall qiskit import significantly
slower (see #5576 for the most recent occurrence), this script will
catch those siutations and report the error.

* Use virtualenv in CI for running new script

The azure ci env was using system python instead of the venv where
qiskit-terra was installed for running the new script. This was causing
an error because to work the script imports qiskit to verify nothing
slow is getting imported. This commit fixes this by explicitly using the
venv python instead of the default system python to execute the script.

* Add script to makefile too

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import time regression after #5358
2 participants