-
Notifications
You must be signed in to change notification settings - Fork 631
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
Fix qml.math.get_interface
for scipy
input
#7015
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
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.
math.dot
might need to have scipy
branch very first since this interface does not have quite some things in numpy
https://github.com/PennyLaneAI/pennylane/actions/runs/13552851763/job/37880423118?pr=7015 It's expected that |
Also not sure how hard it could be to make |
I just manually checked |
The problem is that this is happening under the hood now. import autoray
from autoray import numpy as np
>>> np.coerce(1., like="scipy")
ImportError: autoray couldn't find function 'coerce' for backend 'scipy'. when normally it would be "numpy". |
We should just be able to do:
in |
It seems only the >>> np.coerce
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/yushao.chen/Documents/pennylane/venv/lib/python3.11/site-packages/numpy/__init__.py", line 333, in __getattr__
raise AttributeError("module {!r} has no attribute "
AttributeError: module 'numpy' has no attribute 'coerce'. Did you mean: 'source'?
>>> import autoray as ar
>>> ar.numpy.coerce
functools.partial(<function do at 0x7f26b0e69760>, 'coerce') |
Edit: Ignore this! It's recursive because they are both calling the same function. Hmm, I did the following in that file, def _builtins_coerce(x, like=None):
return ar.numpy.coerce(x, like=like)
...
ar.register_function("builtins", "coerce", _builtins_coerce)
...
# -------------------------------- SciPy --------------------------------- #
# the following is required to ensure that SciPy sparse Hamiltonians passed to
# qml.SparseHamiltonian are not automatically 'unwrapped' to dense NumPy arrays.
...
ar.register_function("scipy", "coerce", ar.numpy.coerce)
... since just adding the scipy one gave me the same
|
>>> X
array([[0, 1],
[1, 0]])
>>> b
<Compressed Sparse Row sparse matrix of dtype 'complex128'
with 2 stored elements and shape (2, 2)>
>>> np.dot(X, b)
array([[<Compressed Sparse Row sparse matrix of dtype 'complex128'
with 2 stored elements and shape (2, 2)> ,
<Compressed Sparse Row sparse matrix of dtype 'complex128'
with 2 stored elements and shape (2, 2)> ],
[<Compressed Sparse Row sparse matrix of dtype 'complex128'
with 2 stored elements and shape (2, 2)> ,
<Compressed Sparse Row sparse matrix of dtype 'complex128'
with 2 stored elements and shape (2, 2)> ]],
dtype=object) |
I just found that we register:
🤦♀️ |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #7015 +/- ##
=======================================
Coverage 99.60% 99.60%
=======================================
Files 484 484
Lines 46249 46305 +56
=======================================
+ Hits 46065 46122 +57
+ Misses 184 183 -1 ☔ View full report in Codecov by Sentry. |
I'm trying to understand, why eventually we successfully got rid of the recursive imports Error 😂 |
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 a lot!
Oh no 💣 it seems still the same bug as before: >>> a = csr_matrix([1])
>>> a
<Compressed Sparse Row sparse matrix of dtype 'int64'
with 1 stored elements and shape (1, 1)>
>>> qml.math.coerce(a, like="scipy")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/yushao.chen/.local/lib/python3.11/site-packages/autoray/autoray.py", line 81, in do
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/yushao.chen/.local/lib/python3.11/site-packages/autoray/autoray.py", line 81, in do
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/yushao.chen/.local/lib/python3.11/site-packages/autoray/autoray.py", line 81, in do
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
[Previous line repeated 990 more times]
File "/home/yushao.chen/.local/lib/python3.11/site-packages/autoray/autoray.py", line 79, in do
backend = _choose_backend(fn, args, kwargs, like=like)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/yushao.chen/.local/lib/python3.11/site-packages/autoray/autoray.py", line 402, in _choose_backend
return _infer_auto(fn, args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/yushao.chen/.local/lib/python3.11/site-packages/autoray/autoray.py", line 93, in _default_infer_from_sig
return _DISPATCHERS[fn](*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/yushao.chen/.local/lib/python3.11/site-packages/autoray/autoray.py", line 1390, in default_dispatcher
return infer_backend(args[0])
^^^^^^^^^^^^^^^^^^^^^^
File "/home/yushao.chen/.local/lib/python3.11/site-packages/autoray/autoray.py", line 301, in infer_backend
return _infer_class_backend_cached(array.__class__)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded in comparison
>>> |
Also note that in |
Co-authored-by: Christina Lee <[email protected]>
…into fix-get-interface
Context:
qml.math.get_interface
defaults incorrectly to"numpy"
when thelen(input) > 1
and the input is not one of the supported ML frameworks. This was specifically noticed for inputs containingscipy
csr_matrix
.Description of the Change:
A few things were done,
Source-code:
get_interface
to handle which interface is returned.multi_dispatch.py
theres no need to usear.numpy
since were already importedautograd.numpy
asnp
. So we can clean that up.Tests:
qml.math.coerce
tests for jax, autograd and scipy (even though they should trivially do nothing because of,Benefits: Correct code.
Possible Drawbacks: None identified.
Related GitHub Issues: Fixes #7012
[sc-85330]