Skip to content

Commit

Permalink
add TensorNetwork.cut_bond
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmgray committed Jan 30, 2024
1 parent 0495458 commit 8c4f425
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

Release notes for `quimb`.


(whats-new-1-7-2)=
## v1.7.2 (unreleased)

**Enhancements:**

- add [`TensorNetwork.cut_bond`](quimb.tensor.tensor_core.TensorNetwork.cut_bond)
for cutting a bond index


(whats-new-1-7-1)=
## v1.7.1 (2024-01-30)

Expand Down
25 changes: 25 additions & 0 deletions quimb/tensor/tensor_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7974,6 +7974,31 @@ def cut_between(self, left_tags, right_tags, left_ind, right_ind):
(tid2,) = self._get_tids_from_tags(right_tags)
self._cut_between_tids(tid1, tid2, left_ind, right_ind)

def cut_bond(self, bond, new_left_ind=None, new_right_ind=None):
"""Cut the bond index specified by ``bond`` between the tensors it
connects. Use ``cut_between`` for control over which tensor gets which
new index ``new_left_ind`` or ``new_right_ind``. The index must
connect exactly two tensors.
Parameters
----------
bond : str
The index to cut.
new_left_ind : str, optional
The new index to give to the left tensor (lowest ``tid`` value).
new_right_ind : str, optional
The new index to give to the right tensor (highest ``tid`` value).
"""
tid1, tid2 = sorted(self.ind_map[bond])
tl, tr = self._tids_get(tid1, tid2)
if new_left_ind is None:
new_left_ind = rand_uuid()
if new_right_ind is None:
new_right_ind = rand_uuid()
tl.reindex_({bond: new_left_ind})
tr.reindex_({bond: new_right_ind})
return new_left_ind, new_right_ind

def isel(self, selectors, inplace=False):
"""Select specific values for some dimensions/indices of this tensor
network, thereby removing them.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_tensor/test_tensor_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,14 @@ def test_fuse_multibonds(self):
tn.fuse_multibonds(inplace=True)
assert len(tn.inner_inds()) == 3

def test_cut_bond(self):
ta = qtn.rand_tensor((2, 2, 2), inds="abc", tags="A")
tb = qtn.rand_tensor((2, 2, 2), inds="cde", tags="B")
tn = ta | tb
tn.cut_bond('c', new_left_ind='l', new_right_ind='r')
assert ta.inds == ('a', 'b', 'l')
assert tb.inds == ('r', 'd', 'e')

def test_draw(self):
import matplotlib
from matplotlib import pyplot as plt
Expand Down

0 comments on commit 8c4f425

Please sign in to comment.