כניסות ויציאות של פרימיטיבים
גרסת הבטא של מודל הרצה חדש זמינה עכשיו. מודל ההרצה המכוון מספק גמישות רבה יותר בהתאמה אישית של זרימת העבודה לצמצום שגיאות. ראה את המדריך מודל הרצה מכוון למידע נוסף.
Package versions
The code on this page was developed using the following requirements. We recommend using these versions or newer.
qiskit[all]~=2.3.0
qiskit-ibm-runtime~=0.43.1
דף זה מספק סקירה כללית של הכניסות והיציאות של פרימיטיבי Qiskit Runtime המריצים עומסי עבודה על משאבי מחשוב של IBM Quantum®. פרימיטיבים אלה מעניקים לך את היכולת להגדיר ביעילות עומסי עבודה מווקטרים באמצעות מבנה נתונים המכונה Primitive Unified Bloc (PUB). PUBs אלה הם יחידת העבודה הבסיסית שה-QPU צריך כדי להריץ עומסי עבודה אלה. הם משמשים כקלטים לשיטת run() עבור פרימיטיבי Sampler ו-Estimator, שמריצים את עומס העבודה המוגדר כמשימה. לאחר השלמת המשימה, התוצאות מוחזרות בפורמט שתלוי הן ב-PUBs שנעשה בהם שימוש והן באפשרויות זמן הריצה שצוינו על ידי פרימיטיבי Sampler או Estimator.
סקירה כללית של PUBs
בעת קריאה לשיטת run() של פרימיטיב, הארגומנט העיקרי הנדרש הוא list של tuple אחת או יותר — אחת לכל Circuit המורץ על ידי הפרימיטיב. כל אחת מ-tuples אלה נחשבת ל-PUB, והאלמנטים הנדרשים של כל tuple ברשימה תלויים בפרימיטיב המשמש. הנתונים המסופקים ל-tuples אלה יכולים גם להיות מסודרים במגוון צורות כדי לספק גמישות בעומס עבודה באמצעות broadcasting — כללים שמתוארים בסעיף הבא.
Estimator PUB
עבור פרימיטיב ה-Estimator, פורמט ה-PUB צריך להכיל לכל היותר ארבעה ערכים:
QuantumCircuitיחיד, שעשוי להכיל אובייקטParameterאחד או יותר- רשימה של observable אחד או יותר, המציינים את ערכי הציפייה להערכה, מסודרים במערך (למשל, observable יחיד המיוצג כמערך ממימד 0, רשימת observables כמערך חד-ממדי, וכן הלאה). הנתונים יכולים להיות בכל אחד מפורמטי
ObservablesArrayLikeכגוןPauli,SparsePauliOp,PauliList, אוstr.הערהאם יש לך שני observables מתחלפים ב-PUBs שונים אך עם אותו Circuit, הם לא יוערכו באמצעות אותה מדידה. כל PUB מייצג בסיס שונה למדידה, ולכן נדרשות מדידות נפרדות לכל PUB. כדי להבטיח שה-observables המתחלפים יוערכו באמצעות אותה מדידה, יש לקבץ אותם באותו PUB.
- אוסף של ערכי פרמטרים לקשירה מול ה-Circuit. ניתן לציין זאת כאובייקט דמוי-מערך יחיד שבו האינדקס האחרון הוא על אובייקטי
Parameterשל ה-Circuit, או להשמיט (או באופן שקול, להגדיר ל-None) אם ל-Circuit אין אובייקטיParameter. - (אופציונלי) דיוק יעד לערכי ציפייה להערכה
Sampler PUB
עבור פרימיטיב ה-Sampler, פורמט ה-PUB מכיל לכל היותר שלושה ערכים:
QuantumCircuitיחיד, שעשוי להכיל אובייקטParameterאחד או יותר הערה: Circuits אלה צריכים לכלול גם הוראות מדידה לכל ה-Qubits שיש לדגום.- אוסף של ערכי פרמטרים לקשירה מול ה-Circuit (נדרש רק אם נעשה שימוש באובייקטי
Parameterשיש לקשור אותם בזמן ריצה) - (אופציונלי) מספר shots למדידת ה-Circuit
הקוד הבא מדגים קבוצת קלטים מווקטרת לדוגמה לפרימיטיב Estimator ומריץ אותם על Backend של IBM® כאובייקט RuntimeJobV2 יחיד.
# Added by doQumentation — required packages for this notebook
!pip install -q numpy qiskit qiskit-ibm-runtime
from qiskit.circuit import (
Parameter,
QuantumCircuit,
ClassicalRegister,
QuantumRegister,
)
from qiskit.transpiler import generate_preset_pass_manager
from qiskit.quantum_info import SparsePauliOp
from qiskit.primitives.containers import BitArray
from qiskit_ibm_runtime import (
QiskitRuntimeService,
EstimatorV2 as Estimator,
SamplerV2 as Sampler,
)
import numpy as np
# Instantiate runtime service and get
# the least busy backend
service = QiskitRuntimeService()
backend = service.least_busy(operational=True, simulator=False)
# Define a circuit with two parameters.
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.ry(Parameter("a"), 0)
circuit.rz(Parameter("b"), 0)
circuit.cx(0, 1)
circuit.h(0)
# Transpile the circuit
pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
transpiled_circuit = pm.run(circuit)
layout = transpiled_circuit.layout
# Now define a sweep over parameter values, the last axis of dimension 2 is
# for the two parameters "a" and "b"
params = np.vstack(
[
np.linspace(-np.pi, np.pi, 100),
np.linspace(-4 * np.pi, 4 * np.pi, 100),
]
).T
# Define three observables. The inner length-1 lists cause this array of
# observables to have shape (3, 1), rather than shape (3,) if they were
# omitted.
observables = [
[SparsePauliOp(["XX", "IY"], [0.5, 0.5])],
[SparsePauliOp("XX")],
[SparsePauliOp("IY")],
]
# Apply the same layout as the transpiled circuit.
observables = [
[observable.apply_layout(layout) for observable in observable_set]
for observable_set in observables
]
# Estimate the expectation value for all 300 combinations of observables
# and parameter values, where the pub result will have shape (3, 100).
#
# This shape is due to our array of parameter bindings having shape
# (100, 2), combined with our array of observables having shape (3, 1).
estimator_pub = (transpiled_circuit, observables, params)
# Instantiate the new estimator object, then run the transpiled circuit
# using the set of parameters and observables.
estimator = Estimator(mode=backend)
job = estimator.run([estimator_pub])
result = job.result()
כללי broadcasting
ה-PUBs מאגדים אלמנטים ממערכים מרובים (observables וערכי פרמטרים) על ידי ביצוע אותם כללי broadcasting כמו NumPy. סעיף זה מסכם בקצרה את הכללים האלה. להסבר מפורט, ראה את תיעוד כללי broadcasting של NumPy.
כללים:
- מערכי קלט אינם צריכים להיות בעלי אותו מספר ממדים.
- למערך המתקבל יהיה אותו מספר ממדים כמו למערך הקלט עם הממד הגדול ביותר.
- הגודל של כל ממד הוא הגודל הגדול ביותר של הממד המתאים.
- ממדים חסרים מניחים שיש להם גודל אחד.
- השוואות צורה מתחילות מהממד הימני ביותר וממשיכות שמאלה.
- שני ממדים תואמים אם הגדלים שלהם שווים או אם אחד מהם הוא 1.
דוגמאות לזוגות מערכים שמבצעים broadcasting:
A1 (1d array): 1
A2 (2d array): 3 x 5
Result (2d array): 3 x 5
A1 (3d array): 11 x 2 x 7
A2 (3d array): 11 x 1 x 7
Result (3d array): 11 x 2 x 7
דוגמאות לזוגות מערכים שאינם מבצעים broadcasting:
A1 (1d array): 5
A2 (1d array): 3
A1 (2d array): 2 x 1
A2 (3d array): 6 x 5 x 4 # This would work if the middle dimension were 2, but it is 5.
EstimatorV2 מחזיר הערכת ערך ציפייה אחת לכל אלמנט של הצורה המבוצעת ב-broadcasting.
להלן מספר דוגמאות לתבניות נפוצות המבוטאות במונחים של broadcasting של מערכים. הייצוג הוויזואלי המלווה שלהן מוצג באיור שלאחר מכן:
קבוצות ערכי פרמטרים מיוצגות על ידי מערכים של n x m, ומערכי observables מיוצגים על ידי מערכי עמודה יחידה אחד או יותר. לכל דוגמה בקוד הקודם, קבוצות ערכי הפרמטרים משולבות עם מערך ה-observables שלהן כדי ליצור הערכות ערך ציפייה מתקבלות.
-
דוגמה 1: (broadcast observable יחיד) כוללת קבוצת ערכי פרמטרים שהיא מערך 5x1 ומערך observables של 1x1. הפריט היחיד במערך ה-observables משולב עם כל פריט בקבוצת ערכי הפרמטרים כדי ליצור מערך יחיד 5x1 שבו כל פריט הוא שילוב של הפריט המקורי בקבוצת ערכי הפרמטרים עם הפריט במערך ה-observables.
-
דוגמה 2: (zip) כוללת קבוצת ערכי פרמטרים של 5x1 ומערך observables של 5x1. הפלט הוא מערך 5x1 שבו כל פריט הוא שילוב של הפריט ה-n בקבוצת ערכי הפרמטרים עם הפריט ה-n במערך ה-observables.
-
דוגמה 3: (outer/product) כוללת קבוצת ערכי פרמטרים של 1x6 ומערך observables של 4x1. השילוב שלהם גורם למערך 4x6 שנוצר על ידי שילוב כל פריט בקבוצת ערכי הפרמטרים עם כל פריט במערך ה-observables, ובכך כל ערך פרמטר הופך לעמודה שלמה בפלט.
-
דוגמה 4: (הכללה סטנדרטית של nd) כוללת מערך קבוצת ערכי פרמטרים של 3x6 ושני מערכי observables של 3x1. אלה משולבים ליצירת שני מערכי פלט של 3x6 בדרך דומה לדוגמה הקודמת.
# Broadcast single observable
parameter_values = np.random.uniform(size=(5,)) # shape (5,)
observables = SparsePauliOp("ZZZ") # shape ()
# >> pub result has shape (5,)
# Zip
parameter_values = np.random.uniform(size=(5,)) # shape (5,)
observables = [
SparsePauliOp(pauli) for pauli in ["III", "XXX", "YYY", "ZZZ", "XYZ"]
] # shape (5,)
# >> pub result has shape (5,)
# Outer/Product
parameter_values = np.random.uniform(size=(1, 6)) # shape (1, 6)
observables = [
[SparsePauliOp(pauli)] for pauli in ["III", "XXX", "YYY", "ZZZ"]
] # shape (4, 1)
# >> pub result has shape (4, 6)
# Standard nd generalization
parameter_values = np.random.uniform(size=(3, 6)) # shape (3, 6)
observables = [
[
[SparsePauliOp(["XII"])],
[SparsePauliOp(["IXI"])],
[SparsePauliOp(["IIX"])],
],
[
[SparsePauliOp(["ZII"])],
[SparsePauliOp(["IZI"])],
[SparsePauliOp(["IIZ"])],
],
] # shape (2, 3, 1)
# >> pub result has shape (2, 3, 6)
כל SparsePauliOp נחשב כאלמנט יחיד בהקשר זה, ללא קשר למספר ה-Paulis הכלולים ב-SparsePauliOp. לכן, לצורך כללי broadcasting אלה, לכל האלמנטים הבאים יש אותה צורה:
a = SparsePauliOp("Z") # shape ()
b = SparsePauliOp("IIIIZXYIZ") # shape ()
c = SparsePauliOp.from_list(["XX", "XY", "IZ"]) # shape ()
הרשימות הבאות של אופרטורים, אף שהן שוות מבחינת המידע הכלול בהן, בעלות צורות שונות:
list1 = SparsePauliOp.from_list(["XX", "XY", "IZ"]) # shape ()
list2 = [SparsePauliOp("XX"), SparsePauliOp("XY"), SparsePauliOp("IZ")] # shape (3, )
סקירה כללית של פלטי פרימיטיבים
לאחר שאחד או יותר PUBs נשלחים ל-QPU לביצוע ומשימה מסתיימת בהצלחה, הנתונים מוחזרים כאובייקט מכיל PrimitiveResult הנגיש על ידי קריאה לשיטת RuntimeJobV2.result(). ה-PrimitiveResult מכיל רשימה ניתנת לאיטרציה של אובייקטי PubResult המכילים את תוצאות הביצוע לכל PUB. בהתאם לפרימיטיב המשמש, נתונים אלה יהיו ערכי ציפייה ופסי השגיאה שלהם במקרה של Estimator, או דגימות של פלט ה-Circuit במקרה של Sampler.
כל אלמנט ברשימה זו מתאים לכל PUB שהוגש לשיטת run() של הפרימיטיב (לדוגמה, משימה שהוגשה עם 20 PUBs תחזיר אובייקט PrimitiveResult המכיל רשימה של 20 PubResultים, אחד המתאים לכל PUB).
לכל אחד מאובייקטי PubResult יש גם מאפיין data וגם מאפיין metadata. מאפיין ה-data הוא DataBin מותאם אישי ת המכיל את ערכי המדידה בפועל, סטיות תקן, וכן הלאה. ל-DataBin זה יש מאפיינים שונים בהתאם לצורה או למבנה של ה-PUB המשויך וכן לאפשרויות צמצום השגיאות שצוינו על ידי הפרימיטיב ששימש להגשת המשימה (לדוגמה, ZNE או PEC). בינתיים, מאפיין ה-metadata מכיל מידע על אפשרויות זמן הריצה וצמצום השגיאות שנעשה בהן שימוש (מוסבר מאוחר יותר בסעיף מטאדאטה של תוצאות בדף זה).
להלן מתווה חזותי של מבנה הנתונים PrimitiveResult:
- Estimator Output
- Sampler Output
└── PrimitiveResult
├── PubResult[0]
│ ├── metadata
│ └── data ## In the form of a DataBin object
│ ├── evs
│ │ └── List of estimated expectation values in the shape
| | specified by the first pub
│ └── stds
│ └── List of calculated standard deviations in the
| same shape as above
├── PubResult[1]
| ├── metadata
| └── data ## In the form of a DataBin object
| ├── evs
| │ └── List of estimated expectation values in the shape
| | specified by the second pub
| └── stds
| └── List of calculated standard deviations in the
| same shape as above
├── ...
├── ...
└── ...
└── PrimitiveResult
├── PubResult[0]
│ ├── metadata
│ └── data ## In the form of a DataBin object
│ ├── NAME_OF_CLASSICAL_REGISTER
│ │ └── BitArray of count data (default is 'meas')
| |
│ └── NAME_OF_ANOTHER_CLASSICAL_REGISTER
│ └── BitArray of count data (exists only if more than one
| ClassicalRegister was specified in the circuit)
├── PubResult[1]
| ├── metadata
| └── data ## In the form of a DataBin object
| └── NAME_OF_CLASSICAL_REGISTER
| └── BitArray of count data for second pub
├── ...
├── ...
└── ...
במילים פשוטות, משימה יחידה מחזירה אובייקט PrimitiveResult ומכילה רשימה של אובייקט PubResult אחד או יותר. אובייקטי PubResult אלה שומרים את נתוני המדידה עבור כל PUB שהוגש למשימה.
לכל PubResult יש פורמטים ומאפיינים שונים בהתאם לסוג הפרימיטיב ששימש למשימה. הפרטים מוסברים להלן.
פלט Estimator
כל PubResult עבור פרימיטיב ה-Estimator מכיל לפחות מערך של ערכי ציפייה (PubResult.data.evs) וסטיות תקן משויכות (אחד מ-PubResult.data.stds או PubResult.data.ensemble_standard_error בהתאם ל-resilience_level שנעשה בו שימוש), אך עשוי להכיל נתונים נוספים בהתאם לאפשרויות צמצום השגיאות שצוינו.
קטע הקוד הבא מתאר את פורמט ה-PrimitiveResult (ו-PubResult המשויך) עבור המשימה שנוצרה לעיל.
print(
f"The result of the submitted job had {len(result)} PUB and has a value:\n {result}\n"
)
print(
f"The associated PubResult of this job has the following data bins:\n {result[0].data}\n"
)
print(f"And this DataBin has attributes: {result[0].data.keys()}")
print(
"Recall that this shape is due to our array of parameter binding sets having shape (100, 2) -- where 2 is the\n\
number of parameters in the circuit -- combined with our array of observables having shape (3, 1). \n"
)
print(
f"The expectation values measured from this PUB are: \n{result[0].data.evs}"
)
The result of the submitted job had 1 PUB and has a value:
PrimitiveResult([PubResult(data=DataBin(evs=np.ndarray(<shape=(3, 100), dtype=float64>), stds=np.ndarray(<shape=(3, 100), dtype=float64>), ensemble_standard_error=np.ndarray(<shape=(3, 100), dtype=float64>), shape=(3, 100)), metadata={'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32})], metadata={'dynamical_decoupling': {'enable': False, 'sequence_type': 'XX', 'extra_slack_distribution': 'middle', 'scheduling_method': 'alap'}, 'twirling': {'enable_gates': False, 'enable_measure': True, 'num_randomizations': 'auto', 'shots_per_randomization': 'auto', 'interleave_randomizations': True, 'strategy': 'active-accum'}, 'resilience': {'measure_mitigation': True, 'zne_mitigation': False, 'pec_mitigation': False}, 'version': 2})
The associated PubResult of this job has the following data bins:
DataBin(evs=np.ndarray(<shape=(3, 100), dtype=float64>), stds=np.ndarray(<shape=(3, 100), dtype=float64>), ensemble_standard_error=np.ndarray(<shape=(3, 100), dtype=float64>), shape=(3, 100))
And this DataBin has attributes: dict_keys(['evs', 'stds', 'ensemble_standard_error'])
Recall that this shape is due to our array of parameter binding sets having shape (100, 2) -- where 2 is the
number of parameters in the circuit -- combined with our array of observables having shape (3, 1).
The expectation values measured from this PUB are:
[[ 0.00948597 0.12163221 0.29100944 0.40535344 0.46625814 0.54716103
0.57690846 0.59809047 0.5784682 0.50924868 0.4579837 0.40035644
0.37174056 0.32887613 0.25850853 0.26396412 0.25852429 0.26074166
0.29282485 0.34388535 0.37368314 0.43562138 0.46912323 0.51955146
0.54430185 0.55467261 0.5162183 0.52744696 0.47261781 0.42613541
0.35400013 0.33217125 0.29600426 0.27561903 0.25307754 0.25672088
0.28783701 0.36612701 0.40433263 0.44428286 0.51028376 0.55034507
0.55979913 0.57160124 0.54127534 0.49753533 0.42942659 0.32552331
0.20215918 0.04303087 -0.08115732 -0.18473659 -0.34015892 -0.44489319
-0.49112115 -0.54588034 -0.60601287 -0.55869218 -0.53353861 -0.51628053
-0.44978534 -0.38090252 -0.32481576 -0.28832245 -0.27057547 -0.26542929
-0.27054473 -0.29367389 -0.31531828 -0.38462352 -0.40276794 -0.47168997
-0.48548191 -0.5382924 -0.52716406 -0.53277032 -0.50776933 -0.48512907
-0.44335198 -0.38756463 -0.34438156 -0.29199194 -0.2729216 -0.24602918
-0.23527174 -0.3019153 -0.35159518 -0.38303379 -0.42434541 -0.47743033
-0.54652609 -0.5877912 -0.59175701 -0.57386895 -0.56416812 -0.48022381
-0.3853372 -0.2639702 -0.12030502 0.02081148]
[ 0.00581765 0.0552677 0.15998546 0.20725389 0.25452232 0.34178711
0.39196437 0.47050268 0.50031815 0.527952 0.57231161 0.64066903
0.72429779 0.77011181 0.78174711 0.86610308 0.88646487 0.91337151
0.94245978 0.98100173 0.97372966 1.00936279 1.01881647 1.0544496
1.01954368 1.03699664 0.99845469 1.03845105 1.00936279 1.00354513
0.95409508 0.95264067 0.91264431 0.91846196 0.8355604 0.80283611
0.77956549 0.74102354 0.69520953 0.64575948 0.58976457 0.53231524
0.43996 0.3956004 0.32069812 0.27706572 0.22470684 0.16653032
0.07272066 -0.00218162 -0.05817653 -0.06253977 -0.15853104 -0.25015908
-0.28506499 -0.34251432 -0.44359604 -0.44432324 -0.53158804 -0.60285429
-0.637033 -0.67630215 -0.71266249 -0.76793019 -0.81519862 -0.86464867
-0.90173621 -0.93155168 -0.9337333 -0.98245614 -0.99627307 -1.01518044
-1.01590764 -1.04863194 -1.00499955 -1.02827016 -1.01663485 -1.0108172
-1.02317971 -0.97518407 -0.96500318 -0.94682302 -0.901009 -0.87846559
-0.79556404 -0.84937733 -0.78101991 -0.73811472 -0.65521316 -0.57667485
-0.59921825 -0.49813653 -0.44577766 -0.36505772 -0.33524225 -0.25888556
-0.21161713 -0.12289792 -0.03781474 0.00654486]
[ 0.01315429 0.18799671 0.42203343 0.603453 0.67799397 0.75253494
0.76185256 0.72567827 0.65661825 0.49054535 0.3436558 0.16004385
0.01918334 -0.11235955 -0.26473006 -0.33817484 -0.36941628 -0.39188819
-0.35681008 -0.29323102 -0.22636339 -0.13812003 -0.08057002 -0.01534667
0.06906002 0.07234859 0.03398191 0.01644286 -0.06412716 -0.15127432
-0.24609482 -0.28829816 -0.32063579 -0.3672239 -0.32940532 -0.28939435
-0.20389148 -0.00876953 0.11345574 0.24280625 0.43080296 0.5683749
0.67963826 0.74760208 0.76185256 0.71800493 0.63414634 0.48451631
0.3315977 0.08824335 -0.10413812 -0.30693341 -0.52178679 -0.6396273
-0.69717731 -0.74924637 -0.76842971 -0.67306111 -0.53548918 -0.42970677
-0.26253768 -0.08550288 0.06303097 0.19128528 0.27404768 0.33379008
0.36064675 0.34420389 0.30309674 0.2132091 0.19073719 0.07180049
0.04494382 -0.02795286 -0.04932858 -0.03727049 0.00109619 0.04055906
0.13647575 0.20005481 0.27624007 0.36283913 0.3551658 0.38640723
0.32502055 0.24554673 0.07782954 -0.02795286 -0.19347767 -0.3781858
-0.49383393 -0.67744588 -0.73773637 -0.78268019 -0.793094 -0.70156207
-0.55905728 -0.40504248 -0.20279529 0.0350781 ]]