在 SDK 中构造电路 - Amazon Braket

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

在 SDK 中构造电路

本节提供了定义电路、查看可用门、扩展电路以及查看每个设备支持的门的示例。它还包含有关如何手动分配qubits、指示编译器完全按照定义运行电路以及如何使用噪声模拟器构建噪声电路的说明。

你也可以在 Braket 中对具有某些 QPU 的各种门进行脉冲电平工作。有关更多信息,请参阅 Amazon Braket 上的脉冲控制

大门和电路

量子门和电路是在 Amazon Braket Python SDK 的braket.circuits类中定义的。在 SDK 中,你可以通过调用来实例化一个新的电路对象。Circuit()

示例:定义电路

该示例首先定义了一个由四个qubits(标记为q0、、和q3)组成的样本电路 q1q2,包括标准的单量子比特 Hadamard 门和两个量子比特 CNOT 门。您可以通过调用print函数来可视化此电路,如下例所示。

# import the circuit module from braket.circuits import Circuit # define circuit with 4 qubits my_circuit = Circuit().h(range(4)).cnot(control=0, target=2).cnot(control=1, target=3) print(my_circuit)
T : |0| 1 | q0 : -H-C--- | q1 : -H-|-C- | | q2 : -H-X-|- | q3 : -H---X- T : |0| 1 |

示例:定义参数化电路

在这个例子中,我们定义了一个电路,其门依赖于自由参数。我们可以指定这些参数的值来创建新电路,或者在提交电路时,在某些设备上作为量子任务运行。

from braket.circuits import Circuit, FreeParameter #define a FreeParameter to represent the angle of a gate alpha = FreeParameter("alpha") #define a circuit with three qubits my_circuit = Circuit().h(range(3)).cnot(control=0, target=2).rx(0, alpha).rx(1, alpha) print(my_circuit)

您可以通过向电路提供单个参数float(这是所有自由参数将采用的值)或关键字参数来指定每个参数的值,从而从参数化电路中创建一个新的非参数化电路,如下所示。

my_fixed_circuit = my_circuit(1.2) my_fixed_circuit = my_circuit(alpha=1.2)

请注意,my_circuit这是未修改的,因此您可以使用它来实例化许多具有固定参数值的新电路。

示例:修改电路中的门

以下示例定义了一个电路,其门使用控制和功率修改器。您可以使用这些修改来创建新的大门,例如受控Ry门。

from braket.circuits import Circuit # Create a bell circuit with a controlled x gate my_circuit = Circuit().h(0).x(control=0, target=1) # Add a multi-controlled Ry gate of angle .13 my_circuit.ry(angle=.13, target=2, control=(0, 1)) # Add a 1/5 root of X gate my_circuit.x(0, power=1/5) print(my_circuit)

只有本地模拟器支持门控修改器。

示例:查看所有可用的登机口

以下示例说明如何查看 Amazon Braket 中所有可用的门。

from braket.circuits import Gate # print all available gates in Amazon Braket gate_set = [attr for attr in dir(Gate) if attr[0].isupper()] print(gate_set)

此代码的输出列出了所有的门。

['CCNot', 'CNot', 'CPhaseShift', 'CPhaseShift00', 'CPhaseShift01', 'CPhaseShift10', 'CSwap', 'CV', 'CY', 'CZ', 'ECR', 'GPi', 'GPi2', 'H', 'I', 'ISwap', 'MS', 'PSwap', 'PhaseShift', 'PulseGate', 'Rx', 'Ry', 'Rz', 'S', 'Si', 'Swap', 'T', 'Ti', 'Unitary', 'V', 'Vi', 'X', 'XX', 'XY', 'Y', 'YY', 'Z', 'ZZ']

通过调用该类型电路的方法,可以将这些门中的任何一个附加到电路中。例如,你可以打电话circ.h(0),在第一个门上添加一扇哈达玛德大门。qubit

注意

门已附加到位,以下示例将上一个示例中列出的所有门添加到同一个电路中。

