Skip to main content

A Python package for Matrix Product Operator (MPO) decomposition

Project description

Matrix2MPO Plus

A Python package for Matrix Product Operator (MPO) decomposition using SVD decomposition and various canonical forms for efficient tensor network representations.

Features

  • MPO Decomposition: Convert matrices to Matrix Product Operator format
  • SVD-based Decomposition: Uses Singular Value Decomposition for tensor decomposition
  • Accelerated SVD: Includes optimized SVD module for faster computation
  • Canonical Forms: Support for left canonical, right canonical, and mixed canonical forms
  • Truncation: Built-in support for rank truncation to control compression
  • PyTorch Integration: Seamless integration with PyTorch tensors
  • Flexible Configuration: Configurable input/output shapes and truncation parameters
  • Fallback Support: Automatic fallback to numpy.linalg.svd if accelerated module is unavailable

Installation

You can install Matrix2MPO Plus using pip:

pip install matrix2mpo-plus

Quick Start

import numpy as np
import torch
from matrix2mpo_plus import MPO

# Create a sample matrix
matrix = np.random.rand(64, 64)

# Define MPO input and output shapes
mpo_input_shape = [8, 8]  # 8 * 8 = 64
mpo_output_shape = [8, 8]  # 8 * 8 = 64

# Create MPO instance
mpo = MPO(
    mpo_input_shape=mpo_input_shape,
    mpo_output_shape=mpo_output_shape,
    truncate_num=16  # Maximum bond dimension
)

# Convert matrix to MPO format
tensor_set, lambda_set, lambda_set_value = mpo.matrix2mpo(matrix)

# Convert back to matrix
reconstructed_matrix = mpo.mpo2matrix(tensor_set)

# Calculate compression ratio
original_params = matrix.size
mpo_params = mpo.calculate_total_mpo_param()
compression_ratio = original_params / mpo_params

print(f"Original parameters: {original_params}")
print(f"MPO parameters: {mpo_params}")
print(f"Compression ratio: {compression_ratio:.2f}x")

API Reference

MPO Class

The main class for MPO decomposition operations.

Parameters

  • mpo_input_shape (list): Input dimensions for MPO decomposition
  • mpo_output_shape (list): Output dimensions for MPO decomposition
  • truncate_num (int): Maximum bond dimension for truncation
  • fix_rank (list, optional): Fixed rank for each bond dimension

Methods

  • matrix2mpo(matrix, cutoff=True): Convert matrix to MPO format
  • mpo2matrix(tensor_set): Convert MPO format back to matrix
  • calculate_total_mpo_param(cutoff=True): Calculate total number of parameters
  • test_difference(matrix1, matrix2): Calculate difference between two matrices

Power Iteration SVD

from matrix2mpo_plus import power_iteration_svd

# Compute SVD using power iteration
U, S, Vt = power_iteration_svd(matrix, k=10, max_iter=100)

Requirements

  • Python >= 3.7
  • NumPy >= 1.19.0
  • PyTorch >= 1.8.0

Performance

This package includes an optimized SVD module (svd_module) that provides faster computation compared to standard numpy.linalg.svd. The package automatically detects and uses the accelerated module when available, with graceful fallback to numpy's implementation if needed.

Development

To run tests:

pip install -e .[dev]
pytest

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Citation

If you use this package in your research, please cite:

@inproceedings{Liu-ACL-2021,
  author    = {Peiyu Liu and
               Ze{-}Feng Gao and
               Wayne Xin Zhao and
               Z. Y. Xie and
               Zhong{-}Yi Lu and
               Ji{-}Rong Wen},
  title     = "Enabling Lightweight Fine-tuning for Pre-trained Language Model Compression
               based on Matrix Product Operators",
  booktitle = {{ACL}},
  year      = {2021},
}

Support

For questions and support, please contact liupeiyustu@163.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matrix2mpo_plus-1.0.0.tar.gz (180.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matrix2mpo_plus-1.0.0-py3-none-any.whl (179.4 kB view details)

Uploaded Python 3

File details

Details for the file matrix2mpo_plus-1.0.0.tar.gz.

File metadata

  • Download URL: matrix2mpo_plus-1.0.0.tar.gz
  • Upload date:
  • Size: 180.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for matrix2mpo_plus-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6efb3b3cd147536406a76f80e0e3b37a706b85273149dc9343abad25cd24cb56
MD5 efd1312dfd6c6ec5a8475079ec740826
BLAKE2b-256 e4093aece1bac79cecfcc67f07db8ed097813237a38a61c2ff0ab155973d07b7

See more details on using hashes here.

File details

Details for the file matrix2mpo_plus-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: matrix2mpo_plus-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 179.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for matrix2mpo_plus-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aca4f3cf5257c40fa3a7b6ed17e8575b1e3e0b5bf1367aead6ea7d4d2096e591
MD5 66682dbad76335edc9a0c48b44e16bc1
BLAKE2b-256 7831e487a79b0bb8f2cbae073a8858cf5eaf8f7ade3e0473033b4f1ab013bfb5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page