Skip to main content
IBM Quantum Platform

見積もりツールの入力と出力

  • このページのコードは、以下の要件に基づいて開発されました。 これらのバージョン以降のご利用をお勧めします。

    qiskit[all]~=2.5.1
    qiskit-ibm-runtime~=0.47.0
    

このページでは、 IBM Quantum® のコンピューティングリソース上でワークロードを実行する「 IBM Quantum Estimator」プリミティブの入力と出力の概要について説明します。 Estimator を使用すると、 Primitive Unified Bloc( PUB ) と呼ばれるデータ構造を活用して、ベクトル化されたワークロードを効率的に定義することができます。 これらは、Estimatorプリミティブのメソッド run() への入力として使用され、そのメソッドは定義されたワークロードをジョブとして実行します。 その後、ジョブが完了すると、結果は、使用されたPUBと、プリミティブから指定された実行時オプションの両方に依存する形式で返されます。


入力

各 PUB は、以下の形式になっています:

(<single circuit>, <one or more observables>, <optional one or more parameter values>, <optional precision>),

オプションは、リストまたは parameter values 単一のパラメータにすることができます。 オブザーバブルの要素とパラメータ値は、「 プリミティブ入力と出力 」のトピックで説明されている NumPy のブロードキャスト規則に従って結合され、ブロードキャストされた形状の各要素に対して1つの期待値の推定値が返されます。

入力に測定値が含まれている場合、それらは無視されます。

Estimatorプリミティブの場合、 PUB には最大4つの値を含めることができます:

  • 1つのオブジェクト、または複数の ParameterQuantumCircuit オブジェクトを含むシングル
  • 推定する期待値を指定する1つ以上の観測変数のリストで、配列として構成されたもの(例えば、単一の観測変数は0次元配列として、観測変数のリストは1次元配列として表されるなど)。 データは、 SparsePauliOp``PauliPauliList、、などのいずれかの ObservablesArrayLike 形式 strで指定できます。
    通勤時の観測値
    • このメソッドを使用すると、 同じ PUB 内のコミューティング観測量がグループ化されます。
    • 異なるPUBにある通勤観測量は、たとえ同じ回路に属していても、同じ測定値を用いて推定されることはありません。 各 PUB は異なる測定基準を表しているため、各 PUB に対して個別の測定を行う必要があります。
    • 通勤に関する観測値が同じ測定値を用いて推定されるようにするため、それらを同じ PUB 内にグループ化してください。
  • 回路をバインドするためのパラメータ値の集合。 これは、単一の配列のようなオブジェクトとして指定できます。この場合、最後のインデックスは回路 Parameter オブジェクトの数を上回る値とするか、回路にオブジェクト Parameter が存在しない場合は省略(あるいは、に設定)します None
  • (任意)推定する期待値の目標精度

以下のコードは、 Estimator プリミティブへのベクトル化された入力の例を示しており、 IBM®RuntimeJobV2 バックエンド上で単一のオブジェクトとして実行します。

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()

出力

1つ以上のPUBがQPUに送信されて実行され、ジョブが正常に完了すると、データはコンテナオブジェクト PrimitiveResult として返され、このオブジェクトには RuntimeJobV2.result() メソッドを呼び出すことでアクセスできます。

には、各 PrimitiveResultPUB の実行結果を含む PubResult オブジェクトの反復可能なリストが含まれています。

このリストの各要素は、プリミティブの run() メソッドに送信された各 PUB に対応しています(たとえば、20個のPUBで送信されたジョブは、20個の PUB に対応する20 PubResult 個のオブジェクトのリストを含むオブジェクト PrimitiveResult を返します)。

各Estimator PubResult プリミティブには、少なくとも期待値の配列 (PubResult.data.evs) と、それに対応する標準偏差(使用 resilience_level される に応じて PubResult.data.ensemble_standard_error または PubResult.data.stds のいずれか)が含まれますが、指定された誤差低減オプションによっては、さらに多くのデータが含まれる場合があります。

