REST API로 IBM Quantum Platform 사용 설정하기
REST API를 통해 양자 프로세서 에 접근할 수 있으며, 이를 통해 어떤 프로그래밍 언어나 프레임워크에서도 QPU를 사용할 수 있습니다.
1. 접근 권한 얻기
- 사용자 계정이 없다면 IBM Quantum 로그인 페이지에서 계정을 만드세요.
- 대시보드에서 API 키(토큰이라고도 함)를 생성하세요. 동일한 API 키를 두 지역 모두에서 사용할 수 있습니다.
- IBM Cloud Identity and Access Management (IAM) 베어러 토큰을 생성하세요. 이 토큰은 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'
예상 응답
{
"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. 인증 방법 선택하기
작업 환경에 따라 적절한 인증 방법을 선택하세요:
환경 변수 만들기 (신뢰할 수 있는 환경)
-
시스템에서 IQP_API_TOKEN 환경 변수를 설정하려면, 쉘 프로파일(예: .bashrc 또는 .zshrc)에 다음 줄을 추가하거나 터미널에서 직접 설정할 수 있습니다:
export IQP_API_TOKEN=<your-API_KEY> # Use the 44-character API_KEY you created and saved from the IBM Quantum Platform Home dashboard코드에서 환경 변수를 사용할 때는 다음 예시와 같이
import os를 포함하세요:import os
api_token = os.environ['IQP_API_TOKEN']환경 변수를 생성할 때 API 키는 여전히 로컬에 일반 텍스트로 저장되므로 안전하게 관리해야 합니다.
-
요청의 헤더에 CRN과 베어러 토큰을 포함하여 Qiskit Runtime REST API에 대한 요청을 인증하세요.
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. 선택 사항: 방화벽 구성하기
필요한 경우 이 정보를 사용하여 IBM Quantum API 엔드포인트에 대한 접근을 활성화하세요.