קלטים ופלטים של Estimator
גרסאות חבילות
הקוד בדף זה פותח תוך שימוש בדרישות הבאות. אנחנו ממליצים להשתמש בגרסאות אלה או חדשות יותר.
qiskit[all]~=2.4.0
qiskit-ibm-runtime~=0.46.1
דף זה נותן סקירה של הקלטים והפלטים של ה-primitive של Qiskit Runtime Estimator, שמריץ עומסי עבודה על משאבי מחשוב IBM Quantum®. Estimator מאפשר לך להגדיר עומסי עבודה מוּוקטרים בצורה יעילה באמצעות מבנה נתונים הנקרא Primitive Unified Bloc (PUB). הם משמשים כקלטים למתודת run() עבור ה-primitive של Estimator, שמריצה את עומס העבודה המוגדר כעבודה. לאחר השלמת העבודה, התוצאות מוחזרות בפורמט התלוי גם ב-PUBs שנעשה בהם שימוש וגם באפשרויות ה-runtime שצוינו מה-primitive.
קלטים
כל PUB נמצא בפורמט זה:
(<מעגל בודד>, <observable אחד או יותר>, <ערכי פרמטרים אופציונליים>, <דיוק אופציונלי>),
ה-parameter values האופציונליים יכולים להיות רשימה או פרמטר בודד. אלמנטים מה-observables ומערכי הפרמטרים משולבים בעקבות כללי שידור NumPy כמתואר בנושא קלטים ופלטים של Primitive, ואומדן ערך ציפייה אחד מוחזר לכל אלמנט בצורה המשודרת.
אם הקלט מכיל מדידות, הן מתעלמות.
עבור ה-primitive של Estimator, PUB יכול להכיל לכל היותר ארבעה ערכים:
QuantumCircuitבודד, שעשוי להכיל אובייקטParameterאחד או יותר- רשימה של observable אחד או יותר, שמציינים את ערכי הציפייה לאמוד, מסודרים במערך (לדוגמה, observable בודד המיוצג כמערך 0-d, רשימת observables כמערך 1-d, וכן הלאה). הנתונים יכולים להיות בכל פורמט
ObservablesArrayLikeכמוPauli,SparsePauliOp,PauliListאוstr.Observables מתחלפים- Observables מתחלפים באותו PUB מקובצים יחד באמצעות שיטה זו.
- Observables מתחלפים ב-PUBs שונים, גם אם יש להם אותו מעגל, אינם מוערכים באמצעות אותה מדידה. כל PUB מייצג בסיס שונה למדידה, ולכן נדרשות מדידות נפרדות לכל PUB.
- כדי להבטיח ש-observables מתחלפים מוערכים באמצעות אותה מדידה, קבץ אותם בתוך אותו PUB.
- אוסף ערכי פרמטרים לאגד עם המעגל. זה יכול להיות מוגדר כאובייקט בודד דמוי-מערך שבו המדד האחרון הוא מעל אובייקטי
Parameterשל המעגל, או שמוּשמט (או שווה ערך, מוגדר ל-None) אם למעגל אין אובייקטיParameter. - (אופציונלי) דיוק יעד לאמידת ערכי ציפייה
הקוד הבא מדגים סט לדוגמה של קלטים מוּוקטרים ל-primitive של 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,
)
from qiskit.transpiler import generate_preset_pass_manager
from qiskit.quantum_info import SparsePauliOp
from qiskit_ibm_runtime import (
QiskitRuntimeService,
EstimatorV2 as Estimator,
)
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()
פלטים
לאחר שנשלח PUB אחד או יותר ל-QPU להרצה ועבודה מושלמת בהצלחה, הנתונים מוחזרים כאובייקט מכיל PrimitiveResult שאליו ניגשים על ידי קריאה למתודת RuntimeJobV2.result().
ה-PrimitiveResult מכיל רשימה איטרבילית של אובייקטי PubResult שמכילים את תוצאות ההרצה לכל PUB.
כל אלמנט ברשימה זו מתאים לכל PUB שנשלח למתודת run() של ה-primitive (לדוגמה, עבודה שנשלחת עם 20 PUBs תחזיר אובייקט PrimitiveResult שמכיל רשימה של 20 אובייקטי PubResult, אחד המתאים לכל PUB).
כל PubResult עבור ה-primitive של Estimator מכיל לפחות מערך ערכי ציפייה (PubResult.data.evs) וסטיות תקן משויכות (כ-PubResult.data.stds או PubResult.data.ensemble_standard_error בהתאם ל-resilience_level שנעשה בו שימוש), אך יכול להכיל נתונים נוספים בהתאם לאפשרויות הפחתת השגיאות שצוינו.
כל אובייקט PubResult מחזיק גם מאפיין data וגם מאפיין metadata.
- מאפיין ה-
dataהואDataBinמותאם אישית שמכיל את ערכי המדידה בפועל, סטיות תקן, וכן הלאה. - ל-
DataBinיש מאפיינים שונים בהתאם לצורה או מבנה ה-PUB המשויך וכן לאפשרויות הפחתת השגיאות שצוינו על ידי ה-primitive שנעשה בו שימוש לשליחת העבודה (לדוגמה, ZNE או PEC). - מאפיין ה-
metadataמכיל מידע על ה-runtime ואפשרויות הפחתת השגיאות שנעשה בהם שימוש (מוסבר בהמשך בחלק מטא-נתוני תוצאה בדף זה).
להלן מתאר חזותי של מבנה הנתונים PrimitiveResult עבור פלט Estimator:
└── 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 אחד או יותר. אובייקטי ה-PubResult הללו אחסנים את נתוני המדידה לכל PUB שנשלח לעבודה.
קטע הקוד הבא מתאר את הפורמט של PrimitiveResult (ושל PubResult המשויך) עבור העבודה שנוצרה לעיל.
print(
f"The result of the submitted job had {len(result)} "
f"PUBs and has a value:\n {result}\n"
)
print(
"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 number of parameters in the "
"circuit, combined with our array of observables having shape (3, 1). \n"
)
with np.printoptions(threshold=200):
print(
"The expectation values measured from this PUB are: \n"
"{result[0].data.evs}\n"
)
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.00369065 0.15107692 0.30110431 ... -0.30159536 -0.15431523
0.00576586]
[ 0.00601655 0.04412133 0.1253447 ... -0.12434194 -0.04662823
0.01153171]
[-0.01339784 0.2580325 0.47686391 ... -0.47884878 -0.26200223
0. ]]
כיצד ה-primitive של Estimator מחשב שגיאה
בנוסף לאומדן הממוצע של ה-observables שהועברו ב-PUBs הקלטיים (שדה ה-evs של ה-DataBin), Estimator גם מנסה לספק אומדן של השגיאה המשויכת לערכי הציפייה הללו. כל שאילתות Estimator יאכלסו את שדה ה-stds בכמות דמוית שגיאת התקן של הממוצע לכל ערך ציפייה, אך חלק מאפשרויות הפחתת השגיאות מייצרות מידע נוסף, כמו ensemble_standard_error.
שקול observable בודד . בהיעדר ZNE, אפשר לחשוב על כל shot של ביצוע Estimator כמספק אומדן נקודתי של ערך הציפייה . אם האומדנים הנקודתיים נמצאים בוקטור Os, אז הערך שמוחזר ב-ensemble_standard_error שקול לאות הבא (שבו הוא סטיית התקן של אומדן ערך הציפייה ו- הוא מספר ה-shots):
שמתייחס לכל ה-shots כחלק מ-ensemble בודד. אם ביקשת twirling שערים (twirling.enable_gates = True), אפשר למיין את האומדנים הנקודתיים של לסדרות שחולקות twirl משותף. קרא לסדרות האומדנים הללו O_twirls, ויש num_randomizations (מספר twirls) מהן. אז stds הוא שגיאת התקן של הממוצע של O_twirls, כ:
כאשר היא סטיית התקן של O_twirls ו- הוא מספר ה-twirls. כאשר אינך מאפשר twirling, stds ו-ensemble_standard_error שווים.
אם מאפשרים ZNE, ה-stds המתוארים לעיל הופכים למשקלות ברגרסיה לא-לינארית לדגם extrapolator. מה שמוחזר לבסוף ב-stds במקרה זה הוא אי-הוודאות של מודל ה-fit שמוערך בגורם רעש אפס. כאשר יש fit גרוע, או אי-ודאות גדולה ב-fit, ה-stds המדווח יכול להיות גדול מאוד. כאשר ZNE מופעל, מאוכלסים גם pub_result.data.evs_noise_factors וגם pub_result.data.stds_noise_factors, כך שניתן לבצע extrapolation משלך.
מטא-נתוני תוצאה
בנוסף לתוצאות ההרצה, גם אובייקטי PrimitiveResult וגם PubResult מכילים מאפיין מטא-נתונים על העבודה שנשלחה. המטא-נתונים המכילים מידע לכל ה-PUBs שנשלחו (כמו אפשרויות ה-runtime השונות הזמינות) נמצאים ב-PrimitiveResult.metatada, בעוד שהמטא-נתונים הספציפיים לכל PUB נמצאים ב-PubResult.metadata.
בשדה המטא-נתונים, מימושי ה-primitive יכולים להחזיר כל מידע על הרצה שרלוונטי להם, ואין זוגות מפתח-ערך שמובטחים על ידי ה-primitive הבסיסי. לפיכך, המטא-נתונים המוחזרים עשויים להיות שונים במימושי primitive שונים.
# Print out the results metadata
print("The metadata of the PrimitiveResult is:")
for key, val in result.metadata.items():
print(f"'{key}' : {val},")
print("\nThe metadata of the PubResult result is:")
for key, val in result[0].metadata.items():
print(f"'{key}' : {val},")
The metadata of the PrimitiveResult is:
'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 metadata of the PubResult result is:
'shots' : 4096,
'target_precision' : 0.015625,
'circuit_metadata' : {},
'resilience' : {},
'num_randomizations' : 32,