487 字
2 分钟
qml.qnn.TorchLayer()的参数传递机制
在 PennyLane 的 qml.qnn.TorchLayer
中,weight_shapes
通过 自动解包参数 的方式传递给 circuit
,主要依赖于 qml.QNode
的 参数绑定机制。
1. qml.qnn.TorchLayer
的作用
TorchLayer
负责:
- 创建 一个
torch.nn.Module
,其中weight_shapes
定义了 可训练参数 的形状。 - 自动初始化 这些参数,并在前向传播时传递给
circuit
。 - 将
weights
参数解包,并按照circuit
函数的参数顺序提供。
2. weight_shapes
如何传递给 circuit
?
在 qml.qnn.TorchLayer(circuit, weight_shapes)
过程中,PennyLane 会:
- 检查
circuit
需要的参数 (inputs
,weights_0
, …,weights_4
)。 - 按照
weight_shapes
定义,创建 可训练的 PyTorch 参数。 - 在前向传播时,把这些参数自动传递给
circuit
。
重要机制:
weight_shapes
的 键名 必须与circuit
形参的 名称匹配。TorchLayer
自动分配 PyTorchnn.Parameter
,并在forward()
传递它们。
3. 代码示例
假设 circuit
定义如下:
import pennylane as qmlgate_param_num = [3, 4, 5, 6] #假设每个gate参数为3gate_num = [10, 11, 12, 13] # 不同的layer,逻辑门数量不同# 存在一个内部由多个多参数weights组成的@qml.qnode(dev)def qlayer(weights, wires): for i in range(0, len(weights)): gate(weights[i], wires = wires);
@qml.qnode(dev)def circuit(inputs, weights_0, weights_1, weights_2, weights_3, weights_4):
qml.ctrl(qlayer)(weights_0, wires=target_wires) qml.ctrl(qlayer)(weights_1, wires=target_wires) qml.ctrl(qlayer)(weights_2, wires=target_wires) qml.ctrl(qlayer)(weights_3, wires=target_wires) qml.RY(weight_4[0], wires=0) qml.RY(weight_4[1], wires=1) qml.RY(weight_4[2], wires=2) qml.RY(weight_4[3], wires=3)
return qml.probs(wires=range(4))
那么,创建 TorchLayer
时:
weight_shapes = { # 不同的layer,逻辑门数量和逻辑门参数不同 "weights_0": (gate_num[0], gate_param_num[0]), "weights_1": (gate_num[1], gate_param_num[1]), "weights_2": (gate_num[2], gate_param_num[2]), "weights_3": (gate_num[3], gate_param_num[3]), # 相同结构的RY门,参数合并为list "weights_4": (4,),}
qlayer = qml.qnn.TorchLayer(circuit, weight_shapes)
4. 传递过程
-
TorchLayer
创建 PyTorchnn.Parameter
: -
前向传播时,这些参数 自动作为
circuit
的参数传入:output = qlayer(inputs)其中
inputs
由 PyTorch 计算图管理,而weights_0, ..., weights_4
由TorchLayer
负责传入circuit
。
5. 关键机制总结
weight_shapes
的键名必须匹配circuit
参数名。TorchLayer
负责创建 PyTorch 参数,并在forward()
传递给circuit
。- 在
circuit
中,这些权重参数被 PennyLane 绑定并用于量子门操作。
qml.layer()介绍
https://docs.pennylane.ai/en/stable/code/api/pennylane.layer.html
注意:qml.layer(template, depth, *args)
中,args参数中,第一个维度必须等于depth
qml.qnn.TorchLayer()的参数传递机制
https://chrisnake11.github.io/blog/posts/others/quantumcomputing/qmlqnntorchlayer的参数传递机制/