# # install and import qiskit 
# !pip3 install qiskit

import qiskit as q
import matplotlib.pyplot as plt
from qiskit import IBMQ


IBMQ.save_account('putyourtoken')

IBMQ.load_account()

qr = q.QuantumRegister(2)
cr = q.ClassicalRegister(2)

circuit = q.QuantumCircuit(qr, cr)
circuit.draw()

# apply the Hadamard gate on 0th qubit
circuit.h(qr[0])
circuit.draw(output='mpl') 

# now we apply the cnot gate 
# as explained before the control qubit should be in superposition state 
circuit.cx(qr[0], qr[1])
circuit.draw(output='mpl')

circuit.measure(qr, cr) # measure quantum bit into classical bit
circuit.draw(output='mpl')

# let's simulate the results on a local computer

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))

provider = IBMQ.get_provider('ibm-q')
qcomp = provider.get_backend('ibmq_london') # 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))

