Run your first Qiskit Serverless workload remotely
This section explores how to use qiskit-ibm-catalog
to list programs available in Qiskit Serverless, pass inputs into these programs, run them remotely, check their status, and retrieve results and logs.
Be sure you have authenticated to Qiskit Serverless with your IBM Quantum account (see Deploy to IBM Quantum Platform for instructions).
List programs available
You can use QiskitServerless.list()
to print out a list of the available programs to run with Qiskit Serverless. This includes the previously uploaded transpile_remote_serverless
.
from qiskit_ibm_catalog import QiskitServerless
serverless = QiskitServerless()
serverless.list()
Output:
[QiskitFunction(transpile_remote_serverless)]
Run an uploaded program and pass inputs
First, set up your inputs. Your program has three inputs: circuits
, backend
, and optimization_level
. You can use random_circuit
to create 30 random circuits:
from qiskit.circuit.random import random_circuit
qc_random = [(random_circuit(4, 4, measure=True)) for _ in range(30)]
qc_random[0].draw(output='mpl', idle_wires=False)
Output:
Next, use QiskitRuntimeService
and least_busy
to select a backend
:
from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService()
backend = service.least_busy(operational=True, simulator=False)
print(backend.name)
Output:
ibm_kyoto
Set your optimization level:
[4] :optimization_level = 3
Select your program with serverless.load('PROGRAM_NAME')
:
transpile_remote_serverless = serverless.load('transpile_remote_serverless')
Next, pass your inputs and run it in a pythonic fashion as follows:
[6] :job = transpile_remote_serverless.run(
circuits=qc_random,
backend=backend.name,
optimization_level=optimization_level
)
job.job_id
Output:
'0ede4d0f-06d6-4360-86a1-5028ca511b3f'
Check job status
With your Qiskit Serverless job_id
, you can check the status of running jobs. This includes the following statuses:
QUEUED
: The remote program is in the Qiskit Serverless queue. The queue priority is currently based on how much you've used Qiskit ServerlessINITIALIZING
: The remote program is starting; this includes setting up the remote environment and installing dependenciesRUNNING
: The program is running. At this stage, if you haveprint()
outputs in your program, you can retrieve logs usingjob.logs()
DONE
: The program is complete, and you can retrieve data stored insave_result()
withjob.results()
job.status()
Output:
'QUEUED'
Currently, the IBM Quantum workloads table only reflects Qiskit Runtime workloads. Use job.status()
to see your Qiskit Serverless workload's current status.
Retrieve logs and results
As mentioned before, once a program is RUNNING
, you can use job.logs()
to fetch logs created from print()
outputs:
logs = job.logs()
print(logs)
Output:
No logs yet.
Once a program is DONE
, you can use job.results()
to fetch the result stored in save_result()
:
result = job.result()
print(result)
Output:
{}
At any time, you can also cancel a job:
[11] :job.stop()
Output:
'Job has been stopped.'
List previously run jobs run with Qiskit Serverless
You can use jobs()
to list all jobs submitted to Qiskit Serverless:
old_jobs = serverless.jobs()
old_jobs
Output:
[<Job | 0ede4d0f-06d6-4360-86a1-5028ca511b3f>,
<Job | abf78e9a-b554-4e38-966a-f99cff877b8c>,
<Job | 90e1109e-809f-4768-a2dc-f45bf71a97b4>,
<Job | 313050f2-aa78-4d7d-99f4-44bdfe98e4d7>]
You've successfully run your first Qiskit Serverless program!
Next steps
- Explore compute and data management tools available to your program, including parallelization.