דלג לתוכן הראשי

כתוב את תוכנית Qiskit Serverless הראשונה שלך

Package versions

הקוד בדף זה פותח באמצעות הדרישות הבאות. אנחנו ממליצים להשתמש בגרסאות אלו או בגרסאות חדשות יותר.

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 ליצירת תוכנית transpilation מקבילית, ולאחר מכן ליישם את qiskit-ibm-catalog כדי להעלות את התוכנית ל-IBM Quantum Platform לשימוש כשירות מרוחק לשימוש חוזר.

סקירת תהליך העבודה

  1. צור תיקייה מקומית וקובץ תוכנית ריק (./source_files/transpile_remote.py)
  2. הוסף קוד לתוכנית שלך שיבצע transpile של circuit בעת העלאה ל-Qiskit Serverless
  3. השתמש ב-qiskit-ibm-catalog כדי לאמת ל-Qiskit Serverless
  4. העלה את התוכנית ל-Qiskit Serverless

לאחר העלאת התוכנית, תוכל להריץ אותה לביצוע transpile של circuit על ידי מעקב אחר המדריך הרצת עומס העבודה הראשון שלך ב-Qiskit Serverless מרחוק.

# Added by doQumentation — required packages for this notebook
!pip install -q qiskit qiskit-ibm-catalog qiskit-ibm-runtime qiskit-serverless

דוגמה: transpilation מרוחק עם Qiskit Serverless

דוגמה זו מדריכה אותך ביצירת קובץ תוכנית והוספת קוד אליו, שכאשר תעלה אותו ל-Qiskit Serverless, יבצע transpile של circuit בהתאם ל-backend נתון ורמת optimization_level יעד.

טיפ

Qiskit Serverless דורש הגדרת קבצי .py של עומס העבודה שלך לתוך תיקייה ייעודית. המבנה הבא הוא דוגמה לפרקטיקה טובה:

serverless_program
├── program_uploader.ipynb
└── source_files
├── transpile_remote.py
└── *.py

Serverless מעלה את תוכן תיקייה ספציפית (בדוגמה זו, תיקיית source_files) להרצה מרחוק. לאחר הגדרתם, תוכל לכוונן את transpile_remote.py כדי לקבל קלטים ולהחזיר פלטים.

צור את התיקייה וקובץ תוכנית ריק

ראשית, צור תיקייה בשם source_files, ולאחר מכן צור קובץ תוכנית בתיקייה, כך שהנתיב שלו יהיה ./source_files/transpile_remote.py. זהו הקובץ שתעלה ל-Qiskit Serverless.

הוסף קוד לקובץ התוכנית שלך

מלא את קובץ התוכנית שלך בקוד הבא ושמור אותו.

זהירות

אם אתה קורא את תאי הקוד באופן מקומי במחברת, תראה את פקודת ה-magic %%writefile. הפעלת תאים עם פקודת magic זו שומרת אותם לדיסק במקום להריצם.

# This cell is hidden from users, it creates a new folder
from pathlib import Path

Path("./source_files").mkdir(exist_ok=True)
%%writefile ./source_files/transpile_remote.py
# 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. כרגע Serverless מוגבל לקבל רק קלטים ופלטים שניתן לסדרייה. מסיבה זו, אינך יכול להעביר את backend ישירות, לכן השתמש ב-backend_name כמחרוזת במקום.

%%writefile --append ./source_files/transpile_remote.py
# 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")

הוסף קוד שקורא ל-backend

הוסף את הקוד הבא לקובץ התוכנית שלך, שקורא ל-backend שלך עם QiskitRuntimeService.

הקוד הבא מניח שכבר עקבת אחר התהליך לשמירת האישורים שלך באמצעות QiskitRuntimeService.save_account, וייטען את החשבון השמור כברירת מחדל שלך אלא אם תציין אחרת. ראה שמור את האישורים שלך ואתחול חשבון שירות Qiskit Runtime שלך למידע נוסף.

%%writefile --append ./source_files/transpile_remote.py
# 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)

הוסף קוד לביצוע transpile

לבסוף, הוסף את הקוד הבא לקובץ התוכנית שלך. קוד זה מריץ את transpile_remote() על כל ה-circuits שהועברו, ומחזיר את transpiled_circuits כתוצאה:

%%writefile --append ./source_files/transpile_remote.py
# 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

השתמש ב-qiskit-ibm-catalog כדי לאמת ל-QiskitServerless עם מפתח ה-API שלך (תוכל להשתמש במפתח ה-API של QiskitRuntimeService שלך, או ליצור מפתח API חדש בלוח המחוונים של IBM Quantum Platform).

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, שמועלה ולאחר מכן מנוקה. ה-entrypoint מזהה את קובץ ההפעלה הראשי של התוכנית שעל Qiskit Serverless להריץ.

transpile_remote_demo = QiskitFunction(
title="transpile_remote_serverless",
entrypoint="transpile_remote.py",
working_dir="./source_files/",
)
serverless.upload(transpile_remote_demo)
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"
)
QiskitFunction(transpile_remote_serverless)
# This cell is hidden from users, it checks the program uploaded correctly
assert _.title == "transpile_remote_serverless" # noqa: F821
# This cell is hidden from users, it checks the program executes correctly
from time import sleep
from qiskit import QuantumCircuit

qc = QuantumCircuit(2)
transpile_remote_serverless = serverless.load("transpile_remote_serverless")
job = transpile_remote_serverless.run(
circuits=[qc],
backend="ibm_sherbrooke",
optimization_level=1,
)
while True:
sleep(5)
status = job.status()
if status not in ["QUEUED", "INITIALIZING", "RUNNING", "DONE"]:
raise Exception(
f"Unexpected job status: '{status}'\n"
+ "Here are the logs:\n"
+ job.logs()
)
if status == "DONE":
break

צעדים הבאים

המלצות