亞馬遜網站上的量子任務示例 - Amazon Braket

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

亞馬遜網站上的量子任務示例

本節將逐步介紹執行範例量子任務的各個階段,從選擇裝置到檢視結果。作為 Amazon Braket 的最佳實踐,我們建議您從模擬器上運行電路開始,例如 SV1.

指定裝置

首先,選擇並指定量子任務的設備。這個例子演示了如何選擇模擬器,SV1.

# choose the on-demand simulator to run the circuit from braket.aws import AwsDevice device = AwsDevice("arn:aws:braket:::device/quantum-simulator/amazon/sv1")

您可以檢視此裝置的某些屬性,如下所示:

print (device.name) for iter in device.properties.action['braket.ir.jaqcd.program']: print(iter)
SV1 ('version', ['1.0', '1.1']) ('actionType', <DeviceActionType.JAQCD: 'braket.ir.jaqcd.program'>) ('supportedOperations', ['ccnot', 'cnot', 'cphaseshift', 'cphaseshift00', 'cphaseshift01', 'cphaseshift10', 'cswap', 'cy', 'cz', 'h', 'i', 'iswap', 'pswap', 'phaseshift', 'rx', 'ry', 'rz', 's', 'si', 'swap', 't', 'ti', 'unitary', 'v', 'vi', 'x', 'xx', 'xy', 'y', 'yy', 'z', 'zz']) ('supportedResultTypes', [ResultType(name='Sample', observables=['x', 'y', 'z', 'h', 'i', 'hermitian'], minShots=1, maxShots=100000), ResultType(name='Expectation', observables=['x', 'y', 'z', 'h', 'i', 'hermitian'], minShots=0, maxShots=100000), ResultType(name='Variance', observables=['x', 'y', 'z', 'h', 'i', 'hermitian'], minShots=0, maxShots=100000), ResultType(name='Probability', observables=None, minShots=1, maxShots=100000), ResultType(name='Amplitude', observables=None, minShots=0, maxShots=0)])

提交量子任務示例

提交量子任務示例以在隨選模擬器上運行。

# create a circuit with a result type circ = Circuit().rx(0, 1).ry(1, 0.2).cnot(0,2).variance(observable=Observable.Z(), target=0) # add another result type circ.probability(target=[0, 2]) # set up S3 bucket (where results are stored) my_bucket = "amazon-braket-your-s3-bucket-name" # the name of the bucket my_prefix = "your-folder-name" # the name of the folder in the bucket s3_location = (my_bucket, my_prefix) # submit the quantum task to run my_task = device.run(circ, s3_location, shots=1000, poll_timeout_seconds = 100, poll_interval_seconds = 10) # the positional argument for the S3 bucket is optional if you want to specify a bucket other than the default # get results of the quantum task result = my_task.result()

device.run()命令通過創建量子任務CreateQuantumTaskAPI。在短暫的初始化時間之後,量子任務將排隊,直到存在以在設備上運行量子任務的容量。在這種情況下,設備是 SV1。 設備完成計算後,Amazon Braket 會將結果寫入呼叫中指定的 Amazon S3 位置。除了本地模擬器之外的所有設備都需要位置參數s3_location

注意

布拉克特量子任務動作的大小限制為 3MB。

提交參數化的工作

Amazon Braket 隨需和本機模擬器,QPUs也支援在任務提交時指定可用參數值。您可以使用inputs引數來執行此操作device.run(),如下列範例所示。inputs必須是字符串-float 對的字典,其中鍵是參數名稱。

參數編譯可以改善某些特定參數化電路的執行效能QPUs。將參數電路作為量子任務提交給支持的電路時QPU,Braket 將編譯電路一次,並緩存結果。對於相同電路的後續參數更新,不會對使用相同電路的任務進行重新編譯,因此執行時間更快。編譯電路時,Braket 會自動使用硬體供應商提供的更新校準資料,以確保獲得最高品質的結果。

注意

所有超導,基於門QPUs的來源都支持參數編譯 Rigetti Computing 除了脈衝水平程序。

from braket.circuits import Circuit, FreeParameter, Observable # create the free parameters alpha = FreeParameter('alpha') beta = FreeParameter('beta') # create a circuit with a result type circ = Circuit().rx(0, alpha).ry(1, alpha).cnot(0,2).xx(0, 2, beta) circ.variance(observable=Observable.Z(), target=0) # add another result type circ.probability(target=[0, 2]) # submit the quantum task to run my_task = device.run(circ, inputs={'alpha': 0.1, 'beta':0.2})

指定 shots

所以此 shots 參數是指期望測量的次數 shots。 模擬器,例如 SV1 支持兩種模擬模式。

  • 用於 shots = 0,模擬器會執行精確的模擬,傳回所有結果類型的 true 值。(不適用於 TN1.)

  • 對於非零值 shots,模擬器從輸出分佈中取樣以模擬 shot 真實的噪音QPUs。QPU裝置僅允許 shots > 0.

有關每個量子任務的最大射擊次數的信息,請參閱 Braket 配額

投票結果

執行時my_task.result(),會使用您在量子任務建立時定義的參數SDK開始輪詢結果:

  • poll_timeout_seconds是在隨選模擬器和/或設備上運行量子任務超時之前輪詢量子任務的秒QPU數。預設值為 432 萬秒,也就是 5 天。

  • 注意:對於QPU諸如 Rigetti 以及 IonQ,我們建議您允許幾天。如果輪詢逾時太短,則輪詢時間內可能不會傳回結果。例如,當 a 不可QPU用時,會傳回本機逾時錯誤。

  • poll_interval_seconds是量子任務進行輪詢的頻率。它指定你多久調用布拉基特 API 在按需模擬器和QPU設備上運行量子任務時獲取狀態。預設值為 1 秒。

這種非同步執行有助於與並非始終可用的QPU設備進行交互。例如,在定期維護時段期間,裝置可能無法使用。

傳回的結果包含與量子工作相關聯的一系列中繼資料。您可以使用下列指令來檢查量測結果:

print('Measurement results:\n',result.measurements) print('Counts for collapsed states:\n',result.measurement_counts) print('Probabilities for collapsed states:\n',result.measurement_probabilities)
Measurement results: [[1 0 1] [0 0 0] [1 0 1] ... [0 0 0] [0 0 0] [0 0 0]] Counts for collapsed states: Counter({'000': 761, '101': 226, '010': 10, '111': 3}) Probabilities for collapsed states: {'101': 0.226, '000': 0.761, '111': 0.003, '010': 0.01}

檢視範例結果

因為您也已指定ResultType,因此您可以檢視傳回的結果。結果類型會依其加入至電路的順序顯示。

print('Result types include:\n', result.result_types) print('Variance=',result.values[0]) print('Probability=',result.values[1]) # you can plot the result and do some analysis import matplotlib.pyplot as plt plt.bar(result.measurement_counts.keys(), result.measurement_counts.values()); plt.xlabel('bitstrings'); plt.ylabel('counts');
Result types include: [ResultTypeValue(type={'observable': ['z'], 'targets': [0], 'type': 'variance'}, value=0.7062359999999999), ResultTypeValue(type={'targets': [0, 2], 'type': 'probability'}, value=array([0.771, 0. , 0. , 0.229]))] Variance= 0.7062359999999999 Probability= [0.771 0. 0. 0.229]
條形圖顯示不同位串的計數,「000」的最高長條包含 700 個計數。