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
Expected behavior
qml.debug_probsdocuments that itsopargument accepts aMeasurementValue: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:Additional information
Root cause is in
pennylane/debugging/debugger.py,debug_probs(~line 409):The
if op:is meant only to check whether anopwas provided (i.e.op is not None) beforerunning the queue-cleanup line. For
op=Noneand for ordinaryOperatorobjects this happens tobehave correctly. But evaluating
if op:on aMeasurementValuecallsMeasurementValue.__bool__, which deliberately raises (a not-yet-evaluated measurement outcomehas no truth value). The function therefore crashes on exactly the
MeasurementValueinput thedocstring says is supported, before reaching
probs(wires, op).The fix is an identity check that never evaluates the object's truth value:
With that fix the underlying
qml.probs(op=<MeasurementValue>)path works and returns theexpected 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
System information
Existing GitHub issues