# 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, $$B$$ q and $$C$$ in q as input and Sum and Cout as output, I should be able to produce the correct outputs by the following gates:

q XOR1 q ---> q

q AND1 q ---> q

q XOR2 q ---> q (SUM)

q AND2 q ---> q

q OR q ---> q (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)  # Comment this line to make Qbit0 = |0>
qc.x(q)  # Comment this line to make Qbit1 = |0>
qc.x(q)  # Comment this line to make Qbit2 = |0> ( carry-in bit )
qc.barrier()

# AND gate1 implementation
qc.ccx(q,q,q)
qc.barrier()

# XOR gate1 implementation
qc.cx(q,q)
qc.cx(q,q)
qc.barrier()

# XOR gate2 implementation
qc.cx(q,q)
qc.cx(q,q)
qc.barrier()

# AND gate2 implementation
qc.ccx(q,q,q)
qc.barrier()

#OR gate implementation
qc.cx(q,q)
qc.cx(q,q)
qc.ccx(q,q,q)
qc.barrier()

# Measuring and put result to classical bit
# ( sum )
qc.measure(q,c)
# ( carry-out )
qc.measure(q,c)

# 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? 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. Nov 19, 2020 at 8:31

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.