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 sparse modules to update dynamics by arraylias #286

Closed to24toro closed 1 year ago

to24toro commented 1 year ago

Summary

I added some modules of jax_sparse and numpy_sparse. I wrote the code, looking at the model class and don't know if it is suite for other classes. In order to make Operator input work, I think it is necessary to add Operator as a new type to dynamics arraylias. Maybe, we need to add list too, but I cannot conclude the necessarily of the list now because it is on going about integrating the model.

Details and comments

DanPuzzuoli commented 1 year ago

In order to make Operator input work, I think it is necessary to add Operator as a new type to dynamics arraylias.

Hmm possibly. I do know though that Operator implements __array__, so:

unp.asarray(Operator.from_label('X'))

works, and even

unp.asarray([Operator.from_label('X'), Operator.from_label('Y')])

already works as is. Maybe this is enough?

to24toro commented 1 year ago

Thanks. It's no need to add "Operator"(https://github.com/Qiskit/qiskit/blob/af643eb09b22fc3833084b3112890b1aebdcbf92/qiskit/quantum_info/operators/operator.py#L120) as you mentioned.

to24toro commented 1 year ago

Do we need rmatmul anymore? I think we had set this up to originally so we could dispatch on the second argument of matmul, but we may just be able to use the _numpy_multi_dispatch function with matmul instead.

Yes, it makes sense but I don't think we can use _numpy_multi_dispatch which only judges whether the array type is "numpy" or "jax" not "sparse. We need to add sparse types to _prefer_libs.

to24toro commented 1 year ago

Beyond this, I guess an issue with scipy sparse is that we store lists of them in numpy arrays, e.g. np.array([csr_matrix(...), csr_matrix(...)]) so the library inference doesn't work. One solution to this could be to define a new class with a name like SparseMatrixList that just stores the numpy object array of sparse matrices, is registered as a scipy_sparse type, and passes all ufuncs/numpy function calls through to the underlying numpy object array.

I think the reason we want to add list type to alias is to treat such as List[spase_matrix]. Therefore, it is a good idea.