結果類型 - Amazon Braket

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

結果類型

Amazon使用測量電路時,Braket 可以返回不同類型的結果ResultType。電路可以傳回以下類型的結果。

  • AdjointGradient返回提供的可觀測值的期望值的梯度(向量導數)。該可觀察到的作用於相對於使用伴隨微分法指定參數的提供目標上。您只能在拍攝 = 0 時使用此方法。

  • Amplitude傳回輸出波函數中指定量子狀態的振幅。它是可用的SV1和僅限本地模擬器。

  • Expectation返回一個給定的可觀察的期望值,它可以用指定Observable本章後面介紹的類。目標qubits用於測量必須指定可觀察到的,並且指定目標的數量必須等於qubits在其上可觀察到的行為。如果沒有指定目標,則可觀察到的必須僅在 1 上操作qubit它適用於所有qubitsparallel

  • Probability返回測量計算基準狀態的概率。如果沒有指定目標,Probability傳回測量所有基準狀態的概率。如果指定了目標,則僅指定的基向量的邊際概率qubits返回。

  • Reduced density matrix返回指定目標的子系統的密度矩陣qubits從一個系統qubits。為了限制此結果類型的大小,Braket 會限制目標的數量qubits最多 8 個。

  • StateVector返回完整的狀態向量。它可在本地模擬器上使用。

  • Sample傳回指定目標的測量計數qubit設置和可觀察的。如果沒有指定目標,則可觀察到的必須僅在 1 上操作qubit它適用於所有qubitsparallel 如果指定了目標,指定目標的數目必須等於qubits在其上可觀察到的行為。

  • Variance返回方差(mean([x-mean(x)]2)) 的指定目標qubit設置和可觀察到的請求的結果類型。如果沒有指定目標,則可觀察到的必須僅在 1 上操作qubit它適用於所有qubitsparallel 否則,指定的目標數量必須等於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

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

若要呼叫ResultType,請將其附加到電路,如下列範例所示。

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在結果對像上,如圖所示文件

可观测量

Amazon胸針包括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