To compute expectation values of observables in PennyLane, we can simply replace the qml.probs of the previous sections with qml.expval and specify the observable to be measured. Common choices are qml.PauliX, qml.PauliY, and qml.PauliZ. The possible outcomes of measuring any Pauli-based expectation value are either or as these are their eigenvalues.

To measure an expectation value in PennyLane, we must specify which observable we are measuring, and which wires it acts on. For example, if we wanted to return a measurement of the PauliZ observable acting on a single qubit, we would write

@qml.qnode(dev) def my_circuit(): # ... return qml.expval(qml.PauliZ(wires=0))

Design and run a PennyLane circuit that performs the following, where indicates measurement of the PauliY observable.

Tip. It is usually more convenient to use the shorthand qml.PauliZ(0) when specifying expectation values. Otherwise, the lines of code will get quite long when you get to the multi-qubit case!

or to submit your code

To interact with codercises, please switch to a larger screen size.

Learning Objectives:

  • Define an observable, and compute its possible measurement outcomes.
  • Compute the expectation value of an observable.