Imagine I have a quantum circuit of n qubits and I want to measure the expected value of a single-qubit operator (let's say PauliZ) for each qubit. This is easily done in Pennylane with the following line:

return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]

Running this on real quantum hardware, how is this measurement done? Does it measure $H=ZIII...I$ first, $H=IZII...I$ afterwards and so forth, does it sample the circuit in computational basis and compute each qubit's expected value out of the odds, or it is something else?

Thank you in advance.


1 Answer 1


Because single qubit observables on different wires commute, this is possible to compute in a single hardware execution.

Internally, PennyLane uses the current approach to compute expectations on hardware:

  1. For any wire that is not measured in the computational basis, the wire is rotated into the computational basis prior to measurement by appending $\mathcal{O}(1)$ single qubit gates (for example, for $\langle X_i \rangle$, a Hadamard gate would be appended to wire $i$).

  2. The HW device is sampled, and (approximate) marginal probabilities are computed for each measured wire.

  3. Finally, the (approximate) expectation value is computed via $\sum_i \lambda_i \mathbb{P}_i$, where $\lambda_i$ are the eigenvalues of the observable being measured, and $\mathbb{P}_i$ are the (potentially rotated) probabilities from step (2). Note that, if we are restricted to Pauli terms as observables, the eigenvalues are known in advance and will simply be in the set $\{-1, 1\}$.

Note that, if the hardware device has its own native method for handling expectation values, PennyLane will instead hand-off to the device, rather than using the above process.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.