# Help debugging implementation of Draper QFT adder

I am attempting to implement the adder found in this paper.

Here is the code:

#build the phi adder circuit
#controlled phase gates are .cp() def phi_add(num_bits):
qc = QuantumCircuit(2*num_bits,num_bits)
#initialize a register
#qc.x(0)
qc.x(1)
#qc.x(2)
#qc.x(3)
#initialize b register
qc.x(4)
qc.x(5)
qc.x(6)
qc.x(7)
#append Fourier transform to second register
qc.append(QFT(num_qubits=4),range(num_bits,2*num_bits))
for k in range(num_bits):
for j in range(num_bits-k):
qc.cp(2*np.pi/2**(j+1),control_qubit=num_bits-k-1-j,target_qubit=2*num_bits-k-1)
#append inverse Fourier transform to second register
qc.append(QFT(num_qubits=4,inverse=True),range(num_bits,2*num_bits))
qc.measure(range(num_bits,2*num_bits), range(num_bits))
return qc


Resulting in this diagram:

Which yields results that don't look very good:

EDIT: I found qiskit.circuit.library.DraperQFTAdder, which implements it but I still can't see where I went wrong.

The main issue is how you are using QFT. do_swaps should be false. And sice its default value is true, you will need to pass it explicitly:

QFT(num_qubits=4, do_swaps=False)


and

QFT(num_qubits=4, do_swaps=False, inverse=True)


Note also that, since the two registers have same size and the second register contains all 1s, the output will always be the value in the first register minus one.

• Thank you, I was aware of the option but didn't understand why it'd need to be off. Works perfectly now. Commented Oct 5, 2023 at 19:33

You can take a look at how to create an adder whether it is a QFT or a ripple-carry, using the answer here

You also need a compiler that interprets the order of the qubits correctly; you can see how it is done here (you have to enforce the classiq compiler to use QFT as other implementations might be more efficient)

If it is important to you to have the qiskit implementation of the adder, you can use this command to get the QASM:

QuantumProgram.parse_raw(qprog).transpiled_circuit.qasm


And then convert to an object according to this answer

Based on a bit of code experimentation, the order in which you target qubits depending on k is wrong: if you target the lines in the circuit from top to bottom as k increases, you'll get the circuit as it is presented in the paper, and the answer on the basis states will be a basis state as well. (You can do that by changing your code to use target_qubit=num_bits+k.)

• I think this is notational. For me, 𝜙3↔𝑞7 ... 𝜙0↔𝑞4 and 𝑎0↔𝑞0 ... 𝑎3↔𝑞3. Commented Oct 5, 2023 at 19:59