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

Wrap qiskit.Aer and qiskit.IBMQ with lazy loading object #5619

Merged
merged 17 commits into from
Feb 5, 2021

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented Jan 12, 2021

Summary

This commit migrates the qiskit.Aer and qiskit.IBMQ module attributes to
be lazy loading instances of lazy loading wrapper classes. The intent
here is to avoid importing from qiskit-aer or qiskit-ibmq-provider from
qiskit-terra, while this is safe while the packages share a shared
namespace we've been actively working to remove the use of namespace
packaging (see #5089, #4767, and #559) and the circular
dependency caused by re-exporting these attributes is blocking progress
on this. By using a lazy loading wrapper class we avoid an import type
circular dependency and opportunistically use qiskit-aer and/or
qiskit-ibmq-provider at runtime only if they're present after everything
is imported. This also may have some benefit for the overall import
performance of qiskit (being tracked in #5100) as it removes qiskit-aer
and qiskit-ibmq-provider from the import path unless they're being used.

Details and comments

Fixes #5532

@mtreinish mtreinish added Changelog: API Change Include in the "Changed" section of the changelog Changelog: Removal Include in the Removed section of the changelog labels Jan 12, 2021
This commit migrates the qiskit.Aer and qiskit.IBMQ module attributes to
be lazy loading instances of lazy loading wrapper classes. The intent
here is to avoid importing from qiskit-aer or qiskit-ibmq-provider from
qiskit-terra, while this is safe while the packages share a shared
namespace we've been actively working to remove the use of namespace
packaging (see Qiskit#5089, Qiskit#4767, and Qiskit#559) and the circular
dependency caused by re-exporting these attributes is blocking progress
on this. By using a lazy loading wrapper class we avoid an import type
circular dependency and opportunistically use qiskit-aer and/or
qiskit-ibmq-provider at runtime only if they're present after everything
is imported. This also may have some benefit for the overall import
performance of qiskit (being tracked in Qiskit#5100) as it removes qiskit-aer
and qiskit-ibmq-provider from the import path unless they're being used.
Although the presence of qiskit.__qiskit_version__ might prevent any
performance improvements as it still imports all the elements to get
version information (and will be tackled in a separate PR).

Fixes Qiskit#5532
@nonhermitian
Copy link
Contributor

I thought these preloading of modules was to be removed. Are these to be removed later?

@mtreinish
Copy link
Member Author

I thought these preloading of modules was to be removed. Are these to be removed later?

My thinking was that this is a prerequisite for a deprecation and eventual removal of the attributes (it will probably be a long deprecation because these are widely used today). This gives us a place to raise a deprecation warnings (which wouldn't be an option otherwise while we still support py3.6) in the future while also fixing a potential import cycle today so we can move forward on removing the namespace packages..

@nonhermitian
Copy link
Contributor

Great. Tired of getting aer warnings when Aer doesn't even build on the platform.

@mtreinish
Copy link
Member Author

Ugh, this is failing the tutorials because ignis is using from qiskit import Aer to determine if Aer is available: https://github.com/Qiskit/qiskit-ignis/blob/master/qiskit/ignis/verification/topological_codes/fitters.py#L29-L34 which I wasn't able to preserve with this approach. To unblock I'll have to update the ignis code and switch CI to use ignis master (and we'll have to co-release ignis and terra)

@mtreinish
Copy link
Member Author

Actually now I'm not sure what's going on. That line is wrong (per the upgrade notes) but it's not clear why it's using BasicAer (which is what I'm assuming is the cause of the timeouts) there. Aer is installed in the tutorial job and from qiskit import Aer will always work with this PR so it should be using Aer. The issue we had in the tests with that for a skip condition was that CI jobs without aer installed would fail because the skip condition was based on from qiskit import Aer raising an importerror which it doesn't anymore

mtreinish added a commit to mtreinish/qiskit-ignis that referenced this pull request Jan 13, 2021
In Qiskit/qiskit#5619 how the qiskit.Aer provider gets loaded will
be changing and it will always be present, not just when Aer is
installed. In the topological codes fitters it was checking for the
presence of Aer using qiskit.Aer which won't continue to work moving
forward. To avoid this potential issue (and potentially unblock
Qiskit/qiskit#5619) this commit switches to using
'from qiskit.providers.aer import Aer' instead which lives in the
qiskit-aer project itself and will always import error if aer isn't
installed.
chriseclectic pushed a commit to qiskit-community/qiskit-ignis that referenced this pull request Jan 13, 2021
In Qiskit/qiskit#5619 how the qiskit.Aer provider gets loaded will
be changing and it will always be present, not just when Aer is
installed. In the topological codes fitters it was checking for the
presence of Aer using qiskit.Aer which won't continue to work moving
forward. To avoid this potential issue (and potentially unblock
Qiskit/qiskit#5619) this commit switches to using
'from qiskit.providers.aer import Aer' instead which lives in the
qiskit-aer project itself and will always import error if aer isn't
installed.
kdk
kdk previously approved these changes Feb 4, 2021
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.

Looks good, one comment on the release note.

@kdk kdk added the automerge label Feb 5, 2021
@mergify mergify bot merged commit 9d1d6a1 into Qiskit:master Feb 5, 2021
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Feb 5, 2021
In Qiskit#5582 we added a script that is used to ensure we don't import
slow optional dependencies in the default 'import qiskit' path. Since
that PR merged we've managed to remove a few more packages from the
default import path (see Qiskit#5619, Qiskit#5485, and Qiskit#5620) which we should add
the same checks to to ensure we don't regress and accidently start
importing them again in the default path. This commit adds all these
packages to the list of imports not allowed as part of 'import qiskit'.
mergify bot pushed a commit that referenced this pull request Feb 5, 2021
In #5582 we added a script that is used to ensure we don't import
slow optional dependencies in the default 'import qiskit' path. Since
that PR merged we've managed to remove a few more packages from the
default import path (see #5619, #5485, and #5620) which we should add
the same checks to to ensure we don't regress and accidently start
importing them again in the default path. This commit adds all these
packages to the list of imports not allowed as part of 'import qiskit'.
mtreinish added a commit to mtreinish/qiskit-ignis that referenced this pull request Feb 8, 2021
In Qiskit/qiskit#5619 how the qiskit.Aer provider gets loaded will
be changing and it will always be present, not just when Aer is
installed. In the topological codes fitters it was checking for the
presence of Aer using qiskit.Aer which won't continue to work moving
forward. To avoid this potential issue (and potentially unblock
Qiskit/qiskit#5619) this commit switches to using
'from qiskit.providers.aer import Aer' instead which lives in the
qiskit-aer project itself and will always import error if aer isn't
installed.

(cherry picked from commit 2e3298f)
@mtreinish mtreinish deleted the lazy-load-aer branch November 25, 2021 12:49
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Jun 27, 2023
* Wrap qiskit.Aer and qiskit.IBMQ with lazy loading object

This commit migrates the qiskit.Aer and qiskit.IBMQ module attributes to
be lazy loading instances of lazy loading wrapper classes. The intent
here is to avoid importing from qiskit-aer or qiskit-ibmq-provider from
qiskit-terra, while this is safe while the packages share a shared
namespace we've been actively working to remove the use of namespace
packaging (see Qiskit#5089, Qiskit#4767, and Qiskit#559) and the circular
dependency caused by re-exporting these attributes is blocking progress
on this. By using a lazy loading wrapper class we avoid an import type
circular dependency and opportunistically use qiskit-aer and/or
qiskit-ibmq-provider at runtime only if they're present after everything
is imported. This also may have some benefit for the overall import
performance of qiskit (being tracked in Qiskit#5100) as it removes qiskit-aer
and qiskit-ibmq-provider from the import path unless they're being used.
Although the presence of qiskit.__qiskit_version__ might prevent any
performance improvements as it still imports all the elements to get
version information (and will be tackled in a separate PR).

Fixes Qiskit#5532

* Fix lint

* Fix test lint

* DNM: test with ignis patch

* Revert "DNM: test with ignis patch"

This reverts commit ac9611c.

* Use ignis from source for tutorial job

* Update release note to be more clear
ElePT pushed a commit to ElePT/qiskit-algorithms-test that referenced this pull request Jul 17, 2023
…it#5619)

* Wrap qiskit.Aer and qiskit.IBMQ with lazy loading object

This commit migrates the qiskit.Aer and qiskit.IBMQ module attributes to
be lazy loading instances of lazy loading wrapper classes. The intent
here is to avoid importing from qiskit-aer or qiskit-ibmq-provider from
qiskit-terra, while this is safe while the packages share a shared
namespace we've been actively working to remove the use of namespace
packaging (see Qiskit/qiskit#5089, Qiskit/qiskit#4767, and Qiskit/qiskit#559) and the circular
dependency caused by re-exporting these attributes is blocking progress
on this. By using a lazy loading wrapper class we avoid an import type
circular dependency and opportunistically use qiskit-aer and/or
qiskit-ibmq-provider at runtime only if they're present after everything
is imported. This also may have some benefit for the overall import
performance of qiskit (being tracked in Qiskit/qiskit#5100) as it removes qiskit-aer
and qiskit-ibmq-provider from the import path unless they're being used.
Although the presence of qiskit.__qiskit_version__ might prevent any
performance improvements as it still imports all the elements to get
version information (and will be tackled in a separate PR).

Fixes Qiskit/qiskit#5532

* Fix lint

* Fix test lint

* DNM: test with ignis patch

* Revert "DNM: test with ignis patch"

This reverts commit ac9611c3ace30d101a70bda06e1987df14662182.

* Use ignis from source for tutorial job

* Update release note to be more clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: API Change Include in the "Changed" section of the changelog Changelog: Removal Include in the Removed section of the changelog performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid potential import cycles from Aer and IBMQ
3 participants