question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `torch` exception for `sort` to math's `single_dispatch.py`.

See original GitHub issue

Feature 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:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
RubidgeCarriecommented, Sep 24, 2021

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?

0reactions
glassnotescommented, Sep 27, 2021

Great thanks @RubidgeCarrie , we’ll give that a review as soon as possible!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found