# My IBMQ job hangs forever?

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)

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)

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.

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.

• 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()) – thespaceman Sep 16 '20 at 19:14
• 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() – jyu00 Sep 16 '20 at 19:28
• 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. – thespaceman Sep 16 '20 at 20:18
• 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. – jyu00 Sep 16 '20 at 21:09
• And we now are at the heart of the issue. As you suspected, I had been using an older version which was clearly bugged. – thespaceman Sep 16 '20 at 21:59

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.