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.