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.

np.random.seed(1967)
def get_matrix(params):
"""
Args:
- params (array): The four parameters of the model.
Returns:
- (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.
Args:
- U (np.array): Goal matrix that we want to approach.
- params (array): The four parameters of the model.
Returns:
- (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

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.

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).

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.

Hi, I also have a problem with this challenge and cannot find a solution to it. I would like to know whether I am going in the right direction. Here is the way I implemented the get_matrix:

def get_matrix(params):
"""
Args:
- params (array): The four parameters of the model.
Returns:
- (matrix): The associated matrix to these parameters.
"""
alpha, beta, gamma, phi = params
rz_gamma = qml.matrix(qml.RZ(gamma, wires=0))
rx_beta = qml.matrix(qml.RX(beta, wires=0))
rz_alpha = qml.matrix(qml.RZ(alpha, wires=0))
#return rz_alpha * rx_beta * rz_gamma * np.exp(1j*phi)
return np.exp(1j * phi) * rz_gamma @ rx_beta @ rz_alpha

Welcome to the forum! Looking at it, it all seems good. However, the choice of loss function seems to be ill-behaved for this problem. It seems to me that it’s finding a stationary point that is not a minimum for some of the test cases . Maybe a different choice of loss function would do the trick? Try some out and let me know. If you still run into issues, I can give you further hints

Hi @Michele_Minervini , I can see some issues in your code. Are you sure you need to use qml.matrix several times? Or would it be enough to use it just once? I hope this helps you!