7.10. Save/Load a UniTensor

Cyntx provides a way to save/read UniTensors to/from a file.

7.10.1. Save a UniTensor

To save a Tensor to a file, simply call UniTensor.Save(filepath).

  • In Python:

 1# Create an untagged unitensor and save
 2T1 = cytnx.UniTensor(cytnx.zeros([4,4]),
 3                     rowrank=1,
 4                     labels=["a","b"],
 5                     name="Untagged_Unitensor")
 6T1.Save("Untagged_ut")
 7
 8# Create an unitensor with symmetry and save
 9bd = cytnx.Bond(cytnx.BD_IN,[[1],[0],[-1]],[1,2,1])
10T2 = cytnx.UniTensor([bd, bd.redirect()],
11                     rowrank=1,
12                     labels=["a","b"],
13                     name="symmetric_Unitensor")
14T2.put_block(cytnx.ones([2,2]),1)
15T2.Save("sym_ut")

This will save UniTensors T1 and T2 to the current directory as Untagged_ut.cytnx and sym_ut.cytnx, with .cytnx as file extension.

7.10.2. Load a UniTensor

Now, let’s load the UniTensor from the file.

1T1_ = cytnx.UniTensor.Load("Untagged_ut.cytnx")
2print(T1_.labels())
3print(T1_)
4
5T2_ = cytnx.UniTensor.Load("sym_ut.cytnx")
6print(T2_.labels())
7print(T2_)

Output >>

['a', 'b']
-------- start of print ---------
Tensor name: Untagged_Unitensor
is_diag    : False
contiguous : True

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




['a', 'b']
-------- start of print ---------
Tensor name: symmetric_Unitensor
braket_form : True
is_diag    : False
[OVERALL] contiguous : True
========================
BLOCK [#0]
 |- []   : Qn index
 |- Sym(): Qnum of correspond symmetry
                 -----------
                 |         |
   [0] U1(1)  -->| 1     1 |-->  [0] U1(1)
                 |         |
                 -----------

Total elem: 1
type  : Double (Float64)
cytnx device: CPU
Shape : (1,1)
[[0.00000e+00 ]]

========================
BLOCK [#1]
 |- []   : Qn index
 |- Sym(): Qnum of correspond symmetry
                 -----------
                 |         |
   [1] U1(0)  -->| 2     2 |-->  [1] U1(0)
                 |         |
                 -----------

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

========================
BLOCK [#2]
 |- []   : Qn index
 |- Sym(): Qnum of correspond symmetry
                  -----------
                  |         |
   [2] U1(-1)  -->| 1     1 |-->  [2] U1(-1)
                  |         |
                  -----------

Total elem: 1
type  : Double (Float64)
cytnx device: CPU
Shape : (1,1)
[[0.00000e+00 ]]

In this example we see how the block date and meta information (name, bonds, labels … ) are kept when we save the UniTensor.