-
Couldn't load subscription status.
- Fork 0
dev/add support for trace #68
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
156b5d0
520984c
babaef3
8d23116
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -488,6 +488,47 @@ 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}.") | ||
|
|
||
| if tensor.arrow[trace_pair[0]] != tensor.arrow[trace_pair[1]]: | ||
| tensor = tensor.reverse((trace_pair[1],)) | ||
|
|
||
| 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)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议你新实现一个只支持对最后两个指标进行trace的版本 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不然你对符号的处理完全不对,最后两个指标trace的时候,T F的话,不需要加任何符号,F T的话,需要根据最后两个指标的parity加符号(2个相互trace的指标的parity应该是完全一致的)。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 那能否通过permute来实现,将需要trace的指标移到最后,然后再按照当前的代码逻辑来求trace? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
是的,确实是应该这么干的,先实现下最后两个指标的,然后外面套个permute。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 只要你能把两个指标的trace写对,后面套permute很难写错,所以建议你先把两个指标的版本写对。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
你现在的符号没处理对啊 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()})." | ||
|
|
||
| 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
相互trace的两个指标的arrow应该是相反的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,已在8d23116中提交了修改。