Skip to content

Imports: Speed improvement for importing subpackages #5100

@drewrisinger

Description

@drewrisinger

What is the expected enhancement?

IMPORT SPEED of top-level qiskit/__init__.py

Inspired by @mtreinish in #4958 .

Currently, it takes on the order of seconds to import just the base qiskit package, and that doesn't improve if you only import subpackages (e.g. qiskit.pulse) due to the top-level import of heavy packages in ./qiskit/__init__.py, e.g. qiskit.providers.ibmq which are always imported.

Here, I am mostly looking in this issue at import speed improvements in qiskit/__init__.py.

Possible Import Improvements

I see a few possible solutions, ranked in order of more breaking changes:

  1. (low-hanging fruit): defer matplotlib.animation import in qiskit.visualization.transition_visualization to the animation function
  2. (low-hanging fruit): defer networkx import in qiskit.circuit.equivalence to the draw() and _build_basis_graph() (only used in draw()) functions so this isn't on the critical path every import.
  3. Add an environment variable on the Aer & IBMQ provider imports to optionally disable them
  4. Add an environment variable on the qiskit.circuit imports to optionally disable them. (e.g. QISKIT_DONT_IMPORT_TOP_LEVEL)

Import Time visualization

This can be checked visually with:

pip install tuna
pip install qiskit-terra qiskit-ibmq-provider
python -X importtime -c "import qiskit.pulse" 2> import_pulse.log
tuna import_pulse.log

Which yields (qiskit-terra==0.15.1, qiskit-ibmq-provider==0.8.0):
image

which is similar to that in #4958 (I must have a slower computer/not be running on SSD):
image

We see that import qiskit.pulse ends up importing the top-level qiskit init file anyways. As a side note, I'd really prefer not to spend ~3 seconds importing qiskit-terra every time.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions