Cytnx
Cytnx copied to clipboard
Index order after .Contract and .Contracts
I am trying to use .Contract and .Contracts to check the results after SVD. After the SVD, the labels in S, U, Vdag are such that they can be directly used in .Contract and .Contracts.
# Create a rank-3 tensor with shape [2,2,6]
M = cytnx.UniTensor.arange(2*2*6).reshape(2,2,6)\
.relabels(["a","b","c"]).set_name("M")
# 1. Permute the indices to ("c","a","b") ordering,
M.permute_(["c","a","b"])
# 2. Set rowrank=1, so that "c" is the row index,
# and "a", "b" are the column indices.
M.set_rowrank_(1)
M.print_diagram()
-----------------------
tensor Name : M
tensor Rank : 3
block_form : False
is_diag : False
on device : cytnx device: CPU
---------
/ \
c ____| 6 2 |____ a
| |
| 2 |____ b
\ /
---------
S, U, Vdag = cytnx.linalg.Svd (M)
U.print_diagram()
S.print_diagram()
Vdag.print_diagram()
-----------------------
tensor Name :
tensor Rank : 2
block_form : False
is_diag : False
on device : cytnx device: CPU
---------
/ \
c ____| 6 4 |____ _aux_L
\ /
---------
-----------------------
tensor Name :
tensor Rank : 2
block_form : False
is_diag : True
on device : cytnx device: CPU
---------
/ \
_aux_L ____| 4 4 |____ _aux_R
\ /
---------
-----------------------
tensor Name :
tensor Rank : 3
block_form : False
is_diag : False
on device : cytnx device: CPU
---------
/ \
_aux_R ____| 4 2 |____ a
| |
| 2 |____ b
\ /
---------
It I use .Contract, it works
US =cytnx.Contract(U,S)
US.print_diagram()
USVdag =cytnx.Contract(M1, Vdag)
USVdag.print_diagram()
Diff = M-USVdag
print(Diff.Norm())
-----------------------
tensor Name :
tensor Rank : 2
block_form : False
is_diag : False
on device : cytnx device: CPU
---------
/ \
c ____| 6 4 |____ _aux_R
\ /
---------
-----------------------
tensor Name :
tensor Rank : 3
block_form : False
is_diag : False
on device : cytnx device: CPU
---------
/ \
c ____| 6 2 |____ a
| |
| 2 |____ b
\ /
---------
Total elem: 1
type : Double (Float64)
cytnx device: CPU
Shape : (1)
[2.70749e-14 ]
But if I use .Contracts, the order of the label is not the same as the original UniTensor M
USVdag =cytnx.Contracts([U,S,Vdag])
USVdag.print_diagram()
-----------------------
tensor Name :
tensor Rank : 3
block_form : False
is_diag : False
on device : cytnx device: CPU
---------
/ \
a ____| 2 2 |____ b
| |
| 6 |____ c
\ /
---------
So I would have to premute_ if I want to calculate the difference.
Generally, you should always do permute to make sure two tensors you want to compare has the same order of legs( this is so that you can explicitly know what you are comparing)
permute is O(1), so there is no need to worry about adding an extra line!