# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.visualization import *
from qiskit.quantum_info import state_fidelity
# Magic function to render plots in the notebook after the cell executing the plot command
%matplotlib inline

qc = QuantumCircuit(3)
qc.h(0)
qc.cnot(0,1)
qc.cnot(1,2)
qc.measure_active()
qc.draw(output='mpl')

# Loading your IBM Q account
IBMQ.load_account()
provider = IBMQ.get_provider(group='open')

# List the existing quantum devices and their number of qubits
device_list = provider.backends(simulator=False)
for dev in device_list:
    print(dev.name() + ': ' + str(dev.configuration().n_qubits) + ' qubits')

# We choose one device which has enough qubits for our experiment and send the job to the device
num_shots_hardware = 1024
hardware_backend = provider.get_backend('ibmq_quito')
job = execute(qc, backend=hardware_backend, shots=num_shots_hardware)

result = job.result()
counts = result.get_counts()
plot_histogram(counts, title='Run on hardware')

simulator_backend = Aer.get_backend('qasm_simulator')
counts_simulator = list()
num_shots_simulator = [1024, 8192]
for num_shots in num_shots_simulator:
    job = execute(qc, backend=simulator_backend, shots=num_shots)
    result_simulator = job.result()
    counts_simulator.append(result_simulator.get_counts())

# We plot a comparison between the different results
plot_histogram([counts] + counts_simulator, title='Hardware vs. simulator',
               legend=['hardware ' + str(num_shots_hardware) + ' shots',
                       'simulator ' + str(num_shots_simulator[0]) + ' shots',
                       'simulator ' + str(num_shots_simulator[1]) + ' shots'])