Set up to use IBM Quantum Platform with REST API
You can access quantum processors with REST APIs, enabling you to work with QPUs using any programming language or framework.
1. Get access
- If you do not already have a user account, get one at the IBM Quantum login page.
- Create an API key (also called a token) on the dashboard. Note that the same API key can be used for either region.
- Generate an IBM Cloud Identity and Access Management (IAM) bearer token. This is a short-lived token used to authenticate requests to the REST API. To generate one, call the IAM Identity Services API as shown in the following sample request:
curl -X POST 'https://iam.cloud.ibm.com/identity/token' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=MY_APIKEY'
Expected Response
{
"access_token": "eyJhbGciOiJIUz......sgrKIi8hdFs",
"refresh_token": "SPrXw5tBE3......KBQ+luWQVY=",
"token_type": "Bearer",
"expires_in": 3600,
"expiration": 1473188353
}
# Use 'service' to invoke operations.
import requests
import json
url = 'https://iam.cloud.ibm.com/identity/token'
api_key = 'MY_APIKEY'
headers = {
'Content-Type': 'application/x-www-form-urlendcoded',
}
data = f'grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey={api_key}'
response = requests.post(url, headers=headers, data=data)
# Bearer token to authorize requests to the REST API
bearer_token = response.json()['access_token']
2. Choose an authentication method
Choose the appropriate authentication method, depending on your working environment:
- Create an environment variable for your API key (trusted Python environments)
- Use your API key directly (untrusted environment)
Create an environment variable (trusted environment)
-
To set the IQP_API_TOKEN environment variable in your system, you can add the following line to your shell profile (for example, .bashrc or .zshrc) or by setting it directly in your terminal:
export IQP_API_TOKEN=<your-token>
When you invoke the environment variable in your code, include
import os
, as in this example:import os api_token = os.environ['IQP_API_TOKEN']
Note that when creating an environment variable, your API key is still stored locally in plain text, and should be safeguarded.
-
Authenticate requests to the Qiskit Runtime REST API by including the CRN and bearer token to the request's headers.
curl -X 'GET' \
'https://quantum.cloud.ibm.com/api/v1/usage' \
'-H accept: application/json' \
'-H authorization: Bearer <BEARER_TOKEN>' \
'-H Service-CRN: crn:v1:bluemix:public:quantum-computing:us-east:a/b947c1c5a9378d64aed96696e4d76e8e:a3a7f181-35aa-42c8-94d6-7c8ed6e1a94b::'
3. Optional: Configure your firewall
If necessary, use this information to enable access to the IBM Quantum API endpoints.
Next steps
- Overview of available plans.
- Configure the Qiskit SDK locally.
- Follow the steps in Hello world to write and run a quantum program.
- Try a tutorial.