Skip to main contentIBM Quantum Documentation Preview
This is a preview build of IBM Quantumâ„¢ documentation. Refer to docs.quantum.ibm.com for the official documentation.

Run your first Qiskit Serverless workload remotely

Package versions

The code on this page was developed using the following requirements. We recommend using these versions or newer.

qiskit[all]~=1.2.4
qiskit-ibm-runtime~=0.33.2
qiskit-aer~=0.15.1
qiskit-serverless~=0.18.0
qiskit-ibm-catalog~=0.2
qiskit-addon-sqd~=0.8.0
qiskit-addon-utils~=0.1.0
qiskit-addon-aqc-tensor~=0.1.2
qiskit-addon-obp~=0.1.0
scipy~=1.14.1
pyscf~=2.7.0

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 fetch 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()
next(
    program
    for program in serverless.list()
    if program.title == "transpile_remote_serverless"
)

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, seed=i)) for i in range(10)]
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_sherbrooke

Set your optimization level:

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:

job = transpile_remote_serverless.run(
    circuits=qc_random,
    backend=backend.name,
    optimization_level=optimization_level,
)
job.job_id

Output:

'ecee85e7-ef3b-4545-90fb-8dcf48405560'

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 Serverless
  • INITIALIZING: The remote program is starting; this includes setting up the remote environment and installing dependencies
  • RUNNING: The program is running. At this stage, if you have print() outputs in your program, you can retrieve logs using job.logs()
  • DONE: The program is complete, and you can retrieve data stored in save_result() with job.results()
job.status()

Output:

'QUEUED'
Tip

Currently, the IBM Quantum workloads table only reflects Qiskit Runtime workloads. Use job.status() to see your Qiskit Serverless workload's current status.

You've successfully run your first Qiskit Serverless program!


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.

At any time, you can also cancel a job:

job.stop()

Output:

'Job has been stopped.'

Once a program is DONE, you can use job.results() to fetch the result stored in save_result():

# We can't get results from a cancelled job, so we'll fetch a completed one instead
completed_job = next(
    job for job in serverless.jobs() if job.status() == "DONE"
)
completed_job.result()

Output:

{'transpiled_circuits': [<qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f0d7d10>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f8c0490>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f8d87d0>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12e81a4d0>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f49e990>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f341b90>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f86a650>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f802890>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f718710>,
  <qiskit.circuit.quantumcircuit.QuantumCircuit at 0x12f82ae90>]}

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 | ecee85e7-ef3b-4545-90fb-8dcf48405560>,
 <Job | 552ec541-8044-41a8-9a97-cde9ca316535>,
 <Job | 8c67f847-99eb-4510-8a39-3758813b1e87>,
 <Job | 56784784-e900-4915-bf08-9a63cbd57c95>,
 <Job | fa906659-18a8-4292-bbee-30624d81f566>,
 <Job | 82dbf9b5-55fd-4741-9561-920701b03367>,
 <Job | cbe95388-4b51-43ab-9558-c040700e2192>,
 <Job | ed883c41-9c09-48ff-a169-f48156cfe221>,
 <Job | 6d6ae253-44b8-4a6d-84a9-7f58b94aa478>,
 <Job | cdeff010-f128-433c-b056-ec9aa02c6d9f>]

Next steps

Recommendations