qiskit-community / qiskit-dynamics

Tools for building and solving models of quantum systems in Qiskit
https://qiskit-community.github.io/qiskit-dynamics/
Apache License 2.0
105 stars 61 forks source link

Add global instances of NumPy/SciPy aliases #260

Closed DanPuzzuoli closed 1 year ago

DanPuzzuoli commented 1 year ago

Summary

Adds the global instances of numpy/scipy aliases into dynamics, as well as an updated way to write tests for multiple array backends.

Details and comments

In the end the biggest change is to the test infrastructure provided in test/dynamics/common.py. The existing solution we have been using for writing test classes for multiple array backends uses inheritance. This has worked pretty well, but given that are going to be making large scale changes to tests, it would be nice if this could be implemented with a single decorator, which would make it easy to add/remove array backends from test cases without having to create a bunch of test classes manually.

The new decorator test_array_backends in common.py aims to achieve this by automating the creation of the subclasses for testing different array backends. It is a little bit "hacky" as it needs to dynamically create new classes, and the implemented solution is the one way I could find to do this. One minor issue is that the subclasses generated by test_array_backends have methods asarray, etc, that the original test class needs to make use of but does not define. (Basically the original test class does not need to be a well-defined class - it can be written to rely on the functionality that gets added to it by the decorator.) A result of this is that no-member warnings get raised by the linter for the test class that is being decorated by test_array_backends. It would be nice to figure out a way to do this more "properly" to get rid of this, but I think I can also live with it.

to24toro commented 1 year ago

@DanPuzzuoli Is this branch going to be merged to the main branch?

DanPuzzuoli commented 1 year ago

@DanPuzzuoli Is this branch going to be merged to the main branch?

Yes - this is the start of making arraylias-related changes to main.

DanPuzzuoli commented 1 year ago

Thanks for the suggestion @to24toro - I think that makes the function a bit cleaner.

Changes: