Skip to main content

Tensor network contraction function with GPU and autograd support via PyTorch.

Project description

ncon-torch

PyPI version

ncon-torch is a fork of the ncon package, modified to include GPU and autograd support via PyTorch

Installation

pip install ncon-torch

Usage

See original package repo for examples.

GPU Benchmark

Below we compare NumPy and PyTorch based contractions of a two-qubit gate with an n-qubit state. The benchmark was done on Google Colab with a T4 GPU.

Benchmark: NumPy vs PyTorch

Automatic differentiation

ncon-torch is compatible with PyTorch's reverse-mode automatic differentiation (autograd). Any contraction involving tensors with requires_grad=True will propagate gradients as expected.

Minimal Example

import torch
from ncon_torch import ncon

A = torch.randn(2, 2, dtype=torch.float64, requires_grad=True)
B = torch.tensor([[1.0, 0.0], [0.0, -1.0]], dtype=torch.float64)

out = ncon([A, B], [[1, -1], [1, -2]])
loss = torch.sum(out**2)
loss.backward()

print("Gradient:", A.grad) 

To validate the efficiency of reverse-mode AD, we benchmarked the time to compute: • The cost function (a scalar-valued tensor contraction) • Gradients via reverse-mode AD (.backward()) • Gradients via finite-difference approximation (scipy.optimize.approx_fprime)

Despite increasing parameter count, reverse-mode AD maintains near-constant overhead relative to the forward cost, unlike finite differences which scale poorly.

Benchmark: reverse mode AD vs finite differences

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

ncon_torch-0.2.8.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

ncon_torch-0.2.8-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file ncon_torch-0.2.8.tar.gz.

File metadata

  • Download URL: ncon_torch-0.2.8.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for ncon_torch-0.2.8.tar.gz
Algorithm Hash digest
SHA256 0d33cbc8c34b68327e117437596b1ba41ff5552cc35b44292c5cd41e48345744
MD5 cf093068cbd808761eecd126cfc82bfa
BLAKE2b-256 2b0f4ce260d9ee4726dec7264b15fdd3a045ca65faa824e186400d34a620646e

See more details on using hashes here.

File details

Details for the file ncon_torch-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: ncon_torch-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for ncon_torch-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 008afa08fbdf054a708550f1d3bc324f09ff4773ea90895f293f6b9c6a514eac
MD5 722241ff1514b362828adf5f52041da4
BLAKE2b-256 2d7c5d11f5c403c6a2d872a098cefcf25ee05bfb77de9baf466cd09e1a435479

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