qxmt.feature_maps.base module

qxmt.feature_maps.base module#

class qxmt.feature_maps.base.BaseFeatureMap(platform, n_qubits)

Bases: ABC

Base class for feature map. This class is used to define the feature map for quantum machine learning. The feature map defined a quantum circuit that maps input data to quantum states. Provide a common interface within the QXMT library by absorbing differences between multiple platforms. User can define their own feature map by inheriting this class and defined in the ‘feature_map’ method.

Examples

>>> import numpy as np
>>> import pennylane as qml
>>> from qxmt.feature_maps.base import BaseFeatureMap
>>> class CustomFeatureMap(BaseFeatureMap):
...     def __init__(self, platform: str, n_qubits: int) -> None:
...         super().__init__(platform, n_qubits)
...
...     def feature_map(self, x: np.ndarray) -> None:
...         self.check_input_shape(x)
...         # define quantum circuit
...         qml.RX(x[0, 0], wires=0)
...
>>> feature_map = CustomFeatureMap("pennylane", 2)
>>> feature_map(np.random.rand(1, 2))
Parameters:
  • platform (str)

  • n_qubits (int)

__init__(platform, n_qubits)

Initialize the feature map class. [NOTE]: currently, only supports ‘pennylane’ platform. multi-platform support will be added in the future.

Parameters:
  • platform (str) – name of quantum platform

  • n_qubits (int) – number of qubits

Return type:

None

check_input_shape(x, idx=-1)

Check if the input data shape matches the number of qubits.

Parameters:
  • x (np.ndarray) – input data

  • idx (int, optional) – index of the dimension of qubit. Defaults to -1.

Raises:

InputShapeError – input data shape does not match the number of qubits.

Return type:

None

abstract feature_map(x)

Feature map function that maps input data to quantum states.

Parameters:

x (np.ndarray) – input data

Return type:

None

output_circuit(x=None, format='default', logger=<Logger qxmt.feature_maps.base (INFO)>, **kwargs)

Output the circuit using the platform’s draw function.

Parameters:
  • x (Optional[np.ndarray], optional) – input example data for output the circuit. Defaults to None.

  • logger (Logger, optional) – logger object. Defaults to LOGGER.

  • format (str)

  • kwargs (Any)

Raises:

NotImplementedError – not supported platform

Return type:

None