PennyLane
PreviousNext

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

Beginner
Getting Started

Returning Probabilities

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 the probability that a rotated qubit is in the ground state \vert 0 \rangle. The completed code should:

  • define a device
  • create a quantum function and QNode
  • run the circuit
  • process the results

The quantum function should:

  • Rotate the qubit around the x-axis by an angle \phi using qml.RX
  • Measure the probabilities of the qubit being in different states

qml.probs(wire) returns a probability for each of the different computational basis states, but we are only interested in the probability that the qubit is in the ground state |0\rangle. Therefore, you will need to select the probability the qubit is in the state |0\rangle.

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 = R_x(\phi) |0\rangle = \mathrm{e}^{-i \phi \sigma_x /2} | 0\rangle
\text{ans} = |\langle 0 | \phi \rangle |^2

Challenge code

In the template code provided, you must complete the simple_circuit function, which takes the argument angle (float)—corresponding to \phi in the challenge statement above—and returns an np.array(float) containing the probabilities of measuring \vert 0 \rangle and \vert 1 \rangle in that order.

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 that receives one float angle, corresponding to the rotation angle \phi in the R_x gate.

Output

The expected output is a float corresponding to the probability of measuring the ground state. This probability will be automatically extracted from the output of your simple_circuit QNode.

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.45783 expected_output: 0.55636 test_input: 0.9572 expected_output: 0.78791

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 "Success!". Otherwise, you will receive an "Incorrect" prompt.

Good luck!

Loading...