مدخلات ومخرجات Estimator
إصدارات الحزم
تم تطوير الكود في هذه الصفحة باستخدام المتطلبات التالية. نوصي باستخدام هذه الإصدارات أو أحدث.
qiskit[all]~=2.4.0
qiskit-ibm-runtime~=0.46.1
تُقدِّم هذه الصفحة نظرة عامة على مدخلات ومخرجات Qiskit Runtime Estimator primitive، الذي ينفِّذ أحمال العمل على موارد الحوسبة الخاصة بـ IBM Quantum®. يتيح لك Estimator تعريف أحمال عمل متجهة بكفاءة باستخدام هيكل بيانات يُسمى كتلة Primitive الموحدة (PUB). تُستخدَم كمدخلات لطريقة run() في Estimator primitive، التي تنفِّذ حمل العمل المُحدَّد كمهمة. ثم بعد اكتمال المهمة، تُعاد النتائج بتنسيق يعتمد على كل من PUBs المُستخدَمة وخيارات الـ runtime المُحدَّدة من الـ primitive.
المدخلات
كل PUB بهذا التنسيق:
(<circuit واحد>، <مراقب واحد أو أكثر>، <قيم معاملات واحدة أو أكثر اختيارية>، <دقة اختيارية>),
يمكن أن تكون parameter values الاختيارية قائمة أو معاملاً واحدًا. تُجمَع عناصر المراقبات وقيم المعاملات باتباع قواعد بث NumPy كما هو موضح في موضوع مدخلات ومخرجات الـ Primitive، ويُعاد تقدير قيمة توقع واحدة لكل عنصر من الشكل المُبثوث.
إذا كانت المدخلات تحتوي على قياسات، فإنها تُتجاهَل.
بالنسبة لـ Estimator primitive، يمكن أن يحتوي PUB على أربع قيم كحد أقصى:
QuantumCircuitواحد، والذي قد يحتوي على كائنParameterواحد أو أكثر- قائمة من مراقب واحد أو أكثر، تُحدِّد قيم التوقع المُراد تقديرها، مُرتَّبة في مصفوفة (مثلاً مراقب واحد مُمثَّل كمصفوفة 0-d، وقائمة مراقبات كمصفوفة 1-d، وما إلى ذلك). يمكن أن تكون البيانات بأي تنسيق من تنسيقات
ObservablesArrayLikeكـPauliأوSparsePauliOpأوPauliListأوstr.المراقبات المتبادِلة التبديل- المراقبات المتبادِلة التبديل في نفس PUB تُجمَّع معًا باستخدام هذه الطريقة.
- المراقبات المتبادِلة التبديل في PUBs مختلفة، حتى لو كانت لها نفس الدائرة، لا تُقدَّر باستخدام نفس القياس. كل PUB يُمثِّل أساسًا مختلفًا للقياس، وبالتالي تكون قياسات منفصلة مطلوبة لكل PUB.
- لضمان تقدير المراقبات المتبادِلة التبديل باستخدام نفس القياس، جمِّعها داخل نفس PUB.
- مجموعة من قيم المعاملات لربط الدائرة بها. يمكن تحديد هذا كائن واحد شبيه بالمصفوفة حيث الفهرس الأخير هو على كائنات
Parameterفي الدائرة أو حذفه (أو بشكل مكافئ، ضبطه علىNone) إذا لم يكن في الدائرة كائناتParameter. - (اختياريًا) دقة مستهدفة لقيم التوقع المُراد تقديرها
الكود التالي يُوضِّح مثالاً على مجموعة من المدخلات المتجهة لـ Estimator primitive وينفِّذها على 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 PUB ستُعيد كائن PrimitiveResult يحتوي على قائمة من 20 كائن PubResult، واحد لكل PUB).
يحتوي كل PubResult في Estimator primitive على الأقل على مصفوفة من قيم التوقع (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. ]]
كيفية حساب Estimator primitive للخطأ
إضافةً إلى تقدير متوسط المراقبات المُمرَّرة في PUBs المدخلة (حقل evs في DataBin)، يحاول Estimator أيضًا تقديم تقدير للخطأ المرتبط بقيم التوقع هذه. ستملأ جميع استعلامات Estimator حقل stds بكمية مثل الخطأ المعياري للمتوسط لكل قيمة توقع، لكن بعض خيارات تخفيف الأخطاء تُنتِج معلومات إضافية، مثل ensemble_standard_error.
نأخذ مراقبًا واحدًا . في غياب ZNE، يمكنك التفكير في كل تشغيل لتنفيذ Estimator كتوفير تقدير نقطي لقيمة التوقع . إذا كانت التقديرات النقطية في متجه Os، فإن القيمة المُعادة في ensemble_standard_error مكافئة لما يلي (حيث هو الانحراف المعياري لتقدير قيمة التوقع و هو عدد التشغيلات):
الذي يعامل جميع التشغيلات كجزء من مجموعة إحصائية واحدة. إذا طلبت تدوير البوابات (twirling.enable_gates = True)، يمكنك فرز التقديرات النقطية لـ في مجموعات تشترك في تدوير مشترك. اسمِ هذه المجموعات من التقديرات O_twirls، وهناك num_randomizations (عدد التدويرات) منها. ثم stds هو الخطأ المعياري للمتوسط من O_twirls، كما في
حيث هو الانحراف المعياري لـ O_twirls و هو عدد التدويرات. عندما لا تُفعِّل التدوير، فإن stds وensemble_standard_error متساويتان.
إذا فعَّلت ZNE، فإن stds الموصوفة أعلاه تصبح أوزانًا في انحدار غير خطي إلى نموذج استقراء. ما يُعاد أخيرًا في stds في هذه الحالة هو عدم يقين نموذج الملاءمة مُقيَّمًا عند عامل ضوضاء صفر. عندما تكون الملاءمة ضعيفة، أو عدم اليقين في الملاءمة كبيرًا، يمكن أن تصبح stds المُبلَّغ عنها كبيرة جدًا. عند تفعيل ZNE، يُملأ أيضًا pub_result.data.evs_noise_factors وpub_result.data.stds_noise_factors، بحيث يمكنك إجراء استقرائك الخاص.
البيانات الوصفية للنتيجة
إضافةً إلى نتائج التنفيذ، يحتوي كل من كائنَي PrimitiveResult وPubResult على خاصية البيانات الوصفية حول المهمة المُرسَلة. يمكن إيجاد البيانات الوصفية التي تحتوي على معلومات لجميع PUBs المُرسَلة (مثل مختلف خيارات الـ runtime المتاحة) في PrimitiveResult.metadata، بينما البيانات الوصفية الخاصة بكل 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,