PennyLane
Next

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

Beginner
Quantum Circuits

Mid-Circuit Measurements

Challenge statement

This challenge is included in the QHack 2023 Flashback Badge Challenge event.

In classical computations, inserting control flow — e.g, if statements — right in the middle of a large computation is no problem at all since measuring variables does not affect the output of the computation. The same can't be said about quantum computations — if we measure, we better be careful!

In this challenge, you'll look at how mid-circuit measurements work in PennyLane.

Challenge code

In the code below, you are given a function called circuit. You must complete this function by constructing the following four-qubit circuit:

The circuit has a Hadamard gate on every qubit, an R_x gate, a couple of CNOTs, and then the mid-circuit measurements. Note here that the measurements happen on the first and third qubits, and that the qml.U3 gate is only applied to the fourth qubit if the following condition is met upon measuring the first and third wires: m_0 + m_2 \geq 1 (i.e. at least one of them is 1). The last operator, qml.PauliZ on the fourth qubit, is applied regardless.

The qml.measure function should be helpful to you!

Input

As input to this problem, you are given:

  • angles (list(float)): a list of angles containing \theta_0, \theta_1, \theta_2, and \theta_3 in that order. Use this to create the circuit!

Output

This code must output a numpy.tensor containing the probabilities associated to a computational basis measurement on the fourth qubit.

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.0, 1.5, 2.0, 2.5] expected_output: [0.79967628, 0.20032372]

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...