circ = Circuit() # toffoli gate with q0, q1 the control qubits and q2 the target. circ.ccnot(0, 1, 2) # cnot gate circ.cnot(0, 1) # controlled-phase gate that phases the |11> state, cphaseshift(phi) = diag((1,1,1,exp(1j*phi))), where phi=0.15 in the examples below circ.cphaseshift(0, 1, 0.15) # controlled-phase gate that phases the |00> state, cphaseshift00(phi) = diag([exp(1j*phi),1,1,1]) circ.cphaseshift00(0, 1, 0.15) # controlled-phase gate that phases the |01> state, cphaseshift01(phi) = diag([1,exp(1j*phi),1,1]) circ.cphaseshift01(0, 1, 0.15) # controlled-phase gate that phases the |10> state, cphaseshift10(phi) = diag([1,1,exp(1j*phi),1]) circ.cphaseshift10(0, 1, 0.15) # controlled swap gate circ.cswap(0, 1, 2) # swap gate circ.swap(0,1) # phaseshift(phi)= diag([1,exp(1j*phi)]) circ.phaseshift(0,0.15) # controlled Y gate circ.cy(0, 1) # controlled phase gate circ.cz(0, 1) # Echoed cross-resonance gate applied to q0, q1 circ = Circuit().ecr(0,1) # X rotation with angle 0.15 circ.rx(0, 0.15) # Y rotation with angle 0.15 circ.ry(0, 0.15) # Z rotation with angle 0.15 circ.rz(0, 0.15) # Hadamard gates applied to q0, q1, q2 circ.h(range(3)) # identity gates applied to q0, q1, q2 circ.i([0, 1, 2]) # iswap gate, iswap = [[1,0,0,0],[0,0,1j,0],[0,1j,0,0],[0,0,0,1]] circ.iswap(0, 1) # pswap gate, PSWAP(phi) = [[1,0,0,0],[0,0,exp(1j*phi),0],[0,exp(1j*phi),0,0],[0,0,0,1]] circ.pswap(0, 1, 0.15) # X gate applied to q1, q2 circ.x([1, 2]) # Y gate applied to q1, q2 circ.y([1, 2]) # Z gate applied to q1, q2 circ.z([1, 2]) # S gate applied to q0, q1, q2 circ.s([0, 1, 2]) # conjugate transpose of S gate applied to q0, q1 circ.si([0, 1]) # T gate applied to q0, q1 circ.t([0, 1]) # conjugate transpose of T gate applied to q0, q1 circ.ti([0, 1]) # square root of not gate applied to q0, q1, q2 circ.v([0, 1, 2]) # conjugate transpose of square root of not gate applied to q0, q1, q2 circ.vi([0, 1, 2]) # exp(-iXX theta/2) circ.xx(0, 1, 0.15) # exp(i(XX+YY) theta/4), where theta=0.15 in the examples below circ.xy(0, 1, 0.15) # exp(-iYY theta/2) circ.yy(0, 1, 0.15) # exp(-iZZ theta/2) circ.zz(0, 1, 0.15) # IonQ native gate GPi with angle 0.15 applied to q0 circ.gpi(0, 0.15) # IonQ native gate GPi2 with angle 0.15 applied to q0 circ.gpi2(0, 0.15) # IonQ native gate MS with angles 0.15, 0.15, 0.15 applied to q0, q1 circ.ms(0, 1, 0.15, 0.15, 0.15)

除了预定义的门设置外,您还可以将自定义的单元门应用于电路。它们可以是单量子比特门(如以下源代码所示),也可以是应用于参数qubits定义的多量子比特门。targets

import numpy as np # apply a general unitary my_unitary = np.array([[0, 1],[1, 0]]) circ.unitary(matrix=my_unitary, targets=[0])

示例:扩展现有电路

您可以通过添加指令来扩展现有电路。A Instruction 是一种量子指令,它描述了要在量子器件上执行的量子任务。 Instruction运算符Gate仅包括类型的对象。

