Cytnx icon indicating copy to clipboard operation
Cytnx copied to clipboard

HOTRG sample code: Construction of initial T-tensor

Open pcchen opened this issue 2 years ago β€’ 4 comments

Here I provide sample code to construct the initial T-tensor for HOTRG calculation. I try to provide sample code with various combination of following configurations. The final T tensor should be the same.

One can do the calculation in

  • Non-symmetric basis
  • Symmetric basis Two basis are related by a unitary transformation (gauge degree of freedom).

One can use

  • non-symmetric tensor
  • symmetric tensor

One can

  • loop over labels to set elements
  • use network contraction

pcchen avatar Apr 28 '23 07:04 pcchen

Non-Symmetric basis, Direct construction of non-symmetri T tensor

import numpy as np
import cytnx
𝛽 = 1

W = np.array([[np.exp(+𝛽),np.exp(-𝛽)],
              [np.exp(-𝛽),np.exp(+𝛽)]])
M = np.array([[+np.sqrt(np.cosh(𝛽)), +np.sqrt(np.sinh(𝛽))],
              [+np.sqrt(np.cosh(𝛽)), -np.sqrt(np.sinh(𝛽))]])
# print(W - M @ M.transpose())
bd = cytnx.Bond(2)
T = cytnx.UniTensor([bd, bd, bd, bd],rowrank=2)
T.set_name('T')
T.set_labels(['u','l','r','d'])
T.print_diagram()
for i in range(2):
    for j in range(2):
        for k in range(2):
            for l in range(2):
                T.set_elem([i,j,k,l], (M[0,i]*M[0,j]*M[0,k]*M[0,l]+M[1,i]*M[1,j]*M[1,k]*M[1,l]))
print(T.get_block().reshape(4,4) )

-----------------------
tensor Name : T
tensor Rank : 4
block_form  : false
is_diag     : False
on device   : cytnx device: CPU
          ---------     
         /         \    
   u ____| 2     2 |____ r
         |         |    
   l ____| 2     2 |____ d
         \         /    
          ---------     

Total elem: 16
type  : Double (Float64)
cytnx device: CPU
Shape : (4,4)
[[4.76220e+00 0.00000e+00 0.00000e+00 3.62686e+00 ]
 [0.00000e+00 3.62686e+00 3.62686e+00 0.00000e+00 ]
 [0.00000e+00 3.62686e+00 3.62686e+00 0.00000e+00 ]
 [3.62686e+00 0.00000e+00 0.00000e+00 2.76220e+00 ]]

pcchen avatar Apr 28 '23 07:04 pcchen

Non-Symmetric basis, Network construction of non-symmetric T tensor

import numpy as np
import cytnx
𝛽 = 1
M = np.array([[+np.sqrt(np.cosh(𝛽)), +np.sqrt(np.sinh(𝛽))],
              [+np.sqrt(np.cosh(𝛽)), -np.sqrt(np.sinh(𝛽))]])
M = cytnx.UniTensor(cytnx.from_numpy(M), rowrank=1)
delta = cytnx.zeros([2,2,2,2])
delta[0,0,0,0]= 1.0
delta[1,1,1,1]= 1.0
delta = cytnx.UniTensor(delta, rowrank=2)
delta.set_name('ẟ')
# print(delta.get_block().reshape(4,4))

N= cytnx.Network()
N.FromString([
    "C: 100,101,102,103",\
    "U: u,100",\
    "L: l,101",\
    "R: 102,r",\
    "D: 103,d",\
    "TOUT: u,l;r,d",\
    "ORDER:"\
    ])
N.PutUniTensors(["C", "U", "L", "R", "D"],
                [delta, M.Dagger(), M.Dagger(), M, M])
T = N.Launch(optimal=True)
T.set_name('T')
T.print_diagram()
print(T.get_block().reshape(4,4))

-----------------------
tensor Name : T
tensor Rank : 4
block_form  : false
is_diag     : False
on device   : cytnx device: CPU
          ---------     
         /         \    
   u ____| 2     2 |____ r
         |         |    
   l ____| 2     2 |____ d
         \         /    
          ---------     

Total elem: 16
type  : Double (Float64)
cytnx device: CPU
Shape : (4,4)
[[4.76220e+00 3.71339e-17 3.71339e-17 3.62686e+00 ]
 [3.71339e-17 3.62686e+00 3.62686e+00 -8.43155e-17 ]
 [-2.38499e-17 3.62686e+00 3.62686e+00 -6.10135e-18 ]
 [3.62686e+00 -6.10135e-18 -6.10135e-18 2.76220e+00 ]]

pcchen avatar Apr 28 '23 07:04 pcchen

Symmetric basis, Network construction of non-symmetric T tensor

import numpy as np
import cytnx
𝛽 = 1
bd = cytnx.Bond(2)
delta_sym = cytnx.UniTensor([bd, bd, bd, bd],rowrank=2)
delta_sym.set_name('delta_sym')
delta_sym.set_labels(['u','l','r','d'])
# delta_sym.print_diagram()
for i in range(2):
    for j in range(2):
        for k in range(2):
            for l in range(2):
                if np.mod(i+j-k-l,2) == 0:
                    delta_sym.set_elem([i,j,k,l], 1.0/2)
