Amazon Braket 上的量子任务示例 - Amazon Braket

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

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之外的所有设备都需要位置参数。

注意

Braket 量子任务动作的大小限制在 3MB 以内。

提交参数化任务

Amazon Braket 按需模拟器和本地模拟器,QPUs还支持在任务提交时指定免费参数的值。您可以通过使用 to 的inputs参数来执行此操作device.run(),如以下示例所示。inputs必须是字符串浮点对的字典,其中键是参数名。

参数化编译可以提高在某些情况下执行参数电路的性能。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,则模拟器执行精确模拟,返回所有结果类型的真实值。(不适用于 TN1.)

  • 对于的非零值 shots,仿真器从输出分布中采样以模拟 shot 真实的噪音QPUs。QPU仅允许使用设备 shots > 0。

有关每个量子任务的最大射门数的信息,请参阅 Braket 配额

投票结果

执行时my_task.result(),SDK开始使用您在创建量子任务时定义的参数轮询结果:

  • poll_timeout_seconds是在按需仿真器和/或QPU设备上运行量子任务时,在量子任务超时之前对其进行轮询的秒数。默认值为 432,000 秒,即 5 天。

  • 注意:对于诸如以下的QPU设备 Rigetti 以及 IonQ,我们建议您留出几天时间。如果您的轮询超时时间太短,则可能无法在轮询时间内返回结果。例如,当 a QPU 不可用时,会返回本地超时错误。

  • poll_interval_seconds是对量子任务进行轮询的频率。它指定了你拨打 Braket 的频率 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 个计数。