Skip to content

Commit fcbb71e

Browse files
committed
use .set_rowrank_() instead of initializer argument (rowrank=...)
method chaining is the preferred way to initialize tensors. The initializer argument is not always available, which caused the code with UniTensor.zeros(..., rowrank=...) to crash See #335
1 parent dec28fa commit fcbb71e

File tree

8 files changed

+77
-73
lines changed

8 files changed

+77
-73
lines changed

docs/code/python/outputs/guide_uniten_manipulation_Transpose_tagged.out

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ contiguous : True
55
valid blocks: 6
66
is diag : False
77
on device : cytnx device: CPU
8-
row col
9-
-----------
10-
| |
8+
row col
9+
-----------
10+
| |
1111
a -->| 2 4 |--> c
12-
| |
13-
b -->| 2 |
14-
| |
15-
-----------
12+
| |
13+
b -->| 2 |
14+
| |
15+
-----------
1616

1717
Rowrank of T = 2
1818
-----------------------
@@ -22,13 +22,13 @@ contiguous : False
2222
valid blocks: 6
2323
is diag : False
2424
on device : cytnx device: CPU
25-
row col
26-
-----------
27-
| |
25+
row col
26+
-----------
27+
| |
2828
c -->| 4 2 |--> a
29-
| |
29+
| |
3030
| 2 |--> b
31-
| |
32-
-----------
31+
| |
32+
-----------
3333

3434
Rowrank of transposed T = 1

docs/source/example/DMRG.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ Now, let's first prepare the MPO, **M**. Here, the **d** is the physical bond di
105105
M[0,2] = M[1,3] = 2**0.5*sm.real()
106106
M = cytnx.UniTensor(M,0)
107107
108-
L0 = cytnx.UniTensor.zeros([4,1,1], rowrank = 0) #Left boundary
109-
R0 = cytnx.UniTensor.zeros([4,1,1], rowrank = 0) #Right boundary
108+
L0 = cytnx.UniTensor.zeros([4,1,1]).set_rowrank_(0) #Left boundary
109+
R0 = cytnx.UniTensor.zeros([4,1,1]).set_rowrank_(0) #Right boundary
110110
L0[0,0,0] = 1.; R0[3,0,0] = 1.
111111
112112
.. Note::
@@ -201,7 +201,7 @@ Next, we are going to prepare our variational ansatz (MPS). Here, **chi** is the
201201
for k in range(1,Nsites):
202202
dim1 = A[k-1].shape()[2]; dim2 = d
203203
dim3 = min(min(chi, A[k-1].shape()[2] * d), d ** (Nsites - k - 1))
204-
A[k] = cytnx.UniTensor.normal([dim1, dim2, dim3],0.,1., rowrank = 2)
204+
A[k] = cytnx.UniTensor.normal([dim1, dim2, dim3],0.,1.).set_rowrank_(2)
205205
206206
lbl = [str(2*k),str(2*k+1),str(2*k+2)]
207207
A[k].relabel_(lbl)

example/DMRG/dmrg_two_sites_U1.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,28 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
4848
bd_inner = cytnx.Bond(cytnx.BD_KET,[[0],[-2],[2],[0]],[1,1,1,1])
4949
bd_phys = cytnx.Bond(cytnx.BD_KET,[[1],[-1]],[1,1])
5050

51-
M = cytnx.UniTensor([bd_inner,bd_inner.redirect(),bd_phys, bd_phys.redirect()],rowrank=2)
51+
M = cytnx.UniTensor([bd_inner,bd_inner.redirect(),bd_phys, bd_phys.redirect()]).set_rowrank_(2)
5252

5353
# I
54-
M.set_elem([0,0,0,0],1);
55-
M.set_elem([0,0,1,1],1);
56-
M.set_elem([3,3,0,0],1);
57-
M.set_elem([3,3,1,1],1);
54+
M.set_elem([0,0,0,0],1)
55+
M.set_elem([0,0,1,1],1)
56+
M.set_elem([3,3,0,0],1)
57+
M.set_elem([3,3,1,1],1)
5858

