Complete the circuit_as_function QNode below that implements the quantum circuit shown below and returns the expectation value on the first wire. The params argument of circuit_as_function is an np.ndarray representing in that order.

The backend will then plot a cross-section of the circuit, where the axis is defined by angles and the axis is output_values. With the given definition of output_values, all the parameters are constant except the angle on the gate on the first wire. Feel free to change angles and output_values so check how the circuit depends on different variables!

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

@qml.qnode(dev)
def circuit_as_function(params):
"""
Implements the circuit shown in the codercise statement.
Args:
- params (np.ndarray): [theta_0, theta_1, theta_2, theta_3]
Returns:
- (np.tensor): <Z0>
"""

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

return # Return the expectation value

angles = np.linspace(0, 4 * np.pi, 200)
output_values = np.array([circuit_as_function([0.5, t, 0.5, 0.5]) for t in angles])

or to submit your code

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

Learning Objectives:

  • Interpret a quantum circuit as a function.
  • Find first and higher order derivatives of quantum circuits.
  • Use optimizers to find extrema of quantum circuit cost functions.