Skip to content

[BUG] qml.debug_probs(op=<MeasurementValue>) crashes on a documented input via an if op: truthiness check #9652

Description

@YujinSong-hep

Expected behavior

qml.debug_probs documents that its op argument accepts a MeasurementValue:

op (Union[Operator, MeasurementValue]): an observable ... that rotates the computational
basis, or a MeasurementValue corresponding to mid-circuit measurements.

So passing a mid-circuit measurement value should return the probability distribution for that
measurement, e.g. [P(m0=0), P(m0=1)].

Actual behavior

qml.debug_probs(op=m0) raises immediately, before computing anything:

ValueError: The truth value of a MeasurementValue is undefined.
To condition on a MeasurementValue, please use qml.cond instead.

Additional information

Root cause is in pennylane/debugging/debugger.py, debug_probs (~line 409):

def debug_probs(wires=None, op=None):
    ...
    if op:                                            # <-- BUG: truthiness test
        QueuingManager.active_context().remove(op)    # cleanup: un-queue op if one was provided
    with QueuingManager.stop_recording():
        m = probs(wires, op)
    return _measure(m)

The if op: is meant only to check whether an op was provided (i.e. op is not None) before
running the queue-cleanup line. For op=None and for ordinary Operator objects this happens to
behave correctly. But evaluating if op: on a MeasurementValue calls
MeasurementValue.__bool__, which deliberately raises (a not-yet-evaluated measurement outcome
has no truth value). The function therefore crashes on exactly the MeasurementValue input the
docstring says is supported, before reaching probs(wires, op).

The fix is an identity check that never evaluates the object's truth value:

if op is not None:
    QueuingManager.active_context().remove(op)

With that fix the underlying qml.probs(op=<MeasurementValue>) path works and returns the
expected distribution.

Source code

import pennylane as qml

with qml.queuing.AnnotatedQueue() as q:
    qml.RX(1.23, wires=0)
    m0 = qml.measure(0)         

qml.debug_probs(op=m0)

Tracebacks

ValueError: The truth value of a MeasurementValue is undefined. To condition on a MeasurementValue, please use qml.cond instead.

System information

Version: 0.45.0
Platform info: macOS
Python version: 3.13.13

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 workingcommunity-botIssue suspected to be found by a bot

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions