Stuck on Codercise I.5.4

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

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.

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.

1 Like

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.

1 Like

Hey @Sfdq! Welcome to the forum :slight_smile:

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

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:

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

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!