Skip to content

Commit 9ee34bc

Browse files
committed
Merge branch 'master' into manuel/doc_update
2 parents 8555352 + 07ba4c5 commit 9ee34bc

17 files changed

Lines changed: 687 additions & 1244 deletions

docs/uv.lock

Lines changed: 0 additions & 697 deletions
This file was deleted.

example/DMRG/dmrg_two_sites_U1.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
4141
return psivec, energy[0].item()
4242

4343

44-
## Initialiaze MPO
44+
## Initialize MPO
4545
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
4646
d = 2
4747
s = 0.5
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()]).set_rowrank_(2)
51+
M = cytnx.UniTensor([bd_inner,bd_inner.redirect(),bd_phys, bd_phys.redirect()]) \
52+
.set_rowrank_(2).set_name("MPO")
5253

5354
# I
5455
M.set_elem([0,0,0,0],1)
@@ -68,8 +69,10 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
6869
q = 0 # conserving glb Qn
6970
VbdL = cytnx.Bond(cytnx.BD_KET,[[0]],[1])
7071
VbdR = cytnx.Bond(cytnx.BD_KET,[[q]],[1])
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
72+
L0 = cytnx.UniTensor([bd_inner.redirect(),VbdL.redirect(),VbdL]) \
73+
.set_rowrank_(1).set_name("L0") #Left boundary
74+
R0 = cytnx.UniTensor([bd_inner,VbdR,VbdR.redirect()]) \
75+
.set_rowrank_(1).set_name("R0") #Right boundary
7376
L0.set_elem([0,0,0],1)
7477
R0.set_elem([3,0,0],1)
7578

@@ -81,7 +84,8 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
8184
cq = -1
8285
qcntr+=cq
8386

84-
A[0] = cytnx.UniTensor([VbdL,bd_phys.redirect(),cytnx.Bond(cytnx.BD_BRA,[[qcntr]],[1])]).set_rowrank_(2)
87+
A[0] = cytnx.UniTensor([VbdL,bd_phys.redirect(),cytnx.Bond(cytnx.BD_BRA,[[qcntr]],[1])]) \
88+
.set_rowrank_(2).set_name("A0")
8589
A[0].get_block_()[0] = 1
8690

8791
lbls = []
@@ -96,7 +100,7 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
96100
qcntr+=cq
97101
B3 = cytnx.Bond(cytnx.BD_BRA,[[qcntr]],[1])
98102

99-
A[k] = cytnx.UniTensor([B1,B2,B3]).set_rowrank_(2)
103+
A[k] = cytnx.UniTensor([B1,B2,B3]).set_rowrank_(2).set_name(f"A{k}")
100104

101105
lbl = [str(2*k),str(2*k+1),str(2*k+2)]
102106
A[k].relabel_(lbl)
@@ -115,8 +119,11 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
115119
"A_Conj: -3,-5,2",\
116120
"TOUT: 0;1,2"])
117121
for p in range(Nsites - 1):
118-
anet.PutUniTensors(["L","A","A_Conj","M"],[LR[p],A[p],A[p].Dagger(),M])
122+
# Dagger() swaps left/right index order; permute_ restores original label order
123+
anet.PutUniTensors(["L","A","A_Conj","M"], \
124+
[LR[p],A[p],A[p].Dagger().permute_(A[p].labels()),M])
119125
LR[p+1] = anet.Launch()
126+
LR[p+1].set_name(f"LR{p+1}")
120127

121128
Ekeep = []
122129
for k in range(1, numsweeps+1):
@@ -146,8 +153,11 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
146153
"M: 0,-2,-4,-5",\
147154
"B_Conj: 2,-5,-3",\
148155
"TOUT: 0;1,2"])
149-
anet.PutUniTensors(["R","B","M","B_Conj"],[LR[p+2],A[p+1],M,A[p+1].Dagger()])
156+
# Dagger() swaps index order; permute_ restores it
157+
anet.PutUniTensors(["R","B","M","B_Conj"], \
158+
[LR[p+2],A[p+1],M,A[p+1].Dagger().permute_(A[p+1].labels())])
150159
LR[p+1] = anet.Launch()
160+
LR[p+1].set_name(f"LR{p+1}")
151161

152162
print('Sweep[r->l]: %d/%d, Loc: %d,Energy: %f' % (k, numsweeps, p, Ekeep[-1]))
153163