# print(delta_sym.get_block().reshape(4,4) )
M = np.array([[+np.sqrt(2*np.cosh(𝛽)), 0],
              [0, +np.sqrt(2*np.sinh(𝛽))]])
M = cytnx.UniTensor(cytnx.from_numpy(M), rowrank=1)
N= cytnx.Network()
N.FromString([
    "C: 100,101,102,103",\
    "U: u,100",\
    "L: l,101",\
    "R: 102,r",\
    "D: 103,d",\
    "TOUT: u,l;r,d",\
    "ORDER:"\
    ])
N.PutUniTensors(["C", "U", "L", "R", "D"],
                [delta_sym, M, M, M, M])
T_sym = N.Launch(optimal=True)
T.set_name('T')
T_sym.print_diagram()
print(T_sym.get_block().reshape(4,4) )

-----------------------
tensor Name : 
tensor Rank : 4
block_form  : false
is_diag     : False
on device   : cytnx device: CPU
          ---------     
         /         \    
   u ____| 2     2 |____ r
         |         |    
   l ____| 2     2 |____ d
         \         /    
          ---------     

Total elem: 16
type  : Double (Float64)
cytnx device: CPU
Shape : (4,4)
[[4.76220e+00 0.00000e+00 0.00000e+00 3.62686e+00 ]
 [0.00000e+00 3.62686e+00 3.62686e+00 0.00000e+00 ]
 [0.00000e+00 3.62686e+00 3.62686e+00 0.00000e+00 ]
 [3.62686e+00 0.00000e+00 0.00000e+00 2.76220e+00 ]]

pcchen avatar Apr 28 '23 07:04 pcchen

Symmetric basis, Network construction of Symmetric T tensor

import numpy as np
import cytnx
𝛽 = 1
bd_u = cytnx.Bond(cytnx.BD_IN, [cytnx.Qs(0)>>1, cytnx.Qs(1)>>1],[cytnx.Symmetry.Zn(2)])
bd_l = cytnx.Bond(cytnx.BD_IN, [cytnx.Qs(0)>>1, cytnx.Qs(1)>>1],[cytnx.Symmetry.Zn(2)])
bd_r = cytnx.Bond(cytnx.BD_OUT, [cytnx.Qs(0)>>1, cytnx.Qs(1)>>1],[cytnx.Symmetry.Zn(2)])
bd_d = cytnx.Bond(cytnx.BD_OUT, [cytnx.Qs(0)>>1, cytnx.Qs(1)>>1],[cytnx.Symmetry.Zn(2)])

delta_sym = cytnx.UniTensor([bd_u, bd_l, bd_r, bd_d])
delta_sym.set_name('T_sym')
delta_sym.set_labels(['u','l','r','d'])
# delta_sym.print_diagram()

for u in range(2):
    for l in range(2):
        for r in range(2):
            for d in range(2):
                if delta_sym.elem_exists([u,l,r,d]):
                    delta_sym.set_elem([u,l,r,d], 0.5) 
bd_i = cytnx.Bond(cytnx.BD_IN, [cytnx.Qs(0)>>1, cytnx.Qs(1)>>1],[cytnx.Symmetry.Zn(2)])
bd_o = cytnx.Bond(cytnx.BD_OUT, [cytnx.Qs(0)>>1, cytnx.Qs(1)>>1],[cytnx.Symmetry.Zn(2)])
M = cytnx.UniTensor([bd_i, bd_o])
M.set_elem([0,0], +np.sqrt(2*np.cosh(𝛽))) 
M.set_elem([1,1], +np.sqrt(2*np.sinh(𝛽))) 
N= cytnx.Network()
N.FromString([
    "C: 100,101,102,103",\
    "U: u,100",\
    "L: l,101",\
    "R: 102,r",\
    "D: 103,d",\
    "TOUT: u,l;r,d",\
    "ORDER:"\
    ])
N.PutUniTensors(["C", "U", "L", "R", "D"],
                [delta_sym, M, M, M, M])
T_sym = N.Launch(optimal=True)
T.set_name('T')
T_sym.print_diagram()
XT_sym = T_sym.clone()
XT_sym.combineBonds(['u','l'])
XT_sym.combineBonds(['r','d'])
XT_sym.set_rowrank(1)
print(XT_sym.get_block(0))
print(XT_sym.get_block(1))

-----------------------
tensor Name : 
tensor Rank : 4
contiguous  : True
valid blocks : 8
is diag   : False
on device   : cytnx device: CPU
      row           col 
         -----------    
         |         |    
   u  -->| 2     2 |-->  r
         |         |    
   l  -->| 2     2 |-->  d
         |         |    
         -----------    


Total elem: 4
type  : Double (Float64)
cytnx device: CPU
Shape : (2,2)
[[4.76220e+00 3.62686e+00 ]
 [3.62686e+00 2.76220e+00 ]]




Total elem: 4
type  : Double (Float64)
cytnx device: CPU
Shape : (2,2)
[[3.62686e+00 3.62686e+00 ]
 [3.62686e+00 3.62686e+00 ]]

pcchen avatar Apr 28 '23 07:04 pcchen