from qiskit import IBMQ

# token is obtained from IBMQ expierence website, just set up an account at https://quantum-computing.ibm.com/ and once run
# IBMQ.enable_account(token)
# IBMQ.save_account(token)

IBMQ.load_account() # this then automatically loads your saved account

IBMQ.providers() # list of providers, in our case just the standard open one since we dont have any special access (yet!)

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

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

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

from qiskit.providers.ibmq import least_busy

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

backend = least_busy(small_devices)

#backend = provider.backends(simulator=False, operational=True)[7]

backend

status = backend.status()
print("pending jobs: ", status.pending_jobs)
print("maximum jobs to submit: ", backend.job_limit().maximum_jobs)
print("operational: ", status.operational)

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

import qiskit

qr = qiskit.QuantumRegister(3)
cr = qiskit.ClassicalRegister(3)
circuit = qiskit.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)

mapped_circuit = qiskit.compiler.transpile(circuit, backend=backend)
#qobj = qiskit.compiler.assemble(mapped_circuit, backend=backend, shots=1024) # apparently deprecated and or unnecessary
job = backend.run(mapped_circuit)

print(status.pending_jobs)
job.status()

job.job_id()

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

# once job is done this should yield result
#result = retrieved_job.result()
#counts = result.get_counts()
#print(counts)

from qiskit.providers.ibmq.managed import IBMQJobManager

sim_backend = provider.get_backend('ibmq_qasm_simulator')
circs = qiskit.compiler.transpile([circuit]*20, backend=sim_backend)

# Submit them all to the backend
job_manager = IBMQJobManager()
job_set = job_manager.run(circs, backend=sim_backend, name='foo')

print(job_set.report())

