PennyLane
PreviousNext

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

Beginner
Getting Started

Comparing Expectation Values

Challenge statement

In this coding challenge, you are asked to construct two circuits that perform two rotations on one qubit and then measure the expectation value of the Pauli X observable. The two circuits you need to implement are pictured in the figure below.

We can see that the circuits implement the same operations, but in a different order. Your task is to measure how different the outputs of both circuits are by calculating the quantity

| \langle \hat{\sigma}^x \rangle_{\text{circuit 1}} - \langle \hat{\sigma}^x \rangle_{\text{circuit 2}} |.

NOTE: Sometimes we think that non-commutativity is exclusive to quantum mechanics. But classical rotations do not commute either, so the commutativity of unitaries is not actually quantum per se. It is the non-commutativity of measurements that is an exclusively quantum phenomenon, and what gives rise to the unintuitive quantum behaviours.


You are asked to create two separate quantum functions that define the two circuits in the figure above, which we call circuit1 (top circuit in the figure) and circuit2 (bottom circuit in the figure). Namely, the circuits perform the following operations:

  • circuit1: Rotates the qubit via the gate R_x(\theta_1) (qml.RX), then via the gate R_y(\theta_2) (qml.RY), and finally outputs \langle \hat{\sigma}^x \rangle_{\text{circuit 1}} (using qml.expval(qml.PauliX(0))).
  • circuit2: Rotates the qubit via the gate R_y(\theta_2) (qml.RY), then via the gate R_x(\theta_1) (qml.RX), and finally outputs \langle \hat{\sigma}^x \rangle_{\text{circuit 2}} (using qml.expval(qml.PauliX(0))).

The code contains a function compare_circuits that you need to complete. Specifically, the completed compare_circuits function should process the results from both circuits to return the absolute value of the difference between the circuit outputs:

| \langle \hat{\sigma}^x \rangle_{\text{circuit 1}} - \langle \hat{\sigma}^x \rangle_{\text{circuit 2}} |.

Challenge code

In the code template shown, you must complete the following helper functions:

  • circuit1: which takes an array [\theta_1, \theta_2] of the two angles and returns the expectation value \langle \hat{\sigma}^x \rangle_{\text{circuit 1}}.
  • circuit2: which takes an array [\theta_1, \theta_2] of the two angles and returns the expectation value \langle \hat{\sigma}^x \rangle_{\text{circuit 2}}.

Then, you must complete the main function compare_circuits, which processes the outputs of circuit1 and circuit2.

  • compare_circuits: which takes an array [\theta_1, \theta_2] and returns | \langle \hat{\sigma}^x \rangle_{\text{circuit 1}} - \langle \hat{\sigma}^x \rangle_{\text{circuit 2}} |.

Input

As an input to this problem, you are given the array [\theta_1, \theta_2] as an np.array(float).

Output

The code must output a float corresponding to the absolute value of the difference between the circuit outputs.

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.79894785, 0.71678115] expected_output: 1.177019 test_input: [5.88344281, 0.30672784] expected_output: 0.023805

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