@@ -173,21 +183,28 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
173183
A[p+1] = cytnx.Contract(s,A[p+1]) ## absorb s into next neighbor.
174184
A[p+1].relabel_(lbls[p+1]) #set the label back to be consistent
175185

186+
A[p].set_name(f"A{p}")
187+
A[p+1].set_name(f"A{p+1}")
188+
176189
# update LR from left to right:
177190
anet = cytnx.Network()
178191
anet.FromString(["L: -2,-1,-3",\
179192
"A: -1,-4,1",\
180193
"M: -2,0,-4,-5",\
181194
"A_Conj: -3,-5,2",\
182195
"TOUT: 0;1,2"])
183-
anet.PutUniTensors(["L","A","A_Conj","M"],[LR[p],A[p],A[p].Dagger(),M])
196+
# Dagger() swaps index order; permute_ restores it
197+
anet.PutUniTensors(["L","A","A_Conj","M"], \
198+
[LR[p],A[p],A[p].Dagger().permute_(A[p].labels()),M])
184199
LR[p+1] = anet.Launch()
200+
LR[p+1].set_name(f"LR{p+1}")
185201

186202
print('Sweep[l->r]: %d/%d, Loc: %d,Energy: %f' % (k, numsweeps, p, Ekeep[-1]))
187203

188204
A[-1].set_rowrank_(2)
189205
_,A[-1] = cytnx.linalg.Gesvd(A[-1],is_U=True,is_vT=False) ## last one.
190-
A[-1].relabel_(lbls[-1]) #set the label back to be consistent
206+
A[-1].set_name(f"A{Nsites-1}")\
207+
.relabel_(lbls[-1]) #set the label back to be consistent
191208

192209
return Ekeep
193210

example/DMRG/dmrg_two_sites_dense.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,29 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
5252
M[0,0] = M[3,3] = eye
5353
M[0,1] = M[2,3] = 2**0.5*sp.real()
5454
M[0,2] = M[1,3] = 2**0.5*sm.real()
55-
M = cytnx.UniTensor(M,0)
55+
M = cytnx.UniTensor(M,0).set_name("MPO")
5656

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
59-
L0[0,0,0] = 1.; R0[3,0,0] = 1.
57+
L0 = cytnx.UniTensor.zeros([4,1,1]).set_rowrank_(0).set_name("L0") #Left boundary
58+
R0 = cytnx.UniTensor.zeros([4,1,1]).set_rowrank_(0).set_name("R0") #Right boundary
59+
L0[0,0,0] = 1.
60+
R0[3,0,0] = 1.
6061

61-
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.).set_rowrank_(2)
64-
A[0].relabel_(["0","1","2"])
65-
lbls.append(["0","1","2"]) # store the labels for later convinience.
63+
A[0] = cytnx.UniTensor.normal([1, d, min(chi, d)], 0., 1.).set_rowrank_(2) \
64+
.relabel_(["0","1","2"]).set_name("A0")
65+
66+
lbls = [] # List for storing the MPS labels
67+
lbls.append(["0","1","2"]) # store the labels for later convenience.
6668

6769
for k in range(1,Nsites):
6870
dim1 = A[k-1].shape()[2]; dim2 = d
6971
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.).set_rowrank_(2)
72+
A[k] = cytnx.UniTensor.normal([dim1, dim2, dim3],0.,1.).set_rowrank_(2) \
73+
.set_name(f"A{k}")
7174

7275
lbl = [str(2*k),str(2*k+1),str(2*k+2)]
7376
A[k].relabel_(lbl)
74-
lbls.append(lbl) # store the labels for later convinience.
77+
lbls.append(lbl) # store the labels for later convenience.
7578

7679
LR = [None for i in range(Nsites+1)]
7780
LR[0] = L0
@@ -84,23 +87,32 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
8487
s, A[p] ,vt = cytnx.linalg.Gesvd(A[p])
8588
A[p+1] = cytnx.Contract(cytnx.Contract(s,vt),A[p+1])
8689

