Pennylane Coding Challenge : Universality of single-qubit gates

I am solving the pennylane coding challenge, and I’m stuck with this problem. This is what my code looks like for the said problem.


def get_matrix(params):
        - params (array): The four parameters of the model.
        - (matrix): The associated matrix to these parameters.
    alpha, beta, gamma, phi = params
    sigma_x = np.array([[0, 1], [1, 0]])
    sigma_y = np.array([[0, -1j], [1j, 0]])
    sigma_z = np.array([[1, 0], [0, -1]])

    # Calculate the individual rotation matrices
    rz_gamma = np.array([[np.exp(-1j * gamma / 2), 0], [0, np.exp(1j * gamma / 2)]])
    rx_beta = np.array([[np.cos(beta / 2), -1j * np.sin(beta / 2)],
                        [-1j * np.sin(beta / 2), np.cos(beta / 2)]])
    rz_alpha = np.array([[np.exp(-1j * alpha / 2), 0], [0, np.exp(1j * alpha / 2)]])

    # Combine the rotation matrices in the specified order
    unitary_matrix = np.exp(1j * phi) * rz_gamma @ rx_beta @ rz_alpha

    return unitary_matrix

    # Put your code here #

    # Return the matrix

def error(U, params):
    This function determines the similarity between your generated matrix and
    the target unitary.

        - U (np.array): Goal matrix that we want to approach.
        - params (array): The four parameters of the model.

        - (float): Error associated with the quality of the solution.

    matrix = get_matrix(params)
    # Put your code here #
    error_value = np.linalg.norm(U - matrix)

    return error_value

    # Return the error

Now, I have two issue with it,
The result this code gives are the parameters, instead it should give the matrix as the result. See the Image
Screenshot 2023-10-04 at 09-40-41 Universality of single-qubit gates PennyLane Challenges

You can see the expected output is the matrix, but my solution output are the params
I tested the code on my local device, and It gives correct result.

It’s the issue with the pennylane result checking, it’s checking the matrix with the parameters.

Hi @Monit_Sharma!

Great job in spotting this. I’ve run it with my solution and I get the same conflict with the expected and solution outputs. We’ll fix this!

However, I do pass the tests with my solution anyway, although the parameters are being printed instead of the matrix, as it happened to you.

Are you sure that your method is returning the correct matrix? I worry that the use of np.linalg.norm may return the matrix multiplied by a global phase, which is not what we want in this case (you are asked to return the exact same matrix).

Let me know! Cheers,


Thanka @Alvaro_Ballon , i got the correct answer. I didnt changed the np.linalg.norm, but still got the correct answer now.

Awesome @Monit_Sharma, thank you so much for your feedback. Keep an eye out for new challenges to come :slight_smile: !


Can you please tell me how you got the correct answer? @Monit_Sharma @Alvaro_Ballon

Hello @Dhawal_Verma, welcome to the forum!

PennyLane staff will not share the solutions, but you’re free to discuss with other users in our Slack. Also, if you have a code snippet with what you’ve done so far, I can give you a push in the right direction.