PennyLane
PreviousNext

To attempt this challenge, please switch to a larger screen size.

Beginner
Optimization

The Hessian of a Circuit

Challenge Statement

You may already be familiar with the calculation of the gradient of a circuit, which can be done very easily thanks to the parameter-shift rule. But what about second derivatives? In calculus, the second derivative of a real function f:\mathbb{R}^n \rightarrow \mathbb{R} is described via a matrix H_f called the Hessian

H_f = \begin{pmatrix} \frac{\partial^2 f}{\partial x_1^2} & \frac{\partial^2 f}{\partial x_1 x_2} & \cdots & \frac{\partial^2 f}{\partial x_1 x_n}\\ \frac{\partial^2 f}{\partial x_2 x_1} & \frac{\partial^2 f}{\partial x_2^2} & \cdots & \frac{\partial^2 f}{\partial x_2 x_n}\\ \vdots & \vdots & \ddots & \vdots \\ \frac{\partial^2 f}{\partial x_n x_1} & \frac{\partial^2 f}{\partial x_n x_2} & \cdots & \frac{\partial^2 f}{\partial x_n^2} \end{pmatrix}.

A variational quantum circuit usually depends on some parameters \theta_1, \ldots \theta_n and returns an expectation value, so it can also be seen as a function f:\mathbb{R}^n \rightarrow \mathbb{R}. Therefore, we can calculate its Hessian and, moreover, there's a parameter-shift rule that allows us to do it. Namely, the elements of the Hessian matrix for such circuit can be calculated via

\begin{align*} \frac{\partial^2 f}{\partial \theta_i \theta_j}(\theta) &= [f(\theta + s(\mathbf{\hat{e}}_i+\mathbf{\hat{e}}_j))-f(\theta +s(-\mathbf{\hat{e}}_i+\mathbf{\hat{e}}_j))\\ &-f(\theta + s(\mathbf{\hat{e}}_i-\mathbf{\hat{e}}_j))+f(\theta - s(\mathbf{\hat{e}}_i+\mathbf{\hat{e}}_j))]/4\sin^2(s), \end{align*}

where s can be any real value, and \mathbf{\hat{e}}_i is the i-th coordinate basis vector.

PennyLane has the built-in function qml.gradients.param_shift_hessian that uses this formula to compute the Hessian. In this challenge we will use it to calculate the Hessian of circuits with an arbitrary number of wires that are of the form

drawing

Note that, in this case, the number of wires and the number of parameters are related:

\text{number of parameters} = \text{number of wires} + 2.

Challenge code

You must complete the compute_hessian function, which should

  • Define a QNode which implements the circuit above and returns the expectation value \langle Z_0\otimes Z_{n-1}\rangle, where n is the number of wires num_wires (int).
  • Return the Hessian of the circuit evaluated on some parameters, enconded in the arguments w (np.ndarray). These parameters are differentiable. The Hessian should be encoded in either a tuple, which is the built-in return type for qml.gradients.param_shift_hessian, or an np.ndarray of shape (5,5) if you decide to compute the Hessian in a different way.

Input

As input to this problem, you are given a list of the form [num_wires, w], where

  • num_wires (int) is the number of wires in the circuit.
  • w is the a list of length num_wires + 2, which contains the values of the parameters at which the Hessian should be evaluated.

Output

The output of your code should be the Hessian. Regardless of whether it's a tuple or an np.ndarray, it will be converted into a numpy array by the evaluation functions, which is what you'll see in the expected_output.

Test cases

The following public test cases are available to you. Note that there are additional hidden test cases that we use to verify that your code is valid in full generality.

test_input: [3,[0.1,0.2,0.1,0.2,0.7]] expected_output: [[0.013, 0.0, 0.013, 0.006, 0.002], [0.0, -0.621, 0.077, 0.125, -0.604], [0.013, 0.077, -0.608, -0.628, -0.073], [0.006, 0.125, -0.628, 0.138, -0.044], [0.002, -0.604, -0.073, -0.044, -0.608]] test_input: [4,[0.78,0.23,0.54,-0.8,-0.3,0.0]] expected_output: [[0.0, 0.0, 0.0, 0.0, 0.0, 0.128], [0.0, -0.582, 0.082, -0.14, 0.0, -0.343], [0.0, 0.082, -0.582, -0.359, 0.0, -0.057], [0.0, -0.14, -0.359, -0.582, 0.0, 0.204], [0.0, 0.0, 0.0, 0.0, 0.0, 0.393], [0.128, -0.343, -0.057, 0.204, 0.393, -0.582]]

If your solution matches the correct one within the given tolerance specified in check (in this case it's a 1e-2 absolute error tolerance), the output will be "Success!" Otherwise, you will receive an "Incorrect" prompt.

Good luck!

Loading...