Skip to content
Open
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
38 changes: 38 additions & 0 deletions grassmann_tensor/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,44 @@ def matmul(self, other: GrassmannTensor) -> GrassmannTensor:
_tensor=tensor,
)

def trace(self, trace_pair: tuple[int, int]) -> torch.Tensor:
assert len(trace_pair) == 2, (
f"The length of trace pair must be 2, but got {len(trace_pair)}."
)

assert trace_pair[0] != trace_pair[1], (
f"Trace requires two distinct axes but got {trace_pair[0]} and {trace_pair[1]}."
)

assert self.arrow[trace_pair[0]] == self.arrow[trace_pair[1]], (
f"Trace requires two different arrows but got {self.arrow[trace_pair[0]]} and {self.arrow[trace_pair[1]]}."
)

tensor = self

if trace_pair[0] > trace_pair[1]:
trace_pair = trace_pair[::-1]

edge_first, edge_end = self.edges[trace_pair[0]], self.edges[trace_pair[1]]
if edge_first != edge_end:
raise ValueError(f"Incompatible edges: {edge_first} and {edge_end}.")

order = list(range(tensor.tensor.dim()))
order_first = order.pop(trace_pair[0])
order_end = order.pop(trace_pair[1])
order[trace_pair[0] : trace_pair[0]] = [order_first, order_end]
tensor = tensor.permute(tuple(order))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议你新实现一个只支持对最后两个指标进行trace的版本

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不然你对符号的处理完全不对,最后两个指标trace的时候,T F的话,不需要加任何符号,F T的话,需要根据最后两个指标的parity加符号(2个相互trace的指标的parity应该是完全一致的)。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那能否通过permute来实现,将需要trace的指标移到最后,然后再按照当前的代码逻辑来求trace?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那能否通过permute来实现,将需要trace的指标移到最后,然后再按照当前的代码逻辑来求trace?

是的,确实是应该这么干的,先实现下最后两个指标的,然后外面套个permute。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只要你能把两个指标的trace写对,后面套permute很难写错,所以建议你先把两个指标的版本写对。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

然后再按照当前的代码逻辑来求trace

你现在的符号没处理对啊

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是没处理对符号,是没处理符号=_=、、、你只需要permute成T F的话,符号才可以恰好不用处理。


shape = tensor.tensor.shape[0] + tensor.tensor.shape[1]
tensor.reshape(
(
shape,
*(-1 for _ in range(tensor.tensor.dim() - 2)),
)
)

return tensor.tensor[0].trace()

def __post_init__(self) -> None:
assert len(self._arrow) == self._tensor.dim(), (
f"Arrow length ({len(self._arrow)}) must match tensor dimensions ({self._tensor.dim()})."
Expand Down
11 changes: 11 additions & 0 deletions tests/trace_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import torch
from grassmann_tensor.tensor import GrassmannTensor


def test_trace() -> None:
gt = GrassmannTensor(
(False, False, False),
((1, 1), (1, 1), (1, 1)),
torch.tensor([[[1, 0], [0, 2]], [[0, 3], [4, 0]]]),
)
gt.trace((0, 1))