打开 Amazon Braket Boto3 客户端 - Amazon Braket

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

打开 Amazon Braket Boto3 客户端

要将 Boto3 与 B Amazon raket 配合使用,必须导入 Boto3,然后定义用于连接到 Braket 的客户端。Amazon API在以下示例中,命名为 Boto3 客户端。braket

注意

为了向后兼容的旧版本 BraketSchemas,调用中省略了 OpenQasm 信息。GetDevice API要获取此信息,用户代理需要提供最新版本的 BraketSchemas (1.8.0 或更高版本)。Braket SDK 会自动为您报告此问题。如果您在使用 Braket SDK 时未在GetDevice响应中看到 OpenQasm 结果,则可能需要设置AWS_EXECUTION_ENV环境变量来配置用户代理。有关如何为、Boto3 和 Go、Java 和/SDK 执行此操作 AWS CLI,请参阅未返回 OpenQasm 结果错误主题中提供的代码示例。GetDevice 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 Braket 中可用的设备。

# 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']}")