Skip to main content

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

Metadata

Release files for ncon-torch 0.3.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 ncon-torch 0.3.0
File Size Uploaded Provenance Actions
ncon_torch-0.3.0.tar.gz 9.5 kB Without provenance Details for ncon_torch-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ncon-torch 0.3.0
File Interpreter ABI Platform Actions
ncon_torch-0.3.0-py3-none-any.whl Python 3 none any Details for ncon_torch-0.3.0-py3-none-any.whl

Total release size: 17.6 kB

Release files / ncon_torch-0.3.0.tar.gz

Download URL ncon_torch-0.3.0.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3d3880c3fd3f8f5d6957dc07edddd3264bbf713cdd5cd9b817f82abe49b4a34a
BLAKE2b-256 checksum
How to use checksums
21ef3830075454b1bd4149cb6419083083c2bb9c7d39d58b7aa24cd183122151
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.12

Release files / ncon_torch-0.3.0-py3-none-any.whl

Download URL ncon_torch-0.3.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23351efd5ab29e9104ff5bed786b0deb0be168970d1318f00fa2f111c887d539
BLAKE2b-256 checksum
How to use checksums
f88d7297744189b1b90fbebaedf688fc1a385e0d4802bd2a1060e29640484fa0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.12

Security for ncon-torch 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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