Certificate Challenge: Introduction to PennyLane

Oh really? Huh that’s weird.
My answers are:

def W(params):
     qml.BasicEntanglerLayers(weights=params, wires=range(len(params[0])), rotation=qml.RY)

def S(g, x, num_wires):
     for _ in range(num_wires):
        qml.exp(op=g(_), coeff=1j*x)

dev = qml.device("default.qubit")
@qml.qnode(dev)
def quantum_model(param_set, g, x):
     for _ in range(len(param_set)):
        if _ != len(param_set) - 1: 
            W(param_set[_])
            S(g, x, len(param_set[_][_]))
        else:
            W(param_set[_])

Hi!
I think I see where the mistake is. You can incur into contradictions by setting if _ != len(param_set) - 1: and then trying to access S(g, x, len(param_set[_][_])).
Think about two examples, one of the public cases has an array [[[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]]] as input, what if instead, you had a smaller array like [[[1.0472, 0.7854, 3.1416, 0.3927]],[[0.7854, 0.31416, 0.62832, 0.5236]],[[0.31416, 0.3927, 0.31416, 0.3927]]]

I don’t think you need the iterator to use as input of S(g, x, len(param_set[_][_])), you could simply let that as a fixed value (select one of the elements and not iterate over them), since all you need is the length.

Let me know if this helps.

1 Like

Ok that makes sense, I had originally implemented len(param_set[0][0]) but then thought maybe for completeness it might be best to loop over the the given inputs.. Didn’t think it would cause an error, after making the correction just got a success.
Thank you Daniela!

Hi, I just started out with Pennylane and I am not sure what is wrong with my code, it just says Assertion Error: Not the correct probabilities for the quantum model..

def W(params):
    num_layers = len(params)
    num_wires = len(params[0]) 
    for layer in range(num_layers):
        for wire in range(num_wires):
            qml.RY(params[layer][wire], wires=wire)
        for wire in range(num_wires-1):
            qml.CNOT(wires=[wire, wire + 1])
        qml.CNOT(wires=[num_wires-1, 0])

def S(g, x, num_wires):
    for i in range(num_wires):
        qml.evolve(g(i), coeff=-x)

dev = qml.device("default.qubit", wires = 4)

@qml.qnode(dev)
def quantum_model(param_set, g, x):
    num_blocks = len(param_set)
    num_wires = len(param_set[0])
    for i in range(num_blocks):
        W(param_set[i])
        if i < num_blocks-1:  
             S(g, x, num_wires)
    return qml.probs(wires=0)

I will delete this if its not allowed

Hi @Yasoup , welcome to the forum!

The quantum_model function is not quite correct. Check your for loop and verify how many times W and S (especially this one) are being executed and if that coincides with your expectations for the solution of the problem.

Let me know if this helps.

hi thank you for your advice, you were right about my quantum model function being incorrect. My error was with the number of wires, my code did not identify the number of wires from the input correctly. I fixed it now i have a success for everything! Thank you so much!!

Hello @CatalinaAlbornoz , I tried to implement the algorithm however I keep getting values different from what I am suppose to have, here is the architecture of my quantum circuit :

also I was wondering if using :

qml.BasicEntanglerLayers(weights=params, wires=range(n_wires), rotation=qml.RY)

is the same as building the entire algorithm by hand as it is much faster ? thank you for your feedback

Hi @wassim , welcome to the forum!
Unfortunately, it is hard for me to spot what might be going wrong just by looking at the circuit? It is a lot easier if you post your code answers. You can blur them as spoilers or I can do it as well.

And yes, using that built-in function is much easier than coding the entire algorithm, feel free to do so.

Let me know!

Hello @daniela.murcillo , thank you for your reply, I manage to solve my problem as it was due to how i reading though the params input. Thank you very much !

1 Like

Hello I’m new here. I have been running the challenge on a browser named Brave. I got so many incorrect feed back several times then I switched to Chrome it worked for the first time. So which browser @CatalinaAlbornoz @daniela.murcillo do you suggest is best when taking the coding challenges?

Hi @Dlamini_Mangaliso_Li
Welcome to the forum!

Thanks for reporting this.
I tried a few codercises myself using Brave and they worked smoothly as expected. Could you be more specific with the code and type of errors that you were receiving so I can reproduce them myself and provide a better answer. I wouldn’t expect this to happen for different browsers, but it is for sure worth looking :eyes:

@daniela.murcillo sorry for the late reply, I wasn’t doing Codercises those work fine in brave, I was doing the Certificate Challenge: Introduction to PennyLane, I am suspecting it wasn’t picking up param_set cause when I ran it locally I had to point the code to the test cases:

In short I got this outputs:
Running submission…

		Running on public test set

			Test 1 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]
				Solution output: null
				Failure message: Runtime Error: Failed to execute solution: unexpected indent (<string>, line 45).

			Test 2 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]
				Solution output: null
				Failure message: Runtime Error: Failed to execute solution: unexpected indent (<string>, line 45).

    
    	Running on private test set

				One or more private tests failed. Try again! 🚫 

Ah! forgive me, the challenges.
I also tried this myself Chrome and Brave and it ran well for both.
Could you provide the code you used in Brave in order to get those outputs? Does the problem still persist?