# import the Gate and Instruction modules from braket.circuits import Gate, Instruction # add instructions directly. circ = Circuit([Instruction(Gate.H(), 4), Instruction(Gate.CNot(), [4, 5])]) # or with add_instruction/add functions instr = Instruction(Gate.CNot(), [0, 1]) circ.add_instruction(instr) circ.add(instr) # specify where the circuit is appended circ.add_instruction(instr, target=[3, 4]) circ.add_instruction(instr, target_mapping={0: 3, 1: 4}) # print the instructions print(circ.instructions) # if there are multiple instructions, you can print them in a for loop for instr in circ.instructions: print(instr) # instructions can be copied new_instr = instr.copy() # appoint the instruction to target new_instr = instr.copy(target=[5]) new_instr = instr.copy(target_mapping={0: 5})

示例:查看每台设备支持的大门

模拟器支持 Braket SDK 中的所有门,但是 QPU 设备支持的子集较小。你可以在设备属性中找到设备支持的门。以下显示了 ionQ 设备的示例:

# import the device module from braket.aws import AwsDevice device = AwsDevice("arn:aws:braket:us-east-1::device/qpu/ionq/Harmony") # get device name device_name = device.name # show supportedQuantumOperations (supported gates for a device) device_operations = device.properties.dict()['action']['braket.ir.openqasm.program']['supportedOperations'] print('Quantum Gates supported by {}:\n {}'.format(device_name, device_operations))
Quantum Gates supported by the Harmony device: ['x', 'y', 'z', 'rx', 'ry', 'rz', 'h', 'cnot', 's', 'si', 't', 'ti', 'v', 'vi', 'xx', 'yy', 'zz', 'swap', 'i']

支持的门可能需要编译成原生门,然后才能在量子硬件上运行。当您提交电路时,AmazonBraket 会自动执行此编译。

示例:以编程方式检索设备支持的原生门的保真度

您可以在 Braket 控制台的 “设备” 页面上查看保真度信息。有时,以编程方式访问相同的信息会很有帮助。以下代码显示如何提取 QPU 的两个qubit门之间的两门保真度。

# import the device module from braket.aws import AwsDevice device = AwsDevice("arn:aws:braket:us-west-1::device/qpu/rigetti/Aspen-M-3") #specify the qubits a=10 b=113 print(f"Fidelity of the XY gate between qubits {a} and {b}: ", device.properties.provider.specs["2Q"][f"{a}-{b}"]["fXY"])

部分测量

按照前面的示例,我们测量了量子电路中的所有量子比特。但是,可以测量单个量子比特或量子比特的子集。

示例:测量量子比特的子集

在这个例子中,我们通过在电路末端添加一条带有目标量子比特的measure指令来演示部分测量。

# Use the local state vector simulator device = LocalSimulator() # Define an example bell circuit and measure qubit 0 circuit = Circuit().h(0).cnot(0, 1).measure(0) # Run the circuit task = device.run(circuit, shots=10) # Get the results result = task.result() # Print the circuit and measured qubits print(circuit) print() print("Measured qubits: ", result.measured_qubits)

手动qubit分配

当你在量子计算机上运行量子电路时Rigetti,你可以选择使用手动qubit分配来控制qubits哪些用于你的算法。Amazon Braket 控制台Amazon Braket SDK 可帮助您检查所选量子处理单元 (QPU) 设备的最新校准数据,以便您可以为实验选择最佳校准数据。qubits

手动qubit分配使您能够更准确地运行电路并调查各个qubit特性。研究人员和高级用户可以根据最新的设备校准数据优化其电路设计,从而获得更准确的结果。

以下示例演示如何qubits显式分配。

circ = Circuit().h(0).cnot(0, 7) # Indices of actual qubits in the QPU my_task = device.run(circ, s3_location, shots=100, disable_qubit_rewiring=True)

欲了解更多信息,请参阅本笔记本上的 Amazon Braket 示例:在 GitHub QPU 设备上分配量子比特

注意

编OQC译器不支持设置disable_qubit_rewiring=True。将此标志设置为会True产生以下错误:An error occurred (ValidationException) when calling the CreateQuantumTask operation: Device arn:aws:braket:eu-west-2::device/qpu/oqc/Lucy does not support disabled qubit rewiring.

