0
$\begingroup$

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

fourier adder circuit

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))
    #append phase addition gates
    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:

Fourier adder circuit diagram

Which yields results that don't look very good:

Aer simulation results

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

$\endgroup$

3 Answers 3

1
$\begingroup$

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.

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

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

$\endgroup$
0
$\begingroup$

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.)

$\endgroup$
1
  • $\begingroup$ I think this is notational. For me, ๐œ™3โ†”๐‘ž7 ... ๐œ™0โ†”๐‘ž4 and ๐‘Ž0โ†”๐‘ž0 ... ๐‘Ž3โ†”๐‘ž3. $\endgroup$ Oct 5, 2023 at 19:59

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.