本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
結果類型清單
使用 測量電路時,Amazon Braket 可以傳回不同類型的結果ResultType
。電路可以傳回以下類型的結果。
-
AdjointGradient
傳回所提供可觀測之預期值的梯度 (向量衍生性)。此可觀測值正在與使用並行差異化方法的指定參數相關的所提供目標上運作。您只能在 shots=0 時使用此方法。 -
Amplitude
傳回輸出波函數中指定量子狀態的振幅。它僅適用於 SV1和本機模擬器。 -
Expectation
會傳回指定可觀測值的預期值,可使用本章稍後介紹的Observable
類別來指定。必須指定qubits用於測量可觀察的目標,且指定目標的數量必須等於可觀察行為qubits的 數量。如果未指定目標,則可觀測項目只能在 1 上操作,qubit並qubits平行套用至所有 。 -
Probability
傳回測量運算基礎狀態的機率。如果未指定目標, 會Probability
傳回測量所有基礎狀態的機率。如果指定了目標,qubits則只會傳回指定之基礎向量的邊際機率。受管模擬器和 QPUs 限制為最多 15 個 qubit,而本機模擬器限制為系統的記憶體大小。 -
Reduced density matrix
qubits會從 系統傳回指定目標之子系統的密度矩陣qubits。若要限制此結果類型的大小,Raket 會將目標的數量限制qubits為最多 8 個。 -
StateVector
會傳回完整狀態向量。它可在本機模擬器上使用。 -
Sample
會傳回指定目標qubit集和可觀察的測量計數。如果未指定目標,則可觀測項目只能在 1 上運作,qubit並qubits平行套用至所有 。如果指定了目標,則指定目標的數量必須等於可觀察動作qubits的 數量。 -
Variance
會傳回指定目標qubit集的變異數 (mean([x-mean(x)]2)
),並可觀察為請求的結果類型。如果未指定目標,則可觀測項目只能在 1 上運作,qubit並qubits平行套用至所有 。否則,指定的目標數量必須等於可qubits觀測項目可套用的 數量。
不同裝置支援的結果類型:
本機 sim |
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
若要呼叫 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 SDK 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 裝置會計算可觀測預期值的相鄰梯度,包括多期間 Hamiltonian。若要區分參數,請指定其名稱 (以字串格式) 或直接參考。
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})