Encoders#
Base Encoder#
- class BaseEncoder(n_qubits=None)[source]#
Bases:
ABCAbstract base class for quantum encoders.
Encoders map classical feature vectors onto quantum states.
- Parameters:
n_qubits (int, optional) – Number of qubits to encode into. If None, inferred from feature dimension.
- n_qubits_#
The number of qubits used (determined during fit).
- Type:
int
- abstractmethod encode(x, wires)[source]#
Apply encoding circuit to the given wires.
- Parameters:
x (array-like) – Feature vector to encode.
wires (Iterable[int]) – Qubit wires to apply the encoding to.
Angle Encoder#
- class AngleEncoder(rotation='Y', n_qubits=None)[source]#
Bases:
BaseEncoderAngle 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_))
Amplitude Encoder#
- class AmplitudeEncoder(n_qubits=None, normalize=True)[source]#
Bases:
BaseEncoderAmplitude 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=Falseis passed toqml.AmplitudeEmbeddingbecause normalization is handled manually above (usingself.normalize) to support the zero-vector edge case.