Skip to content

Commit 5c0d6ab

Browse files
committed
update identity contraction handling
1 parent ce6091e commit 5c0d6ab

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

cotengra/interface.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,18 @@ def _build_expression(
539539
# 'contraction' is a no-op
540540

541541
def fn(*arrays, backend=None):
542+
# this is surprisingly complicated 'to do nothing', since we
543+
# have to respect potential backend conversion
544+
x = arrays[0]
542545
if backend is None:
543-
return arrays[0]
544-
return ar.do("array", arrays[0], like=backend)
546+
# haven't requested a specific backend, just return
547+
return x
548+
elif backend == ar.infer_backend(x):
549+
# requested backend is the same as the input, just return
550+
return x
551+
else:
552+
# requested backend is different, convert
553+
return ar.do("array", arrays[0], like=backend)
545554

546555
elif len(term) == len(output):
547556
# 'contraction' is just a transposition

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
],
3131
extras_require={
3232
"recommended": [
33-
"cotengrust",
33+
"cotengrust>=0.1.3",
3434
"cytoolz",
3535
"kahypar",
3636
"networkx",
@@ -76,6 +76,7 @@
7676
"Programming Language :: Python :: 3.9",
7777
"Programming Language :: Python :: 3.10",
7878
"Programming Language :: Python :: 3.11",
79+
"Programming Language :: Python :: 3.12",
7980
],
8081
keywords="tensor network contraction graph hypergraph partition einsum",
8182
)

0 commit comments

Comments
 (0)