Configurazione per l'utilizzo della piattaforma IBM Quantum con API REST
È possibile accedere ai processori quantistici con le API REST, che consentono di lavorare con le QPU utilizzando qualsiasi linguaggio o framework di programmazione.
1. Ottieni l'accesso
- Se non disponi ancora di un account utente, creane uno alla pagina di accesso IBM Quantum.
- Crea una chiave API (chiamata anche token) sulla dashboard. Si noti che la stessa chiave API può essere utilizzata per entrambe le regioni.
- Generare un token bearer IBM Cloud Identity and Access Management (IAM). È un token di breve durata utilizzato per autenticare le richieste all'API REST. Per generarne uno, chiamare l' API IAM Identity Services come mostrato nella seguente richiesta di esempio:
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
Scegliere il metodo di autenticazione appropriato, a seconda dell'ambiente di lavoro:
- Creare una variabile d'ambiente per la propria chiave API (ambienti fidati Python )
- Utilizzare direttamente la chiave API (ambiente non attendibile)
Creare una variabile di ambiente (ambiente affidabile)
-
Per impostare la variabile d'ambiente IQP_API_TOKEN nel vostro sistema, potete aggiungere la seguente riga al vostro profilo di shell (ad esempio,.bashrc o.zshrc) o impostarla direttamente nel vostro 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 si richiama la variabile d'ambiente nel codice, includere
import os, come in questo esempio:import os api_token = os.environ['IQP_API_TOKEN']Si noti che quando si crea una variabile d'ambiente, la chiave API è ancora memorizzata localmente in testo normale e deve essere protetta.
-
Autentica le richieste all'API REST includendo l' CRN e e il token bearer nelle intestazioni 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: configurare il firewall
Se necessario, utilizzare queste informazioni per abilitare l'accesso agli endpoint API di IBM Quantum.
Passi successivi
- Panoramica dei piani disponibili.
- Configurare l' Qiskit SDK e localmente.
- Segui i passaggi indicati nella guida Esegui il tuo primo circuito su hardware per scrivere ed eseguire un programma quantistico.
- Provate a seguire un' esercitazione.