Skip to main content
This project is in beta. Found a bug? Open a GitHub issue. Question or idea? Ask in Discussions — we'd love your feedback!

Execution modes using REST API

Package versions

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

qiskit[all]~=2.3.0

You can run your IBM Quantum primitive workloads using REST APIs in one of three execution modes, depending on your needs: job, session, and batch. This topic explains these modes.

note

This documentation utilizes the Python requests module to demonstrate the IBM Quantum Compute Service REST API. However, this workflow can be executed using any language or framework that supports working with REST APIs. Refer to the API reference documentation for details.

Job mode with REST API​

In job mode, a single primitive request of Estimator or Sampler is made without a context manager. See how to run a quantum circuit using Estimator and Sampler for some examples.

Session mode with REST API​

A session is a feature that lets you efficiently run multi-job iterative workloads on quantum computers. Using sessions helps avoid delays caused by queuing each job separately, which can be particularly useful for iterative tasks that require frequent communication between classical and quantum resources. More details about Sessions can be found in the documentation.

note

Open Plan users cannot submit session jobs.

Start a session​

Begin by creating a session and obtaining a session ID.

import json
import requests

sessionsUrl = "https://quantum.cloud.ibm.com/api/v1/sessions"
auth_id = "Bearer <YOUR_BEARER_TOKEN>"
backend = "<BACKEND_NAME>"
crn = "<SERVICE-CRN>"

headersList = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": auth_id,
"Service-CRN": crn
}

payload = json.dumps({
"backend": backend,
"mode": 'dedicated',
})

response = requests.request("POST", sessionsUrl, data=payload, headers=headersList)

sessionId = response.json()['id']

print(response.json())

Output

{'id': 'crw9s7cdbt40008jxesg'}

Close a session​

It is good practice to close a Session when all jobs are done. This will reduce wait time for subsequent users.

closureURL="https://quantum.cloud.ibm.com/api/v1/sessions/"+sessionId+"/close"

headersList = {
"Accept": "application/json",
"Authorization": auth_id,
"Service-CRN": crn
}

closure_response = requests.request(
"DELETE",
closureURL,
headers=headersList
)

print("Session closure response ok?:",closure_response.ok,closure_response.text)

Output

Session closure response ok?: True

Batch mode with REST API​

Alternatively, you can submit a batch job by specifying the mode in the request payload. Batch mode can help shorten processing time if all jobs can be provided at the outset. Learn about batch mode in the introduction to execution modes guide.

import json
import requests

sessionsUrl = "https://quantum.cloud.ibm.com/api/v1/sessions"

headersList = {
"Accept": "application/json",
"Authorization": auth_id,
"Service-CRN": crn,
'Content-Type': 'application/json'
}

payload = json.dumps({
"backend": backend,
"instance": "hub1/group1/project1",
"mode": "batch"
})

response = requests.request("POST", sessionsUrl, data=payload, headers=headersList)

sessionId = response.json()['id']

Examples of jobs submitted in a session​

Once a session is set up, one or more Sampler or Estimator jobs can be submitted to the same session by specifying the session ID.

note

The <parameter values> in a PUB can either be a single parameter or a list of parameters. It also supports numpy broadcasting.

Estimator jobs in session mode​

job_input = {
'program_id': 'estimator',
"backend": backend,
"session_id": sessionId, # This specifies the previously created Session
"params": {
"pubs": [[resulting_qasm, [obs1, obs2, obs3, obs4]]], #primitive unified blocs (PUBs) containing one circuit each.
"options":{
"transpilation":{"optimization_level": 1},
"twirling": {"enable_gates": True,"enable_measure": True},
# "dynamical_decoupling": {"enable": True, "sequence_type": "XpXm"}, #(optional)
},
}

}

Sampler jobs in session mode​

job_input = {
'program_id': 'sampler',
"backend": backend,
"session_id": sessionId, # This specifies the previously created Session
"params": {
"pubs": [[resulting_qasm]], #primitive unified blocs (PUBs) containing one circuit each
"options":{
"transpilation":{"optimization_level": 1},
"twirling": {"enable_gates": True,"enable_measure": True},
# "dynamical_decoupling": {"enable": True, "sequence_type": "XpXm"}, #(optional)
},
}

}

Next steps​

Recommendations
  • Review detailed Sampler primitives examples using REST API.
  • Review detailed Estimator primitives examples using REST API.
  • Practice with primitives by working through the Cost function lesson in IBM Quantum® Learning.
  • Learn how to transpile locally in the Transpile section.