Skip to content

Commit 2ee8e7c

Browse files
committed
Tensor.gate: add transpose option
1 parent 09adc14 commit 2ee8e7c

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

docs/changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Release notes for `quimb`.
1919
- HV1BP: vectorize both contraction and message initialization
2020
- add [`qu.plot_multi_series_zoom`](quimb.utils_plot.plot_multi_series_zoom) for plotting multiple series with a zoomed inset, useful for various convergence plots such as BP
2121
- add `info` option to [`tn.gauge_all_simple`](quimb.tensor.tensor_core.TensorNetwork.gauge_all_simple) for tracking extra information such as number of iterations and max gauge difffi
22+
- [`Tensor.gate`](quimb.tensor.tensor_core.Tensor.gate): add `transposed` option
2223

2324
**Bug fixes:**
2425

quimb/tensor/tensor_core.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -2318,18 +2318,35 @@ def gate(
23182318
G,
23192319
ind,
23202320
preserve_inds=True,
2321+
transposed=False,
23212322
inplace=False,
23222323
):
2323-
"""Gate this tensor - contract a matrix into one of its indices without
2324+
r"""Gate this tensor - contract a matrix into one of its indices without
23242325
changing its indices. Unlike ``contract``, ``G`` is a raw array and the
2325-
tensor remains with the same set of indices.
2326+
tensor remains with the same set of indices. This is like applying:
2327+
2328+
.. math::
2329+
2330+
x \leftarrow G x
2331+
2332+
or if ``transposed=True``:
2333+
2334+
.. math::
2335+
2336+
x \leftarrow x G
23262337
23272338
Parameters
23282339
----------
23292340
G : 2D array_like
23302341
The matrix to gate the tensor index with.
23312342
ind : str
23322343
Which index to apply the gate to.
2344+
preserve_inds : bool, optional
2345+
If ``True``, the order of the indices is preserved, otherwise the
2346+
gated index will be left at the first axis, avoiding a transpose.
2347+
transposed : bool, optional
2348+
If ``True``, the gate is effectively transpose and applied, or
2349+
equivalently, contracted to its left rather than right.
23332350
23342351
Returns
23352352
-------
@@ -2359,7 +2376,11 @@ def gate(
23592376
t = self if inplace else self.copy()
23602377

23612378
ax = t.inds.index(ind)
2362-
new_data = do("tensordot", G, t.data, ((1,), (ax,)))
2379+
2380+
if transposed:
2381+
new_data = do("tensordot", G, t.data, ((0,), (ax,)))
2382+
else:
2383+
new_data = do("tensordot", G, t.data, ((1,), (ax,)))
23632384

23642385
if preserve_inds:
23652386
# gated index is now first axis, so move it to the correct position

0 commit comments

Comments
 (0)