So far, we've learned that the different outcome probabilities are related to the amplitudes of a qubit's state vector. If no measurement basis is specified, you can generally assume that measurement is done in the computational basis; this is the default in PennyLane.

Outcome probabilities of the basis states can be returned directly in PennyLane. Rather than putting

return qml.state()

at the end of our QNodes, we can swap it out for

return qml.probs(wires=...)

Note that we must explicitly specify the wire labels of the qubits we would like to measure.

For this codercise, you will be asked to write a simple circuit that applies a Hadamard gate to either or and returns the measurement outcome probabilities. What do you notice about these probabilities?

dev = qml.device("default.qubit", wires=1)


@qml.qnode(dev)
def apply_h_and_measure(state):
"""Complete the function such that we apply the Hadamard gate
and measure in the computational basis.

Args:
state (int): Either 0 or 1. If 1, prepare the qubit in state |1>,
otherwise leave it in state 0.

Returns:
np.array[float]: The measurement outcome probabilities.
"""
if state == 1:
qml.PauliX(wires=0)

##################
# YOUR CODE HERE #
##################

# APPLY HADAMARD AND MEASURE

return


print(apply_h_and_measure(0))
print(apply_h_and_measure(1))

or to submit your code

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

Learning Objectives:

  • Define a projective measurement.
  • Perform a projective measurement in the computational basis.
  • Perform measurements in an alternative measurement basis.