2
$\begingroup$

I am running a very simple script just to learn how to use IBMQ, but my job will hang indefinitely and I am forced to terminate it.

from qiskit import *
from qiskit.compiler import transpile, assemble
from qiskit import IBMQ
from qiskit import QuantumCircuit, execute, BasicAer
import logging


logging.basicConfig(filename='log',level=logging.DEBUG)

IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q', group='open', project='main')

#SELECT A BACKEND
backend = provider.get_backend('ibmq_qasm_simulator')

#ALGO
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)

print('About to run job')   
job = execute(circuit, backend)
print('Job Finished')
result = job.result()
counts = result.get_counts(circuit)
print(counts)

In an attempt to debug, I am using "logging.basicConfig" which stops at:

"websocket._connect:DEBUG:2020-09-16 11:26:57,813: Starting new websocket connection: wss://wss.quantum-computing.ibm.com/jobs/5f6258f09234fe0012dbd748/status/v/1"

When I go to my IBMQ home page it says that the job has run successfully. It is only at the line result = job.result() that it fails.

So it seems that the websocket is hanging forever. I'd appreciate any help I can get to fix this issue.

$\endgroup$

2 Answers 2

1
$\begingroup$

The function execute is non-blocking. That means that it will return after sending the job, but not necessarily with the result. In your code, you should wait for the status of the job to be DONE:

print('About to run job')   
job = execute(circuit, backend)
job.status()
JobStatus.QUEUED

After waiting some time:

job.status()
JobStatus.DONE

Then, job.result() will work.

$\endgroup$
5
  • $\begingroup$ This works! Although, I am looking to improve my method if possible. I have added the following loop to check when the job is done. Is there a better way to do this? while str(job.status()) != 'JobStatus.DONE': print(job.status()) $\endgroup$ Commented Sep 16, 2020 at 19:14
  • 3
    $\begingroup$ job.result() actually waits for the job to finish (so the result can be retrieved), and that's why it appears to "hang". If you want to have a separate step to wait for the job, you can use job.wait_for_final_state(), which also supports a callback function that you can use to say print the current status. It uses websockets, which is more efficient than polling with job.status() $\endgroup$
    – jyu00
    Commented Sep 16, 2020 at 19:28
  • $\begingroup$ Your explanation makes sense to me, but unfortunately it is not working. I am using "job.wait_for_final_state(timeout=1,callback='job_status')" but I still get hung up on the same websocket as previously. So far, the only thing to work is the inefficient method of using a while loop with job.status(). I very much appreciate your help so far and would welcome any more suggestions you may have. $\endgroup$ Commented Sep 16, 2020 at 20:18
  • 2
    $\begingroup$ Which version of qiskit-ibmq-provider are you using? There was a problem with websockets on Windows + Python 3.8 (see github.com/Qiskit/qiskit-ibmq-provider/issues/691) that was fixed in a recent release. By the way, the argument for callback needs to be a function. $\endgroup$
    – jyu00
    Commented Sep 16, 2020 at 21:09
  • $\begingroup$ And we now are at the heart of the issue. As you suspected, I had been using an older version which was clearly bugged. $\endgroup$ Commented Sep 16, 2020 at 21:59
2
$\begingroup$

To add on to the previous answer, if you want to see the progress of your job in real time, you can try the following:

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

job_monitor shows you ever step, including job initialization, validation, being queued, to running and completion.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.