87-
## Calculate enviroments:
90+
A[p].set_name(f"A{p}")
91+
A[p+1].set_name(f"A{p+1}")
92+
93+
## Calculate environments:
8894
anet = cytnx.Network()
8995
anet.FromString(["L: -2,-1,-3",\
9096
"A: -1,-4,1",\
9197
"M: -2,0,-4,-5",\
9298
"A_Conj: -3,-5,2",\
9399
"TOUT: 0,1,2"])
94100
# or you can do: anet = cytnx.Network("L_AMAH.net")
95-
anet.PutUniTensors(["L","A","A_Conj","M"],[LR[p],A[p],A[p].Conj(),M])
101+
102+
# Dagger() swaps left/right index order; permute_ restores original label order
103+
anet.PutUniTensors(["L","A","A_Conj","M"], \
104+
[LR[p],A[p],A[p].Dagger().permute_(A[p].labels()),M])
96105
LR[p+1] = anet.Launch()
106+
LR[p+1].set_name(f"LR{p+1}")
97107

98108
# Recover the original MPS labels
99109
A[p].relabel_(lbls[p])
100110
A[p+1].relabel_(lbls[p+1])
101111

102112
_,A[-1] = cytnx.linalg.Gesvd(A[-1],is_U=True,is_vT=False) ## last one.
103-
A[-1].relabel_(lbls[-1]) # Recover the original MPS labels
113+
A[-1].set_name(f"A{Nsites-1}") \
114+
.relabel_(lbls[-1]) # Recover the original MPS labels
115+
104116

105117
Ekeep = []
106118
for k in range(1, numsweeps+1):
@@ -123,6 +135,9 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
123135
A[p] = cytnx.Contract(A[p],s) # absorb s into next neighbor
124136
A[p].relabel_(lbls[p]); # set the label back to be consistent
125137

138+
A[p].set_name(f"A{p}")
139+
A[p+1].set_name(f"A{p+1}")
140+
126141
# update LR from right to left:
127142
anet = cytnx.Network()
128143
anet.FromString(["R: -2,-1,-3",\
@@ -131,14 +146,19 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
131146
"B_Conj: 2,-5,-3",\
132147
"TOUT: 0;1,2"])
133148
# or you can do: anet = cytnx.Network("R_AMAH.net")
134-
anet.PutUniTensors(["R","B","M","B_Conj"],[LR[p+2],A[p+1],M,A[p+1].Conj()])
149+
150+
# Dagger() swaps index order; permute_ restores it
151+
anet.PutUniTensors(["R","B","M","B_Conj"], \
152+
[LR[p+2],A[p+1],M,A[p+1].Dagger().permute_(A[p+1].labels())])
135153
LR[p+1] = anet.Launch()
154+
LR[p+1].set_name(f"LR{p+1}")
136155

137156
print('Sweep[r->l]: %d/%d, Loc: %d,Energy: %f' % (k, numsweeps, p, Ekeep[-1]))
138157

139158
A[0].set_rowrank_(1)
140159
_,A[0] = cytnx.linalg.Gesvd(A[0],is_U=False, is_vT=True)
141160
A[0].relabel_(lbls[0]); #set the label back to be consistent
161+
A[0].set_name("A0")
142162

143163
for p in range(Nsites-1):
144164
dim_l = A[p].shape()[0]
@@ -158,6 +178,9 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
158178
A[p+1] = cytnx.Contract(s,A[p+1]) ## absorb s into next neighbor.
159179
A[p+1].relabel_(lbls[p+1]); #set the label back to be consistent
160180

181+
A[p].set_name(f"A{p}")
182+
A[p+1].set_name(f"A{p+1}")
183+
161184
# update LR from left to right:
162185
anet = cytnx.Network()
163186
anet.FromString(["L: -2,-1,-3",\
@@ -167,14 +190,18 @@ def optimize_psi(psi, functArgs, maxit=2, krydim=4):
167190
"TOUT: 0,1,2"])
168191
# or you can do: anet = cytnx.Network("L_AMAH.net")
169192

170-
anet.PutUniTensors(["L","A","A_Conj","M"],[LR[p],A[p],A[p].Conj(),M])
193+
# Dagger() swaps index order; permute_ restores it
194+
anet.PutUniTensors(["L","A","A_Conj","M"], \
195+
[LR[p],A[p],A[p].Dagger().permute_(A[p].labels()),M])
171196
LR[p+1] = anet.Launch()
197+
LR[p+1].set_name(f"LR{p+1}")
172198

173199
print('Sweep[l->r]: %d/%d, Loc: %d,Energy: %f' % (k, numsweeps, p, Ekeep[-1]))
174200

