Certificate Challenge: Introduction to PennyLane

Use this topic to ask your questions about the Certificate Challenge: Introduction to PennyLane.

I seem to get an out of bounds error from my code
Running submission…

          Running on public test set

                Test 1 of 2 failed! 🚫
                    Input: [[[[0.62832, 0.3927, 1.0472, 0.7854],[0.7854, 0.31416, 0.62832, 0.5236]],[[0.31416, 0.7854, 0.7854, 0.3927],[0.31416, 0.3927, 0.31416, 0.3927]]],"PauliY", 0.5236]
                    Expected output: [0.68594, 0.31406]
                    Failure message: Runtime Error: Failed to execute run() function: index 2 is out of bounds for axis 0 with size 2.

                Test 2 of 2 failed! 🚫
                    Input: [[[[1.0472, 0.7854, 3.1416, 0.3927],[1.0472, 0.7854, 3.1416, 0.5236]],[[1.0472, 0.7854, 1.5708, 0.3927],[0.7854, 0.7854, 1.5708, 0.7854]]],"PauliX", 0.7854]
                    Expected output: [0.46653, 0.53347]
                    Failure message: Runtime Error: Failed to execute run() function: index 2 is out of bounds for axis 0 with size 2.

          Running on private test set

                One or more private tests failed. Try again! 🚫 

Looks like you are trying to access array[2] where array is of length 2, so only array[0] and array[1] are accessible. I would suggest to copy the problem notebook and running it on an editable ipynb( like colab or jupyter), and try to see which step exactly is throwing the error.

1 Like

Great analysis @sujay3sriv, and welcome to the forum!

@Hanah_Rahman, a good way to start is using Google Colab. Open a new notebook there and run the following at the beginning of your notebook (including the exclamation mark): !pip install pennylane

You can of course keep using the challenge editor, but if you’re having trouble with debugging then using a notebook can help.

Good luck with the challenge!

About the ‘g’ as the input of S, do I have to convert it into a matrix and then create a unitary gate usibg QubitUnitary with e^{ixG} (where G is the Hermitian matrix) as the input?

While that could work, qml.evolve already implements e^{-ixG}.

The A simple Trotterization challenge also explains a possible way to approximate the qml.evolve.

Thank you very much, I will try it.

Now I got this error

Failure message: Runtime Error: Failed to execute check() function: Cannot run circuit(s) on default.qubit as they contain wires not found on the device: {3}.

Do I have to specify how many qubits required when defining the device?

tells that if the wires argument is omitted the device can still figuring it out automatically.

That’s right @LdBeth !

@verstrikt , if you create a default.qubit device specifying wires=3, then you will have a device with three wires: {0, 1, 2}. However if, within the circuit, you add a gate on wires=3 then you’ll get the error wires not found on the device: {3} because there’s no wire 3 on the device, there’s only wire 0, wire 1, and wire 2.

Let me know if this makes the error more clear to understand!

Right now for my encoding block I’m attempting to use

op=qml.evolve(g(wires=i),coeff=x*-1j)

I’m not getting the correct output although it the probabilities are normalized am I missing something obvious with the approximating e^(ixG)?

Should not be multiplying by -1j, because qml.evolve does e^{-ixG} and what implemented by your operation right now is e^{-xG}

I tried multiplying x by -1 as well to make it e^(ixG) which I would expect to work but I instead get very strange and large results. Like unnormalized probabilities in the 30000s and such.

What does the public tests says about your trainable blocks and encoding blocks?

Btw, the S function is not supposed to return any results so I think it is kinda strange to assign the operation to a variable.

my s function looks like this right now:

 for i in range(num_wires):
        op=qml.evolve(g(wires=i),coeff=x*-1)
        qml.apply(op)

However it still says something is wrong with the encoding block.

See the document of qml.apply — PennyLane 0.36.0 documentation

The warning section:

If you use apply on an operator that has already been queued, it will be queued for a second time. For example:

I believe that is the case why your implementation doesn’t work: you have accidentally applied the evolve operation twice.

Thank you so much, it took a while to understand what was happening but, yes you are correct, the qml.evolve applies the operator alone so using the qml.apply on the returned op was applying it again. For others with similar issues, you can inspect the context tape via print(qml.QueuingManager.active_context())

1 Like

It’s great to see this conversation here @LdBeth and @jwoodrg !

My circuit looks like this (for the first test case). I feel I got everything right. But I am getting “AssertionError: Not the correct probabilities for the quantum model.”
Can somebody help?

Update: last S block in the circuit was the issue. Removing the last S block worked for me.

Hi @rishihg, welcome to the Forum.

I’m glad you solved your issue. Remember to accept your certificate in your account settings and share it on LinkedIn!

Note: I’ve blurred your circuit and solution so that others don’t get a spoiler.