@@ -807,7 +807,7 @@ def from_block2(bmpo):
807
807
)
808
808
else :
809
809
assert False
810
- tensors [i ] = Tensor (blocks = list (map_blocks .values ()))
810
+ tensors [i ] = Tensor (blocks = sorted (map_blocks .values (), key = lambda t : ( t . q_labels , t . reduced )))
811
811
return MPO (tensors = tensors , const_e = bmpo .const_e )
812
812
813
813
@staticmethod
@@ -1017,4 +1017,55 @@ def to_block2(mpo, basis, tag="PYMPO", add_ident=True):
1017
1017
bmpo = bs .SimplifiedMPO (bmpo , bs .Rule (), False , False )
1018
1018
if add_ident :
1019
1019
bmpo = bs .IdentityAddedMPO (bmpo )
1020
- return bmpo
1020
+ return bmpo
1021
+
1022
+ @staticmethod
1023
+ def from_itensor (xdata ):
1024
+ """
1025
+ Translate itensor MPO (json data) to pyblock2 MPO.
1026
+ """
1027
+ from block2 import SAny , SAnySymmTypes
1028
+ def init_sz (Nf = 0 , Sz = 0 ):
1029
+ q = SAny ()
1030
+ q .types [0 ] = SAnySymmTypes .U1
1031
+ q .types [1 ] = SAnySymmTypes .U1
1032
+ q .values [0 ] = Nf
1033
+ q .values [1 ] = Sz
1034
+ return q
1035
+ _parse_qn = lambda Nf = 0 , Sz = 0 : init_sz (Nf , Sz )
1036
+ tensors = []
1037
+ for k , xd in enumerate (xdata ):
1038
+ qns , blocks = xd ['Q' ], xd ['D' ]
1039
+ qns = [[(_parse_qn (** dict (xqn )), nn ) for xqn , nn in qn ] for qn in qns ]
1040
+ uniq_qns = []
1041
+ for qn in qns :
1042
+ dd = {}
1043
+ for xqn , nn in qn :
1044
+ dd [xqn ] = dd .get (xqn , 0 ) + nn
1045
+ d2 , d3 = {xqn : 0 for xqn , _ in sorted (dd .items ())}, []
1046
+ for xqn , nn in qn :
1047
+ d3 .append ((d2 [xqn ], d2 [xqn ] + nn ))
1048
+ d2 [xqn ] += nn
1049
+ uniq_qns .append ((dd , d3 ))
1050
+ xblocks = {}
1051
+ for tag , shape , arr in blocks :
1052
+ data = np .array (arr ).reshape (tuple (shape ), order = 'F' )
1053
+ q_labels = tuple (qns [ii ][ix ][0 ] for ii , ix in enumerate (tag ))
1054
+ if q_labels not in xblocks :
1055
+ xblocks [q_labels ] = np .zeros (tuple (uniq_qns [ii ][0 ][q ] for ii , q in enumerate (q_labels )))
1056
+ xblocks [q_labels ][tuple (slice (* uniq_qns [ii ][1 ][ix ]) for ii , ix in enumerate (tag ))] = data
1057
+ xblocks = sorted (xblocks .items ())
1058
+ if k == 0 :
1059
+ xblocks = [([q [2 ], q [1 ], q [0 ]], d .transpose (2 , 1 , 0 )) for q , d in xblocks ]
1060
+ for q , _ in xblocks :
1061
+ assert q [0 ] - q [1 ] == q [2 ]
1062
+ elif k != len (xdata ) - 1 :
1063
+ xblocks = [([q [0 ], q [3 ], q [2 ], q [1 ]], d .transpose (0 , 3 , 2 , 1 )) for q , d in xblocks ]
1064
+ for q , _ in xblocks :
1065
+ assert q [0 ] + q [1 ] - q [2 ] == q [3 ]
1066
+ else :
1067
+ xblocks = [([q [0 ], q [2 ], q [1 ]], d .transpose (0 , 2 , 1 )) for q , d in xblocks ]
1068
+ for q , _ in xblocks :
1069
+ assert q [0 ] + q [1 ] == q [2 ]
1070
+ tensors .append (Tensor ([SubTensor (q_labels = tuple (q ), reduced = d ) for q , d in sorted (xblocks )]))
1071
+ return MPO (tensors )
0 commit comments