PennyLane
PreviousNext

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

Advanced
Algorithms

Counting Mountains

Challenge statement

This challenge was part of the Canadian Quantum Cup 2023 coding competition.

A few months ago, we were working on a very interesting experiment. We were tasked with counting the number of "mountains" of very particular states. In this challenge, a mountain is a peak that we see when plotting the amplitudes of a state, as you will see in the following examples. The amplitudes of these states always follow the same pattern:

  • Amplitude values are real numbers.
  • We start with a mountain.
  • After a mountain there is always a valley whose depth is equal to the height of the mountain.
  • After a valley, we find again a mountain of the same height as the previous one.
  • This pattern is repeated several times before ending with a valley.

In more technical terms, they describe a normalized sine function (the sum of the squared amplitudes must be equal to 1). These quantum states can be made up of different numbers of qubits.

Let us look at two examples.

Here we can see an example in which we have 4 qubits and the state has 2 mountains.

In this other example, we can see what the state would be like if we had 5 qubits and 3 mountains.

When we did this experiment, we labeled each state with the number of mountains it had. However, the labels have since become mixed up and we are no longer sure if the labeling is correct. To address this issue, we will design a circuit that can tell us whether a state is properly labeled. The circuit must have the form shown below.

Your goal will be to implement the operator U(\ell) that depends on the label \ell of the input state |\phi\rangle. After applying U, a measurement in the computational basis is done on the last qubit. This measurement must return a 1 in the last qubit if |\phi\rangle is properly labeled, and 0 otherwise. You should be able to determine if the state is properly labeled using a single shot.

Challenge code

You are asked to complete the quantum function U so that the circuit behaves as described in the statement. The arguments of U are the number n_wires (int) of wires in the circuit and the label (int) stating the (possibly incorrect) number of mountains in the state |\phi\rangle.

Input

The input is composed of 3 different elements:

  • n_wires (int): Number of qubits in the circuit.
  • phi (array(float)): Vector representing the quantum state |\phi\rangle (we will assume that the amplitude has only real numbers). The size of the vector is 2^{(\text{n}_{\text{wires}} - 1)}. In case you want to try to create your own states, they have all been created following a standardized sine function (see below).
  • label (int): Label for the state |\phi\rangle.

Output

The output is either 1 or 0, which indicates whether the state is properly labeled or not. Use this as a reference for you to know what the answer is for each state, but bear in mind that all you are asked is to complete the function U.

Test cases

The test cases are given as arrays of the form [n_wires, phi, label]. They are too long to write here explicitly, since phi will be of length 2^{(\text{n}_{\text{wires}} - 1)}. Instead, we give you the following script to generate phi so that you can build your own test cases.

def get_sin(x,n_wires, mountains): return np.sqrt(2) * np.sin(2 * mountains * np.pi *x/2**(n_wires-1)) / np.sqrt(2 ** (n_wires-1))

For example, phi for the first test case is generated using

mountains = 13 n_wires = 7 phi = [get_sin(x, n_wires, mountains) for x in range(2 ** (n_wires-1))]

Loading...