Source code for psipose.feature_maps.base

"""Base feature map class.

Feature maps encode classical data into quantum states, a fundamental
technique in quantum machine learning known as quantum data embedding.
The quality of the feature map affects the expressivity and trainability
of variational quantum algorithms.
"""

import abc

import pennylane as qml


[docs] class FeatureMap(abc.ABC): """Abstract base class for quantum feature maps. A feature map is a parameterized quantum circuit that encodes classical data :math:`\\mathbf{x}` into a quantum state :math:`|\\phi(\\mathbf{x})\\rangle`. This quantum embedding can map data into a high-dimensional Hilbert space, potentially enabling better separation for classification tasks. The feature map design is critical for quantum kernel methods and variational quantum classifiers. See Schuld et al. (2021), "Evaluating the impact of quantum classifiers on learning tasks" (arXiv:2105.14446) for a discussion on feature map choices. Parameters ---------- n_qubits : int, optional Number of qubits to encode into. If None, inferred from feature dimension during fit(). Attributes ---------- n_qubits_ : int The number of qubits used (determined during fit). """ def __init__(self, n_qubits=None): self.n_qubits = n_qubits self.n_qubits_ = None
[docs] @abc.abstractmethod def encode(self, x, wires): """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. """ pass
[docs] def fit(self, X): """Determine the number of qubits needed based on data. Parameters ---------- X : array-like, shape (n_samples, n_features) Training data. Returns ------- self : FeatureMap Returns the fitted feature map. """ n_features = X.shape[1] if self.n_qubits is None: # Default: one qubit per feature, capped at reasonable size self.n_qubits_ = min(n_features, 10) else: self.n_qubits_ = self.n_qubits return self
[docs] def transform(self, X): """Encode the data into quantum states. Parameters ---------- X : array-like, shape (n_samples, n_features) Data to encode. Returns ------- quantum_states : list List of quantum state descriptions (implementation-specific). """ states = [] for x in X: with qml.tape.QuantumTape() as tape: self.encode(x, wires=range(self.n_qubits_)) states.append(tape) return states