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

Add arraylias demo repo #207

Closed to24toro closed 1 year ago

to24toro commented 1 year ago

Summary

I tested arraylike demo in the notebook.

Details and comments

In docs(https://numpy.org/devdocs/reference/typing.html#numpy.typing.ArrayLike),

Among others this includes the likes of:

So, we can use ArrayLike as a type hint which includes List, numpy.array, jax.numpy.array, and tensorflow.Tensor.

DanPuzzuoli commented 1 year ago

One concern this raises is that using numpy's ArrayLike may imply that anything implementing __array__ is valid, whereas we want to imply something more vague than that: ArrayLike means anything that a given instance of a numpy alias recognizes :).

This makes me lean towards defining our own ArrayLike, and documenting that it is synonymous with whatever is returned by Alias.registered_types(). @chriseclectic do you think it would make sense to add a static class property to Alias generically called something like RegisteredType, and we can override it for numpy_alias to return ArrayLike? (Or maybe ArrayliasLike?) So, e.g. in a file in Dynamics, we could do:

from qiskit_dynamics.arraylias_state import DYNAMICS_ALIAS
ArrayLike = DYNAMICS_ALIAS.RegisteredType

def some_function(a: ArrayLike, b: ArrayLike):
     ...

For comparison, JAX defines their own ArrayLike here. This isn't really analogous to the intended vagueness of what we're trying to do, but I just thought it was interesting they used the same type-hint name.

to24toro commented 1 year ago

My concerns consist of two points.

If we use this ArrayLike, we use it as a type hint that has array and can use

isinstance(np.array([1.2]), ArrayLike)

It is one way of defining our own ArrayLike.