ข้ามไปยังเนื้อหาหลัก
โครงการนี้อยู่ในช่วงเบต้า พบข้อผิดพลาด? สร้าง GitHub Issue มีคำถามหรือไอเดีย? ถามใน Discussions — ยินดีรับฟังความคิดเห็น!
คุณภาพการแปลในหน้านี้เป็นอย่างไร?

รัน parameter sweep

เวอร์ชันของแพ็กเกจ

โค้ดในหน้านี้พัฒนาขึ้นโดยใช้ข้อกำหนดต่อไปนี้ เราแนะนำให้ใช้เวอร์ชันเหล่านี้หรือใหม่กว่า

qiskit-serverless~=0.38.0

parameter sweep ทำงานเหมือน Slurm job array เรียก run() หนึ่งครั้งต่อชุดอินพุตแต่ละชุด แล้ว job จะรันพร้อมกัน แต่ละอันบนโหนดของตัวเอง ใช้รูปแบบเดียวกันกับอินพุตอิสระชุดใดก็ได้ เช่น หนึ่ง job ต่อโมเลกุลหรือต่อ backend แต่ละตัว job ส่งข้อมูลถึงกันไม่ได้ ดังนั้นให้แบ่งงานเป็นอินพุตที่เป็นอิสระต่อกัน

ตัวอย่างในหน้านี้ใช้เวิร์กโฟลว์จากรันเวิร์กโฟลว์ที่มีการจัดการ ซึ่งอ่านมุมหนึ่งค่า theta:

# Added by doQumentation — installs the packages this notebook needs if they are missing
import importlib.util

_needed = {"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")
# This cell is hidden from users. It creates the working directory.
from pathlib import Path

Path("./source_files").mkdir(exist_ok=True)
%%writefile ./source_files/my_workflow.py
import math

from qiskit_serverless import get_arguments, save_result

arguments = get_arguments()
theta = arguments["theta"]
if not 0 <= theta <= math.pi:
raise ValueError(f"theta must be between 0 and pi, got {theta}")

print(f"Computing the expectation value for theta={theta}", flush=True)
save_result({"theta": theta, "expectation_value": math.cos(theta)})
Writing ./source_files/my_workflow.py
# This cell is hidden from users. It uploads the workflow under a title that
# other test notebooks do not use, and makes load("my-workflow") return it.
import logging

from qiskit_ibm_catalog import ManagedWorkflow, ManagedWorkflowClient

logging.getLogger("qiskit_ibm_runtime").setLevel(logging.ERROR)

_test_title = "docs-run-parameter-sweep"
ManagedWorkflowClient().upload(
ManagedWorkflow(
title=_test_title,
entrypoint="my_workflow.py",
working_dir="./source_files/",
)
)
_load = ManagedWorkflowClient.load
ManagedWorkflowClient.load = lambda self, title, *a, **k: _load(
self, _test_title if title == "my-workflow" else title, *a, **k
)

เริ่ม job​

รับเวิร์กโฟลว์ด้วยชื่อ แล้วเรียก run() หนึ่งครั้งต่ออินพุตแต่ละชุด บันทึก job ID ลงไฟล์: run_workflow_remotely.ipynb

import json

from qiskit_ibm_catalog import ManagedWorkflowClient

client = ManagedWorkflowClient()
my_workflow = client.load("my-workflow")

thetas = [0.1, 0.2, 0.3]

job_ids = {
f"theta-{theta}": my_workflow.run(theta=theta).job_id for theta in thetas
}

with open("job_ids.json", "w") as f:
json.dump(job_ids, f)

รวบรวมผลลัพธ์​

รับแต่ละ job ด้วย ID และดักจับ job ที่ล้มเหลวเพื่อไม่ให้ความล้มเหลวหนึ่งครั้งหยุดลูป: run_workflow_remotely.ipynb

from qiskit_serverless.exception import QiskitServerlessException

with open("job_ids.json") as f:
job_ids = json.load(f)

results, errors = {}, {}
for run_name, sweep_job_id in job_ids.items():
try:
results[run_name] = client.job(sweep_job_id).result()
except QiskitServerlessException as error:
errors[run_name] = str(error)

print(f"{len(results)} jobs finished, {len(errors)} failed")
3 jobs finished, 0 failed
# This cell is hidden from users. It checks the sweep results.
assert len(results) == 3 and not errors, (results, errors)
assert all(results[f"theta-{t}"]["theta"] == t for t in thetas)

หากต้องการรันอินพุตที่ล้มเหลวอีกครั้ง ให้เรียก run() อีกครั้งด้วยอาร์กิวเมนต์เดิม

ขีดจำกัดงานที่ใช้งานอยู่​

คุณมีงานที่อยู่ในคิว กำลังเริ่มต้น กำลังรัน หรือกำลังยกเลิกพร้อมกันได้สูงสุด 50 งาน ขีดจำกัดนี้นับงานทั้งหมดของคุณ รวมถึงงานของเวิร์กโฟลว์อื่นด้วย หากเกินขีดจำกัดนี้ run() จะเกิด QiskitServerlessException และจะไม่สร้างหรือจัดคิวงาน:

| Message: Http bad request.
| Code: 429
| Details: Active job limit reached. The maximum allowed is 50.

ขั้นตอนถัดไป​

คำแนะนำ
  • จัดการไฟล์ข้ามการรัน เพื่อใช้ไฟล์อินพุตร่วมกันระหว่างการรัน และตั้งชื่อไฟล์ของแต่ละการรันให้ไม่ซ้ำกัน
  • เลือก compute profile เพื่อรันแต่ละงานของ sweep ด้วยขนาดที่เหมาะกับอินพุตของงานนั้น