# How do I check what is wrong in my full-adder code?

I am trying to solve the first question on the qiskit test which is writing a code for a full adder.

So based on my research if I have $$A$$ q[0], $$B$$ q[1] and $$C$$ in q[2] as input and Sum and Cout as output, I should be able to produce the correct outputs by the following gates:

q[0] XOR1 q[1] ---> q[4]

q[0] AND1 q[1] ---> q[3]

q[2] XOR2 q[4] ---> q[5] (SUM)

q[2] AND2 q[4] ---> q[6]

q[3] OR q[6] ---> q[7] (COUT)



Writing the following program I get that my answer is producing wrong results :

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import IBMQ, Aer, execute

##### build your quantum circuit here

#Define registers and a quantum circuit
q = QuantumRegister(8)
c = ClassicalRegister(2)
qc = QuantumCircuit(q,c)

# Preparing inputs
qc.x(q[0])  # Comment this line to make Qbit0 = |0>
qc.x(q[1])  # Comment this line to make Qbit1 = |0>
qc.x(q[2])  # Comment this line to make Qbit2 = |0> ( carry-in bit )
qc.barrier()

# AND gate1 implementation
qc.ccx(q[0],q[1],q[3])
qc.barrier()

# XOR gate1 implementation
qc.cx(q[0],q[4])
qc.cx(q[1],q[4])
qc.barrier()

# XOR gate2 implementation
qc.cx(q[2],q[5])
qc.cx(q[4],q[5])
qc.barrier()

# AND gate2 implementation
qc.ccx(q[2],q[4],q[6])
qc.barrier()

#OR gate implementation
qc.cx(q[3],q[7])
qc.cx(q[6],q[7])
qc.ccx(q[3],q[6],q[7])
qc.barrier()

# Measuring and put result to classical bit
# ( sum )
qc.measure(q[5],c[0])
# ( carry-out )
qc.measure(q[7],c[1])

# execute the circuit by qasm_simulator
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1000)
result = job.result()
count = result.get_counts()
print(count)
qc.draw(output='mpl')


Grading tells me that my results are not matching, but I cannot figure out what is wrong with my code. Thank you so much for help.

• What was wrong with your result? Commented Nov 19, 2020 at 8:13
• Maybe you can leave a hyperlink to the source website. And I'm not sure what is your carry-in qubit refers to. Commented Nov 19, 2020 at 8:31
• This is too not practical to try to make addition in low level language, try to use high level compiler with classiq that follows the order of the qubits quantumcomputing.stackexchange.com/questions/32848/… Commented May 20 at 8:39

If I am correct, I suppose you are talking about the Qiskit Challenge 2020. A possible reason why your circuit is being graded wrong is because the question asks you to construct the circuit for full adder and give it the input $$A=1$$, $$B=0$$ and $$X=1$$. However, I think as per your code, you are preparing the qubits to be $$|ABX\rangle = |111\rangle$$ instead of $$|101\rangle$$. Baring that, your circuit works perfectly fine from what I could analyze.