5959
# S-
60-
M.set_elem([0,1,1,0],2**0.5);
60+
M.set_elem([0,1,1,0],2**0.5)
6161
# S+
62-
M.set_elem([0,2,0,1],2**0.5);
62+
M.set_elem([0,2,0,1],2**0.5)
6363
# S+
64-
M.set_elem([1,3,0,1],2**0.5);
64+
M.set_elem([1,3,0,1],2**0.5)
6565
# S-
66-
M.set_elem([2,3,1,0],2**0.5);
66+
M.set_elem([2,3,1,0],2**0.5)
6767

6868
q = 0 # conserving glb Qn
6969
VbdL = cytnx.Bond(cytnx.BD_KET,[[0]],[1])
7070
VbdR = cytnx.Bond(cytnx.BD_KET,[[q]],[1])
71-
L0 = cytnx.UniTensor([bd_inner.redirect(),VbdL.redirect(),VbdL],rowrank=1) #Left boundary
72-
R0 = cytnx.UniTensor([bd_inner,VbdR,VbdR.redirect()],rowrank=1) #Right boundary
71+
L0 = cytnx.UniTensor([bd_inner.redirect(),VbdL.redirect(),VbdL]).set_rowrank_(1) #Left boundary
72+
R0 = cytnx.UniTensor([bd_inner,VbdR,VbdR.redirect()]).set_rowrank_(1) #Right boundary
7373
L0.set_elem([0,0,0],1)
7474
R0.set_elem([3,0,0],1)
7575

@@ -81,21 +81,22 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
8181
cq = -1
8282
qcntr+=cq
8383

84-
A[0] = cytnx.UniTensor([VbdL,bd_phys.redirect(),cytnx.Bond(cytnx.BD_BRA,[[qcntr]],[1])],rowrank=2)
84+
A[0] = cytnx.UniTensor([VbdL,bd_phys.redirect(),cytnx.Bond(cytnx.BD_BRA,[[qcntr]],[1])]).set_rowrank_(2)
8585
A[0].get_block_()[0] = 1
8686

8787
lbls = []
8888
lbls.append(["0","1","2"]) # store the labels for later convinience.
8989
for k in range(1,Nsites):
90-
B1 = A[k-1].bonds()[2].redirect(); B2 = A[k-1].bonds()[1];
90+
B1 = A[k-1].bonds()[2].redirect()
91+
B2 = A[k-1].bonds()[1]
9192
if qcntr <= q:
9293
cq = 1
9394
else:
9495
cq = -1
9596
qcntr+=cq
9697
B3 = cytnx.Bond(cytnx.BD_BRA,[[qcntr]],[1])
9798

98-
A[k] = cytnx.UniTensor([B1,B2,B3],rowrank=2)
99+
A[k] = cytnx.UniTensor([B1,B2,B3]).set_rowrank_(2)
99100

100101
lbl = [str(2*k),str(2*k+1),str(2*k+2)]
101102
A[k].set_labels(lbl)
@@ -131,12 +132,12 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
131132

132133
psi.set_rowrank_(2) # maintain rowrank to perform the svd
133134
s,A[p],A[p+1] = cytnx.linalg.Svd_truncate(psi,new_dim)
134-
A[p+1].relabels_(lbls[p+1]); # set the label back to be consistent
135+
A[p+1].relabels_(lbls[p+1]) # set the label back to be consistent
135136

136137
s = s/s.Norm().item() # normalize s
137138

138139
A[p] = cytnx.Contract(A[p],s) # absorb s into next neighbor
139-
A[p].relabels_(lbls[p]); # set the label back to be consistent
140+
A[p].relabels_(lbls[p]) # set the label back to be consistent
140141

141142
# update LR from right to left:
142143
anet = cytnx.Network()
@@ -152,7 +153,7 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
152153

153154
A[0].set_rowrank_(1)
154155
_,A[0] = cytnx.linalg.Gesvd(A[0],is_U=False, is_vT=True)
155-
A[0].relabels_(lbls[0]); #set the label back to be consistent
156+
A[0].relabels_(lbls[0]) #set the label back to be consistent
156157

157158
for p in range(Nsites-1):
158159
dim_l = A[p].shape()[0]
@@ -165,12 +166,12 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
165166

