# Pennylane Coding Challenge : Universality of single-qubit gates

**URL:** <https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506>\
**Category:** PennyLane Challenges\
**Created:** [October 4, 2023, 1:43am UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506 "2023-10-04T01:43:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Monit\_Sharma](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/monit_sharma/32/1621_2.png) [@Monit\_Sharma](https://discuss.pennylane.ai/u/Monit_Sharma)\
**Post date:** [October 4, 2023, 1:43am UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/1 "2023-10-04T01:43:21Z")

</div>

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](https://pennylane.ai/challenges/universality_of_single_qubit_gates).

```auto
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](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/6/6ffba187cb91da046d45846ed36d6ca5d2dc1c8c.png)

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.

---

<div class="post-metadata">

**Author:** ![Alvaro\_Ballon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alvaro_ballon/32/874_2.png) [@Alvaro\_Ballon](https://discuss.pennylane.ai/u/Alvaro_Ballon)\
**Post date:** [October 4, 2023, 3:27pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/4 "2023-10-04T15:27:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Monit\_Sharma](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/monit_sharma/32/1621_2.png) [@Monit\_Sharma](https://discuss.pennylane.ai/u/Monit_Sharma)\
**Post date:** [October 4, 2023, 11:33pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/5 "2023-10-04T23:33:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alvaro\_Ballon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alvaro_ballon/32/874_2.png) [@Alvaro\_Ballon](https://discuss.pennylane.ai/u/Alvaro_Ballon)\
**Post date:** [October 5, 2023, 2:42pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/6 "2023-10-05T14:42:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dhawal\_Verma](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/dhawal_verma/32/1996_2.png) [@Dhawal\_Verma](https://discuss.pennylane.ai/u/Dhawal_Verma)\
**Post date:** [October 10, 2023, 9:18pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/7 "2023-10-10T21:18:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alvaro\_Ballon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alvaro_ballon/32/874_2.png) [@Alvaro\_Ballon](https://discuss.pennylane.ai/u/Alvaro_Ballon)\
**Post date:** [October 11, 2023, 2:04pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/8 "2023-10-11T14:04:57Z")

</div>

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](https://join.slack.com/t/xanadu-quantum/shared_invite/zt-24watnlec-TWQT4JS~y9ClqIi6dSBbaA). 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

---

<div class="post-metadata">

**Author:** ![vindem](https://avatars.discourse-cdn.com/v4/letter/v/ccd318/32.png) [@vindem](https://discuss.pennylane.ai/u/vindem)\
**Post date:** [February 4, 2024, 1:04pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/9 "2024-02-04T13:04:34Z")

</div>

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:

```auto
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:

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

---

<div class="post-metadata">

**Author:** ![Alvaro\_Ballon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alvaro_ballon/32/874_2.png) [@Alvaro\_Ballon](https://discuss.pennylane.ai/u/Alvaro_Ballon)\
**Post date:** [February 6, 2024, 7:04pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/10 "2024-02-06T19:04:08Z")

</div>

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 🙂

Cheers,

Alvaro

---

<div class="post-metadata">

**Author:** ![Michele\_Minervini](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/michele_minervini/32/2488_2.png) [@Michele\_Minervini](https://discuss.pennylane.ai/u/Michele_Minervini)\
**Post date:** [April 13, 2024, 1:48pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/11 "2024-04-13T13:48:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [April 15, 2024, 9:18pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/12 "2024-04-15T21:18:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michele\_Minervini](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/michele_minervini/32/2488_2.png) [@Michele\_Minervini](https://discuss.pennylane.ai/u/Michele_Minervini)\
**Post date:** [April 17, 2024, 8:48am UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/13 "2024-04-17T08:48:39Z")

</div>

My code looks like this:

```auto
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:

 ![Screenshot 2024-04-17 at 10.46.51](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/f/f2e9f608cbc6e66508f33bae88e2be88cb7e002d.png)

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

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [April 19, 2024, 12:57am UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/14 "2024-04-19T00:57:20Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Michele\_Minervini](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/michele_minervini/32/2488_2.png) [@Michele\_Minervini](https://discuss.pennylane.ai/u/Michele_Minervini)\
**Post date:** [April 23, 2024, 8:30pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/15 "2024-04-23T20:30:17Z")

</div>

I tried to implement this:

```auto

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?

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [April 24, 2024, 11:40pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/16 "2024-04-24T23:40:24Z")

</div>

Hi @Michele_Minervini,

You don’t need to create a qnode or return its state. Note that in the challenge description you’re only being asked to return a unitary matrix.

I hope this helps you!

Note: we’ve created a new [Forum category for PennyLane Challenges](https://discuss.pennylane.ai/c/pennylane-challenges/24)! 🎉 If you have questions about challenges in the future, feel free to add them within this category in the thread for the relevant challenge.

---

<div class="post-metadata">

**Author:** ![dpbm136](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@dpbm136](https://discuss.pennylane.ai/u/dpbm136)\
**Post date:** [April 25, 2024, 1:32pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/17 "2024-04-25T13:32:51Z")

</div>

Hi, I had the same issue with the `error` function and, for me, the problem was being caused by the `np.linalg.norm`, as @Alvaro_Ballon said in [#4](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/4). So, the solution that I’ve found was to get rid of this numpy function, calculating all the norm by hand using just python statements and simple np methods like `np.sqrt()`.

---

<div class="post-metadata">

**Author:** ![Michele\_Minervini](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/michele_minervini/32/2488_2.png) [@Michele\_Minervini](https://discuss.pennylane.ai/u/Michele_Minervini)\
**Post date:** [April 25, 2024, 7:38pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/18 "2024-04-25T19:38:45Z")

</div>

Thanks @CatalinaAlbornoz and @dpbm136 for your help! I followed your suggestion by simply defining numpy matrices (similar to Monit\_Sharma’s code) and using np.abs(U - matrix).max() to evaluate the error between the two matrices, and this time I passed the public tests but not the private one:

 ![Screenshot 2024-04-25 at 21.35.57](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/6/60bfd931b7755cbac66852090d60e6477eee6772.png)

Do you have any idea why this might happen?

---

<div class="post-metadata">

**Author:** ![Alvaro\_Ballon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alvaro_ballon/32/874_2.png) [@Alvaro\_Ballon](https://discuss.pennylane.ai/u/Alvaro_Ballon)\
**Post date:** [April 26, 2024, 3:26pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/19 "2024-04-26T15:26:22Z")

</div>

Hi @dbpm136

I’m not _entirely_ sure what’s going on, but I think putting a `max()` may be causing differentiability issues with the cost function (more than it already has thanks to `np.abs` 😅 ). Maybe change the `max` for something more differentiable, like `sum`?

Hope that helps!

Alvaro

---

<div class="post-metadata">

**Author:** ![Michele\_Minervini](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/michele_minervini/32/2488_2.png) [@Michele\_Minervini](https://discuss.pennylane.ai/u/Michele_Minervini)\
**Post date:** [May 1, 2024, 7:44pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/20 "2024-05-01T19:44:13Z")

</div>

Yes, now it works correctly. Thanks!

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [May 1, 2024, 11:22pm UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/21 "2024-05-01T23:22:48Z")

</div>

That’s great to hear @Michele_Minervini !

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [August 21, 2024, 3:35am UTC](https://discuss.pennylane.ai/t/pennylane-coding-challenge-universality-of-single-qubit-gates/3506/22 "2024-08-21T03:35:10Z")

</div>

For anyone looking at this issue, at the moment it’s ok for the solution output and expected output not to match. The grading is still happening correctly. [Please see this post for more details](https://discuss.pennylane.ai/t/pennylane-challenge-universality-of-single-qubit-gates/4505/12).
