1
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ What was wrong with your result? $\endgroup$
    – KAJ226
    Nov 19, 2020 at 8:13
  • 1
    $\begingroup$ Maybe you can leave a hyperlink to the source website. And I'm not sure what is your carry-in qubit refers to. $\endgroup$ Nov 19, 2020 at 8:31

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thank you so much :), yes that was the problem, silly mistake. $\endgroup$ Nov 19, 2020 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.