166167
psi.set_rowrank_(2) # maintain rowrank to perform the svd
167168
s,A[p],A[p+1] = cytnx.linalg.Svd_truncate(psi,new_dim)
168-
A[p].relabels_(lbls[p]); #set the label back to be consistent
169+
A[p].relabels_(lbls[p]) #set the label back to be consistent
169170

170171
s = s/s.Norm().item() # normalize s
171172

172173
A[p+1] = cytnx.Contract(s,A[p+1]) ## absorb s into next neighbor.
173-
A[p+1].relabels_(lbls[p+1]); #set the label back to be consistent
174+
A[p+1].relabels_(lbls[p+1]) #set the label back to be consistent
174175

175176
# update LR from left to right:
176177
anet = cytnx.Network()
@@ -186,7 +187,7 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
186187

187188
A[-1].set_rowrank_(2)
188189
_,A[-1] = cytnx.linalg.Gesvd(A[-1],is_U=True,is_vT=False) ## last one.
189-
A[-1].relabels_(lbls[-1]); #set the label back to be consistent
190+
A[-1].relabels_(lbls[-1]) #set the label back to be consistent
190191

191192
return Ekeep
192193

example/DMRG/dmrg_two_sites_dense.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
5454
M[0,2] = M[1,3] = 2**0.5*sm.real()
5555
M = cytnx.UniTensor(M,0)
5656

57-
L0 = cytnx.UniTensor.zeros([4,1,1], rowrank = 0) #Left boundary
58-
R0 = cytnx.UniTensor.zeros([4,1,1], rowrank = 0) #Right boundary
57+
L0 = cytnx.UniTensor.zeros([4,1,1]).set_rowrank_(0) #Left boundary
58+
R0 = cytnx.UniTensor.zeros([4,1,1]).set_rowrank_(0) #Right boundary
5959
L0[0,0,0] = 1.; R0[3,0,0] = 1.
6060

6161
lbls = [] # List for storing the MPS labels
6262
A = [None for i in range(Nsites)]
63-
A[0] = cytnx.UniTensor.normal([1, d, min(chi, d)], 0., 1., rowrank = 2)
63+
A[0] = cytnx.UniTensor.normal([1, d, min(chi, d)], 0., 1.).set_rowrank_(2)
6464
A[0].relabels_(["0","1","2"])
6565
lbls.append(["0","1","2"]) # store the labels for later convinience.
6666

6767
for k in range(1,Nsites):
6868
dim1 = A[k-1].shape()[2]; dim2 = d
6969
dim3 = min(min(chi, A[k-1].shape()[2] * d), d ** (Nsites - k - 1))
70-
A[k] = cytnx.UniTensor.normal([dim1, dim2, dim3],0.,1., rowrank = 2)
70+
A[k] = cytnx.UniTensor.normal([dim1, dim2, dim3],0.,1.).set_rowrank_(2)
7171

7272
lbl = [str(2*k),str(2*k+1),str(2*k+2)]
7373
A[k].relabels_(lbl)

example/TDVP/tdvp1_dense.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ def time_evolve_Lan_b(psi, functArgs, delta):
6868

