结果类型 - Amazon Braket

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

结果类型

Amazon使用ResultType测量电路时,Braket 可以返回不同类型的结果。电路可以返回以下类型的结果。

  • AdjointGradient返回所提供的可观测值的期望值的梯度(向量导数)。该可观察对象使用伴随微分法根据指定参数对提供的目标起作用。只有在 shots=0 时才能使用此方法。

  • Amplitude返回输出波函数中指定量子态的振幅。它仅在SV1和本地模拟器上可用。

  • Expectation返回给定可观察对象的期望值,该值可以通过本章后面介绍的Observable类来指定。必须指定qubits用于测量可观测值的目标,并且指定目标的数量必须等于可观察对象所针对的qubits数量。如果未指定目标,则可观察对象必须仅在 1 上运行,qubit并行应用qubits于所有目标。

  • Probability返回测量计算基态的概率。如果未指定目标,则Probability返回测量所有基态的概率。如果指定了目标,则仅返回指定qubits基向量的边际概率。

  • Reduced density matrix返回系统中指定目标qubits子系统的密度矩阵。qubits为了限制此结果类型的大小,Braket 将目标数量限制qubits为最多 8。

  • StateVector返回完整的状态向量。它可在本地模拟器上使用。

  • Sample返回指定目标qubit集和可观察对象的测量计数。如果未指定目标,则可观察对象必须仅在 1 上运行,qubit并行应用qubits于所有目标。如果指定了目标,则指定目标的数量必须等于可观察对象作用的数量。qubits

  • Variance返回指定目标qubit集的方差 (mean([x-mean(x)]2)),并作为请求的结果类型进行观察。如果未指定目标,则可观察对象必须仅在 1 上运行,qubit并行应用qubits于所有目标。否则,指定的目标数量必须等于可以应用可观察对象的数量。qubits

不同设备支持的结果类型:

本地模拟卡

SV1

DM1

TN1

Rigetti

IonQ

OQC

伴随渐变

Y

Amplitude

Y

Y

期望

Y

Y

Y

Y

Y

Y

Y

Probability

Y

Y

Y

是*

Y

Y

低密度矩阵

Y

Y

状态向量

Y

样本

Y

Y

Y

Y

Y

Y

Y

方差

Y

Y

Y

Y

Y

Y

Y

注意

* Rigetti 仅支持最多 40 的概率结果类型qubits。

您可以通过检查设备属性来检查支持的结果类型,如以下示例所示。

device = AwsDevice("arn:aws:braket:us-west-1::device/qpu/rigetti/Aspen-M-3") # print the result types supported by this device for iter in device.properties.action['braket.ir.jaqcd.program'].supportedResultTypes: print(iter)
name='Sample' observables=['x', 'y', 'z', 'h', 'i'] minShots=10 maxShots=100000 name='Expectation' observables=['x', 'y', 'z', 'h', 'i'] minShots=10 maxShots=100000 name='Variance' observables=['x', 'y', 'z', 'h', 'i'] minShots=10 maxShots=100000 name='Probability' observables=None minShots=10 maxShots=100000

要调用 aResultType,请将其附加到电路中,如以下示例所示。

from braket.circuits import Observable circ = Circuit().h(0).cnot(0, 1).amplitude(state=["01", "10"]) circ.probability(target=[0, 1]) circ.probability(target=0) circ.expectation(observable=Observable.Z(), target=0) circ.sample(observable=Observable.X(), target=0) circ.state_vector() circ.variance(observable=Observable.Z(), target=0) # print one of the result types assigned to the circuit print(circ.result_types[0])
注意

例如Rigetti,有些设备将测量结果作为结果提供,而另一些设备则提供概率作为结果(例如IonQ和OQC)。SDK 在结果上提供了测量属性,但对于返回概率的设备,该属性是事后计算的。因此,诸如由提供的设备IonQ和OQC其测量结果由概率决定的设备,因为不会返回每次拍摄的测量结果。您可以通过查看结果对象measurements_copied_from_device上的(如本文件所示)来检查结果是否经过后期计算。

