Move heavy visualization imports outside base namespace imports (#4958)
Move heavy visualization imports outside base namespace imports (#4958)

* Make ipython detection occur at runtime

Importing IPython takes at least >150ms and is only used directly in one
function (and then only if it's in jupyter). This is a pretty heavy
import to run unconditionally at the module level, because we end up
wasting time if IPython is installed even if we're not inside jupyter
confirming the module is present despite never being able to use it.
This commit moves it to runtime to avoid inside the function to avoid
this import penalty, it will add som small overhead to the function but
since its not performance critical this is a fine tradeoff.

* Move PassManager.draw() visualization import to runtime

This commit moves the import of the qiskit visualization module for the
pass manager drawer from the module level in
qiskit.transpiler.passmanager to a run time import in the draw() method.
The visualization module import time is fairly heavy (when matplotlib is
installed) because of its usage of matplotlib. By having the import at
the module level this forces this import time penaly on just a bare
import of qiskit even if no visualization is going to be used. This is
because the passmaanger gets pulled in from the top level namespaces of
qiskit. To avoid this unecessary overhead this makes it a runtime import
so unless you go to use the passmanager.draw() method it won't be
imported. This will add some overhead to the draw() method (especially
if matplotlib hasn't already been imported) but since visualization
methods aren't performance critical and are typically quite slow this
isn't a a problem.

* Fix docs build

* Revert "Move PassManager.draw() visualization import to runtime"

Moving the visualization import to runtime has a backwards compatibility
issue in that the visualization module is no longer imported as part of
qiskit. This can potentially break users since visualization
functionality would be present without a second import. This reverts
this change to avoid that breaking change. A follow-on PR will try to
address the import time performance from matplotlib in a different way.

This reverts commit bcdb37f5ffd5881b3dee22282cb46d80d8e65d0e.

* Revert "Fix docs build"

This reverts commit 2967f0b77acfa7eac7a42550e169ed68c85d1b8f.

Co-authored-by: Kevin Krsulich <78cdb98aeddd45630046ff39f0a19d75c82aaaa7@ibm.com>
Co-authored-by: mergify[bot] <b09a6ee808b67e98a221404e7aaa52e0398a4954@users.noreply.github.com>
QISKit/qiskit-sdk-py,QISKit/qiskit-sdk-py,QISKit/qiskit-sdk-py