Dictionary-like subscripting for CountsMP class

I am hoping to use the qml.counts function for a certain project, and based on the way that the CountsMP class is described in the documentation, it looks like it follows a dictionary-like data structure, but it is not possible to call specific values by subscripting. Is there a way to convert a CountsMP object into a dictionary?

Nevermind, silly question. I think I’m ok now.

Hey @DarthMalloc! Welcome to the forum :rocket:!

Glad you answered your own question! But, just want to be sure that everything is fine here. You can do the following:

import pennylane as qml

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

@qml.qnode(dev)
def circuit():
    qml.Hadamard(0)
    return qml.counts()

results = circuit()
print(results['0'], results['1'])

for key, val in zip(results.keys(), results.values()):
    print(key, val)

The results object behaves like a normal python dictionary :slight_smile:.

Hi @isaacdevlugt , Thank you very much! That gives me a better sense of how Pennylane works in general as well. It sounds like when you return any kind of measurement process from a function designated as a qml.node, the measurement process object gets “unwrapped” in the function call scope so that you can interact with it as a simple data structure.

1 Like

@DarthMalloc you got it! PennyLane QNodes behave very similarly to regular Python functions — this is intentional!