from qiskit import IBMQ, QuantumRegister, QuantumCircuit, ClassicalRegister, transpile
from qiskit.tools import backend_overview, backend_monitor
from qiskit.providers.ibmq.job import job_monitor
from qiskit.quantum_info import hellinger_fidelity
from qiskit.providers.ibmq import IBMQAccountCredentialsNotFound
from qiskit.providers.aer import AerProvider
from qiskit.tools.visualization import plot_gate_map, plot_histogram

token = 'f5876d2c6ea00ce4cefe8fcf6324956e042cd2a7cd57f6efc1e7ba38727964f10cef560830337f64c1de3743c420c1b23ded52bc01b197c6d8ce96394f13f1e0'# Input token here
try:
    IBMQ.disable_account()
except IBMQAccountCredentialsNotFound:
    pass
IBMQ.enable_account(token)

backend_overview()

IBMQ_provider = IBMQ.get_provider()
ibmq_manila_backend = IBMQ_provider.get_backend('ibmq_manila')
backend_monitor(ibmq_manila_backend)

plot_gate_map(ibmq_manila_backend, plot_directed=True)

qr = QuantumRegister(3)
cr = ClassicalRegister(3)
ghz3 = QuantumCircuit(qr, cr)
ghz3.h(qr[0])
ghz3.cx(qr[0], qr[1])
ghz3.cx(qr[0], qr[2])
ghz3.barrier()
ghz3.measure(qr, cr)

ghz3.draw('mpl')

job_exp = ibmq_manila_backend.run(ghz3, shots=2048)
job_monitor(job_exp) # An error must be raised here.

print(ibmq_manila_backend.configuration().basis_gates)

transpiled_circuit = transpile(ghz3, backend=ibmq_manila_backend)
transpiled_circuit.draw('mpl')

transpiled_circuit_opt = transpile(ghz3, backend=ibmq_manila_backend, optimization_level=3)
transpiled_circuit_opt.draw('mpl')

job_exp = ibmq_manila_backend.run(transpiled_circuit_opt, shots=2048)
job_monitor(job_exp)

noisy_counts = job_exp.result().get_counts()
plot_histogram(noisy_counts)

qasm_backend = AerProvider().get_backend('qasm_simulator')
job_ideal = qasm_backend.run(ghz3, shots=2048)
ideal_counts = job_ideal.result().get_counts()

print(hellinger_fidelity(noisy_counts, ideal_counts))

from qiskit.test.mock import FakeRome

fake_rome = FakeRome()

rome_transpiled = transpile(ghz3, backend=fake_rome, optimization_level=3)
job_fake_rome = fake_rome.run(rome_transpiled, shots=2048)
counts_fake_rome = job_fake_rome.result().get_counts()
plot_histogram(counts_fake_rome)