Overcoming not subscriptable error

Hello!
Borrowing from an example from the PennyLane website, I’m trying to return the maximum value of probability measurements in a general way.
For example when I run:

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def circuit(x):
    qml.Hadamard(wires=1)
    #return qml.probs(wires=[0, 1])
    return [qml.math.where(qml.probs(wires=i)[0] > qml.probs(wires=i)[1], qml.probs(wires=i)[0], qml.probs(wires=i)[1]) for i in range(2)]

print(circuit(1.))

I get back

TypeError: 'ProbabilityMP' object is not subscriptable

Is there a way to get around the subscriptable error?

Thanks in advance for any and all ideas and help!

Hey @QML! Welcome back!

You can’t return a subscript of a measurement from a QNode — it must just be the measurement (un-subscripted). For example, this gives the same error you’re getting:

import pennylane as qml

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def circuit(x):
    qml.Hadamard(wires=1)
    #  return qml.probs(wires=0)[0] # this also gives the same thing
    return qml.probs(wires=[0, 1])[0]

print(circuit(1.))

If you want certain elements from the probability vector, it must be done after the measurement has been done, i.e., in post-processing, as follows:

import pennylane as qml

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def circuit(x):
    qml.Hadamard(wires=1)
    return qml.probs(wires=0)

print(circuit(1.)) # [1. 0.]
print(circuit(1.)[0]) # 0.9999999999999998

Hope this helps!

Hey @isaacdevlugt - yes, that does help - thanks for letting me know!

Awesome! Glad I could help :smiley: