Ansatze#
Base Ansatz#
- class BaseAnsatz(n_qubits)[source]#
Bases:
ABCAbstract base class for quantum ansatze.
An ansatz is a parameterized quantum circuit that can be trained by adjusting its parameters (weights).
- Parameters:
n_qubits (int) – Number of qubits in the circuit.
- abstractmethod circuit(weights, wires)[source]#
Build the parameterized quantum circuit.
- Parameters:
weights (array-like) – Trainable parameters for the ansatz. Shape depends on the ansatz.
wires (Iterable[int]) – Qubit wires to apply the circuit to.
Hardware Efficient Ansatz#
- class HardwareEfficientAnsatz(n_qubits, layers=2)[source]#
Bases:
BaseAnsatzHardware-efficient ansatz with alternating rotations and entangling layers.
Implements the hardware-efficient ansatz from Kandala et al., “Hardware-efficient variational quantum eigensolver for small molecules and quantum magnets” (Nature 549, 242-246, 2017).
Each layer consists of: 1. Single-qubit rotations (RX, RY, RZ) on each qubit 2. Entangling gates (CNOTs) in a nearest-neighbor chain
- Parameters:
n_qubits (int) – Number of qubits.
layers (int, default=2) – Number of repeating layers.
Examples
>>> ansatz = HardwareEfficientAnsatz(n_qubits=4, layers=3) >>> weight_shape = ansatz.weight_shape(4) # (layers, n_qubits, 3)
- circuit(weights, wires)[source]#
Build the quantum circuit.
Implements the hardware-efficient ansatz from Kandala et al. (Nature 2017), where each layer applies RX, RY, RZ rotations on every qubit followed by a chain of CNOT entangling gates.
- Parameters:
weights (array-like, shape (layers, n_qubits, 3)) – Rotation parameters. weights[l, q, 0] is RX angle, weights[l, q, 1] is RY angle, weights[l, q, 2] is RZ angle for qubit q in layer l.
wires (Iterable[int]) – Qubit wires.
Notes
Weights can also be a 1D array of length layers * n_qubits * 3, which will be reshaped automatically.
Strongly Entangling Ansatz#
- class StronglyEntanglingAnsatz(n_qubits, layers=2)[source]#
Bases:
BaseAnsatzStrongly entangling ansatz based on PennyLane’s template.
Applies a strongly entangling layer structure where each qubit receives rotations around all three axes, followed by entangling gates between all neighboring qubits.
This follows the circuit-centric quantum classifier approach from:
Schuld, Bocharov, Svore & Wiebe (2018), “Circuit-centric quantum classifiers” (arXiv:1804.00633). Introduces layered circuits with single-qubit rotations and entangling gates for variational quantum classification.
PennyLane’s
qml.StronglyEntanglingLayers()template, which implements this architecture directly.
Mathematically, each layer \(l\) applies:
\[U_l(\boldsymbol{\theta}) = \text{CNOT}_{\text{chain}} \, \prod_{q=0}^{n-1} R_Z(\theta_{l,q,2}) R_Y(\theta_{l,q,1}) R_X(\theta_{l,q,0})\]where \(\boldsymbol{\theta}\) has shape \((\text{layers}, n_{\text{qubits}}, 3)\).
- Parameters:
n_qubits (int) – Number of qubits.
layers (int, default=2) – Number of repeating layers.
- weight_shape_#
The shape of weights returned by weight_shape(): (layers, n_qubits, 3).
- Type:
tuple[int, int]
Examples
>>> ansatz = StronglyEntanglingAnsatz(n_qubits=4, layers=3) >>> weights = ansatz.initialize_weights(seed=42) >>> # weights.shape == (3, 4, 3)
- circuit(weights, wires)[source]#
Build the quantum circuit using PennyLane’s StronglyEntanglingLayers.
- Parameters:
weights (array-like, shape (layers, n_qubits, 3)) – Rotation parameters for each layer and qubit. The last dimension contains [RX, RY, RZ] rotation angles.
wires (Iterable[int]) – Qubit wires.