PennyLane
PreviousNext

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

Intermediate
Getting Started

Certificate Challenge: Introduction to PennyLane

Welcome to the Introduction to PennyLane certificate challenge. Upon completion of this challenge, you will receive a certificate that attests to your proficiency with the use of basic PennyLane functionalities. In particular, you should be able to

  • Define the quantum device that addresses your needs,
  • Create circuits that depend on real parameters,
  • Concatenate subcircuits into a larger QNode,
  • Return observable quantities.

Challenge statement

One of the main goals of Quantum Machine Learning is to come up with quantum circuits that produce a family of interesting models. Some quantum circuits that achieve this are of the following form

The W blocks are known as trainable blocks and depend on some parameters collectively represented by the matrices \Theta_i. The S blocks are known as encoding blocks and depend on the input data x. Let's take a look a these blocks separately.

The trainable blocks

The trainable blocks W entangle the input qubits to allow for a larger number of possible outputs. For the purposes of this challenge, they are constructed by concatenating several layers of the form

For m+1 layers and n+1 wires, the full block W reads

We will encode the rotation parameters shown in the figure in an (m+1)\times (n+1) matrix

\Theta = \begin{pmatrix} \theta_{00} & \theta_{01} & \cdots &\theta_{0n}\\ \theta_{10} & \theta_{11} & \cdots & \theta_{1n} \\ \vdots & \vdots & \ddots & \vdots \\ \theta_{m0} & \theta_{m1} & \cdots & \theta_{mn}\end{pmatrix}.

In our model, we have N training blocks. Each one has different parameters encoded in the matrices \Theta_{i}, i = 0, \ldots N-1.

The encoding blocks

Let's now turn to the encoding blocks S. They all depend on the single input data x, which we take to be a real number, and they're all of the form

where G is a Hermitian matrix.

Your task in this challenge is to build the entire quantum model given a set of matrices \lbrace \Theta_{0}, \Theta_{1}, \ldots \Theta_{N-1}\rbrace, a data input x, and a generator G.

Challenge code

To make your task more streamlined, the code is divided into three functions that you must complete.

First, you should complete the subcircuit W which, given a matrix params of real (float) parameters (\Theta in the statement), constructs a single trainable block W. As we saw above, the number of rows in params is equal to the number of trainable blocks, and the number of columns in params is equal to the number of wires used in the model.

Second, you should complete the subcircuit S which takes in three arguments:

  • g (pennylane.Operator): A Hermitian PennyLane operator (such as qml.PauliX) which acts as the generator G for the unitary e^{ixG}.
  • x (float): The coefficient x of the operator G.
  • num_wires (int): The number of wires in this block.

Finally, you should define a device dev and build the quantum_model QNode that depends on the following:

  • param_set (np.array). A sequence of matrices \Theta_0, \ldots \Theta_{N-1} which encode the parameters for each trainable block. The length of this list is equal to the number of trainable blocks, which is always greater than, or equal to 2.
  • g (pennylane.Operator): A Hermitian PennyLane operator (such as qml.PauliX) which acts as the generator G for the unitary e^{ixG}.
  • x (float): The coefficient x of the operator G.

This QNode should return the computational basis measurement probabilities on the first wire.

Input

As input to this problem, you are given:

  • param_set (np.array): An array [\Theta_0, \ldots, \Theta_{N-1}] of matrices, one for each encoding block in the circuit read from left to right. All of these matrices have the same number of columns, equal to the wires in the circuit. The number of rows may differ depending on the number of layers in each encoding block.
  • g (string): A string with the PennyLane name for the generator G of the gates e^{iGx}, for example, "PauliX". You don't need to parse this string into an operator, this is done for you by the grader. If you're curious about how this was done, look into the getattr Python function!
  • x (float): The coefficient of the operator G.

Output

This code must output an np.array of shape (2,) corresponding to the computational basis measurement probabilities on the first wire for the entire quantum model.

Test cases

The following public test cases are available for you to check your work. There are also some hidden test cases that we will use to check that your solution works in full generality.

test_input = [[[1.0472, 0.7854, 3.1416, 0.3927], [1.0472, 0.7854, 3.1416, 0.5236]], [[1.0472, 0.7854, 1.5708, 0.3927], [0.7854, 0.7854, 1.5708, 0.7854]]], "PauliX", 0.7854 expected_output = [0.46653, 0.53347] expected_input = [[[0.62832, 0.3927, 1.0472, 0.7854], [0.7854, 0.31416, 0.62832, 0.5236]], [[0.31416, 0.7854, 0.7854, 0.3927], [0.31416, 0.3927, 0.31416, 0.3927]]], "PauliY", 0.5236 expected_output = [0.68594, 0.31406]

If your answer matches the exact answer up to an absolute error of 1\times 10^{-3}, the output will be "Success!". Otherwise, you will receive an "Incorrect" prompt.

Loading...