import time

import qiskitpool

from qiskit import IBMQ, QuantumCircuit
import qiskit.tools.jupyter

IBMQ.load_account()
provider = IBMQ.get_provider(group='open', project='main')

%qiskit_job_watcher

pool = qiskitpool.QPool(provider)
backend = provider.get_backend('simulator_statevector')

n_shots = 10

circ = QuantumCircuit(1, 1)
circ.x(0)
circ.measure(0, 0)

job = pool(circ, backend, shots=n_shots)

pool

while not job.poll():
    time.sleep(2)
    

assert(job.result().get_counts()['1'] == n_shots)

pool = qiskitpool.QPool(provider)
backend = provider.get_backend('simulator_statevector')

n_shots = 10
n_rounds = 20

circ = QuantumCircuit(1, 1)
circ.x(0)
circ.measure(0, 0)

# Create jobs
jobs = [pool(circ, backend, shots=n_shots) for i in range(n_rounds)]

print(pool)

# Wait for jobs to finish
n_complete = 0
while n_complete < n_rounds - 1:
    n_complete = 0
    for job in jobs:
        if job.poll():
            n_complete += 1

for job in jobs:
    assert(job.poll())
    assert(job.result().get_counts()['1'] == n_shots)

pool['simulator_statevector'].finished_jobs

pool['simulator_statevector'][0]

pool.join()