Ansatzes#
An ansatz is the trainable part of the circuit. A model builds one from the class you hand it, using the model’s own qubit count.
from pyqit.ansatzes import SELAnsatz
from pyqit.models import VQCClassifier
model = VQCClassifier(n_qubits=4, n_layers=3, ansatz=SELAnsatz)
Available ansatzes#
Every ansatz implements a published circuit. Its page names the paper, the gates in one layer and the shape of its weights.
Strongly entangling layers of Schuld et al. (2020). |
|
Basic entangler layers: one rotation per qubit and a CNOT ring per layer. |
|
The variational block of Mari et al. (2020): a CNOT ladder, then RY. |
|
Simplified two-design ansatz of Cerezo et al. 2021 (Nat. |
|
Qiskit's RealAmplitudes: RY layers separated by CX entanglers. |
|
Qiskit's EfficientSU2: RY and RZ layers separated by CX entanglers. |
What every ansatz provides#
get_weight_shapes() returns a dict from weight name to shape. The model
draws its initial weights from that dict, so the shapes are all a model needs
to know about an ansatz. build_circuit(weights) takes a dict with the same
keys and applies the gates.
SELAnsatz(n_qubits=4, n_layers=3).get_weight_shapes()
# {"weights": (3, 4, 3)}
Depth comes from the model’s n_layers. More layers buy expressivity and cost
you gradient variance, which is what the barren-plateau check measures.
An ansatz that wraps another library’s circuit names the package in its
python_dependencies tag. Building one without the package raises an
ImportError that names the extra to install.
Adding an ansatz#
Subclass BaseAnsatz and implement build_circuit,
get_weight_shapes and get_test_params(). The object_type tag of
"ansatz" is inherited, and the test suite enrolls the class by walking the
package. Then add its name to the list above. See
the contributing guide.
Base class for a parameterized quantum circuit block. |