Commutation of Observables

I understand that qml.sample() only accepts observables that commute (which is reasonable from quantum mechanics). But I was wondering why the following is a valid operation in pennylane: qml.sample(qml.PauliY(0) @ qml.PauliX(0)) and does not return the same error as this: qml.sample(qml.PauliY(0)), qml.sample(qml.PauliX(0))? The only difference is that the later can be combined by multiplying the two outcomes to get same result as the former, assuming the two operators commute.

Hi @gidu,
I get no error when running:

import pennylane as qml
dev = qml.device("default.qubit", wires=2, shots=4)

@qml.qnode(dev)
def circuit(x):
    qml.RX(x, wires=0)
    qml.Hadamard(wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.sample(qml.PauliY(0)), qml.sample(qml.PauliX(0))

print(circuit(2))

Could you please check if your pennylane installation is up-to-date? Or perhaps provide a working example that reproduces your error?
Let me know if this helps, and if you have any other questions.

Thanks @adusko, it works with the updated version of pennylanne. My question then would be why the following is a valid operation:

pennylaneDiscussion

Considering that @ in this context is considered a tensor product according to Pennylanne documentation. Also, note that this operation is done on a single qubit, 0. I was wondering if the tensor product of the two Paulis qml.PauliY(0) @ qml.PauliX(0) should result in a 4 X 4 matrix observable on a qubit. Or is this just a normal matrix product?

Hi @gidu,
The @ operator default to the matrix product in this case.
A nice way to check this is:

import pennylane as qml
print((qml.PauliX(0) @ qml.PauliY(0)).matrix)
result:
[[0.+1.j 0.+0.j]
 [0.+0.j 0.-1.j]]
print((qml.PauliX(0) @ qml.PauliY(1)).matrix)
result:
[[0.+0.j 0.-0.j 0.+0.j 0.-1.j]
 [0.+0.j 0.+0.j 0.+1.j 0.+0.j]
 [0.+0.j 0.-1.j 0.+0.j 0.-0.j]
 [0.+1.j 0.+0.j 0.+0.j 0.+0.j]]

Please let us know if you have more questions.

1 Like

Thanks @adusko, this clarifies my doubts.

Gideon Uchehara

1 Like