175201
A[-1].set_rowrank_(2)
176202
_,A[-1] = cytnx.linalg.Gesvd(A[-1],is_U=True,is_vT=False) ## last one.
177-
A[-1].relabel_(lbls[-1]); #set the label back to be consistent
203+
A[-1].set_name(f"A{Nsites-1}") \
204+
.relabel_(lbls[-1]); #set the label back to be consistent
178205
return Ekeep
179206

180207
if __name__ == '__main__':

example/TDVP/tdvp1_dense.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def get_energy(A, M):
8181
"TOUT: 0,1,2"])
8282
# or you can do: anet = cytnx.Network("L_AMAH.net")
8383
for p in range(0, N):
84-
anet.PutUniTensors(["L","A","A_Conj","M"],[L,A[p],A[p].Conj(),M])
84+
# Dagger() swaps left/right index order; permute_ restores original label order
85+
anet.PutUniTensors(["L","A","A_Conj","M"], \
86+
[L,A[p],A[p].Dagger().permute_(A[p].labels()),M])
8587
L = anet.Launch()
8688
E = cytnx.Contract(L, R0).item()
8789
print('energy:', E)
@@ -140,7 +142,8 @@ def get_energy(A, M):
140142
"A_Conj: -3,-5,2",\
141143
"TOUT: 0,1,2"])
142144
# or you can do: anet = cytnx.Network("L_AMAH.net")
143-
anet.PutUniTensors(["L","A","A_Conj","M"],[LR[p],A[p],A[p].Conj(),M])
145+
anet.PutUniTensors(["L","A","A_Conj","M"], \
146+
[LR[p],A[p],A[p].Dagger().permute_(A[p].labels()),M]) # Dagger() swaps index order; permute_ restores it
144147
LR[p+1] = anet.Launch()
145148

146149
# Recover the original MPS labels
@@ -177,7 +180,8 @@ def get_energy(A, M):
177180
"B_Conj: 2,-5,-3",\
178181
"TOUT: ;0,1,2"])
179182
# or you can do: anet = cytnx.Network("R_AMAH.net")
180-
anet.PutUniTensors(["R","B","M","B_Conj"],[LR[p+1],A[p],M,A[p].Conj()])
183+
anet.PutUniTensors(["R","B","M","B_Conj"], \
184+
[LR[p+1],A[p],M,A[p].Dagger().permute_(A[p].labels())]) # Dagger() swaps index order; permute_ restores it
181185
old_LR = LR[p].clone()
182186
if p != 0:
183187
LR[p] = anet.Launch()
@@ -217,7 +221,8 @@ def get_energy(A, M):
217221
"A_Conj: -3,-5,2",\
218222
"TOUT: 0,1,2"])
219223

220-
anet.PutUniTensors(["L","A","A_Conj","M"],[LR[p],A[p],A[p].Conj(),M])
224+
anet.PutUniTensors(["L","A","A_Conj","M"], \
225+
[LR[p],A[p],A[p].Dagger().permute_(A[p].labels()),M]) # Dagger() swaps index order; permute_ restores it
221226
old_LR = LR[p+1].clone()
222227

223228

@@ -248,15 +253,17 @@ def Local_meas(A, B, Op, site):
248253
"TOUT: 2;4"])
249254
for i in range(0, N):
250255
if i != site:
251-
anet.PutUniTensors(["l","A","B"],[l,A[i],B[i].Conj()])
256+
anet.PutUniTensors(["l","A","B"], \
257+
[l,A[i],B[i].Dagger().permute_(B[i].labels())]) # Dagger() swaps index order; permute_ restores it
252258
l = anet.Launch()
253259
else:
254260
tmp = A[i].relabel(1, "_aux_up")
255261
Op = Op.relabel(["_aux_up", "_aux_low"])
256262
tmp = cytnx.Contract(tmp, Op)
257263
tmp.relabel_("_aux_low", A[i].labels()[1])
258264
tmp.permute_(A[i].labels())
259-
anet.PutUniTensors(["l","A","B"],[l,tmp,B[i].Conj()])
265+
anet.PutUniTensors(["l","A","B"], \
266+
[l,tmp,B[i].Dagger().permute_(B[i].labels())]) # Dagger() swaps index order; permute_ restores it
260267
l = anet.Launch()
261268

262269
return l.reshape(1).item()

