from qiskit import IBMQ
import networkx as nx
from qiskit.providers.ibmq import least_busy
import matplotlib.pyplot as plt

IBMQ.load_account() # Load account from disk
IBMQ.providers()    # List all available providers

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

backends = provider.backends(filters=lambda x: x.configuration().n_qubits >= 5
                                    and not x.configuration().simulator
                                    and x.status().operational==True)

print(f" Backend with only {least_busy(backends).status().pending_jobs} pending jobs is {least_busy(backends).name()}")

backend = provider.get_backend('ibmq_16_melbourne')
print(f" {backend.name()} currently has {backend.status().pending_jobs} pending jobs")

melbourne_conf = backend.configuration()
G = nx.Graph()
G.add_nodes_from(range(0,melbourne_conf.n_qubits))
G.add_edges_from(melbourne_conf.coupling_map)
pos = nx.kamada_kawai_layout(G)
nx.draw_networkx(G, pos, with_labels = True)

melbourne_props = backend.properties()
melbourne_props.qubits[1]

melbourne_conf.basis_gates

gates_on_qubit_1 = [gate.to_dict() for gate in melbourne_props.gates if 1 in gate.qubits]
len(gates_on_qubit_1)

for gate in gates_on_qubit_1 : 
    if gate['qubits'][0] == 1:
        print("{} involves qubits:{} and has an error rate of {}".format(gate['gate'],gate['qubits'],melbourne_props.gate_error(gate['gate'], gate['qubits'])))

