最初の Qiskit Serverless プログラムを書いてみましょう
このページのコードは、以下の要件に基づいて開発された。 これらのバージョンまたは新しいバージョンの使用をお勧めします。
qiskit[all]~=1.3.1 qiskit-ibm-runtime~=0.34.0 qiskit-aer~=0.15.1 qiskit-serverless~=0.18.1 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 Serverless GitHub ページでご覧いただけます。
この例では、ツール qiskit-serverless を使用して並列トランスパイルプログラムを作成し、その後、 IBM Quantum Platform にプログラムをアップロード qiskit-ibm-catalog して再利用可能なリモートサービスとして利用する方法について説明します。
ワークフローの概要
- ローカルディレクトリを作成し、プログラムファイルを空にする (
./source_files/transpile_remote.py) - プログラムに、 Qiskit Serverless にアップロードすると回路をトランスパイルするコードを追加してください
- Qiskit Serverless への
qiskit-ibm-catalog認証に使用 - プログラムを Qiskit Serverless にアップロードしてください
プログラムをアップロードしたら、 「最初の『 Qiskit Serverless 』ワークロードをリモートで実行する 」ガイドに従って、プログラムを実行し、回路をトランスパイルすることができます。
例: Qiskit Serverless を使用したリモート・トランスパイル
この例では、プログラムファイルの作成と追加の手順を解説します。このファイルを Qiskit Serverless にアップロードすると、指定された に対して circuit をトランスパイルし、 optimization_level``backend ターゲット としてコンパイルされます。
Qiskit Serverlessでは、ワークロードの .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 には3つの入力がある: 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")バックエンドを呼び出すコードを追加する
プログラムファイルに以下のコードを追加してください。これにより、 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_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService()
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キー QiskitServerless を使用して qiskit-ibm-catalog に認証してください(お手持ちの 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ロードした後、削除します。 は、 Qiskit Serverlessentrypoint が実行するためのメインプログラムの実行ファイルを指定します。
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 ワークロードの初回リモート実行 」のトピックで、入力の渡し方やプログラムのリモート実行方法について学びましょう。