PennyLane
PreviousNext

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

Beginner
Quantum Circuits

My First Quantum Simulation

Challenge statement

The goal of this challenge is to build a "toy version" of PennyLane's circuit building capabilities. It's always useful to understand how our software works in the backend! We will program a quantum simulation using numpy instead of PennyLane. Hopefully, this will help you see all the coding steps we can skip thanks to PennyLane!

Using only numpy instead of pennylane, in this challenge you will write code to simulate a quantum algorithm that does the following:

  1. Initializes a single qubit in the state \vert 0 \rangle,
  2. Applies a single-qubit unitary U,
  3. Simulates 20 computational basis measurements on the output state.

We use the convention that the state \vert 0 \rangle is represented via the array [1,0] and the state \vert 1 \rangle is represented by [0,1].

Challenge code

In the template code provided, you must complete three helper functions:

  • initialize_state: prepares a qubit in the state \vert 0 \rangle.
  • apply_u: applies a unitary U, represented via an np.array of shape (2,2), to a quantum state represented as a unit vector with two entries (np.array of shape (2,)).
  • measure_state: for a state (np.array of shape (2,)) and a number of measurements num_meas (int), generates an array of length num_meas representing the measurement results.

Then, you must complete the main function quantum_algorithm, where you use the helper functions to prepare the state, apply the unitary U, and simulate 20 measurements in the computational basis. The quantum_algorithm function only depends on the unitary U.

Input

As an input to this problem, you are given the unitary U as a 2\times 2 np.array.

Output

The code must output an np.array(int) of 20 measurement 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: [[0.70710678, 0.70710678], [0.70710678, -0.70710678]] sample_output: [1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1] test_input: [[0.8660254, -0.5],[0.5, 0.8660254]] sample_output: [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1]

The output of a quantum measurement is random. For this reason, we have fixed a seed equal to 0 within the quantum_algorithm function. For autograding purposes, you should not change the seed value, since it might result in wrong outputs. However, feel free to change it locally or remove it altogether to see different measurement outputs!

If your solution matches the correct one with the seed provided, the output will be "Success!". Otherwise, you will receive an "Incorrect" prompt.

Good luck!

Loading...