6969
def get_energy(A, M):
7070
N = len(A)
71-
L0 = cytnx.UniTensor.zeros([5,1,1], rowrank = 0) #Left boundary
72-
R0 = cytnx.UniTensor.zeros([5,1,1], rowrank = 0) #Right boundary
73-
L0[0,0,0] = 1.; R0[4,0,0] = 1.
71+
L0 = cytnx.UniTensor.zeros([5,1,1]).set_rowrank_(0) #Left boundary
72+
R0 = cytnx.UniTensor.zeros([5,1,1]).set_rowrank_(0) #Right boundary
73+
L0[0,0,0] = 1.
74+
R0[4,0,0] = 1.
7475
L = L0
7576
anet = cytnx.Network()
7677
anet.FromString(["L: -2,-1,-3",\
@@ -106,9 +107,10 @@ def get_energy(A, M):
106107
M[0,3] = Jz*sz
107108
M = cytnx.UniTensor(M,0)
108109

109-
L0 = cytnx.UniTensor.zeros([5,1,1], rowrank = 0) #Left boundary
110-
R0 = cytnx.UniTensor.zeros([5,1,1], rowrank = 0) #Right boundary
111-
L0[0,0,0] = 1.; R0[4,0,0] = 1.
110+
L0 = cytnx.UniTensor.zeros([5,1,1]).set_rowrank_(0) #Left boundary
111+
R0 = cytnx.UniTensor.zeros([5,1,1]).set_rowrank_(0) #Right boundary
112+
L0[0,0,0] = 1.
113+
R0[4,0,0] = 1.
112114

113115
lbls = [] # List for storing the MPS labels
114116
Nsites = len(A)
@@ -166,7 +168,7 @@ def get_energy(A, M):
166168

167169
psi.set_rowrank_(1) # maintain rowrank to perform the svd
168170
s,_,A[p] = cytnx.linalg.Svd_truncate(psi,new_dim)
169-
A[p].relabels_(lbls[p]); # set the label back to be consistent
171+
A[p].relabels_(lbls[p]) # set the label back to be consistent
170172
# update LR from right to left:
171173
anet = cytnx.Network()
172174
anet.FromString(["R: -2,-1,-3",\
@@ -193,7 +195,7 @@ def get_energy(A, M):
193195

194196
A[0].set_rowrank_(1)
195197
_,A[0] = cytnx.linalg.Gesvd(A[0],is_U=False, is_vT=True)
196-
A[0].relabels_(lbls[0]); #set the label back to be consistent
198+
A[0].relabels_(lbls[0]) #set the label back to be consistent
197199

198200

199201
for p in range(Nsites):
@@ -206,7 +208,7 @@ def get_energy(A, M):
206208

207209
psi.set_rowrank_(2) # maintain rowrank to perform the svd
208210
s,A[p],_ = cytnx.linalg.Svd_truncate(psi,new_dim)
209-
A[p].relabels_(lbls[p]); #set the label back to be consistent
211+
A[p].relabels_(lbls[p]) #set the label back to be consistent
210212
# update LR from left to right:
211213
anet = cytnx.Network()
212214
anet.FromString(["L: -2,-1,-3",\
@@ -232,13 +234,13 @@ def get_energy(A, M):
232234

233235
A[-1].set_rowrank_(2)
234236
_,A[-1] = cytnx.linalg.Gesvd(A[-1],is_U=True,is_vT=False) ## last one.
235-
A[-1].relabels_(lbls[-1]); #set the label back to be consistent
237+
A[-1].relabels_(lbls[-1]) #set the label back to be consistent
236238
As.append(A.copy())
237239
return As, Es # all time step states
238240

239241
def Local_meas(A, B, Op, site):
240242
N = len(A)
241-
l = cytnx.UniTensor.eye(1, rowrank = 1)
243+
l = cytnx.UniTensor.eye(1).set_rowrank_(1)
242244
anet = cytnx.Network()
243245
anet.FromString(["l: 0,3",\
244246
"A: 0,1,2",\
@@ -263,14 +265,15 @@ def Local_meas(A, B, Op, site):
263265
def prepare_rand_init_MPS(Nsites, chi, d):
264266
lbls = []
265267
A = [None for i in range(Nsites)]
266-
A[0] = cytnx.UniTensor.normal([1, d, min(chi, d)], 0., 1., seed=0, rowrank = 2)
268+
A[0] = cytnx.UniTensor.normal([1, d, min(chi, d)], 0., 1., seed=0).set_rowrank_(2)
267269
A[0].relabels_(["0","1","2"])
268270
lbls.append(["0","1","2"]) # store the labels for later convinience.
269271

270272
for k in range(1,Nsites):
271-
dim1 = A[k-1].shape()[2]; dim2 = d
273+
dim1 = A[k-1].shape()[2]
274+
dim2 = d
272275
dim3 = min(min(chi, A[k-1].shape()[2] * d), d ** (Nsites - k - 1))
273-
A[k] = cytnx.UniTensor.normal([dim1, dim2, dim3],0.,1., seed=0, rowrank = 2)
276+
A[k] = cytnx.UniTensor.normal([dim1, dim2, dim3],0.,1., seed=0).set_rowrank(2)
274277

275278
lbl = [str(2*k),str(2*k+1),str(2*k+2)]
276279
A[k].relabels_(lbl)
@@ -306,7 +309,7 @@ def prepare_rand_init_MPS(Nsites, chi, d):
306309
As, Es = tdvp1_XXZmodel_dense(J, Jz, hx, hz, GS, chi, dt, time_step)
307310

308311
# measure middle site <Sz>
309-
Sz = cytnx.UniTensor(cytnx.physics.pauli('z').real(), rowrank = 1)
312+
Sz = cytnx.UniTensor(cytnx.physics.pauli('z').real()).set_rowrank(1)
310313
Szs = []
311314
mid_site = int(Nsites/2)
312315
for i in range(0, len(As)):

example/UniTensor/fromTensor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
T = zeros([4,4])
9-
CyT = UniTensor(T,rowrank=2) #create un-tagged UniTensor from Tensor
9+
CyT = UniTensor(T).set_rowrank_(2) #create un-tagged UniTensor from Tensor
1010
CyT.print_diagram()
1111

1212
print("before:")
@@ -22,13 +22,13 @@
2222

2323
#If we want a new instance of memery, use clone at initialize:
2424
print("[non-share example]")
25-
CyT_nonshare = UniTensor(T.clone(),rowrank=2);
25+
CyT_nonshare = UniTensor(T.clone()).set_rowrank_(2)
2626

2727
print("before:")
2828
print(T)
2929
print(CyT_nonshare)
3030

31-
CyT_nonshare.set_elem([1,1],2.345);
31+
CyT_nonshare.set_elem([1,1],2.345)
3232

3333
print("after")
3434
print(T) # T is unchanged!

example/iTEBD/iTEBD.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ def itebd_tfim(chi = 20, J = 1.0, Hx = 1.0, dt = 0.1, CvgCrit = 1.0e-10):
4242
# | |
4343
# --A-la-B-lb--
4444
#
45-
A = cytnx.UniTensor([cytnx.Bond(chi),cytnx.Bond(2),cytnx.Bond(chi)],labels=['a','0','b']);
46-
B = cytnx.UniTensor(A.bonds(),rowrank=1,labels=['c','1','d']);
47-
cytnx.random.normal_(B.get_block_(), mean=0, std=0.2, seed=0);
48-
cytnx.random.normal_(A.get_block_(),mean=0, std=0.2, seed=0);
45+
A = cytnx.UniTensor([cytnx.Bond(chi),cytnx.Bond(2),cytnx.Bond(chi)],labels=['a','0','b'])
46+
B = cytnx.UniTensor(A.bonds(),labels=['c','1','d']).set_rowrank_(1)
47+
cytnx.random.normal_(B.get_block_(), mean=0, std=0.2, seed=0)
48+
cytnx.random.normal_(A.get_block_(),mean=0, std=0.2, seed=0)
4949
A.print_diagram()
5050
B.print_diagram()
5151
#print(A)
5252
#print(B)
5353

5454
la = cytnx.UniTensor([cytnx.Bond(chi),cytnx.Bond(chi)],labels=['b','c'],is_diag=True)
5555
lb = cytnx.UniTensor([cytnx.Bond(chi),cytnx.Bond(chi)],labels=['d','e'],is_diag=True)
56-
la.put_block(cytnx.ones(chi));
57-
lb.put_block(cytnx.ones(chi));
56+
la.put_block(cytnx.ones(chi))
57+
lb.put_block(cytnx.ones(chi))
5858
la.print_diagram()
5959
lb.print_diagram()
6060
#print(la)
@@ -132,7 +132,7 @@ def itebd_tfim(chi = 20, J = 1.0, Hx = 1.0, dt = 0.1, CvgCrit = 1.0e-10):
132132
#
133133
# again, but A' and B' are updated
134134
lb_inv = 1./lb
135-
# lb_inv.print_diagram();
135+
# lb_inv.print_diagram()
136136
lb_inv.set_labels(['e','d'])
137137

138138
A = cytnx.Contract(lb_inv,A)

0 commit comments

Comments
 (0)