Tensor network contraction function with GPU and autograd support via PyTorch.
Project description
ncon-torch
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.
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ncon_torch-0.2.9.tar.gz.
File metadata
- Download URL: ncon_torch-0.2.9.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76bdb0262ec480bd36c2d97ad6935a7066c9597b8800a980516449e80c991f8f
|
|
| MD5 |
333eadaa4b9e3c34d5a2dfc5a70ba4c3
|
|
| BLAKE2b-256 |
e2b2f80baccd9a4925d6b26bc0114adbcb34ae5bd4c9e9f239d379686aeee581
|
File details
Details for the file ncon_torch-0.2.9-py3-none-any.whl.
File metadata
- Download URL: ncon_torch-0.2.9-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba0f458ea9b10dec83e6f0be75ccf21fc6c5f338cfa1ca5b2f7b2dd098926b70
|
|
| MD5 |
cae3a72becf3b4f64be3b3b55b8649fd
|
|
| BLAKE2b-256 |
b05931a282e8c387b5befc00913872091c45f4020d2db583be2cce23f318ce68
|