-
Notifications
You must be signed in to change notification settings - Fork 60
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
Add sparse modules to update dynamics by arraylias #286
Add sparse modules to update dynamics by arraylias #286
Conversation
Hmm possibly. I do know though that
works, and even
already works as is. Maybe this is enough? |
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.
Overall looks good - excited for these changes.
Comments:
- I made a bunch of suggestions - just making sure the file and function doc strings are full sentences.
- Can we get rid of
to_numeric_matrix_type
by registering a suitable default forasarray
instead? - Do we need
rmatmul
anymore? I think we had set this up to originally so we could dispatch on the second argument ofmatmul
, but we may just be able to use the_numpy_multi_dispatch
function withmatmul
instead.
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.
from qiskit_dynamics.type_utils import isinstance_qutip_qobj | ||
|
||
|
||
def register_to_numeric_matrix_type(alias): |
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.
I'm wondering now if this is actually necessary - this basically functions as unp.asarray
except it has a default. Can we instead just register a default for asarray
? I think this should work - is there some intended distinction between asarray
and to_numeric_matrix_type
that I'm not remembering?
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.
Yes. I'm just considering it, but I cannot decide it because it is used in model files I have not seen yet.
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.
Certainly, I also feel we can integrate it to asarray.
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.
Perhaps for now just remove it and we'll see if it can be replaced effectively with asarray
in the model files.
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. |
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
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. |
I think the reason we want to add |
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.
Aside from the direct comments, another change I would suggest is to put the register_fallback
and register_default
functions together in the files (currently the default
functions appear at the beginning, and the fallback
functions appear at the end).
I believe these suggestions should fix the current issues. There is another problem with tests raising warnings, but I think these are the result of a JAX update possibly. I will see if I can resolve these in a separate PR.
Edit: the docs build warnings should be fixed in #287 .
from qiskit_dynamics.type_utils import isinstance_qutip_qobj | ||
|
||
|
||
def register_to_numeric_matrix_type(alias): |
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.
Perhaps for now just remove it and we'll see if it can be replaced effectively with asarray
in the model files.
Co-authored-by: Daniel Puzzuoli <[email protected]>
Co-authored-by: Daniel Puzzuoli <[email protected]>
removing import of non-existant register functions
Removing calls of non-existant functions
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.
This LGTM. The only thing missing is some tests - can you add a few test cases for the new types/functions?
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.
Thanks Kento - the tests look good. I've left a few comments.
The main thing is to add validation for the output type of the dispatched functions. I've commented in a few places, but I think this should be done everywhere.
"""Test matmul for scipy_sparse.""" | ||
x = csr_matrix([[1, 0], [0, 1]]) | ||
y = csr_matrix([[2, 2], [2, 2]]) | ||
self.assertAllClose(csr_matrix.toarray(unp.matmul(x, y)), [[2, 2], [2, 2]]) |
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.
validate type of unp.matmul(x, y)
|
||
x = BCOO.fromdense([[1, 0], [0, 1]]) | ||
y = BCOO.fromdense([[2, 2], [2, 2]]) | ||
self.assertAllClose(BCOO.todense(unp.matmul(x, y)), [[2, 2], [2, 2]]) |
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.
same thing here, validate type of unp.matmul(x, y)
from ...common import QiskitDynamicsTestCase | ||
|
||
|
||
class TestMultiplyFunction(QiskitDynamicsTestCase): |
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.
TestMatmulFunction
from ...common import QiskitDynamicsTestCase | ||
|
||
|
||
class TestMultiplyFunction(QiskitDynamicsTestCase): |
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.
TestRmatmulFunction
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