本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
打開 Amazon Braket 博托 3 客戶端
要將 Boto3 與 Amazon Braket 一起使用,您必須導入 Boto3,然後定義一個用於連接到 Braket 的客戶端。Amazon API在下列範例中,會命名為 Boto3 用戶端。braket
注意
為了與舊版的向後相容性 BraketSchemas,OpenQASM 資訊會在呼叫中GetDevice
API省略。若要取得此資訊,使用者代理程式必須呈現最新版本的 BraketSchemas (1.8.0 或更新版本)。Braket SDK 會自動為您報告此問題。如果您在使用 Braket SDK 時沒有看到 OpenQASM 產生GetDevice
回應的結果,您可能需要設定AWS_EXECUTION_ENV
環境變數來設定使用者代理程式。請參閱GetDevice 不傳回 OpenQASM 結果錯誤主題中提供的程式碼範例,瞭解如何針對 AWS CLI、Boto3 和 Go、Java 和/SDK 執行此動作。 JavaScript TypeScript
import boto3 import botocore braket = boto3.client("braket", config=botocore.client.Config(user_agent_extra="BraketSchemas/1.8.0"))
現在您已經建立了一個braket
客戶,您可以從 Amazon Braket 服務發出請求並處理響應。您可以在 API 參考
搜尋裝置
-
search_devices(**kwargs)
使用指定的篩選器搜尋裝置。
# Pass search filters and optional parameters when sending the # request and capture the response response = braket.search_devices(filters=[{ 'name': 'deviceArn', 'values': ['arn:aws:braket:::device/quantum-simulator/amazon/sv1'] }], maxResults=10) print(f"Found {len(response['devices'])} devices") for i in range(len(response['devices'])): device = response['devices'][i] print(device['deviceArn'])
擷取裝置
-
get_device(deviceArn)
檢索Amazon布拉克特中可用的設備。
# Pass the device ARN when sending the request and capture the repsonse response = braket.get_device(deviceArn='arn:aws:braket:::device/quantum-simulator/amazon/sv1') print(f"Device {response['deviceName']} is {response['deviceStatus']}")
建立量子任務
-
create_quantum_task(**kwargs)
創建一個量子任務。
# Create parameters to pass into create_quantum_task() kwargs = { # Create a Bell pair 'action': '{"braketSchemaHeader": {"name": "braket.ir.jaqcd.program", "version": "1"}, "results": [], "basis_rotation_instructions": [], "instructions": [{"type": "h", "target": 0}, {"type": "cnot", "control": 0, "target": 1}]}', # Specify the SV1 Device ARN 'deviceArn': 'arn:aws:braket:::device/quantum-simulator/amazon/sv1', # Specify 2 qubits for the Bell pair 'deviceParameters': '{"braketSchemaHeader": {"name": "braket.device_schema.simulators.gate_model_simulator_device_parameters", "version": "1"}, "paradigmParameters": {"braketSchemaHeader": {"name": "braket.device_schema.gate_model_parameters", "version": "1"}, "qubitCount": 2}}', # Specify where results should be placed when the quantum task completes. # You must ensure the S3 Bucket exists before calling create_quantum_task() 'outputS3Bucket': 'amazon-braket-examples', 'outputS3KeyPrefix': 'boto-examples', # Specify number of shots for the quantum task 'shots': 100 } # Send the request and capture the response response = braket.create_quantum_task(**kwargs) print(f"Quantum task {response['quantumTaskArn']} created")
擷取量子任務
-
get_quantum_task(quantumTaskArn)
擷取指定的量子任務。
# Pass the quantum task ARN when sending the request and capture the response response = braket.get_quantum_task(quantumTaskArn='arn:aws:braket:us-west-1:123456789012:quantum-task/ce78c429-cef5-45f2-88da-123456789012') print(response['status'])
搜尋量子任務
-
search_quantum_tasks(**kwargs)
搜尋符合指定篩選器值的量子工作。
# Pass search filters and optional parameters when sending the # request and capture the response response = braket.search_quantum_tasks(filters=[{ 'name': 'deviceArn', 'operator': 'EQUAL', 'values': ['arn:aws:braket:::device/quantum-simulator/amazon/sv1'] }], maxResults=25) print(f"Found {len(response['quantumTasks'])} quantum tasks") for n in range(len(response['quantumTasks'])): task = response['quantumTasks'][n] print(f"Quantum task {task['quantumTaskArn']} for {task['deviceArn']} is {task['status']}")
取消量子任務
-
cancel_quantum_task(quantumTaskArn)
取消指定的量子任務。
# Pass the quantum task ARN when sending the request and capture the response response = braket.cancel_quantum_task(quantumTaskArn='arn:aws:braket:us-west-1:123456789012:quantum-task/ce78c429-cef5-45f2-88da-123456789012') print(f"Quantum task {response['quantumTaskArn']} is {response['cancellationStatus']}")