Skip to content

[BUG] MCMs postselecting on an impossible outcome with fill-shots produce confusing results. #8332

@astralcai

Description

@astralcai

Expected behavior

Some kind of meaningful warning or error message when postselecting on an impossible outcome.

Actual behavior

Consider the following circuit:

import pennylane as qml

@qml.set_shots(10000)
@qml.qnode(qml.device('default.qubit'), postselect_mode='fill-shots')
def circuit():
    m = qml.measure(0, postselect=1)
    return qml.counts(op=m, all_outcomes=True)

>>> circuit()
{0.0: 0, 1.0: 10000}

According to the documentation, fill-shots should be interpreted as "sampling until the number of valid samples matches the total number of shots", but what is the expected behaviour when it's impossible to get a single valid sample? Creating impossible measurement outcomes out of thin air is confusing. This happens with the default tree-traversal and one-shot.

When deferred is used, we get a different behaviour which is also confusing:

@qml.set_shots(10000)
@qml.qnode(qml.device('default.qubit'), mcm_method="deferred", postselect_mode='fill-shots')
def circuit():
    m = qml.measure(0, postselect=1)
    return qml.counts(op=m, all_outcomes=True)

>>> circuit()
{0: 10000, 1: 0}

which is not unphysical anymore, but the postselect condition is blatantly violated.

Additional information

No response

Source code

Tracebacks

System information

Name: pennylane
Version: 0.43.0.dev59
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: 
Author: 
Author-email: 
License-Expression: Apache-2.0
Location: /Users/.../pennylane/venv/lib/python3.11/site-packages
Editable project location: /Users/.../pennylane
Requires: appdirs, autograd, autoray, cachetools, diastatic-malt, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, tomlkit, typing_extensions
Required-by: pennylane-qrack, pennylane_catalyst, pennylane_lightning, pennylane_lightning_kokkos

Platform info:           macOS-15.7-arm64-arm-64bit
Python version:          3.11.13
Numpy version:           1.26.4
Scipy version:           1.15.3
JAX version:             0.6.2
Installed devices:
- default.clifford (pennylane-0.43.0.dev59)
- default.gaussian (pennylane-0.43.0.dev59)
- default.mixed (pennylane-0.43.0.dev59)
- default.qubit (pennylane-0.43.0.dev59)
- default.qutrit (pennylane-0.43.0.dev59)
- default.qutrit.mixed (pennylane-0.43.0.dev59)
- default.tensor (pennylane-0.43.0.dev59)
- null.qubit (pennylane-0.43.0.dev59)
- reference.qubit (pennylane-0.43.0.dev59)
- lightning.kokkos (pennylane_lightning_kokkos-0.43.0.dev32)
- nvidia.custatevec (pennylane_catalyst-0.13.0.dev57)
- nvidia.cutensornet (pennylane_catalyst-0.13.0.dev57)
- oqc.cloud (pennylane_catalyst-0.13.0.dev57)
- softwareq.qpp (pennylane_catalyst-0.13.0.dev57)
- lightning.qubit (pennylane_lightning-0.43.0.dev32)
- qrack.ace (pennylane-qrack-0.22.9)
- qrack.simulator (pennylane-qrack-0.22.9)
- qrack.stabilizer (pennylane-qrack-0.22.9)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions