# bisum
PyTorch Sparse-Tensor Partial-Trace
This program traces 2 sparse-tensor (torch.tensor objects) via 3 Tracing-Prescription:
1. {einsum} string (like numpy, str, labelling each tensor axis)
2. ncon (used in the tensor-network community, list of 1d int torch.tensor, labelling each tensor axis)
3. adjacency-matrix (as in numpy.tensordot, (2,n) 2d int torch.tensor, with n being the number of indices idenified between the two tensors)
## API
Let's begin by initializing the 2 tensors, we can initialize random-sparse-tensors
```python
from bisum.bisum import bisum
import torch
shape_A = torch.tensor([8,7,7,4,11,6])
shape_B = torch.tensor([9,7,3,7,11,8])
A = torch.rand(shape_A)
B = torch.rand(shape_B)
```
Suppose we would like to compute the following partial-trace/tensor-contraction $C_{njwl} = A_{iksndj} B_{wklsdi}$:
```python
C_einsum = bisum("iksndj, wklsdi -> njwl", A, B)
C_ncon = bisum([[-1,-2,-3,4,-5,6],[1,-2,3,-3,-5,-1]], A, B)
C_adjmat = bisum(torch.tensor([[0,1,2,4],[5,1,3,4]]), A, B)
print( torch.allclose(C_einsum, C_ncon) and torch.allclose(C_ncon, C_adjmat) )
```
while the pure tensor-product, $\otimes$ is:
```python
C_einsum = bisum("abcdef, ghijkl", A, B)
C_ncon = bisum([], A, B)
C_adjmat = bisum(torch.tensor([]), A, B)
print( np.allclose(C_einsum, C_ncon) and np.allclose(C_ncon, C_adjmat) )
```
## Install
```bash
pip install bisum
```
Raw data
{
"_id": null,
"home_page": "https://github.com/jcandane/bisum/",
"name": "bisum",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "pytorch,torch,tensors,Sparse Tensor,Sparse,contraction,partial-tracing,einsum,tensordot,attention",
"author": "Julio Candanedo",
"author_email": "juliojcandanedo@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/08/b6/6e4e33a211efd7bb16463855bb56d3f1f7e89c750142ad23d37eb10c39f0/bisum-0.1.1.tar.gz",
"platform": null,
"description": "# bisum\n\nPyTorch Sparse-Tensor Partial-Trace\nThis program traces 2 sparse-tensor (torch.tensor objects) via 3 Tracing-Prescription:\n1. {einsum} string (like numpy, str, labelling each tensor axis)\n2. ncon (used in the tensor-network community, list of 1d int torch.tensor, labelling each tensor axis)\n3. adjacency-matrix (as in numpy.tensordot, (2,n) 2d int torch.tensor, with n being the number of indices idenified between the two tensors)\n\n## API\n\nLet's begin by initializing the 2 tensors, we can initialize random-sparse-tensors \n```python\nfrom bisum.bisum import bisum\nimport torch\n\nshape_A = torch.tensor([8,7,7,4,11,6])\nshape_B = torch.tensor([9,7,3,7,11,8])\nA = torch.rand(shape_A)\nB = torch.rand(shape_B)\n```\n\nSuppose we would like to compute the following partial-trace/tensor-contraction $C_{njwl} = A_{iksndj} B_{wklsdi}$:\n```python\nC_einsum = bisum(\"iksndj, wklsdi -> njwl\", A, B)\nC_ncon = bisum([[-1,-2,-3,4,-5,6],[1,-2,3,-3,-5,-1]], A, B)\nC_adjmat = bisum(torch.tensor([[0,1,2,4],[5,1,3,4]]), A, B)\n\nprint( torch.allclose(C_einsum, C_ncon) and torch.allclose(C_ncon, C_adjmat) )\n```\nwhile the pure tensor-product, $\\otimes$ is:\n```python\nC_einsum = bisum(\"abcdef, ghijkl\", A, B)\nC_ncon = bisum([], A, B)\nC_adjmat = bisum(torch.tensor([]), A, B)\n\nprint( np.allclose(C_einsum, C_ncon) and np.allclose(C_ncon, C_adjmat) )\n```\n\n## Install\n\n```bash\npip install bisum\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "binary sparse and dense tensor partial-tracing",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/jcandane/bisum/"
},
"split_keywords": [
"pytorch",
"torch",
"tensors",
"sparse tensor",
"sparse",
"contraction",
"partial-tracing",
"einsum",
"tensordot",
"attention"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c5059153dd0121c54fdf6ab156386c278e832331dad78fa2c024e49e25c0d4ef",
"md5": "c8f221f3b76435fa203e7238772760b8",
"sha256": "9d514726d026b9e86c799ab53bedd7629dbfb7c8bd1e40e3d513c14eedad5677"
},
"downloads": -1,
"filename": "bisum-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c8f221f3b76435fa203e7238772760b8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 13300,
"upload_time": "2023-07-27T00:31:49",
"upload_time_iso_8601": "2023-07-27T00:31:49.480205Z",
"url": "https://files.pythonhosted.org/packages/c5/05/9153dd0121c54fdf6ab156386c278e832331dad78fa2c024e49e25c0d4ef/bisum-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "08b66e4e33a211efd7bb16463855bb56d3f1f7e89c750142ad23d37eb10c39f0",
"md5": "3d4b23737c551f631b39c079a84f07ef",
"sha256": "2bbb978500f76dada72e94ec25a73e9ce1eb966dfdfee9a8a65fea533aeb01ca"
},
"downloads": -1,
"filename": "bisum-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "3d4b23737c551f631b39c079a84f07ef",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11875,
"upload_time": "2023-07-27T00:31:51",
"upload_time_iso_8601": "2023-07-27T00:31:51.152231Z",
"url": "https://files.pythonhosted.org/packages/08/b6/6e4e33a211efd7bb16463855bb56d3f1f7e89c750142ad23d37eb10c39f0/bisum-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-27 00:31:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jcandane",
"github_project": "bisum",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "bisum"
}