本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
结果类型列表
使用测量电路时,Amazon Braket 可以返回不同类型的结果。ResultType
电路可以返回以下类型的结果。
-
AdjointGradient
返回所提供的可观测值的期望值的梯度(向量导数)。该可观测值使用伴随微分法根据指定参数对提供的目标起作用。只有在 shots=0 时才能使用此方法。 -
Amplitude
返回输出波函数中指定量子态的振幅。它仅在SV1和本地模拟器上可用。 -
Expectation
返回给定可观察对象的期望值,该值可以通过本章后面介绍的Observable
类来指定。必须指定qubits用于测量可观测值的目标,并且指定目标的数量必须等于可观察对象所针对的qubits数量。如果未指定目标,则可观察对象必须仅在 1 上运行,qubit并行应用qubits于所有目标。 -
Probability
返回测量计算基态的概率。如果未指定目标,则Probability
返回测量所有基态的概率。如果指定了目标,则仅返回指定qubits基向量的边际概率。托管模拟器, QPUs 最大量子比特限制为 15 个,本地模拟器仅限于系统的内存大小。 -
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 |
IQM |
|
伴随渐变 |
N |
Y |
N |
N |
N |
N |
N |
Amplitude |
Y |
Y |
N |
N |
N |
N |
N |
期望 |
Y |
Y |
Y |
Y |
Y |
Y |
Y |
Probability |
Y |
Y |
Y |
N |
Y |
Y |
Y |
低密度矩阵 |
Y |
N |
Y |
N |
N |
N |
N |
状态向量 |
Y |
N |
N |
N |
N |
N |
N |
样本 |
Y |
Y |
Y |
Y |
Y |
Y |
Y |
方差 |
Y |
Y |
Y |
Y |
Y |
Y |
Y |
您可以通过检查设备属性来检查支持的结果类型,如以下示例所示。
from braket.aws import AwsDevice device = AwsDevice("arn:aws:braket:us-west-1::device/qpu/rigetti/Ankaa-3") # Print the result types supported by this device for iter in device.properties.action['braket.ir.openqasm.program'].supportedResultTypes: print(iter)
name='Sample' observables=['x', 'y', 'z', 'h', 'i'] minShots=10 maxShots=50000 name='Expectation' observables=['x', 'y', 'z', 'h', 'i'] minShots=10 maxShots=50000 name='Variance' observables=['x', 'y', 'z', 'h', 'i'] minShots=10 maxShots=50000 name='Probability' observables=None minShots=10 maxShots=50000
要调用 aResultType
,请将其附加到电路中,如以下示例所示。
from braket.circuits import Circuit, 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设备则提供概率。Amazon Braket SDK 为所有结果提供了一个测量属性。但是,对于返回概率的设备,这些测量值是事后计算的,并且基于概率,因为无法进行逐次测量。要确定结果是否经过后期计算,请检查结果对象measurements_copied_from_device
上的。此操作在 Amazon Braket 软件开发工具包 GitHub 存储库中的 gate_model_quantum_task_result.py
可观察对象
Amazon Braket 的Observable
类允许您测量特定的可观察对象。
您只能将一个可观察的唯一非身份应用于每个。qubit如果您将两个或多个不同的非身份可观察对象指定为相同对象,则会发生错误。qubit为此,张量乘积的每个因子都算作一个单独的可观察对象。这意味着你可以将多个张量乘积放在同一个上qubit,只要作用于这些乘积的因子qubit保持不变。
可以缩放观测值并添加其他可观察对象(无论是否缩放)。Sum
这将创建可在AdjointGradient
结果类型中使用的。
该Observable
类包括以下可观察对象。
import numpy as np Observable.I() Observable.H() Observable.X() Observable.Y() Observable.Z() # Get the eigenvalues of the observable print("Eigenvalue:", Observable.H().eigenvalues) # Or rotate the basis to be computational basis print("Basis rotation gates:", Observable.H().basis_rotation_gates) # Get the tensor product of the 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()) # 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, observables from braket.parametric import FreeParameter theta = FreeParameter("theta") phi = FreeParameter("phi") circ = Circuit().h(0).rx(0, phi).ry(0, phi).cnot(0, 1).xx(0, 1, theta)
伴随梯度
该SV1器件计算可观测期望值(包括多项哈密顿值)的伴随梯度。要区分参数,请指定其名称(字符串格式)或通过直接引用来指定。
from braket.aws import AwsDevice from braket.devices import Devices device = AwsDevice(Devices.Amazon.SV1) circ.adjoint_gradient(observable=3 * Observable.Z(0) @ Observable.Z(1) - 0.5 * observables.X(0), parameters = ["phi", theta])
将固定参数值作为参数传递给参数化电路将移除空闲参数。使用此电路运行AdjointGradient
会产生错误,因为空闲参数已不存在。以下代码示例演示了正确和不正确的用法:
# Will error, as no free parameters will be present #device.run(circ(0.2), shots=0) # Will succeed device.run(circ, shots=0, inputs={'phi': 0.2, 'theta': 0.2})