可观察对象

AmazonBraket 包括一个Observable类,该类可用于指定要测量的观测值。

您最多可以对每个应用一个可观察到的唯一非身份。qubit如果您将两个或多个不同的非身份可观察对象指定为相同的对象qubit,则会看到错误。为此,张量乘积的每个因子都算作一个单独的可观测值,因此允许有多个张量乘积作用于同一个因子qubit,前提是作用于该因子的因子相同。qubit

您还可以缩放可观测值并添加可观察对象(无论是否缩放)。Sum这将创建可在AdjointGradient结果类型中使用的。

Observable类包括以下可观察对象。

Observable.I() Observable.H() Observable.X() Observable.Y() Observable.Z() # get the eigenvalues of the observable print("Eigenvalue:", Observable.H().eigenvalues) # or whether to rotate the basis to be computational basis print("Basis rotation gates:",Observable.H().basis_rotation_gates) # get the tensor product of observable for the multi-qubit case tensor_product = Observable.Y() @ Observable.Z() # view the matrix form of an observable by using print("The matrix form of the observable:\n",Observable.Z().to_matrix()) print("The matrix form of the tensor product:\n",tensor_product.to_matrix()) # also factorize an observable in the tensor form print("Factorize an observable:",tensor_product.factors) # self-define observables given it is a Hermitian print("Self-defined Hermitian:",Observable.Hermitian(matrix=np.array([[0, 1],[1, 0]]))) print("Sum of other (scaled) observables:", 2.0 * Observable.X() @ Observable.X() + 4.0 * Observable.Z() @ Observable.Z())
Eigenvalue: [ 1 -1] Basis rotation gates: (Ry('angle': -0.7853981633974483, 'qubit_count': 1),) The matrix form of the observable: [[ 1.+0.j 0.+0.j] [ 0.+0.j -1.+0.j]] The matrix form of the tensor product: [[ 0.+0.j 0.+0.j 0.-1.j 0.-0.j] [ 0.+0.j -0.+0.j 0.-0.j 0.+1.j] [ 0.+1.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j -0.-1.j 0.+0.j -0.+0.j]] Factorize an observable: (Y('qubit_count': 1), Z('qubit_count': 1)) Self-defined Hermitian: Hermitian('qubit_count': 1, 'matrix': [[0.+0.j 1.+0.j], [1.+0.j 0.+0.j]]) Sum of other (scaled) observables: Sum(TensorProduct(X('qubit_count': 1), X('qubit_count': 1)), TensorProduct(Z('qubit_count': 1), Z('qubit_count': 1)))

参数

电路可能包含自由参数,您可以以 “构造一次——运行多次” 的方式使用这些参数并计算梯度。自由参数具有字符串编码的名称,您可以使用该名称来指定其值或确定是否对其进行区分。

from braket.circuits import Circuit, FreeParameter, Observable theta = FreeParameter("theta") phi = FreeParameter("phi") circ = Circuit().h(0).rx(0, phi).ry(0, phi).cnot(0, 1).xx(0, 1, theta) circ.adjoint_gradient(observable=Observable.Z() @ Observable.Z(), target=[0, 1], parameters = ["phi", theta]

对于要区分的参数,请使用其名称(作为字符串)或通过直接引用来指定它们。请注意,使用AdjointGradient结果类型计算梯度是相对于可观测值的期望值完成的。

注意:如果您通过将自由参数的值作为参数传递给参数化电路来固定这些值,则运行指定了结果类型和参数的电路将产生错误。AdjointGradient这是因为我们用来区分的参数已不复存在。请参阅以下示例。

device.run(circ(0.2), shots=0) # will error, as no free parameters will be present device.run(circ, shots=0, inputs={'phi'=0.2, 'theta'=0.2) # will succeed