Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract (Dense, diag) #18

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/cytnx_torch/linalg/matmul_dg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import torch


def _tensordot_dg(
A: torch.Tensor,
B: torch.Tensor,
is_diag_left: bool = False,
is_diag_right: bool = False,
):
"""
Matrix multiplication of two tensors A and B.

Args:
A (torch.Tensor): the first tensor
B (torch.Tensor): the second tensor

Returns:
torch.Tensor: the result of the matrix multiplication
"""

if is_diag_left and is_diag_right:
return A * B

if is_diag_left:
# check shape:
pass
58 changes: 55 additions & 3 deletions src/cytnx_torch/unitensor/regular_unitensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from dataclasses import dataclass, field
from beartype.typing import List, Optional, Union, Tuple
import torch
from dataclasses import dataclass, field
from numbers import Number
import string
import torch
import numpy as np
from ..bond import Bond, BondType
from ..converter import RegularUniTensorConverter
Expand Down Expand Up @@ -294,7 +295,58 @@ def contract(
) -> "RegularUniTensor":
match rhs:
case RegularUniTensor():
raise NotImplementedError("TODO")
if self.is_diag or rhs.is_diag:
# TODO
raise ValueError(
"contract with diagonal tensor is under developement."
)

# TODO optimize this:
self_lbls = set(self.labels)
rhs_lbls = set(rhs.labels)

# get common labels:
unique_labels = self_lbls | rhs_lbls
contracted_labels = self_lbls & rhs_lbls
mapper = {lbl: s for s, lbl in zip(string.ascii_letters, unique_labels)}

lhs_str = "".join([mapper[lbl] for lbl in self.labels])
rhs_str = "".join([mapper[lbl] for lbl in rhs.labels])

# keep the order of labels:
lhs_remain_idx = [
i
for i, lbl in enumerate(self.labels)
if lbl not in contracted_labels
]
rhs_remain_idx = [
i
for i, lbl in enumerate(rhs.labels)
if lbl not in contracted_labels
]

lhs_remain_str = "".join(
[mapper[self.labels[i]] for i in lhs_remain_idx]
)
rhs_remain_str = "".join(
[mapper[rhs.labels[i]] for i in rhs_remain_idx]
)
res_str = f"{lhs_remain_str}{rhs_remain_str}"

new_data = torch.einsum(
f"{lhs_str},{rhs_str}->{res_str}", self.data, rhs.data
)

# construct new ut:
return RegularUniTensor(
labels=[self.labels[i] for i in lhs_remain_idx]
+ [rhs.labels[i] for i in rhs_remain_idx],
bonds=[self.bonds[i] for i in lhs_remain_idx]
+ [rhs.bonds[i] for i in rhs_remain_idx],
backend_args=self.backend_args,
data=new_data,
)

case RegularUniTensorConverter():
return rhs._contract(is_lhs=False, utensor=self)
case _:
Expand Down