Codebook: Quantum Fourier Transform

Ask here about the “Quantum Fourier Transform” Codebook topic from the “Quantum Fourier Transform” module.

Hi,
in “QFT circuit for two qubit” there is a calculation for HS and the result is given as a martix (numpy notation): ( [1, 1], [i, -i] ).

I miss the 1 / sqrt(2)

Is this just a typo or do I miss something?

Best regards,
jomu

Hi,
in “QFT circuit for two qubit” there are two diagrams for the circuit.
I implemented the first and it looks OK.
This is the code:
qml.Hadamard(wires = 0)
qml.ctrl(qml.S, control=1)(wires=0)
qml.Hadamard(wires = 1)
qml.SWAP(wires=[0,1])
return qml.state()

Then I tried the second but now the systems responds “not OK”.
This is the code:
qml.Hadamard(wires = 1)
qml.ctrl(qml.S, control=0)(wires=1)
qml.Hadamard(wires = 0)
return qml.state()

But when I do a swap at the very first beginning then again it OK:
qml.SWAP(wires=[0,1])
qml.Hadamard(wires = 1)
qml.ctrl(qml.S, control=0)(wires=1)
qml.Hadamard(wires = 0)
return qml.state()

But in this case there is not reduction in the depth of the circuit.

Is there something wrong with the sketch?

Best rergards,
jomu

Hi,
in “Properties of the QFT” you write:
“We will look at special functions where the amplitude is non-zero at multiples of
(and zero otherwise) and has no offset”

I tested this with numpy FFT.
When I use an input like:
poly_a = np.array([1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0 ], dtype=complex)
then the FFT output is periodic as expected.

When I use an input like:
poly_a = np.array([1, 0, 0, 1,
1, 0, 0, 1,
1, 0, 0, 1,
1, 0, 0, 1 ], dtype=complex)
then the FFT output is not periodic any more.

My question: is this “and zero otherwise” a strict requirement for a periodic output?

Best regards,
jomu

Hi @jomu, yes there’s a typo. We’re missing the 1/sqrt(2). Good catch!

Hi @jomu , I think there might be something wrong with the sketch indeed because the output of the two circuits is different. Let me check and get back to you.

Hi @jomu,

Regarding your last question, “zero otherwise” is indeed a strict requirement for FFT to work as expected.

Hi Catalina,
thank you so much for all these clarifications and help. I really appreciate it!

Regards,
jomu

1 Like

Hello

Regarding this question

Think that this sketch is still not the correct one (as noted by jomu), right?

Hi @Vitor_Fernandes, welcome to the forum!
You are right, this sketch is not the correct one.
I am looking more into this and will have an good answer on how to fix it by the end of the week.
Will let you know more.

Thanks for bringing this up!

2 Likes

Hi!
It’s me again. Upon reviewing this, I don’t think there is a mistake in the circuit, but rather that the section lacks clarification about what applying this transform to get rid of SWAPs entails, because as you noted, the circuits do not give the same quantum state for the same inputs. Removing SWAPs is an optimization technique and the resulting circuits are equivalent up to a permutation of the wires. But this is still advantageous, since that permutation can be done in software, by relabeling the qubits.
You can verify that the optimization technique is correctly applied to the circuit using the transform undo_swaps

import pennylane as qml
from pennylane import numpy as np
num_wires = 2
dev = qml.device("default.qubit", wires=num_wires)
#@qml.transforms.undo_swaps # uncomment this line to get rid of SWAPs
@qml.qnode(dev)
def circuit1(basis_id):
    bits = [int(x) for x in np.binary_repr(basis_id, width=num_wires)]
    qml.BasisState(bits, wires=[0, 1])
    
    qml.Hadamard(wires = 0)
    qml.ctrl(qml.S, control=1)(wires=0)
    qml.Hadamard(wires = 1)
    qml.SWAP(wires=[0,1])
    return qml.state()

print(qml.draw(circuit1)(1))

I will correct the Codercise so it is clear that you have to use the QFT circuit with SWAP. Additionally, I will provide more context in the theory section to further clarify this.

You can verify that both circuits give the same states if you relabel the wires in postprocessing.

Thank you!

Hi Daniela

Thanks for your answer

Still have two questions, if you don’t mind answering:

  1. How is the software relabelling done in PennyLane?

  2. Since both circuits should be equal, then it should be possible to deduce one from the other through the equational form of the circuits, right? Did a little exercise and did the following deduction

Think that this deduction confirms what you said, right?

Hi!

  1. I think I didn’t mean it that literally, my bad. For me, it was more that one keeps track of the permutation if one needs to follow a certain convention or compare results. For example, for these two circuits, we just keep in mind that the wires would be flipped and the outcome of 01 in circuit 1 is the outcome of 10 in circuit 2. However, PennyLane does provide the possibility to specify the wires for a lot of subroutines and in those instances, a more direct “relabelling” is possible. See this demo for an example in the subsection Prepare Dicke states with k excitations. I know it is not QFT, but in there, the wires are specified in reversed order because the original algorithm was written in that way.
  2. Yes, your derivation is accurate and confirms what we’ve discussed.

Thanks!

Hello

Thank you very much for your answer

Everything is clear now :slight_smile:

1 Like

Hi all,

@daniela.murcillo has added some clarifications in the Codebook so hopefully it will be much clearer from now on! It should be visible in the Codebook tomorrow.