Ansatze#
Ansatze are parameterized quantum circuits with trainable weights.
HardwareEfficientAnsatz#
- 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.
- Parameters:
weights (array-like, shape (layers, n_qubits, 3)) – Rotation parameters. weights[l, q, 0] gives RX angle, weights[l, q, 1] gives RY angle, weights[l, q, 2] gives 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.
- weight_shape(n_qubits)[source]
Return the shape of the weight array: (layers, n_qubits, 3).
The 3 corresponds to RX, RY, RZ rotation angles per qubit per layer, following Kandala et al. (Nature 2017).
StronglyEntanglingAnsatz#
- 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 in that order.
wires (Iterable[int]) – Qubit wires.
Notes
This uses PennyLane’s built-in StronglyEntanglingLayers template. The circuit structure follows the pattern described in Schuld et al. (2018): each layer consists of single-qubit rotations followed by nearest-neighbor CNOT entangling gates in a ring pattern.
- weight_shape(n_qubits)[source]
Return the shape of the weight array: (layers, n_qubits, 3).