從 Amazon Braket 開發套件追蹤量子任務 - Amazon Braket

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

從 Amazon Braket 開發套件追蹤量子任務

該命令device.run(…​)定義具有唯一量子任務 ID 的量子任務。您可以查詢和跟踪狀態,如下面的例子。task.state()

注意task = device.run()是一個異步操作,這意味著您可以在系統在後台處理量子任務時繼續工作。

擷取結果

當您呼叫時task.result(),SDK 會開始輪詢 Amazon Braket 以查看量子任務是否已完成。SDK 會使用您在中定義的輪詢參數.run()。量子任務完成後,SDK 會從 S3 儲存貯體擷取結果,並將其作為QuantumTaskResult物件傳回。

# create a circuit, specify the device and run the circuit circ = Circuit().rx(0, 0.15).ry(1, 0.2).cnot(0,2) device = AwsDevice("arn:aws:braket:::device/quantum-simulator/amazon/sv1") task = device.run(circ, s3_location, shots=1000) # get ID and status of submitted task task_id = task.id status = task.state() print('ID of task:', task_id) print('Status of task:', status) # wait for job to complete while status != 'COMPLETED': status = task.state() print('Status:', status)
ID of task: arn:aws:braket:us-west-2:123412341234:quantum-task/b68ae94b-1547-4d1d-aa92-1500b82c300d Status of task: QUEUED Status: QUEUED Status: QUEUED Status: QUEUED Status: QUEUED Status: QUEUED Status: QUEUED Status: QUEUED Status: RUNNING Status: RUNNING Status: COMPLETED

取消量子任務

若要取消量子工作,請呼叫cancel()方法,如下列範例所示。

# cancel quantum task task.cancel() status = task.state() print('Status of task:', status)
Status of task: CANCELLING

檢查元數據

您可以檢查已完成量子工作的中繼資料,如下列範例所示。

# get the metadata of the quantum task metadata = task.metadata() # example of metadata shots = metadata['shots'] date = metadata['ResponseMetadata']['HTTPHeaders']['date'] # print example metadata print("{} shots taken on {}.".format(shots, date)) # print name of the s3 bucket where the result is saved results_bucket = metadata['outputS3Bucket'] print('Bucket where results are stored:', results_bucket) # print the s3 object key (folder name) results_object_key = metadata['outputS3Directory'] print('S3 object key:', results_object_key) # the entire look-up string of the saved result data look_up = 's3://'+results_bucket+'/'+results_object_key print('S3 URI:', look_up)
1000 shots taken on Wed, 05 Aug 2020 14:44:22 GMT. Bucket where results are stored: amazon-braket-123412341234 S3 object key: simulation-output/b68ae94b-1547-4d1d-aa92-1500b82c300d S3 URI: s3://amazon-braket-123412341234/simulation-output/b68ae94b-1547-4d1d-aa92-1500b82c300d

擷取量子任務或結果

如果您的核心在您提交量子任務後死亡,或者關閉筆記本電腦或計算機,則可以使用其唯一的 ARN(量子任務 ID)重建該task對象。然後,您可task.result()以調用從存儲它的 S3 存儲桶中獲取結果。

from braket.aws import AwsSession, AwsQuantumTask # restore task with unique arn task_load = AwsQuantumTask(arn=task_id) # retrieve the result of the task result = task_load.result()