import qiskit
from qiskit import *
# import matplotlib.pyplot as plt
%matplotlib inline
print(qiskit.__version__)

circuit = QuantumCircuit(2,2)
circuit.h(0)
circuit.cx(0,1)
circuit.measure([0,1],[0,1])
# circuit.measure_all()
circuit.barrier()
# circuit.draw(output='mpl')

circuit.draw(output='mpl')

simulator = Aer.get_backend('aer_simulator')
result = execute(circuit, backend=simulator, shots=1024).result()
count = result.get_counts()
print(count)

from qiskit.tools.visualization import plot_histogram
plot_histogram(count)

from qiskit import IBMQ
IBMQ.save_account("b32678329f7f6dd426b8cf18f20bea23c2cd056b0bee2b4bcf49744b612e598f20f7170a8da4bfd99b009b6fa59d596edea7a6926fd388be158843d8ef******", overwrite=True)

IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q', group='open', project='main')

provider.backends()

from qiskit.providers.ibmq import least_busy
backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2 
                                       and not x.configuration().simulator 
                                       and x.status().operational==True))
print("least busy backend: ", backend)

job = execute(circuit, backend=backend, shots=1024)

from qiskit.tools.monitor import job_monitor
job_monitor(job)

result = job.result()
count = result.get_counts()
print(count)

from qiskit.tools.visualization import plot_histogram
plot_histogram(count)

