첫 번째 Qiskit Serverless 프로그램을 작성하세요
이 페이지의 코드는 다음 요구 사항을 사용하여 개발되었습니다. 다음 버전 이상을 사용하는 것이 좋습니다.
qiskit[all]~=1.3.1 qiskit-ibm-runtime~=0.34.0 qiskit-aer~=0.15.1 qiskit-serverless~=0.27.0 qiskit-ibm-catalog~=0.2 qiskit-addon-sqd~=0.8.1 qiskit-addon-utils~=0.1.0 qiskit-addon-mpf~=0.2.0 qiskit-addon-aqc-tensor~=0.1.2 qiskit-addon-obp~=0.1.0 scipy~=1.15.0 pyscf~=2.8.0
Qiskit Serverless 업그레이드를 진행 중이며, 기능이 빠르게 변화하고 있습니다. 이 개발 단계 동안, 릴리스 노트와 최신 문서는 Qiskit ServerlessGitHub 페이지에서 확인하실 수 있습니다.
이 예제는 도구를 qiskit-serverless 사용하여 병렬 트랜스파일링 프로그램을 만드는 방법을 보여주고, 이를 qiskit-ibm-catalog 구현하여 프로그램을 IBM Quantum Platform 에 업로드함으로써 재사용 가능한 원격 서비스로 활용하는 방법을 설명합니다.
작업 흐름 개요
- 로컬 디렉터리를 생성하고 프로그램 파일을 비워주세요 (
./source_files/transpile_remote.py) - 프로그램에 코드를 추가하여, Qiskit Serverless 에 업로드하면 회로를 트랜스파일링하도록 하세요
- Qiskit Serverless 에 인증하려면
qiskit-ibm-catalog사용하십시오 - 프로그램을 Qiskit Serverless 에 업로드하세요
프로그램을 업로드한 후, ‘첫 번째 Qiskit Serverless 워크로드를 원격으로 실행하기’ 가이드를 따라 프로그램을 실행하여 회로를 트랜스파일할 수 있습니다.
예시: Qiskit Serverless 를 사용한 원격 트랜스파일링
이 예제는 프로그램 파일을 생성하고 내용을 추가하는 과정을 단계별로 안내합니다. 이 파일을 optimization_levelQiskit Serverless 에 업로드하면, 지정된 backend 및 대상 을 기준으로 circuit 를 트랜스파일합니다.
키스킷 서버리스를 사용하려면 워크로드의 .py 파일을 전용 디렉토리에 설정해야 합니다. 다음 구조는 모범 사례의 예입니다:
serverless_program
├── program_uploader.ipynb
└── source_files
├── transpile_remote.py
└── *.pyServerless는 특정 디렉터리의 내용(이 예시에서는 디렉터리 source_files )을 업로드하여 원격으로 실행합니다. 이 설정들이 완료되면, 입력을 가져오고 출력을 transpile_remote.py 반환하도록 조정할 수 있습니다.
디렉터리와 빈 프로그램 파일을 생성합니다
먼저 라는 source_files 이름의 디렉터리를 만들고, 그 디렉터리 안에 프로그램 파일을 생성하여 경로가 가 되도록 ./source_files/transpile_remote.py하세요. 이 파일을 Qiskit Serverless 에 업로드하시면 됩니다.
프로그램 파일에 코드를 추가하세요
프로그램 파일에 다음 코드를 입력한 다음 저장하세요.
노트북에서 코드 셀을 로컬로 실행하면 매직 %%writefile 명령어가 표시됩니다. 이 마법 같은 명령어를 사용하여 셀을 실행하면, 셀이 실제로 실행되는 대신 디스크에 저장됩니다.
# This cell is hidden from users, it creates a new folder
from pathlib import Path
Path("./source_files").mkdir(exist_ok=True)# If you include the preceding `%%writefile` command (visible only when you read this
# locally in a notebook), running this cell saves to disk rather than executing the code.
from qiskit.transpiler import generate_preset_pass_manager
def transpile_remote(circuit, optimization_level, backend):
"""Transpiles an abstract circuit into an ISA circuit for a given backend."""
pass_manager = generate_preset_pass_manager(
optimization_level=optimization_level,
backend=backend
)
isa_circuit = pass_manager.run(circuit)
return isa_circuit프로그램 인수를 가져오려면 코드를 추가하세요
이제 프로그램 파일에 다음 코드를 추가하세요. 이 코드는 프로그램 인수를 설정합니다.
이니셜 transpile_remote.py 에는 세 가지 입력이 있습니다: circuits, backend_name, optimization_level 입니다. 서버리스는 현재 직렬화 가능한 입력과 출력만 허용하도록 제한되어 있습니다. 따라서 backend 을 직접 전달할 수 없으므로 대신 backend_name 을 문자열로 사용하세요.
# If you include the preceding `%%writefile` command (visible only when you read this
# locally in a notebook), running this cell saves to disk rather than executing the code.
from qiskit_serverless import get_arguments, save_result, distribute_task, get
# Get program arguments
arguments = get_arguments()
circuits = arguments.get("circuits")
backend_name = arguments.get("backend_name")
optimization_level = arguments.get("optimization_level")백엔드를 호출하는 코드를 추가하세요
프로그램 파일에 다음 코드를 추가하세요. 이 코드는 런타임 서비스를 가져와 이를 사용하여 백엔드를 선택합니다.
Qiskit Serverless 에서 실행되는 코드 내에서, 를 직접 QiskitRuntimeService() 인스턴스화하는 대신 ( get_runtime_service() 에서 가져온 qiskit_serverless) 를 사용하여 런타임 서비스를 생성하십시오.
get_runtime_service() IBM Quantum 의 모든 기본 작업과 이를 통해 실행되는 세션을 상위 Qiskit Serverless 작업에 등록하는, 해당 QiskitRuntimeService 작업을 둘러싼 드롭인 래퍼를 반환합니다. 이 연동 기능을 통해 플랫폼은 서버리스 작업을 해당 작업이 생성하는 QPU 작업 및 세션과 연결하여, 상태 추적, 로그 기록, 사용량 및 과금 처리를 수행할 수 있습니다. 'bare'는 플랫폼이 해당 작업을 사용자의 서버리스 작업과 절대 연결하지 않도록 작업을 QiskitRuntimeService 제출하므로, 두 작업 간의 연결이 끊어집니다.
모든 인수가
선택적입니다. 프로그램 소스 코드 내에서 권장되는 형식은 인수를 지정하지 않는 get_runtime_service() 것으로, 이 경우 환경에서 인증 데이터를 가져옵니다. 이 함수는 와 동일한 channel, token, instance, 및 url 인수들을 선택적으로 받아들입니다 QiskitRuntimeService.
다음 코드는 사용자가 이미 를 사용하여 인증 정보를 저장하는 절차를 완료했다고 가정하며 QiskitRuntimeService.save_account, 별도로 지정하지 않는 한 저장된 기본 계정을 불러옵니다. 자세한 내용은 ‘로그인 자격 증명 저장’ 및 ‘ IBM Quantum Compute Service 계정 초기화’를 참조하십시오.
# If you include the preceding `%%writefile` command (visible only when you read this
# locally in a notebook), running this cell saves to disk rather than executing the code.
from qiskit_serverless import get_runtime_service
service = get_runtime_service()
backend = service.backend(backend_name)트랜스파일할 코드 추가
마지막으로, 프로그램 파일에 다음 코드를 추가하세요. 이 코드는 전달된 circuits 모든 값에 대해 transpile_remote() 실행되며, 그 transpiled_circuits 결과를 반환합니다:
# If you include the preceding `%%writefile` command (visible only when you read this
# locally in a notebook), running this cell saves to disk rather than executing the code.
# Each circuit is being transpiled and will populate the array
results = [
transpile_remote(circuit, 1, backend)
for circuit in circuits
]
save_result({
"transpiled_circuits": results
})Qiskit Serverless 에 인증하기
API 키를 사용하여 qiskit-ibm-catalog``QiskitServerless 에 인증하십시오(기존 QiskitRuntimeService API 키를 사용하거나 IBM Quantum Platform 대시보드 에서 새 API 키를 생성할 수 있습니다).
from qiskit_ibm_catalog import QiskitServerless, QiskitFunction
# Authenticate to the remote cluster and submit the pattern for remote execution
serverless = QiskitServerless()코드를 실행하여 업로드하세요
프로그램을 업로드하려면 다음 코드를 실행하세요. Qiskit Serverless (이 경우 working_dir ) source_files의 내용을 로 압축한 tar 뒤, 이를 업로드하고 정리합니다. 는 entrypointQiskit Serverless 가 실행할 주 실행 파일을 지정합니다.
transpile_remote_demo = QiskitFunction(
title="transpile_remote_serverless",
entrypoint="transpile_remote.py",
working_dir="./source_files/",
)serverless.upload(transpile_remote_demo)Output:
QiskitFunction(transpile_remote_serverless)
업로드 확인
업로드가 성공적으로 완료되었는지 확인하려면 다음 코드에서와 같이 를 serverless.list()사용하세요:
# Get program from serverless.list() that matches the title of the one we uploaded
next(
program
for program in serverless.list()
if program.title == "transpile_remote_serverless"
)Output:
QiskitFunction(transpile_remote_serverless)
다음 단계
- ‘첫 번째 Qiskit Serverless 워크로드를 원격으로 실행하기’ 항목에서 입력값을 전달하고 프로그램을 원격으로 실행하는 방법을 알아보세요.