ตั้งค่าเพื่อใช้ IBM Quantum Platform กับ REST API
สามารถเข้าถึง quantum processors ด้วย REST APIs ซึ่งช่วยให้ทำงานกับ QPUs โดยใช้ภาษาโปรแกรมหรือ framework ใดก็ได้
1. รับสิทธิ์การเข้าถึง
- ถ้ายังไม่มีบัญชีผู้ใช้ ลงทะเบียนได้ที่ หน้าเข้าสู่ระบบ IBM Quantum
- สร้าง API key (เรียกอีกชื่อว่า token) ได้ที่ dashboard โปรดทราบว่าสามารถใช้ API key เดียวกันสำหรับ region ใดก็ได้
- สร้าง IBM Cloud Identity and Access Management (IAM) bearer token นี่คือ token ระยะสั้นที่ใช้ยืนยันตัวตนสำหรับคำขอไปยัง REST API หากต้องการสร้าง ให้เรียก IAM Identity Services API ดังตัวอย่างคำขอต่อไปนี้:
- 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'
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. เลือกวิธีการยืนยันตัวตน
เลือกวิธีการยืนยันตัวตนที่เหมาะสมตาม environment ที่ใช้งาน:
- สร้าง environment variable สำหรับ API key (Python environments ที่เชื่อถือได้)
- ใช้ API key โดยตรง (environment ที่ไม่เชื่อถือ)
สร้าง environment variable (environment ที่เชื่อถือได้)
-
หากต้องการตั้งค่า environment variable IQP_API_TOKEN ในระบบ สามารถเพิ่มบรรทัดต่อไปนี้ใน shell profile (เช่น .bashrc หรือ .zshrc) หรือตั้งค่าโดยตรงใน terminal:
export IQP_API_TOKEN=<your-API_KEY> # Use the 44-character API_KEY you created and saved from the IBM Quantum Platform Home dashboardเมื่อเรียกใช้ environment variable ในโค้ด ให้ include
import osดังตัวอย่างนี้:import os
api_token = os.environ['IQP_API_TOKEN']โปรดทราบว่าเมื่อสร้าง environment variable API key ยังคงถูกเก็บในเครื่องในรูปแบบ plain text และควรป้องกันอย่างเหมาะสม
-
ยืนยันตัวตนคำขอไปยัง Qiskit Runtime REST API โดยรวม CRN และ bearer token ใน headers ของคำขอ
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. ไม่บังคับ: กำหนดค่า firewall
หากจำเป็น ใช้ ข้อมูลนี้ เพื่อเปิดใช้งานการเข้าถึง IBM Quantum API endpoints