Ansatze#

Base Ansatz#

class BaseAnsatz(n_qubits)[source]#

Bases: ABC

Abstract base class for quantum ansatze.

An ansatz is a parameterized quantum circuit that can be trained by adjusting its parameters (weights). In variational quantum algorithms, the ansatz works in concert with a feature map to learn from data.

Subclasses must implement circuit() and weight_shape().

Parameters:

n_qubits (int) – Number of qubits in the circuit.

n_qubits#

Number of qubits.

Type:

int

n_params#

Total number of parameters (set by subclasses if applicable).

Type:

int, optional

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.

weight_shape(n_qubits)[source]#

Return the shape of the weight array needed.

Parameters:

n_qubits (int) – Number of qubits.

Returns:

Shape of the weight array. Can be a single integer (total params) or a tuple (e.g., (n_layers, n_qubits, 3) for layered ansatze).

Return type:

tuple[int, …] or int

initialize_weights(seed=None)[source]#

Initialize random weights.

Parameters:

seed (int, optional) – Random seed for reproducibility.

Returns:

Randomly initialized weights with the correct shape.

Return type:

ndarray

Hardware Efficient Ansatz#

class HardwareEfficientAnsatz(n_qubits, layers=2)[source]#

Bases: BaseAnsatz

Hardware-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).

Strongly Entangling Ansatz#

class StronglyEntanglingAnsatz(n_qubits, layers=2)[source]#

Bases: BaseAnsatz

Strongly 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).