Skip to main content
logo

Tensor Networks for Machine Learning

CI Pre-merge checks Docs
tn4ml is a Python library that handles tensor networks for machine learning applications.
It is built on top of Quimb, for Tensor Network objects, and JAX, for optimization pipeline.
For now, the library supports 1D Tensor Network structures:

  • Matrix Product State
  • Matrix Product Operator
  • Spaced Matrix Product Operator

It supports different embedding functions, initialization techniques, objective functions and optimization strategies.

Installation

First create a virtualenv using pyenv or conda. Then install the package and its dependencies.

With pip (tag v1.1.0):

pip install tn4ml

or directly from github:

pip install -U git+https://github.com/bsc-quantic/tn4ml.git

If you want to test and edit the code, you can clone the local version of the package and install it.

git clone https://github.com/bsc-quantic/tn4ml.git
pip install -e tn4ml/

If you want to install dependices for docs, test and examples:

pip install "tn4ml[docs]"
pip install "tn4ml[test]"
pip install "tn4ml[examples]"

Developer checks

Install the pre-commit hooks before contributing:

pip install pre-commit
pre-commit install

Run all hooks manually:

pre-commit run --all-files

The hooks run fast local checks from CI/CD (ruff, mypy, bandit) and strip notebook outputs/local metadata with nbstripout, including notebook kernel display names such as local environment names.

Accelerated runtime

(Optional) To improve runtime precision set these flags:

jax.config.update("jax_enable_x64", True)
jax.config.update('jax_default_matmul_precision', 'highest')

Running on GPU
Before everything install JAX version that supports CUDA and its suitable for runs on GPU.
Checkout how to install here: jax[cuda]

Next, at the beginning of your script set:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"  # Use GPU 0 - or set any GPU ID
import jax
jax.config.update("jax_platform_name", 'gpu')

Then when training Model set:

device = 'gpu'
model.configure(device=device)

Documentation

Visit tn4ml.readthedocs.io

Example notebooks

TN for Classification
TN for Anomaly Detection
TN for Anomaly Detection with DMRG-like method

Examples from the paper

Breast Cancer Classification
Unsupervised Learning with MNIST
MPS for Anomaly Detection in the Latent Space of Proton Collision Events at the LHC

Citation

If you use tn4ml in your work, please cite the following paper: arXiv:2502.13090

@article{puljak2025tn4mltensornetworktraining,
      title={tn4ml: Tensor Network Training and Customization for Machine Learning},
      author={Ema Puljak and Sergio Sanchez-Ramirez and Sergi Masot-Llima and Jofre Vallès-Muns and Artur Garcia-Saez and Maurizio Pierini},
      year={2025},
      eprint={2502.13090},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2502.13090},
      }

License

MIT license - check it out here

Metadata

Release files for tn4ml 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tn4ml 1.1.0
File Size Uploaded
tn4ml-1.1.0.tar.gz 63.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tn4ml 1.1.0
File Interpreter ABI Platform
tn4ml-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 135.7 kB

Release files / tn4ml-1.1.0.tar.gz

Download URL tn4ml-1.1.0.tar.gz
Size 63.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ccbbafcd1a14f663d2daa9337c7febcd9944486ef9af45d9a245da2bc7e93d10
BLAKE2b-256 checksum
How to use checksums
1dc3a2dde5ef161164dd768ae40c440b502e2f3aedbdcb9d1f60b0c9f5b17e71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / tn4ml-1.1.0-py3-none-any.whl

Download URL tn4ml-1.1.0-py3-none-any.whl
Size 72.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f998a9d7cbaeeb196d7f6ca3a1f86ec26d8ae37409fbac09f6f4996569f1735
BLAKE2b-256 checksum
How to use checksums
1a0783d1982bad17697b8a3f8c2cad6e2f299837abc0efbbacd0bb7b4f779cc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page