各オブジェクト PubResult は、 data とという2 metadata つの属性を持っています。

  • この data 属性は、実際の測定値や標準偏差などを含む、 DataBin カスタマイズされたデータセットです。
  • このオブジェクトは、関連する PUB の形状や構造、およびジョブの送信に使用された DataBin プリミティブによって指定されたエラー軽減オプション( Z NE やPECなど)に応じて、さまざまな属性を持つ。
  • この metadata 属性には、使用された実行時およびエラー軽減オプションに関する情報が含まれています(詳細は、このページの「 結果メタデータ 」セクションで後述します)。

以下は、Estimatorの出力に関するデータ PrimitiveResult 構造の概要図です:

└── 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
    ├── ...
    ├── ...
    └── ...

簡単に言えば、1つのジョブは オブジェクトを PrimitiveResult 返し、1つ以上の PubResult オブジェクトのリストを含んでいます。 これらの PubResult オブジェクトは、そのジョブに送信された各 PUB の測定データを保存します。

以下のコードスニペットは、上記で作成したジョブの(および関連する PubResultPrimitiveResult フォーマットを示しています。

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"
    )

Output:

The result of the submitted job had 1 PUBs 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:
 {result[0].data}

And this DataBin has attributes: dict_keys(['evs', 'stds', 'ensemble_standard_error'])
Recall that this shape is due to our array of parameter binding setshaving 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: 
{result[0].data.evs}

Estimatorプリミティブによる誤差の計算方法

Estimatorは、入力PUB(のフィールド evs``DataBin )に渡された観測値の平均値の推定に加え、それらの期待値に関連する誤差の推定値も提供しようと試みます。 Estimatorへのすべてのクエリでは、各期待値について平均の標準誤差のような値が stds フィールドに設定されますが、一部の誤差低減オプションでは、などの追加情報が生成 ensemble_standard_errorされます。

単一の観測量 O\mathcal{O} について考える。 ZNE が適用されない場合、推定器の実行における各ショットは、期待値 O\langle \mathcal{O} \rangle の点推定値を提供するものと考えられる。点ごとの推定値がベクトルである場合、で Os返される ensemble_standard_error 値は、以下に等しい(ここで、 σO\sigma_{\mathcal{O}}期待値の推定値の標準偏差、 NshotsN_{shots} はショットの数である):

σONshots,\frac{ \sigma_{\mathcal{O}} }{ \sqrt{N_{shots}} },

すべてのショットを単一のアンサンブルの一部として扱う。 ート・トゥワリング(twirling.enable_gates = True)を指定した場合、 O\langle \mathcal{O} \rangle の点ごとの推定値を、共通のトゥワリングを持つグループに分類することができます。 これらの推定値の集合を O_twirlsと呼び、その数は (ツイ num_randomizations ールの数)個である。 ここで、 O_twirlsはの平均の標準誤差 stds であり、次のように表される

σONtwirls,\frac{ \sigma_{\mathcal{O}} }{ \sqrt{N_{twirls}} },

ここで、 σO\sigma_{\mathcal{O}} はの標準偏差であり O_twirlsNtwirlsN_{twirls} はツイールの数である。 「トゥワリング」を有効にしていない場合、 stdsensemble_standard_error は等しくなります。

ZNEを有効にすると、前述の stds 要素は、外挿モデルに対する非線形回帰における重みとなります。 この stds 場合、最終的に返されるのは、ノイズ係数がゼロの時点で評価された適合モデルの不確実性である。 適合度が低い場合や、適合に大きな不確実性がある場合、報告される値は非常に stds 大きくなることがあります。 ZNEを有効にすると、 pub_result.data.evs_noise_factorspub_result.data.stds_noise_factors にも値が入力されるため、独自の外挿を行うことができます。


結果のメタデータ

実行結果に加え、および PubResult``PrimitiveResult オブジェクトには、送信されたジョブに関するメタデータ属性が含まれています。 提出されたすべてのPUBに関する情報(利用可能な各種実行時オプションなど)を含むメタデータは にあり PrimitiveResult.metatada、各 PUB 固有のメタデータは にあります PubResult.metadata

Note

メタデータフィールドでは、プリミティブの実装は、自身に関連する実行に関するあらゆる情報を返すことができ、ベースプリミティブによって保証されるキーと値のペアは存在しません。 したがって、返されるメタデータは、プリミティブの実装によって異なる場合があります。

# 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},")

Output:

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,
このページは役に立ちましたか?
バグや誤字の報告、またはコンテンツの要求はGitHubで行ってください。