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] Confusing errors when post-selection produces no valid shots. #6504

Open
1 task done
astralcai opened this issue Nov 1, 2024 · 0 comments
Open
1 task done
Labels
bug 🐛 Something isn't working

Comments

@astralcai
Copy link
Contributor

astralcai commented Nov 1, 2024

Expected behavior

When a circuit containing mid-circuit measurements with post-selection is executed with finite shots, there is always a possibility that none of the shots produced the measurement outcome that we are post-selecting on. The way we handle this scenario should be

  1. Consistent across different error methods.
  2. Clear to the user what is causing the error and/or invalid results.

Actual behavior

Consider the following circuit:

import pennylane as qml

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

def circuit():
    qml.X(0)
    m = qml.measure(0, postselect=0)
    return qml.probs(wires=[0])

When we use dynamic one-shot, the circuit executes to completion and returns nan:

>>> qml.QNode(circuit, dev, mcm_method="one-shot")()
nan

With tree-traversal, we now get an error:

>>> qml.QNode(circuit, dev, mcm_method="tree-traversal")() 
ZeroDivisionError: division by zero

With deferred measurement, we get a different error:

>>> qml.QNode(circuit, dev, mcm_method="deferred")()
ValueError: cannot reshape array of size 0 into shape (0)

This can be difficult to debug for a user with complex circuits.

Additional information

No response

Source code

No response

Tracebacks

No response

System information

Name: PennyLane
Version: 0.39.0.dev31
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/astral.cai/Workspace/pennylane
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing_extensions
Required-by: PennyLane-Catalyst, PennyLane_Lightning, PennyLane_Lightning_Kokkos

Platform info:           macOS-15.1-arm64-arm-64bit
Python version:          3.10.14
Numpy version:           1.26.4
Scipy version:           1.12.0
Installed devices:
- default.clifford (PennyLane-0.39.0.dev31)
- default.gaussian (PennyLane-0.39.0.dev31)
- default.mixed (PennyLane-0.39.0.dev31)
- default.qubit (PennyLane-0.39.0.dev31)
- default.qutrit (PennyLane-0.39.0.dev31)
- default.qutrit.mixed (PennyLane-0.39.0.dev31)
- default.tensor (PennyLane-0.39.0.dev31)
- null.qubit (PennyLane-0.39.0.dev31)
- reference.qubit (PennyLane-0.39.0.dev31)
- lightning.qubit (PennyLane_Lightning-0.39.0.dev48)
- nvidia.custatevec (PennyLane-Catalyst-0.9.0.dev41)
- nvidia.cutensornet (PennyLane-Catalyst-0.9.0.dev41)
- oqc.cloud (PennyLane-Catalyst-0.9.0.dev41)
- softwareq.qpp (PennyLane-Catalyst-0.9.0.dev41)
- lightning.kokkos (PennyLane_Lightning_Kokkos-0.39.0.dev38)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@astralcai astralcai added the bug 🐛 Something isn't working label Nov 1, 2024
@astralcai astralcai changed the title [BUG] Confusing errors raised when post-selection produces no valid shots. [BUG] Confusing errors when post-selection produces no valid shots. Nov 1, 2024
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

No branches or pull requests

1 participant