QLayerV2 is a new, simplified quantum layer for hybrid QNNs. It is constructed from a synthesized quantum program and an optional set of observables, and produces a trainable torch.nn.Module whose outputs are the expectation values of those observables.
The new interface is designed to be more intuitive than the classic QLayer and - just as importantly - it is much faster behind the scenes, so training loops run considerably quicker.
Use it when your quantum layer’s output is naturally a set of expectation values. For layers that need sampling, counts, or custom classical post-processing, keep using the classic QLayer (see Choosing between the two layers).
QLayerV2 currently runs only inside Classiq Studio and is limited to circuits of up to 12 qubits. Constructing it in any other environment, or with a larger circuit, raises an error. Extending its scale and reach is planned.
QLayerV2 is a temporary name. It ships alongside the existing QLayer (also available under the alias QLayerV1), which is unchanged for now so existing code keeps working. QLayerV2 is the interface we are moving toward: once it covers the full feature set, the classic interface will go through the standard deprecation process and QLayerV2 will become the single, canonical QLayer. That deprecation has not started yet.
Interface
QLayerV2 is imported from the same package as QLayer:
qprog - a synthesized QuantumProgram. Its parameters are classified by name: the input/i_ prefixes mark inputs (the encoded classical data) and the weight/w_ prefixes mark trainable weights (for example input_0, i_x, weight_0, w_theta). See Parameters: Inputs Versus Weights.
observables - one output feature per Pauli observable. Defaults to one <Z_i> per qubit, so the default output width equals the circuit width. Accepts a single SparsePauliOp or a sequence of them. An observable narrower than the program is padded with identities to the program’s width.
The layer trains like any other torch module. Its forward is rank-preserving: an input of shape (batch, in_features) produces (batch, out_features), and an unbatched input of shape (in_features,) produces (out_features,).
Examples
Minimal example
With only a qprog, the layer measures one <Z_i> per qubit using the default observables:
The result is a trainable torch layer that plugs into an ordinary network:
Custom observables
Pass one SparsePauliOp per output feature to control what the layer measures. Here the single output feature is <Z_0>:
Choosing between the two layers
The classic QLayer is also available under the alias QLayerV1, so the two interfaces can be named side by side during the transition.
QLayerV1 remains available today for sampling, counts, and custom post-processing. QLayerV2 is the interface we are building toward and will eventually become the single, canonical QLayer. Reach for QLayerV2 when you want the simplest, fastest path to an expectation-value layer.