Embeddings#

class tn4ml.embeddings.Embedding[source][source]

Base class for data embeddings (feature maps).

This abstract base class defines the interface for all embedding implementations. Each embedding maps input data to a higher dimensional space for tensor network operations.

dtype

Data type for computations. Defaults to float32.

Type:

numpy.dtype

__init__(dtype=<class 'numpy.float32'>)[source][source]

Initialize the embedding.

Parameters:

dtype (numpy.dtype, optional) – Data type for computations, by default float32

__new__(**kwargs)

Trigonometric#

class tn4ml.embeddings.TrigonometricEmbedding[source][source]

TrigonometricEmbedding feature map with multiple frequency components.

Maps input x to \(\phi(x) = \frac{1}{\sqrt{k}}[\cos(\frac{\pi}{2}x), \sin(\frac{\pi}{2}x), ..., \cos(\frac{\pi}{2^k}x), \sin(\frac{\pi}{2^k}x)]\)

k

Number of frequency components (dim/2)

Type:

int

__init__(k=1, **kwargs)[source][source]

Initialize the TrigonometricEmbedding.

Parameters:
  • k (int, optional) – Number of frequency components, by default 1

  • **kwargs (Any) – Additional arguments passed to parent class

Raises:

AssertionError – If k < 1

property dim: int

Get the output dimension (2k).

property input_dim: int

Get the input dimension (1 for scalar input).

Fourier#

class tn4ml.embeddings.FourierEmbedding[source][source]

Fourier feature map with multiple frequency components.

Maps input x to \(\phi(x) = \frac{1}{\sqrt{p}}[\cos(2\pi 0 x), ..., \cos(2\pi (p-1) x), \sin(2\pi 0 x), ..., \sin(2\pi (p-1) x)]\)

p

Number of frequency components

Type:

int

__init__(p=2, **kwargs)[source][source]

Initialize the Fourier embedding.

Parameters:
  • p (int, optional) – Number of frequency components, by default 2

  • **kwargs (Any) – Additional arguments passed to parent class

Raises:

AssertionError – If p < 1

property dim: int

Get the output dimension (2p).

property input_dim: int

Get the input dimension (1 for scalar input).

Linear Complement#

class tn4ml.embeddings.LinearComplementEmbedding[source][source]#

Linear complement feature map.

Maps input x to either [x, 1-x] or [1, x, 1-x] where x is in [0,1].

p#

Output dimension (2 or 3)

Type:

int

__init__(p=2, **kwargs)[source][source]#

Initialize the linear complement embedding.

Parameters:
  • p (int, optional) – Output dimension (2 or 3), by default 2

  • **kwargs (Any) – Additional arguments passed to parent class

Raises:

ValueError – If p is not 2 or 3

property dim: int#

Get the output dimension.

property input_dim: int#

Get the input dimension (1 for scalar input).

Gaussian RBF#

class tn4ml.embeddings.GaussianRBFEmbedding[source][source]#

Gaussian Radial Basis Function embedding.

Maps input x to Gaussian RBF features centered at specified points.

centers#

Centers for Gaussian RBFs

Type:

onp.ndarray

gamma#

Scaling factor \(\gamma=\frac{1}{2\sigma^2}\)

Type:

float

__init__(centers=None, gamma=None, **kwargs)[source][source]#

Initialize the Gaussian RBF embedding.

Parameters:
  • centers (Optional[onp.ndarray], optional) – Centers for Gaussian RBFs, by default None

  • gamma (Optional[float], optional) – Scaling factor, by default None

  • **kwargs (Any) – Additional arguments passed to parent class

property dim: int#

Get the output dimension (product of centers shape).

property input_dim: int#

Get the input dimension (1 for scalar input).

Polynomial#

class tn4ml.embeddings.PolynomialEmbedding[source][source]#

PolynomialEmbedding feature map.

Maps input x to PolynomialEmbedding features up to specified degree.

degree#

Maximum PolynomialEmbedding degree

Type:

int

n#

Number of input features

Type:

int

include_bias#

Whether to include constant term

Type:

bool

__init__(degree, n, include_bias=False, include_cross_terms=False, **kwargs)[source][source]#

Initialize the PolynomialEmbedding.

Parameters:
  • degree (int) – Maximum PolynomialEmbedding degree

  • n (int) – Number of input features

  • include_bias (bool, optional) – Whether to include constant term, by default False

  • **kwargs (Any) – Additional arguments passed to parent class

  • include_cross_terms (bool)

Raises:

ValueError – If degree < 1

property dim: int#

Get the output dimension based on degree, bias, and cross terms options.

property input_dim: int#

Get the input dimension.

Jax Arrays#

class tn4ml.embeddings.JaxArraysEmbedding[source][source]#

Simple embedding that converts input arrays to JAX arrays.

Optionally adds a bias term to the input.

dim#

Output dimension

Type:

Optional[int]

add_bias#

Whether to add bias term

Type:

bool

input_dim#

Input dimension

Type:

Optional[int]

__init__(dim=None, add_bias=False, input_dim=None, **kwargs)[source][source]#

Initialize the JAX arrays embedding.

Parameters:
  • dim (Optional[int], optional) – Output dimension, by default None

  • add_bias (bool, optional) – Whether to add bias term, by default False

  • input_dim (Optional[int], optional) – Input dimension, by default None

  • **kwargs (Any) – Additional arguments passed to parent class

property dim: int#

Get the output dimension.

property input_dim: int#

Get the input dimension.

Patch Amplitude Embedding#

class tn4ml.embeddings.PatchAmplitudeEmbedding[source][source]#

Embedding that converts image patches to MPS using amplitude encoding.

create_statevector(x)[source][source]#

Create statevector using amplitude encoding.

Parameters:

x (jax.numpy.ndarray) – Input patch data

Returns:

Statevector and number of qubits

Return type:

Tuple[jax.numpy.ndarray, int]

Patch Embedding#

class tn4ml.embeddings.PatchEmbedding[source][source]#

Embedding that converts image patches to MPS using basis encoding.

flatten_snake(image)[source][source]#

Flatten image in snake-like fashion.

Parameters:

image (jax.numpy.ndarray) – Input image

Returns:

Flattened image in snake-like order

Return type:

jax.numpy.ndarray

create_statevector(x)[source][source]#

Create statevector using basis encoding.

Parameters:

x (jax.numpy.ndarray) – Input patch data

Returns:

Statevector and number of qubits

Return type:

Tuple[jax.numpy.ndarray, int]

Embed Function#

tn4ml.embeddings.embed(x, phi, **mps_opts)[source][source]#

Create product state from feature vector.

Works only if features are separated and not correlated.

Parameters:
  • x (onp.ndarray) – Vector or matrix of features

  • phi (Union[Embedding, ComplexEmbedding, StateVectorToMPSEmbedding]) – Feature map for each feature

  • **mps_opts (Any) – Additional arguments passed to MatrixProductState

Returns:

Product state representation

Return type:

qtn.MatrixProductState

Raises:

TypeError – If phi is not a valid embedding type