# Stuck on Codercise I.5.4

**URL:** <https://discuss.pennylane.ai/t/stuck-on-codercise-i-5-4/4151>\
**Category:** Codebook\
**Created:** [February 24, 2024, 6:35am UTC](https://discuss.pennylane.ai/t/stuck-on-codercise-i-5-4/4151 "2024-02-24T06:35:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sfdq](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sfdq/32/2042_2.png) [@Sfdq](https://discuss.pennylane.ai/u/Sfdq)\
**Post date:** [February 24, 2024, 6:35am UTC](https://discuss.pennylane.ai/t/stuck-on-codercise-i-5-4/4151/1 "2024-02-24T06:35:22Z")

</div>

Hello,  
I was able to simplify the circuit as follows:

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/c/cc01e4ad63bce14bd651376f12ddcc9727940fc4.png)

However, I received the following error:  
Incorrect: Your resulting output doesn’t look quite right.  
Incorrect: more than 1 T gates are needed.  
Incorrect: the optimal depth of the circuit is not 4.  
Incorrect: the optimal T-count of the circuit is not 1.  
Incorrect: the optimal T-depth of the circuit is not 1.

In wire 0: the pairs of H-gates, T-gates, and adjoint T-gates cancel each other, and one H-gate remains  
In wire 1: the pair of H-gates cancel each other and the 2 pairs of T-gates become S-gates, one T-gate, and H-gate remain  
In wire 2: the pair of H-gates cancel each other, the 2 pairs of adjoint T-gates become adjoint S-gates, and one H-gate remains

I am not sure where the simplification went wrong.

---

<div class="post-metadata">

**Author:** ![AvanishM](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/avanishm/32/2326_2.png) [@AvanishM](https://discuss.pennylane.ai/u/AvanishM)\
**Post date:** [February 25, 2024, 8:44pm UTC](https://discuss.pennylane.ai/t/stuck-on-codercise-i-5-4/4151/2 "2024-02-25T20:44:22Z")

</div>

The simplification went wrong because you can only cancel out the pairs of gates when they are right next to each other. For example H T H cannot be simplified to T, but H H T can be. This is because the operators do not commute: you cannot generally swap the ordering of matrix transformations around.

Now with this I’m sure your simplification will be very different.

---

<div class="post-metadata">

**Author:** ![Sfdq](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sfdq/32/2042_2.png) [@Sfdq](https://discuss.pennylane.ai/u/Sfdq)\
**Post date:** [February 26, 2024, 11:09am UTC](https://discuss.pennylane.ai/t/stuck-on-codercise-i-5-4/4151/3 "2024-02-26T11:09:19Z")

</div>

Yes, my updated simplification is now different. I forgot about the non-commutativity, looks like I need to revise the basics. Thank you for your explanation.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [February 26, 2024, 9:50pm UTC](https://discuss.pennylane.ai/t/stuck-on-codercise-i-5-4/4151/4 "2024-02-26T21:50:08Z")

</div>

Hey @Sfdq! Welcome to the forum 🙂

If you have any further questions or concerns, write back!

---

<div class="post-metadata">

**Author:** ![GianAsara](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@GianAsara](https://discuss.pennylane.ai/u/GianAsara)\
**Post date:** [May 10, 2024, 10:42pm UTC](https://discuss.pennylane.ai/t/stuck-on-codercise-i-5-4/4151/5 "2024-05-10T22:42:35Z")

</div>

Hi,

I am also stuck with the error:

“Error: Hadamard: wrong number of wires. 3 wires given, 1 expected.”  
I guess it is something related to the optimisation and the reduction of the number of Hadamard gates, but I really don’t see it. Any advice?

Thanks,

Gian

Here is my circuit:

```auto
dev = qml.device('default.qubit', wires=3)

@qml.qnode(dev)
def too_many_ts():
    """You can implement the original circuit here as well, it may help you with
    testing to ensure that the circuits have the same effect.

    Returns:
        array[float]: The measurement outcome probabilities.
    """
    qml.Hadamard(wires=[0,1,2])
    
    qml.T(wires=[0,1])
    qml.adjoint(qml.T(wires=2))
    
    qml.T(wires=0)
    
    qml.Hadamard(wires=[0,1,2])
    
    qml.adjoint(qml.T(wires=[0,2]))
    qml.T(wires=1)
    
    qml.adjoint(qml.T(wires=[0,2]))
    qml.T(wires=1)
    
    qml.T(wires=1)
    qml.adjoint(qml.T(wires=2))
    
    qml.T(wires=1)
    qml.Hadamard(wires=[0,1,2])
    
    return qml.probs(wires=[0, 1, 2])

@qml.qnode(dev)
def just_enough_ts():
    """Implement an equivalent circuit as the above with the minimum number of 
    T and T^\dagger gates required.

    Returns:
        array[float]: The measurement outcome probabilities.
    """

    ##################
    # YOUR CODE HERE #
    ##################

    # IMPLEMENT THE CIRCUIT, BUT COMBINE AND OPTIMIZE THE GATES
    # TO MINIMIZE THE NUMBER OF TS
    
    qml.Hadamard(wires=[0,1,2])
    
    qml.S(wires=0)
    qml.T(wires=1)
    qml.adjoint(qml.T(wires=2))
    
    qml.Hadamard(wires=[0,1,2])
    
    qml.adjoint(qml.S(wires=[0, 2]))
    qml.S(wires=1)
    
    
    qml.S(wires=1)
    qml.adjoint(qml.T(wires=2))
    
    qml.Hadamard(wires = [0, 1, 2])

    return qml.probs(wires=[0, 1, 2])

##################
# YOUR CODE HERE #
##################

# FILL IN THE CORRECT VALUES FOR THE ORIGINAL CIRCUIT
original_depth = 8
original_t_count = 13
original_t_depth = 6

# FILL IN THE CORRECT VALUES FOR THE NEW, OPTIMIZED CIRCUIT
optimal_depth = 6
optimal_t_count = 3
optimal_t_depth = 2

```

---

<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 14, 2024, 12:04am UTC](https://discuss.pennylane.ai/t/stuck-on-codercise-i-5-4/4151/6 "2024-05-14T00:04:40Z")

</div>

Hi @GianAsara,

What the error is saying is that you’re trying to apply the Hadamard to 3 qubits at the same time but it can only be applied to one at a time. So `qml.Hadamard(wires=[0,1,2])` should actually be split into three lines if you want to apply it to the three wires.

I hope this helps!
