F.3.1 Error in QFT

Can anyone point out what is the issue with the circuit for F.3.1? I’m confused why this isn’t working.

dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def three_qubit_QFT(basis_id):
    # Prepare the basis state |basis_id>
    bits = [int(x) for x in np.binary_repr(basis_id, width=dev.num_wires)]
    qml.BasisStatePreparation(bits, wires=[0, 1, 2])

    #R_2 = S gate and R_3 = T gate
    qml.Hadamard(wires=0)
    qml.ctrl(qml.S(wires=0), control=1)
    qml.ctrl(qml.T(wires=0), control=2)
    qml.Hadamard(wires=1)
    qml.ctrl(qml.S(wires=1), control=2)
    qml.Hadamard(wires=2)
    qml.SWAP(wires=[0,2])

    return qml.state()

Hi @Xavier_Mootoo,

This is not very obvious in the documentation for the qml.ctrl function but the parameters for the operators are not applied within the operator but instead outside. In this case the ‘wires’ argument in the S and T operators is a parameter. Hence you would apply the controlled operation as follows:
qml.ctrl(qml.S, control=1)(wires=0)

Repeating this change for the other controlled operations should result in the question being marked as correct!

I suppose
qml.ctrl(qml.S, control=1)(wires=0)
is equivalent to
qml.ctrl(qml.RZ, control=1)(np.pi/2,wires=0)
up to a global phase. But this does not seem be an acceptable answer for F.3.1

Hi @ksuresh,

it’s a bit of a subtle point, but you actually have to be cautious with global phases when doing controlled operations. While it is generally true that the S-gate and the RZ(\pi/2) gate are equivalent up to a global phase, that is no longer true for controlled operations. In other words, a controlled S-gate is no longer equivalent to a controlled RZ(\pi/2), which is why your solution is not being accepted.

I would recommend checking this (the issue with global phases in controlled operations) for yourself with pen and paper for a simple case so you can see why this is the case.

1 Like

Thanks for the clarification; I suspected something along these lines. I will check with pen & paper.

1 Like