-
Notifications
You must be signed in to change notification settings - Fork 192
Delay imports of optional dependencies #2485
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
base: main
Are you sure you want to change the base?
Conversation
`qiskit_aer` and `scipy` were eagerly imported in the `utils.json` module (with fallbacks for when they were not installed) which is imported along with `import qiskit_ibm_runtime`. Here these imports are delayed until JSON serialization or deserialization are attempted. In the serialization case, the packages would have already been imported before objects from them would need to be serialized any way. Avoiding these imports saves roughly half a second (but the number depends on the computer running the test).
|
I don't have any urgency for merging this. It was something I noticed while debugging qiskit-experiments' imports (that scipy and qiskit-aer were hardly used but eagerly imported), so I thought I would see what it looks like to avoid importing them. By the way, deserialization of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice thinking! I have not done any investigating yet about whether this would fit in qiskit-ibm-runtime specifically, but I'm wondering if any of qiskit's tooling for optionals might help us here, or if you think it would be better to just do it manually as this PR currently implements?
I found it convenient in samplomatic to declare consts of optional dependencies and to use them.
|
The problem with the Qiskit tooling is that it is aimed at avoiding import errors for optional dependencies that might not be installed. For the serialization code modified in this PR, we are trying to avoid importing a package that is installed but is not needed. The serialization code is going through a big list of tests to see which kind of object the current one is and use its serialization method. For Aer and Scipy, it wants to check the object with (We probably don't need to worry about it but I could imagine some clever code that implements the We could use the Qiskit tooling on the deserialization part. We already have to try the import right away in that case. As Qiskit has narrowed its focus over the recent years into 1.0 and 2.0, I have been more hesitant to use features that are not related to the primary focus of quantum circuit building but I also don't feel that strongly against using the lazy tester in serialization code. It would still avoid the import time hit on |
qiskit_aerandscipywere eagerly imported in theutils.jsonmodule (with fallbacks for when they were not installed) which is imported along withimport qiskit_ibm_runtime. Here these imports are delayed until JSON serialization or deserialization are attempted. In the serialization case, the packages would have already been imported before objects from them would need to be serialized any way.Avoiding these imports saves roughly half a second (but the number depends on the computer running the test).