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.3.0.tar.gz (9.5 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.3.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ncon_torch-0.3.0.tar.gz
  • Upload date:
  • Size: 9.5 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.3.0.tar.gz
Algorithm Hash digest
SHA256 3d3880c3fd3f8f5d6957dc07edddd3264bbf713cdd5cd9b817f82abe49b4a34a
MD5 316318ca9fb2d70c9c92f7ffdb80de19
BLAKE2b-256 21ef3830075454b1bd4149cb6419083083c2bb9c7d39d58b7aa24cd183122151

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ncon_torch-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23351efd5ab29e9104ff5bed786b0deb0be168970d1318f00fa2f111c887d539
MD5 05cb83c0e1a9e21fc4d9dc6c3561b856
BLAKE2b-256 f88d7297744189b1b90fbebaedf688fc1a385e0d4802bd2a1060e29640484fa0

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