7.3. Changing labels¶
We can set and change the labels of the Bonds in a UniTensor as desired. This is particularly helpful for contractions with cytnx.Contract() and cytnx.Contracts(). As will be explained in Contract(s), these functions contract bonds with the same name on different UniTensors. Therefore, we might need to change the labels for some bond(s) to initiate the correct tensor contraction.
To change the label associated to a certain leg of a UniTensor, one can use:
- UniTensor.relabel_(index, new_label)¶
- Parameters:
index ([int]) – the index (order) of the bond in the UniTensor
new_label ([string]) – the new label that you want to change to
Alternatively, if we don’t know the index of the target bond in the current order, we can also specify the old label:
- UniTensor.relabel_(old_label, new_label)¶
- Parameters:
old_label ([string]) – the current label of the bond
new_label ([string]) – the new label that you want to change to
If we wish to change the labels of all legs, we can use:
- UniTensor.relabels_(new_labels)¶
- Parameters:
new_labels (List[string]) – a list of new labels
or
- UniTensor.relabels_(old_labels, new_labels)¶
- Parameters:
old_labels (List[string]) – a list of current labels
new_labels (List[string]) – a list of the corresponding new labels
For example:
In Python:
1T = cytnx.arange(2*3*4).reshape(2,3,4)
2uT = cytnx.UniTensor(T)
3
4uT.relabel_(1,"xx")
5uT.print_diagram()
6
7uT.relabels_(["a","b","c"])
8uT.print_diagram()
Output >>
-----------------------
tensor Name :
tensor Rank : 3
block_form : False
is_diag : False
on device : cytnx device: CPU
---------
/ \
0 ____| 2 3 |____ xx
| |
| 4 |____ 2
\ /
---------
-----------------------
tensor Name :
tensor Rank : 3
block_form : False
is_diag : False
on device : cytnx device: CPU
---------
/ \
a ____| 2 3 |____ b
| |
| 4 |____ c
\ /
---------
Note
One cannot have duplicate labels within the same UniTensor!
Warning
The previously provided method set_label(s) is deprecated and should be replaced by relabel(s)_.