!pip3 install qiskit

import qiskit as q

print ("check version: ", q.__qiskit_version__)

from qiskit import IBMQ
import matplotlib.pyplot as plt


IBMQ.save_account('find your own number')

IBMQ.load_account()

circuit = q.QuantumCircuit(2, 2) # creates a new circuit with 2 classical and 2 quantum bits
# apply a simple not gate 
circuit.x(1) # as both the bits are at 0, 0 this will flip the second bit to 1. # the number generally always represents the index, i.e. 1-- second index
circuit.draw() 

# after flipping the second bit to 1 now we apply the controlled not gate
circuit.cx(1, 0) # conrolled qubit is the 2nd one and the target qubit is the 1st one 
# QuantumCircuit.cx(control_qubit, target_qubit, *, label=None, ctrl_state=None, ctl=None, tgt=None)
# here's the description [https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.cx.html]. 

# since the controlled bit is 1 so it will flip the target qubit which was initially sitting at 0,  to 1.  
# rather than values think of this 0, 1 inside circuit.cx() as indices of the qubits.  
# so at this stage we will have 1, 1 as output. 
circuit.draw() 

circuit.measure([0, 1], [0, 1]) # measure: quantum bit into classical bit, 
# again think of the 0, 1 as indices, first list is the quantum register consisting of 2 qubits
# second list is the classical register consisting of 2 classical bits. 
# think of this as value for qubit 0 is what will be returned for classical bit 0 and so on. 
circuit.draw(output='mpl')   

# let's simulate this before sending it to a quantum computer
# idea is we simulate the quantum computer computation in local computer using Aer framework

simulator = q.Aer.get_backend('qasm_simulator') # qasm : quantum assembly lang.
results = q.execute(circuit, backend=simulator).result() # execute the circuit based on the simulator as backend
q.visualization.plot_histogram(results.get_counts(circuit))

# Now we will use IBM Quantum Computer to see the result 
provider = IBMQ.get_provider('ibm-q')
qcomp = provider.get_backend('ibmq_burlington') # check which computer has 0 jobs on queue
job = q.execute(circuit, backend=qcomp)
q_result = job.result()

q.visualization.plot_histogram(q_result.get_counts(circuit))

