Encoders#

Encoders map classical data to quantum states.

AngleEncoder#

class AngleEncoder(rotation='Y', n_qubits=None)[source]#

Bases: BaseEncoder

Angle encoding of classical data into quantum rotations.

Each feature \(x_i\) is encoded as a rotation angle on a separate qubit via \(R_{\text{axis}}(x_i)\). By default, uses Y rotations. Supports X, Y, or Z rotations.

This follows the angle encoding approach from:

  • Farhi & Neven (2018), “Classification with Quantum Neural Networks on Near Term Processors” (arXiv:1802.06002). Uses parametrized rotation gates to encode classical data into quantum states.

  • Schuld, Bocharov, Svore & Wiebe (2018), “Circuit-centric quantum classifiers” (arXiv:1804.00633). Encodes features as rotation angles in variational quantum circuits.

Mathematically, for a feature vector \(\mathbf{x} = (x_1, \dots, x_n)\), the encoded state is:

\[|\phi(\mathbf{x})\rangle = \bigotimes_{i=1}^{n} R_{\text{axis}}(x_i) |0\rangle\]

where \(R_{\text{axis}} \in \{R_X, R_Y, R_Z\}\).

Parameters:
  • rotation ({"X", "Y", "Z"}, default="Y") – Which rotation gate to use for encoding.

  • n_qubits (int, optional) – Number of qubits to use. If None, uses one qubit per feature (or the minimum of n_features and 10).

Examples

>>> encoder = AngleEncoder(rotation="Y")
>>> encoder.fit(X_train)  # determines n_qubits_
>>> circuit = encoder.encode(x_sample, wires=range(encoder.n_qubits_))
encode(x, wires)[source]#

Apply angle encoding to the given wires.

Only encodes as many features as there are wires. If x has more features than wires, the excess features are truncated.

AmplitudeEncoder#

class AmplitudeEncoder(n_qubits=None, normalize=True)[source]#

Bases: BaseEncoder

Amplitude encoding of a classical vector into quantum amplitudes.

Maps a classical feature vector \(\mathbf{x}\) to the amplitudes of a quantum state \(|\phi(\mathbf{x})\rangle\). Requires \(n_{\text{qubits}}\) such that \(2^{n_{\text{qubits}}} \ge n_{\text{features}}\).

This implements the state preparation method from:

  • Möttönen, Vartiainen, Bergholm & Salomaa (2004), “Quantum circuit for preparing arbitrary states” (arXiv:quant-ph/0407010). The qml.AmplitudeEmbedding() template uses this algorithm.

  • Schuld & Petruccione (2018), “Machine Learning with Quantum Computers”, Springer. Chapter 6 covers amplitude encoding as a feature map.

Mathematically, for a normalized feature vector \(\mathbf{x} = (x_1, \dots, x_d)\) with \(\|\mathbf{x}\| = 1\):

\[|\phi(\mathbf{x})\rangle = \sum_{i=1}^{d} x_i |i-1\rangle\]

where \(d = 2^{n_{\text{qubits}}}\). If the input dimension is not a power of 2, it is padded with zeros to the next power of 2.

Parameters:
  • n_qubits (int, optional) – Number of qubits to use. If None, the smallest power of 2 that can accommodate the feature dimension is used.

  • normalize (bool, default=True) – Whether to normalize the input vector before encoding.

n_features_#

The original number of features (set after fit).

Type:

int

Examples

>>> encoder = AmplitudeEncoder()
>>> encoder.fit(X_train)  # determines n_qubits_ based on n_features
>>> # X_train.shape[1] must be <= 2**n_qubits
fit(X)[source]#

Determine the number of qubits needed.

For amplitude encoding, we need \(2^{n_{\text{qubits}}} \ge n_{\text{features}}\). If n_qubits is not specified, choose the smallest that works.

encode(x, wires)[source]#

Apply amplitude encoding to the given wires.

Pads or truncates the input to match \(2^{\text{len(wires)}}\).

Note: normalize=False is passed to qml.AmplitudeEmbedding because normalization is handled manually above (using self.normalize) to support the zero-vector edge case.