from qiskit import IBMQ 

IBMQ.backends()

IBMQ.delete_accounts()

IBMQ.stored_accounts()

import Qconfig_IBMQ_experience
import Qconfig_IBMQ_network

IBMQ.enable_account(Qconfig_IBMQ_experience.APItoken)

# uncomment to print to screen (it will show your token and url)
# IBMQ.active_accounts()

IBMQ.backends()

IBMQ.disable_accounts(token=Qconfig_IBMQ_experience.APItoken)

IBMQ.backends()

IBMQ.save_account(Qconfig_IBMQ_experience.APItoken, overwrite=True)
IBMQ.save_account(Qconfig_IBMQ_network.APItoken, Qconfig_IBMQ_network.url, overwrite=True)

# uncomment to print to screen (it will show your token and url)
# IBMQ.stored_accounts()

IBMQ.active_accounts()

IBMQ.backends()

IBMQ.load_accounts()

IBMQ.backends()

IBMQ.backends(hub='ibm-q-internal')

IBMQ.disable_accounts(hub='ibm-q-internal')

# uncomment to print to screen (it will show your token and url)
# IBMQ.active_accounts()

IBMQ.backends()

IBMQ.disable_accounts()
IBMQ.load_accounts(hub=None)
IBMQ.backends()

IBMQ.backends(operational=True, simulator=False)

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

from qiskit.providers.ibmq import least_busy

small_devices = IBMQ.backends(filters=lambda x: x.configuration().n_qubits == 5 and
                                                       not x.configuration().simulator)
least_busy(small_devices)

IBMQ.get_backend('ibmq_16_melbourne')

backend = least_busy(small_devices)

backend.provider

backend.name()

backend.status()

backend.configuration()

backend.properties()

backend.hub

backend.group

backend.project

for ran_job in backend.jobs(limit=5):
    print(str(ran_job.job_id()) + " " + str(ran_job.status()))

job = backend.retrieve_job(ran_job.job_id())

job.status()

backend_temp = job.backend()
backend_temp

job.job_id()

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

job.creation_date()

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import compile

qr = QuantumRegister(3)
cr = ClassicalRegister(3)
circuit = QuantumCircuit(qr, cr)
circuit.x(qr[0])
circuit.x(qr[1])
circuit.ccx(qr[0], qr[1], qr[2])
circuit.cx(qr[0], qr[1])
circuit.measure(qr, cr)

qobj = compile(circuit, backend=backend, shots=1024)
job = backend.run(qobj)

job.status()

import time
#time.sleep(10)

job.cancel()

job.status()

job = backend.run(qobj)

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

counts = result.get_counts()
print(counts)

