1

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!

Sanchayan Dutta
  • 17,945
  • 8
  • 50
  • 112

0 Answers0