PennyLane
PreviousNext

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

Beginner
Getting Started

Returning Tensor Product Observables

Review of quantum circuits in PennyLane

Skip to Challenge statement if you are already familiar with simple quantum circuits in PennyLane.

If you're new to PennyLane, this is the perfect coding challenge for you! In this challenge, you will build a simple PennyLane circuit.

Central to PennyLane is the QNode, or Quantum Node. This object combines:

  • A device
  • A quantum function
  • Any additional configuration information

We first need a device. The device evaluates a quantum function. It could be either a simulator or actual quantum hardware. For the purpose of this coding challenge, we use "default.qubit", a simple built-in simulator that does not require external dependencies. To initialize a "default.qubit" device, we also need to specify the number of wires.

num_wires = 2 dev = qml.device('default.qubit', wires=num_wires)

Usually, a wire is just a qubit. Any hashable object can label a wire, but people often denote individual wires by numbers (0, 1, 2) or strings ("alice", "auxiliary").

Quantum functions accept classical input, apply quantum operations, and return one or more quantum measurement statistics. We write quantum functions as plain, old Python functions, but with some constraints; the function always needs to return a measurement.

We can put these components together to see the required structure of a quantum function:

def my_quantum_function(param): qml.PauliZ(wires=0) # a single-wire gate qml.RX(param, wires=0) # a single-wire parameterized gate qml.CNOT(wires=[0, 1]) # a two-wire gate # Finally we return a measurement of an operator on a wire return qml.expval(qml.PauliX(0))

While quantum functions look like Python functions, you can't call my_quantum_function on its own and have the described circuit executed. We instead have to call a QNode, an object containing both a quantum function and a device.

To quickly create a qnode, we apply a decorator that modifies the quantum function. The decorator takes the function as input and spits out something new that we can evaluate.

@qml.qnode(dev) def my_quantum_function(param): qml.PauliZ(wires=0) # a single-wire gate qml.RX(param, wires=0) # a single-wire parameterized gate qml.CNOT(wires=[0, 1]) # a two-wire gate # Finally we return a measurement of an operator on a wire return qml.expval(qml.PauliX(0)) result = my_quantum_function(0.1)

Challenge statement

You are tasked with calculating a tensor-product observable for an entangled state. The completed code should

  • define a device
  • create a quantum function and QNode
  • execute the QNode

The circuit should:

  1. Create the entangled Bell State:
|\Phi^{+}\rangle = \frac{1}{\sqrt{2}}\left(|00\rangle + |11\rangle\right).
  1. Rotate the first qubit around the y-axis by the provided angle using 'qml.RY'

  2. Measure the tensor observable qml.PauliZ(0) @ qml.PauliZ(1)

Here is a drawing of the circuit you should code.

For those that prefer to see the mathematics, we can write the problem as:

| \Phi^{+} \rangle = \frac{1}{\sqrt{2}} \left(| 00\rangle + | 11\rangle \right)
| \phi \rangle = R_{y, 0} (\phi) | \Phi^{+}\rangle = \mathrm{e}^{-i (\sigma_y \otimes I )\phi /2} | \Phi^{+} \rangle
\text{ans} = \langle \phi | \sigma^0_z \otimes \sigma^1_z | \phi \rangle.

Challenge code

In the code shown, you must complete the simple_circuit function, which takes the argument angle (float)—corresponding to \phi in the challenge statement above— and returns a float corresponding to the expectation value of the Z_{0}\otimes Z_{1} observable.

Additionally, you must complete some lines to define a PennyLane device and add a decorator to define a QNode.

Here are some helpful resources and hints:

  • Introduction
  • Circuits
  • Quantum Operations
  • Measurements

Input

The simple_circuit function is a QNode receives one float angle, corresponding to the rotation angle \phi in the R_y gate in the circuit depicted above.

Output

The expected output is a float corresponding to the expectation value of the Z_{0}\otimes Z_{1} observable.

Note: The use of qml.expval in the QNode will output an np.tensor containing a single float. The float within it will be extracted by the testing function, so you do not need to do it yourself.

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: 1.23456 expected_output: 0.3299365180851774 test_input: 1.86923 expected_output: -0.2940234756205866

If your solution matches the correct one within the given tolerance specified in check (in this case it's a 1e-4 relative error tolerance), the output will be "Correct!" Otherwise, you will receive a "Wrong answer" prompt.

Good luck!

Loading...