pip install qiskit

from qiskit import assemble, Aer, QuantumCircuit, __qiskit_version__, execute
from qiskit.visualization import plot_histogram

__qiskit_version__

circ = QuantumCircuit(4,2) #4 qubits, 2 measurements

circ.x(0)#Comment this out if q0 is 0
circ.x(1)#Comment this out if q1 is 0

circ.barrier()

circ.cx(0,2)#cnot between q0 and q2;0 is the control bit, 2 is target
circ.cx(1,2)#cnot between q1 and q2;1 is the control bit, 2 is target

#if both inputs are same, then cnot will return final measurement 0; 
#if both inputs are different, then cnot will return final mes 1;
#Basically, the above statements achieve a XOR gate on q0 and q1, with 
#output on q2

circ.ccx(0,1,3)#Toffoli gate with q0,q1 as controls, q3 as target
#basically an AND gate between q0 and q1, with output on q3
#this is necessary because in the case we add 1+1, we get 10,i.e. we need a one on the left bit, and this will
#return one if both the inputs are 1

circ.barrier()

circ.measure(2,0)#Measuring XOR output of qubit 2 on bit 1
circ.measure(3,1)#Measuring AND output of qubit 3 on bit 2

circ.draw('mpl')

#simulator = Aer.get_backend('qasm_simulator') Initialising backend
#job = assemble(circ) Assembling the circuit
#results = simulator.run(circ).result() finding results
#counts = results.get_counts() obtaining the counts

#much better way to do this using execute
backend = Aer.get_backend('qasm_simulator')#Same as simulator above
job = execute(circ, backend)#no need to assemble
result = job.result()
status = job.status()
print(status)

counts = result.get_counts()
plot_histogram(counts)
#Note - bits printed from right to left, i.e. if q2 is bit a and q3 is bit b, then printed in the order ba, NOT ab
#in the above case, q2 is 0 and q3 is 1, so the output is 10, NOT 01(as bits printed in reverse order; would have changed if measurement of 2 taken on bit2 instead of bit1, then the output would be normal ab instead of ba)

