# What is the difference between qml.measure and qml.PauliZ?

**URL:** <https://discuss.pennylane.ai/t/what-is-the-difference-between-qml-measure-and-qml-pauliz/4388>\
**Category:** PennyLane Help\
**Created:** [April 3, 2024, 12:41pm UTC](https://discuss.pennylane.ai/t/what-is-the-difference-between-qml-measure-and-qml-pauliz/4388 "2024-04-03T12:41:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nilserik](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nilserik](https://discuss.pennylane.ai/u/nilserik)\
**Post date:** [April 3, 2024, 12:41pm UTC](https://discuss.pennylane.ai/t/what-is-the-difference-between-qml-measure-and-qml-pauliz/4388/1 "2024-04-03T12:41:49Z")

</div>

Hello,  
`qml.measure` measures the qubit in the computational basis, which is the Pauli Z basis, I would say. But why do I get different results with `qml.expval(op=measurement)` and `qml.expval(qml.PauliZ(wires=0)`?  
Below you can find a minimal working example:

```auto
import pennylane as qml
from pennylane import numpy as np

device = qml.device('default.qubit', wires=1)

@qml.qnode(device)
def circuit_pauli_z(params):
    qml.RX(params, wires=0)
    return qml.expval(qml.PauliZ(wires=0))

@qml.qnode(device)
def circuit_measure(params):
    qml.RX(params, wires=0)
    measurement = qml.measure(wires=0)

    return qml.expval(op=measurement)

params = 0.75 * np.pi

result_pauli_z = circuit_pauli_z(params)
result_measurement = circuit_measure(params)

print(result_pauli_z)
print(result_measurement)

```

which gives:

```auto
-0.7071067811865475
0.8535533905932737

```

From my understanding, the first result is the expected one. But what is the second output (the one from `qml.measure`?

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [April 3, 2024, 7:05pm UTC](https://discuss.pennylane.ai/t/what-is-the-difference-between-qml-measure-and-qml-pauliz/4388/2 "2024-04-03T19:05:46Z")

</div>

Hey @nilserik! Great question.

Indeed, `qml.expval(op=m0)` averages over computational basis measurements (pauli Z basis), but in the _0/1 notation_, not ±1 notation. You can see this with finite shots and `sample`:

```auto
device = qml.device('default.qubit', wires=1, shots=100)

@qml.qnode(device)
def circuit_measure():
    qml.Hadamard(0)
    measurement = qml.measure(wires=0)

    return qml.expval(op=measurement), qml.sample(measurement)

result_measurement = circuit_measure()
print(result_measurement)

```

```auto
(tensor(0.39, requires_grad=True), tensor([0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,
        0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0,
        1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0,
        0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0,
        1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1], requires_grad=True))

```

So, to answer your question, `qml.expval(qml.Z(0))` and `qml.expval(op=measurement)` here are the exact same thing… up to a linear transformation of multiplying by 2 and substracting one 😅.

This could definitely be clearer in the documentation for `qml.measure`. I’ll make sure to make this obvious!

---

<div class="post-metadata">

**Author:** ![nilserik](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nilserik](https://discuss.pennylane.ai/u/nilserik)\
**Post date:** [April 4, 2024, 6:15am UTC](https://discuss.pennylane.ai/t/what-is-the-difference-between-qml-measure-and-qml-pauliz/4388/3 "2024-04-04T06:15:10Z")

</div>

Hi @isaacdevlugt, thanks for your reply. This clarifies everything 😀!

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [April 4, 2024, 1:41pm UTC](https://discuss.pennylane.ai/t/what-is-the-difference-between-qml-measure-and-qml-pauliz/4388/4 "2024-04-04T13:41:18Z")

</div>

Another thing to say here is that `qml.measure(0)` and `qml.Projector([1], wires=0)` are similar, since `qml.measure` is really the `|1><1|` observable behind the scenes. So `qml.expval(qml.measure(wire))` and `qml.expval(qml.Projector([1], wire))` should be the same 🙂

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [April 4, 2024, 6:57pm UTC](https://discuss.pennylane.ai/t/what-is-the-difference-between-qml-measure-and-qml-pauliz/4388/5 "2024-04-04T18:57:53Z")

</div>

Here’s the PR for the docstring fix: [Choice of computational basis convention for `qml.measure` is clear in its docstring by isaacdevlugt · Pull Request #5474 · PennyLaneAI/pennylane · GitHub](https://github.com/PennyLaneAI/pennylane/pull/5474)

---

<div class="post-metadata">

**Author:** ![nilserik](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nilserik](https://discuss.pennylane.ai/u/nilserik)\
**Post date:** [April 4, 2024, 7:21pm UTC](https://discuss.pennylane.ai/t/what-is-the-difference-between-qml-measure-and-qml-pauliz/4388/6 "2024-04-04T19:21:49Z")

</div>

Cool, I really appreciate your effort! 🙂
