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

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

• Hello :) could you be more specific about what the issue you are facing is? – met927 Dec 2 '19 at 15:34
• @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 – Rupansh Sekar Dec 2 '19 at 15:37
• 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. – met927 Dec 2 '19 at 15:47
• @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 – Rupansh Sekar Dec 2 '19 at 15:59
• @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 – Rupansh Sekar Dec 2 '19 at 16:08