Skip to content
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

[BUG] Torch interface incompatibility with finite-shot mid-circuit measurements #5442

Closed
1 task done
trbromley opened this issue Mar 27, 2024 · 3 comments · Fixed by #5672
Closed
1 task done

[BUG] Torch interface incompatibility with finite-shot mid-circuit measurements #5442

trbromley opened this issue Mar 27, 2024 · 3 comments · Fixed by #5672
Labels
bug 🐛 Something isn't working

Comments

@trbromley
Copy link
Contributor

Expected behavior

Able to execute the following QNode which includes:

  • Use of torch interface
  • Use of mid-circuit measurements
  • Finite shots and a sample-based output

Actual behavior

Error when following QNode is executed.

Additional information

No response

Source code

import pennylane as qml
import torch

dev = qml.device("default.qubit", shots=10)

@qml.qnode(dev, interface="torch")
def f(x):
    qml.RX(x, 0)
    for i in range(5):
        qml.Hadamard(0)
        qml.measure(0)
    return qml.sample(qml.PauliX(0))

x = torch.tensor(0.4)
f(x)

Tracebacks

AttributeError: 'Tensor' object has no attribute 'append'

System information

On the PL dev branch.

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@trbromley trbromley added the bug 🐛 Something isn't working label Mar 27, 2024
@trbromley
Copy link
Contributor Author

Related to this forum post.

@trbromley
Copy link
Contributor Author

CC @vincentmr.

@albi3ro
Copy link
Contributor

albi3ro commented Mar 28, 2024

Different errors, but also fails for jax and tensorflow.

albi3ro added a commit that referenced this issue Apr 1, 2024
**Context:**

While investigating Issue #5442, I was getting really confused by why my
tensorflow execution was giving a torch error. I realized that when the
execution fails, we do not properly reset `QNode.interface` back to
`"auto"`.

**Description of the Change:**

Pulls a block of the code to a helper method so that we can wrap it in a
`try-finally` block.

**Benefits:**

We can continue using a qnode after a failure has occured during
execution.

**Possible Drawbacks:**

**Related GitHub Issues:**

[sc-60047]

---------

Co-authored-by: Astral Cai <astral.cai@xanadu.ai>
@mudit2812 mudit2812 linked a pull request May 2, 2024 that will close this issue
@mudit2812 mudit2812 linked a pull request May 15, 2024 that will close this issue
mudit2812 added a commit that referenced this issue May 24, 2024
**Context:**
Opened in favour of #5630. Bug fix for #5442. This PR updates
`dynamic_one_shot` so that it has better compatibility with the `torch`
and `jax` interfaces.

**Description of the Change:**
* Change casting method from `array.astype()` to `qml.math.cast` in the
`apply_operation` dispatch for `MidMeasureMP`.
* Update usage of `qml.math` in `dynamic_one_shot`.
* When using `qml.counts`, cast results to ints before converting to
strings for lists of MCM values and floats for single MCM values. This
is needed because jax arrays are not hashable, and the hash of torch
tensors seems to be independent of the value(s) stored inside it. Thus,
neither can be used as keys for dictionaries.

**Benefits:**
Better interface support with `dynamic_one_shot`.

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: Vincent Michaud-Rioux <vincent.michaud-rioux@xanadu.ai>
Co-authored-by: Vincent Michaud-Rioux <vincentm@nanoacademic.com>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Astral Cai <astral.cai@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
Co-authored-by: Pietropaolo Frisoni <pietropaolo.frisoni@xanadu.ai>
Co-authored-by: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com>
Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
Co-authored-by: Guillermo Alonso-Linaje <65235481+KetpuntoG@users.noreply.github.com>
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Isaac De Vlugt <34751083+isaacdevlugt@users.noreply.github.com>
Co-authored-by: Diksha Dhawan <40900030+ddhawan11@users.noreply.github.com>
Co-authored-by: Isaac De Vlugt <isaacdevlugt@gmail.com>
Co-authored-by: Diego <67476785+DSGuala@users.noreply.github.com>
Co-authored-by: trbromley <brotho02@gmail.com>
Co-authored-by: erick-xanadu <110487834+erick-xanadu@users.noreply.github.com>
Co-authored-by: David Ittah <dime10@users.noreply.github.com>
Co-authored-by: soranjh <40344468+soranjh@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants