Add `torch` exception for `sort` to math's `single_dispatch.py`.
See original GitHub issueFeature details
Update handling of sort in qml.math such that the return values are the same across all interfaces.
The current behaviour is as follows. For numpy, jax, and tf,
>>> a = np.array([1, 3, 4, 2])
>>> qml.math.sort(a)
tensor([1, 2, 3, 4], requires_grad=True)
But for torch, the argument indices are also returned:
>>> a = torch.tensor([1, 3, 4, 2])
>>> qml.math.sort(a)
torch.return_types.sort(
values=tensor([1, 2, 3, 4]),
indices=tensor([0, 3, 1, 2]))
Implementation
It should be possible to just add a custom implementation in math/single_dispatch.py and then register the new function. For example (taken from the current implementation of _take_torch):
def _sort_torch(tensor):
# Do the sort
# return only the sorted array
ar.register_function("torch", "sort", _sort_torch)
How important would you say this feature is?
1: Not important. Would be nice to have.
Additional information
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
python3: singledispatch in class, how to dispatch self type
You cannot use functools.singledispatch on methods at all, not as a decorator at least. Python 3.8 adds a new option, just for methods: ......
Read more >Requirements and discussion of a type dispatcher for the ...
A multiple-dispatcher can insert a DuckArray into the type hierarchy. ... A type-hierarchy would be a way to “sort” which loop to prefer, ......
Read more >codenamewei/pydata-science-playground: Snippets of data ... - GitHub
Snippets of data science reusable components in Python - GitHub ... Return a sorted array: sorted(arr); Get index of a value: arr.index(value); Add...
Read more >Multiple dispatch in __torch_function__ – PyTorch ... - Podtail
Python is a single dispatch OO language, but there are some ... so that invocations of torch.add with different subclasses work properly.
Read more >Package List — Spack 0.18.0 documentation
This is a list of things you can install using Spack. ... adwaita-icon-theme, openmolcas, py-torch-scatter ... boinc-client, perl-math-cdf, r-affycontam.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi @glassnotes I am new to the open source community and wanted to ask if it is alright if I try this as my first issue?
Great thanks @RubidgeCarrie , we’ll give that a review as soon as possible!