

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

# 结果类型列表
<a name="braket-result-types"></a>

使用测量电路时，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 的数量。

 **不同提供程序支持的结果类型：**


|  |  |  |  |  |  |  |  |  | 
| --- |--- |--- |--- |--- |--- |--- |--- |--- |
|  |  本地 SIM  |   SV1   |   DM1   |   TN1   |   AQT   |   IonQ   |   IQM   |   Rigetti   | 
|  伴随梯度  |  N  |  Y  |  N  |  N  |  N  |  N  |  N  |  N  | 
|  振幅  |  Y  |  Y  |  N  |  N  |  N  |  N  |  N  |  N  | 
|  期望  |  Y  |  Y  |  Y  |  Y  |  Y  |  Y  |  Y  |  Y  | 
|  概率  |  Y  |  Y  |  Y  |  N  |  Y  |  Y  |  Y  |  Y  | 
|  低密度矩阵  |  Y  |  N  |  Y  |  N  |  N  |  N  |  N  |  N  | 
|  状态向量  |  Y  |  N  |  N  |  N  |  N  |  N  |  N  |  N  | 
|  样本  |  Y  |  Y  |  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
```

要调用 `ResultType`，请将其追加到电路中，如以下示例所示。

```
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\$1model\$1quantum\$1task\$1result.py](https://github.com/aws/amazon-braket-sdk-python/blob/ca5b08dada4839ca31c012ff50aa20b656fd1879/src/braket/tasks/gate_model_quantum_task_result.py#L70-L72) 文件中有详细介绍。

## 可观测值
<a name="braket-result-types-observables"></a>

Amazon Braket 的 `Observable` 类可测量特定的可观测值。

您只能将一个唯一非身份可观测值应用于每个 qubit。如果您将两个或多个不同的非身份可观测值指定为相同的 qubit，则会发生错误。为此，张量乘积的每个因子都算作一个单独的可观测值。这意味着您可以将多个张量乘积放在同一个 qubit 上，前提是作用于 qubit 的因子保持不变。

可以扩展可观测值并添加其他可观测值（无论是否扩展）。这将创建可在 `AdjointGradient` 结果类型中使用的 `Sum`。

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

## 参数
<a name="braket-result-types-parameters"></a>

电路可以包含自由参数。这些自由参数只需要构造一次即可运行多次，并且可用于计算梯度。

每个自由参数都使用字符串编码的名称，该名称用于：
+ 设置参数值
+ 确定要使用的参数

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

## 伴随梯度
<a name="braket-result-types-adjoint-gradient"></a>

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})
```