DenseClassifier#

class pyqit.models.layers.DenseClassifier(n_features, n_classes=2)[source]#

Bases: BaseModel, ClassifierMixin

Classical head stage: one dense layer, then sigmoid or softmax.

The last stage of a hybrid QuantumPipeline, turning the features of the stage before it into class probabilities and hard labels. Like every pyqit classifier it emits probabilities, not logits. Weights sit under dense.weight and dense.bias.

Parameters:
  • n_features (int)

  • n_classes (int, default 2)

Examples

>>> from pyqit.models.layers import DenseClassifier
>>> head = DenseClassifier(n_features=4, n_classes=3)
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

forward(X, **custom_weights)[source]#

Return class probabilities.

Probability of class 1 for binary; a (n_samples, n_classes) matrix otherwise.

classmethod get_test_params()[source]#

List constructor kwargs used to parametrize this class in the test suite.

is_fitted() → bool#

Whether Trainer.fit has trained this model.

predict_step(X)#

Predict hard class labels for X.

Parameters:

X (array-like) – Input batch.

Returns:

One label per row: 0/1 for binary, argmax index for multi-class.

Return type:

array-like

register_dense(name: str, n_in: int, n_out: int, weights=None)#

Register a classical dense layer X @ weight.T + bias under 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 with execute_qnode.

Parameters:
  • name (str)

  • n_in (int)

  • n_out (int)

  • weights (dict, optional) – {"weight", "bias"} from init_dense_weights; drawn when omitted.

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.