Is there a way to get the value of each individual observable in a hamiltonian?


I was wondering if there was a way to obtain the expectation value of a single term in a summation of a hamiltonian.

For instance, for the observable 0.5X_1 + 0.5Z_1, i want to get the observable 0.5X_1 and 0.5Z_1 individually.

Hey @trifire! You can totally do this. Here is an example:

import pennylane as qml

# H = 0.5X_1 + 0.5Z_1

H = 0.5 * qml.PauliX(0) + 0.5 * qml.PauliZ(0)

# Out: [PauliX(wires=[0]), PauliZ(wires=[0])]

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

def circuit(H):
    return [qml.expval(op) for op in H.ops]

# Out: tensor([0., 1.], requires_grad=True)

Is there a way to do this while preserving the qubit wise commuting measurement groups?

This is actually already done under the hood as of v0.24 :tada:! You can find the entry in the v0.24 changelog by searching for qml.split_non_commuting, or check out the documentation.