# Commutation of Observables

**URL:** <https://discuss.pennylane.ai/t/commutation-of-observables/1635>\
**Category:** Codebook\
**Created:** [January 26, 2022, 2:45am UTC](https://discuss.pennylane.ai/t/commutation-of-observables/1635 "2022-01-26T02:45:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gidu](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@gidu](https://discuss.pennylane.ai/u/gidu)\
**Post date:** [January 26, 2022, 2:45am UTC](https://discuss.pennylane.ai/t/commutation-of-observables/1635/1 "2022-01-26T02:45:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adusko](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/adusko/32/883_2.png) [@adusko](https://discuss.pennylane.ai/u/adusko)\
**Post date:** [January 26, 2022, 4:45pm UTC](https://discuss.pennylane.ai/t/commutation-of-observables/1635/2 "2022-01-26T16:45:19Z")

</div>

Hi @gidu,  
I get no error when running:

```auto
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.

---

<div class="post-metadata">

**Author:** ![gidu](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@gidu](https://discuss.pennylane.ai/u/gidu)\
**Post date:** [January 26, 2022, 8:13pm UTC](https://discuss.pennylane.ai/t/commutation-of-observables/1635/3 "2022-01-26T20:13:27Z")

</div>

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

![pennylaneDiscussion](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/28946164b1d98031aadc8193c42c4afdcab60c64.png)

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?

---

<div class="post-metadata">

**Author:** ![adusko](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/adusko/32/883_2.png) [@adusko](https://discuss.pennylane.ai/u/adusko)\
**Post date:** [January 26, 2022, 9:22pm UTC](https://discuss.pennylane.ai/t/commutation-of-observables/1635/4 "2022-01-26T21:22:57Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![gidu](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@gidu](https://discuss.pennylane.ai/u/gidu)\
**Post date:** [January 26, 2022, 9:27pm UTC](https://discuss.pennylane.ai/t/commutation-of-observables/1635/5 "2022-01-26T21:27:06Z")

</div>

Thanks @adusko, this clarifies my doubts.

Gideon Uchehara
