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.

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
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,

Alvaro

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: !

3 Likes

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.

Cheers,

Alvaro

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

and here is the error function:

    matrix = get_matrix(params)
    err = np.linalg.norm(U - matrix)

    return err

would be great to have a push in the right direction, I am stuck on this since a couple of days and cannot figure out what I am doing wrong.

thank you for your attention! Cheers,

Hi @vindem

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 :slight_smile:

Cheers,

Alvaro

Hi @Monit_Sharma @Alvaro_Ballon , I am facing the same issue and struggling to find a solution. Can you share any tips on how you solved it?

Hi @Michele_Minervini , welcome to the forum!
What have you tried and where are you struggling? What are the results of the public tests?

1 Like

My code looks like this:

def get_matrix(params):
    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))
    U = np.exp(1j * phi) * rz_gamma @ rx_beta @ rz_alpha
    return U

def error(U, params):
    error_value = np.linalg.norm(U - matrix)    
    return error_value

The results of the public tests are the following:

I am having difficulty identifying the root cause of the issue.

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!

I tried to implement this:


def get_matrix(params):
    alpha, beta, gamma, phi = params

    dev = qml.device('default.qubit', wires=1)
        @qml.qnode(dev)
        def circuit(alpha, beta, gamma):
            qml.RZ(alpha, wires=0)
            qml.RX(beta, wires=0)
            qml.RZ(gamma, wires=0)
            return qml.state()
    
        U_rotations = qml.matrix(circuit)(alpha, beta, gamma)
        U = np.exp(1j * phi) * U_rotations
        return U

But still the public tests fail. Is my interpretation of using qml.matrix only once correct?