include/Bond.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ namespace cytnx {
799799

800800
/**
801801
@brief Group the duplicated quantum number and return the new instance
802-
of the Bond ojbect.
802+
of the Bond object.
803803
@details This function will group the duplicated quantum number and return
804804
the new instance of the Bond object. It will also the \p mapper, where
805805
\p mapper is about the new index from old index via\n

include/UniTensor.hpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5000,16 +5000,17 @@ namespace cytnx {
50005000

50015001
/**
50025002
@brief Take the transpose of the UniTensor.
5003-
@details This function will take the transpose of the UniTensor. If the UniTensor is
5004-
tagged (i.e. the Bonds are directional), it will swap the direction of the Bonds but
5005-
the rowrank will not change. If the UniTensor is untagged (i.e. the Bonds are
5006-
BondType::BD_REG), it will change the rowrank to the opposite side.
5007-
For fermionic UniTensors, the index order will be reversed without sign flips, and the
5008-
direction of all Bonds will swapped.
5009-
@return UniTensor
5003+
@details This function takes the transpose of a UniTensor:
5004+
1) The order of the indices is inverted.
5005+
2) Incoming legs become outgoing ones, and vice versa.
5006+
3) The rowrank is set to rank - old rowrank, such that left indices become right indices and
5007+
vice versa.
5008+
@return UniTensor
5009+
@note This function does not only exchange left- and right indices, but inverts the order of all
5010+
indices.
50105011
@note Compared to Transpose_(), this function will return new UniTensor object.
5011-
@see Transpose_()
5012-
*/
5012+
@see Transpose_()
5013+
*/
50135014
UniTensor Transpose() const {
50145015
UniTensor out;
50155016
out._impl = this->_impl->Transpose();
@@ -5018,10 +5019,11 @@ namespace cytnx {
50185019

50195020
/**
50205021
@brief Take the transpose of the UniTensor, inplacely.
5021-
@return UniTensor
5022+
@return UniTensor
5023+
@note This function inverts the order of all indices.
50225024
@note Compared to Transpose(), this function is an inplace function.
5023-
@see Transpose()
5024-
*/
5025+
@see Transpose()
5026+
*/
50255027
UniTensor &Transpose_() {
50265028
this->_impl->Transpose_();
50275029
return *this;
@@ -5126,10 +5128,11 @@ namespace cytnx {
51265128

51275129
/**
51285130
@brief Take the conjugate transpose to the UniTensor.
5129-
@return UniTensor
5130-
@note Compared to Dagger_(), this function will create a new UniTensor ojbect.
5131-
@see Dagger_(), Transpose()
5132-
*/
5131+
@return UniTensor
5132+
@note This function inverts the order of all indices.
5133+
@note Compared to Dagger_(), this function will create a new UniTensor object.
5134+
@see Dagger_(), Transpose()
5135+
*/
51335136
UniTensor Dagger() const {
51345137
UniTensor out;
51355138
out._impl = this->_impl->Dagger();
@@ -5138,10 +5141,11 @@ namespace cytnx {
51385141

51395142
/**
51405143
@brief Take the conjugate transpose to the UniTensor, inplacely.
5141-
@return UniTensor&
5144+
@return UniTensor&
5145+
@note This function inverts the order of all indices.
51425146
@note Compared to Dagger(), this is an inplace function.
5143-
@see Dagger()
5144-
*/
5147+
@see Dagger()
5148+
*/
51455149
UniTensor &Dagger_() {
51465150
this->_impl->Dagger_();
51475151
return *this;

misc_doc/version.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ v1.0.0
44
3. [Change] Merge relabel and relabels into relabel, and relabel_ and relabels_ into relabel_.
55
4. [New] Add an optional argument min_blockdim to svd_truncate to define a minimum dimension for each block.
66
5. [New] Add Eig/Eigh functions for Block UniTensor.
7-
6. [New] Add Lancos-like algoirthm, Lanczos_Exp, to approximate exponential operator acting on a state.
7+
6. [New] Add Lanczos-like algoirthm, Lanczos_Exp, to approximate exponential operator acting on a state.
88
7. [Change] Migrate cuTENSOR APIs to the version 2.
99
8. [Change] reshape_ and permute_ to return the object itself instead of None.
1010
9. [Change] Remove the magma dependency.

0 commit comments

Comments
 (0)