# run this cell if you're executing this notebook in your browser
!pip install qiskit

from IPython.display import clear_output
clear_output()

from qiskit import *

from qiskit import IBMQ

# IBMQ.save_account('APITOKEN', overwrite=True) ## Uncomment and replace APITOKEN with your API token to save new credentials
IBMQ.load_account()
IBMQ.providers()

provider = IBMQ.get_provider('ibm-q')

for backend in provider.backends():
    print(backend)

import qiskit.tools.jupyter 

backend = provider.get_backend('ibmq_manila') # replace ibmq_manila with whatever backend you want information on
backend 

eligible_devices = provider.backends(filters=lambda x:x.configuration().n_qubits>3 and not x.configuration().simulator)

for backend in eligible_devices:
    print(backend)

from qiskit.providers.ibmq import least_busy

chosen_backend = least_busy(eligible_devices)
print(chosen_backend.name())

chosen_backend

from qiskit.visualization import plot_gate_map

plot_gate_map(chosen_backend, plot_directed = False) # toggle plot_directed and see what happens

from qiskit.visualization import plot_error_map

plot_error_map(chosen_backend) 

qc_bell = QuantumCircuit(2)

qc_bell.h(0)
qc_bell.cx(0,1)
qc_bell.x(1)
qc_bell.z(1)
qc_bell.measure_all()

display(qc_bell.draw('mpl'))

from qiskit.tools import job_monitor

job = execute(qc_bell, chosen_backend)
job_monitor(job)

job_id = job.job_id()

retrieved_job = chosen_backend.retrieve_job(job_id)
result = retrieved_job.result()
counts_real = result.get_counts()
print(counts_real)

from qiskit.visualization import plot_histogram

sim_backend = Aer.get_backend('qasm_simulator')
job_sim = execute(qc_bell, sim_backend)
counts_sim = job_sim.result().get_counts()

title = 'Real vs. Simulated'
legend = ['Real Counts', 'Sim Counts']

plot_histogram([counts_real, counts_sim], legend = legend, title = title, color = ['teal', 'black'], bar_labels = False)

