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
106 stars 60 forks source link

Arraylias integration - Rotating frame - #214

Closed to24toro closed 1 year ago

to24toro commented 1 year ago

Summary

I started to migrate Arraylias to rotating_frame.py. The first difficulty is dealing with csr_matrix and BCOO.

Details and comments

[TODO]

DanPuzzuoli commented 1 year ago

The to_array, to_csr, to_BCOO, and to_numeric_matrix_type are things that we'll need to think carefully about when converting to arraylias. Dynamics has been very loose with array types, and that has been partly enabled by these functions, which are used throughout the package to funnel a variety of types (including lists, lists of arrays, etc) into array and sparse types. It would be nice to not need these methods, but I'm not sure to what extent we can avoid using them.

One option would be to just start being very strict about types. E.g. the RotatingFrame methods could all require the inputs be ArrayLike (assuming csr_matrix and BCOO become registered Arraylias types). The reason I hesitate with fully going this direction is that it's natural to specify operators in models in a variety of ways, either as raw arrays/sparse matrices, qiskit Operator/QuantumState instances, qutip Qobj, or lists of these. We could just force users to convert these to their desired array types in most places, and make the to_* functions into publically available helper functions. I wouldn't want to do this everywhere, e.g. in Solver.solve, there is some high level handling of QuantumState types that I wouldn't want to get rid of.

Aside from these concerns, I don't think we want to register csr_matrix or BCOO as straight 'numpy'/'jax' types. Both require special syntax, so I think it will be necessary to register them as their own libs. E.g., in arraylias_state.py, I think we can do something like (with suitable try blocks in case JAX isn't installed):

DYNAMICS_ALIAS.register_type(csr_matrix, lib="scipy_sparse")

"""
register required custom versions of functions for csr type here
"""

DYNAMICS_ALIAS.register_type(BCOO, lib="jax_sparse")
"""
register required custom versions of functions for BCOO type here
"""

I think for scipy, there are also multiple different sparse types that we'll need to register (they get transformed into each other). The scipy.sparse function issparse is used to check for all of these types. Maybe we can look there for all of the types.

Some considerations with functions that need to be registered:

CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

to24toro commented 1 year ago

close and move to https://github.com/Qiskit-Extensions/qiskit-dynamics/pull/282.