Configurazione per utilizzare IBM Quantum Platform con la REST API
Puoi accedere ai processori quantistici tramite le REST API, che ti permettono di lavorare con le QPU usando qualsiasi linguaggio di programmazione o framework.
1. Ottieni l'accesso
-
Se non hai già un account utente, creane uno dalla pagina di accesso a IBM Quantum.
-
Crea una chiave API (chiamata anche token) nella dashboard. Tieni presente che la stessa chiave API può essere utilizzata per entrambe le regioni.
-
Genera un bearer token di IBM Cloud Identity and Access Management (IAM). Si tratta di un token a breve durata utilizzato per autenticare le richieste alla REST API. Per generarne uno, chiama la IAM Identity Services API come mostrato nella seguente richiesta di esempio:
- Curl
- Python
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'
Risposta attesa
{
"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-urlencoded',
}
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. Scegli un metodo di autenticazione
Scegli il metodo di autenticazione appropriato in base al tuo ambiente di lavoro:
-
Crea una variabile d'ambiente per la tua chiave API (ambienti Python affidabili)
-
Usa la tua chiave API direttamente (ambiente non affidabile)
Crea una variabile d'ambiente (ambiente affidabile)
-
Per impostare la variabile d'ambiente IQP_API_TOKEN nel tuo sistema, puoi aggiungere la seguente riga al tuo profilo shell (ad esempio, .bashrc o .zshrc) oppure impostarla direttamente nel terminale:
export IQP_API_TOKEN=<your-API_KEY># Use the 44-character API_KEY you created and saved# from the IBM Quantum Platform Home dashboardQuando richiami la variabile d'ambiente nel tuo codice, includi
import os, come in questo esempio:import osapi_token = os.environ['IQP_API_TOKEN']Tieni presente che, quando crei una variabile d'ambiente, la tua chiave API viene comunque salvata localmente in chiaro e deve essere protetta.
-
Autentica le richieste alla REST API includendo il CRN e il bearer token negli header della richiesta.
curl -X 'GET' \
'https://quantum.cloud.ibm.com/api/v1/usage' \
'-H accept: application/json' \
'-H authorization: Bearer <BEARER_TOKEN>' \
'-H Service-CRN: <INSTANCE_CRN>'
3. Facoltativo: configura il firewall
Se necessario, usa queste informazioni per abilitare l'accesso agli endpoint API di IBM Quantum.
Passi successivi
- Panoramica dei piani disponibili.
- Configura Qiskit SDK in locale.
- Segui i passaggi nella guida Esegui il tuo primo circuito su hardware per scrivere ed eseguire un programma quantistico.
- Prova un tutorial.