1
$\begingroup$

I have been trying to implement a ripple carry adder using qsikit in IBM Q Experience. I was able to implement full carry adder but I am having trouble with ripple carry adder. I am able to add 100 + 001 or 100 + 010 etc fine however when i try to perform 100+100 i get rubbish output. Here's my implementation in qsikit

def storeValues(x, y):
    circuit = QuantumCircuit((len(x)*3)+1,len(x)+1)
    for i in range(0, len(x)):
        if x[i] == "1":
            circuit.x(i)
    for j in range(0, len(y)):
        if y[j] == "1":
            circuit.x(len(x)+j)
    return circuit

def fullAdder(input1, input2, carryin, carryout, circuit):
    circuit.ccx(input1, input2, carryout)
    circuit.cx(input1, input2)
    circuit.ccx(input2, carryin, carryout)
    circuit.cx(input2, carryin)
    circuit.cx(input1, input2)

x = '10'
y = '10'
circuit = storeValues(x, y)
for i in range(len(x)):
    fullAdder(i, len(x)+i, len(x)+len(y)+i, len(x)+len(y)+i+1, circuit)
    circuit.barrier()

for i in range(len(x)+1):
    circuit.measure([(len(x)*2)+i], [i])
#circuit.draw()
simulator = Aer.get_backend("qasm_simulator")
result = execute(circuit, backend=simulator, shots=1).result()
print(list(result.get_counts().keys())[0])

Here's a circuit diagram circuit

I do not understand why using Cout as Cin for another full adder is causing problems!

$\endgroup$
  • $\begingroup$ Hello :) could you be more specific about what the issue you are facing is? $\endgroup$ – met927 Dec 2 at 15:34
  • $\begingroup$ @met927 I am unable to perform operations which should set the carry qubit to 1 for nqbits > 2. For example, 10+10 gives the following output : 010 While the expected output is 100 $\endgroup$ – Rupansh Sekar Dec 2 at 15:37
  • $\begingroup$ I think your circuit it adding 01 and 01 as you perform operations on the bits that have had the X applied first. Normally adders start with the least significant bit and then move towards more significant bits. $\endgroup$ – met927 Dec 2 at 15:47
  • $\begingroup$ @met927 Interestingly enough, If i flip the not gate to be applied on 0 bits, the circuit surprisingly works. Unfortunately it breaks addition of a value with 0 (i.e 10+00) :( . I tried reversing the loop instead(Without flipping x) but no luck there. Here's the circuit with freversed addition. Carry qbit is still 0 imgur $\endgroup$ – Rupansh Sekar Dec 2 at 15:59
  • 1
    $\begingroup$ @met927I reversed x and y to be 01 and 01 resp and it did 10+10! I think i understand the problem! Thanks for the help :D $\endgroup$ – Rupansh Sekar Dec 2 at 16:08