BaseQuantumModel#
- class pyqit.models.BaseQuantumModel(device='default.qubit', shots=None, diff_method='best')[source]#
Bases:
BaseModelBase class wiring a PennyLane QNode into either backend.
Subclasses build a QNode and call register_qnode, which handles the pennylane/torch fork. The weight registry itself is BaseModel’s.
- diff_methods(X) dict[source]#
Differentiation method per QNode.
- Parameters:
X (array-like) – One prescaled batch; only its shape matters.
- Returns:
QNode name to method name.
- Return type:
dict
- execute_qnode(name: str, X, **custom_weights)#
Run the QNode or dense layer registered under name on a batch.
- Parameters:
name (str) – Name passed to register_qnode.
X (array-like)
**custom_weights – Flat “<name>.<weight>” overrides; unprefixed keys are ignored. Falls back to the model’s own weights when empty.
- Return type:
array-like
- init_weights(weight_shapes: dict) dict[source]#
Draw uniform
[0, 1)starting weights from numpy’s global RNG.The range matches Qiskit ML’s default
initial_pointand PennyLane’s template examples; uniform[0, 2pi)is the Haar-like regime where gradients vanish (McClean et al. 2018).Call this before building the device: a PennyLane device seeded
"global"consumes numpy’s RNG at construction by a device-dependent amount, so weights drawn after it differ per device for the same seed.- Parameters:
weight_shapes (dict) – Weight name to shape, as returned by an ansatz’s get_weight_shapes.
- Return type:
dict
- is_fitted() bool#
Whether Trainer.fit has trained this model.
- register_dense(name: str, n_in: int, n_out: int, weights=None)#
Register a classical dense layer
X @ weight.T + biasunder name.Lives in the same registry as the QNodes, so
weights,update_weights, checkpoints and the flat-kwargs routing cover it with no further plumbing. Run it withexecute_qnode.- Parameters:
name (str)
n_in (int)
n_out (int)
weights (dict, optional) –
{"weight", "bias"}from init_dense_weights; drawn when omitted.
- register_qnode(name: str, qnode: QNode, weight_shapes: dict, weights=None)[source]#
Wrap qnode for the active backend and store it under name.
- Parameters:
name (str) – Key under which the node’s weights appear in weights.
qnode (qml.QNode)
weight_shapes (dict) – Weight name to shape, as returned by an ansatz’s get_weight_shapes.
weights (dict, optional) – Starting weights from init_weights; drawn here when omitted, so both backends start from the same point for the same seed.
- update_weights(flat_weights_dict)#
Write flat_weights_dict into the model’s own weights.
No-op under torch, where autograd owns the nn.Parameter objects directly.
- Parameters:
flat_weights_dict (dict) – Keyed like weights.
- weight_groups() dict#
weights keys by group,
"quantum"(QNodes) and"classical".Empty groups are omitted. The training loops build one optimizer per group, which is what lets
Trainer(learning_rate={...})set a rate per group.
- property weights#
Flat
{"<qnode_name>.<weight_name>": array}dict, both backends.