Avoid module level sympy yet another time #5576
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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