逐字记录汇编

当你在Rigetti、IonQ或 Oxford Quantum Circuits (OQC) 的量子计算机上运行量子电路时,你可以指示编译器完全按照定义运行你的电路,而无需进行任何修改。使用逐字编译,您可以指定要么精确地保留整个电路(由RigettiIonQ、和支持OQC),要么仅保留其中的特定部分(仅受Rigetti支持)。在为硬件基准测试或错误缓解协议开发算法时,你需要可以选择精确指定在硬件上运行的门和电路布局。Verbatim 编译使您可以通过关闭某些优化步骤来直接控制编译过程,从而确保您的电路完全按照设计运行。

、和 Oxford Quantum Circuits (OQC) 设备目前支持 Verbatim 编译 RigettiIonQ,并且需要使用原生门。使用逐字编译时,建议检查设备的拓扑结构,以确保在连接时调用门,qubits并且电路使用硬件支持的本机门。以下示例说明如何以编程方式访问设备支持的原生门列表。

device.properties.paradigm.nativeGateSet

对于Rigetti,必须通过设置disableQubitRewiring=True为与逐字编译一起使用来关闭qubit重新布线。disableQubitRewiring=False如果在编译中使用逐字记录框时设置,则量子电路验证失败且无法运行。

如果为电路启用了逐字编译并在不支持逐字编译的 QPU 上运行,则会生成错误,表示任务因不支持的操作而导致任务失败。随着越来越多的量子硬件原生支持编译器功能,该功能将扩展到包括这些设备。使用以下代码查询时,支持逐字编译的设备将其列为支持的操作。

from braket.aws import AwsDevice from braket.device_schema.device_action_properties import DeviceActionType device = AwsDevice("arn:aws:braket:us-west-1::device/qpu/rigetti/Aspen-M-3") device.properties.action[DeviceActionType.OPENQASM].supportedPragmas

使用逐字记录汇编不会产生任何额外费用。对于在 Braket QPU 设备、笔记本实例和按需模拟器上执行的量子任务,您仍需按照 Amazon Braket 定价页面上指定的当前费率付费。有关更多信息,请参阅 Verbatim 编译示例笔记本。

注意

如果您使用 OpenQasm 为OQC和IonQ设备编写电路,并且希望将电路直接映射到物理量子比特,则需要使用,因#pragma braket verbatim为 OpenQasm 会完全忽略该disableQubitRewiring标志。

噪声模拟

要实例化本地噪声模拟器,你可以按如下方式更改后端。

device = LocalSimulator(backend="braket_dm")

您可以通过两种方式构建噪音电路:

  1. 自下而上地建造嘈杂的电路。

  2. 采用现有的无噪音电路,并在整个过程中注入噪音。

以下示例显示了使用带有去极化噪声的简单电路和自定义 Kraus 通道的方法。

# Bottom up approach # apply depolarizing noise to qubit 0 with probability of 0.1 circ = Circuit().x(0).x(1).depolarizing(0, probability=0.1) # create an arbitrary 2-qubit Kraus channel E0 = scipy.stats.unitary_group.rvs(4) * np.sqrt(0.8) E1 = scipy.stats.unitary_group.rvs(4) * np.sqrt(0.2) K = [E0, E1] # apply a two-qubit Kraus channel to qubits 0 and 2 circ = circ.kraus([0,2], K)
# Inject noise approach # define phase damping noise noise = Noise.PhaseDamping(gamma=0.1) # the noise channel is applied to all the X gates in the circuit circ = Circuit().x(0).y(1).cnot(0,2).x(1).z(2) circ_noise = circ.copy() circ_noise.apply_gate_noise(noise, target_gates = Gate.X)

运行电路的用户体验与以前相同,如以下两个示例所示。

示例 1

task = device.run(circ, s3_location)

Or

示例 2

task = device.run(circ_noise, s3_location)

有关更多示例,请参阅 Braket 入门噪声模拟器示例