File: hello_quantum.py
import sys, os
from qiskit import QuantumProgram, QISKitError, RegisterSizeError
# Create a QuantumProgram object instance.
Q_program = QuantumProgram()
try:
import Qconfig
Q_program.set_api(Qconfig.APItoken, Qconfig.config["url"])
except:
offline = True
print("WARNING: There's no connection with IBMQuantumExperience servers.");
print("The backends available for use are: {}\n".format(",".join(Q_program.available_backends())))
backend = 'ibmqx5'
try:
# Create a Quantum Register called "qr" with 2 qubits.
qr = Q_program.create_quantum_register("qr", 2)
# Create a Classical Register called "cr" with 2 bits.
cr = Q_program.create_classical_register("cr", 2)
# Create a Quantum Circuit called "qc". involving the Quantum Register "qr"
# and the Classical Register "cr".
qc = Q_program.create_circuit("bell", [qr], [cr])
# Add the H gate in the Qubit 0, putting this qubit in superposition.
qc.h(qr[0])
# Add the CX gate on control qubit 0 and target qubit 1, putting
# the qubits in a Bell state
qc.cx(qr[0], qr[1])
# Add a Measure gate to see the state.
qc.measure(qr, cr)
# Compile and execute the Quantum Program in the local_qasm_simulator.
result = Q_program.execute(["bell"], backend=backend, shots=1024, seed=1)
# Show the results.
print(result)
print(result.get_data("bell"))
except QISKitError as ex:
print('There was an error in the circuit!. Error = {}'.format(ex))
except RegisterSizeError as ex:
print('Error in the number of registers!. Error = {}'.format(ex))
File: Qconfig.py
APItoken = 'XXX'
config = {
'url': 'https://quantumexperience.ng.bluemix.net/api',
}
$ python hello_quantum.py
The backends available for use are: ibmqx5,ibmqx4,ibmqx_hpc_qasm_simulator,ibmqx2,ibmqx_qasm_simulator,local_qasm_simulator,local_clifford_simulator,local_qiskit_simulator,local_unitary_simulator
ERROR
There was an error in the circuit!. Error = 'QISkit Time Out'
python hello_quantum.pyI am trying to run hello_quantum.py which I have modified slightly and configured API in Qconfig.py, however, it is timing out. At /qx/account/advanced I can see that ibmqx5 is available.
@kenorb Please increase the timeout in the Q_program.execute(). Pass something like: timeout=600. The default is timeout=60 (seconds) which can be too little when the queue is busy. Doing so will let QISKit know that you are willing to wait 10 mins, while that instruction is blocking the rest of the script.
Please let me know if this works.
Thanks for your help. Now by these timeouts runs, I got into another MAX_CREDITS_EXCEEDED error, so I can't test it. Do you know if these credits are per day or any way of topping them up?
When the jobs are in the queue, your credits are being used and not returned. They will be returned only when the jobs have been executed and out of the queues.
@kenorb Your credits will replenish when the job is done executing.
Note that in qiskit, when a job times out, it only times out in the frontend. The actual job will remain on the queue and eventually execute. You can come back later and see the results of your executions. Do something like this:
#setup
from qiskit import QuantumProgram
import Qconfig
qp = QuantumProgram()
qp.set_api(Qconfig.APItoken, Qconfig.config['url'])
#download details of all the jobs you've ever submitted (well, up to some limit; the default is 50)
my_jobs = qp.get_api().get_jobs(limit=999)
#filter down to get a list of completed jobs
done_jobs = [j for j in my_jobs if j['status']=='COMPLETED']
#print the results for all of your completed jobs
for j in done_jobs:
for q in j['qasms']:
print(q['qasm'])
print(q['result'])
I think increasing the timeout helped. Probably executing the script on different backends ibmqx4, ibmqx5, ibmqx2 could help to avoid the credit limit issue.
I've tested ibmqx5 now and got execution times in the average of 50-120 seconds.
Re credits, I've created this Q&A: How to add credits?
Thank you so much, extending the timeout time totally solved my issue.
@ajavadia Hi Ali!
Is there a way to get the value of j at the time of execution? It would be good to be able to call up the results for a specific j in done_jobs, rather than checking the qasm.
@decodoku Hi James, not currently. But very soon you will be able to. All the code to do that is there, but we need to clean up a bit and write a tutorial on how to use it. It is indeed useful to be able to get the id of a submitted job, see its position in the queue, cancel it to get your credits back, or just retrieve the results at a future time.
All of this feedback is very good, so we can do this properly.
Most helpful comment
@kenorb Please increase the timeout in the
Q_program.execute(). Pass something like:timeout=600. The default istimeout=60(seconds) which can be too little when the queue is busy. Doing so will let QISKit know that you are willing to wait 10 mins, while that instruction is blocking the rest of the script.Please let me know if this works.