本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
結果類型
Amazon使用ResultType
測量電路時,Braket 可以返回不同類型的結果。電路可以傳回以下類型的結果。
-
AdjointGradient
返回提供的可觀測值的期望值的梯度(向量導數)。該可觀察到的作用於相對於使用伴隨微分法指定參數的所提供目標上。您只能在拍攝 = 0 時使用此方法。 -
Amplitude
傳回輸出波函數中指定量子狀態的振幅。它僅適用於SV1和本機模擬器。 -
Expectation
返回給定的可觀察的,它可以與本章後面介紹的Observable
類來指定的期望值。必須指定qubits用於測量可觀察到的目標,並且指定目標的數量必須等於可觀察到qubits的作用的數量。如果沒有指定目標,可觀察到的必須僅在 1 操作,qubit並且它被應用到所有 qubits parallel。 -
Probability
返回測量計算基準狀態的概率。如果未指定目標,則Probability
傳回測量所有基準狀態的可能性。如果指定了目標,則僅返回指定qubits的基向量的邊際概率。 -
Reduced density matrix
從系統傳回指定目標子系統qubits的密度矩陣qubits。為了限制此結果類型的大小,Braket qubits 將目標數量限制為最多 8 個。 -
StateVector
返回完整的狀態向量。它可在本地模擬器上使用。 -
Sample
返回指定目標qubit集合和可觀察的測量計數。如果沒有指定目標,可觀察到的必須僅在 1 操作,qubit並且它被應用到所有 qubits parallel。如果指定的目標,指定的目標的數量必須等於其qubits上的可觀察到的行為的數量。 -
Variance
返回指定目標qubit集合的變異數(mean([x-mean(x)]2)
),並作為請求的結果類型觀察。如果沒有指定目標,可觀察到的必須僅在 1 操作,qubit並且它被應用到所有 qubits parallel。否則,指定的目標的數目必須等於其中可觀察qubits到的可以被應用的數量。
不同設備支持的結果類型:
本地 SIM 卡 |
SV1 |
DM1 |
TN1 |
Rigetti |
IonQ |
OQC |
|
伴隨漸變 |
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 |
注意
* 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)))
參數
電路可以包括自由參數,你可以在一個「構造一次-運行多次」的方式使用和計算梯度。Free 參數具有字串編碼的名稱,可用來指定其值或決定是否要相對於它們進行區分。
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