דלג לתוכן הראשי
פרויקט זה בשלב בטא. מצאת באג? פתח issue ב-GitHub. יש שאלה או רעיון? שאל ב-Discussions — נשמח לשמוע ממך!
מה איכות התרגום בדף זה?

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

Package versions

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

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 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 — installs the packages this notebook needs if they are missing
import importlib.util

_needed = {"qiskit": "qiskit", "qiskit_ibm_catalog": "qiskit-ibm-catalog", "qiskit_serverless": "qiskit-serverless"}
_missing = [pip for module, pip in _needed.items()
if importlib.util.find_spec(module) is None]
# One at a time, so a package that fails to install does not block the others
for _pip in _missing:
%pip install -q {_pip}
if not _missing:
print("\u2713 All required packages are installed")

דוגמה: 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​

הוסף את הקוד הבא לקובץ התוכנית שלך, שמקבל שירות ריצה (runtime service) ומשתמש בו כדי לבחור את ה-backend שלך.

השתמש ב-get_runtime_service בתוך תוכניות Qiskit Serverless

בתוך קוד שרץ על גבי Qiskit Serverless, צור את שירות הריצה שלך באמצעות get_runtime_service() (מיובא מ-qiskit_serverless) במקום ליצור מופע של QiskitRuntimeService() ישירות.

get_runtime_service() מחזיר עטיפה (wrapper) סביב QiskitRuntimeService שאפשר להשתמש בה כתחליף ישיר, ושרושמת כל עבודת primitive של IBM Quantum וכל session שהיא מפעילה מול עבודת ה-Qiskit Serverless ההורה. קישור זה הוא מה שמאפשר לפלטפורמה לקשר בין עבודת Serverless לבין עבודות ה-QPU וה-sessions שהיא יוצרת, לצורך מעקב סטטוס, לוגים, ושימוש וחיוב. שימוש ב-QiskitRuntimeService בלבד שולח עבודות שהפלטפורמה לעולם לא מקשרת לעבודת ה-Serverless שלך, ולכן הקישור הזה אובד.

כל הארגומנטים הם אופציונליים. בתוך קוד המקור של התוכנית, הצורה המומלצת היא get_runtime_service() ללא ארגומנטים, שמושכת את נתוני האימות שלך מהסביבה. היא מקבלת את אותם ארגומנטים אופציונליים channel, token, instance ו-url כמו QiskitRuntimeService.

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

%%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_runtime_service

service = get_runtime_service()
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

צעדים הבאים​

המלצות