Skip to content

Commit d500b08

Browse files
Fix exp check for fermionic ITensors (#1664)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c019c65 commit d500b08

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/tensor_operations/matrix_algebra.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ function exp(A::ITensor, Linds, Rinds; ishermitian=false)
6565
end
6666

6767
# <fermions>
68-
auto_fermion_enabled = using_auto_fermion()
69-
if auto_fermion_enabled
68+
fermionic_itensor = using_auto_fermion() && has_fermionic_subspaces(inds(A))
69+
if fermionic_itensor
7070
# If fermionic, bring indices into i',j',..,dag(j),dag(i)
7171
# ordering with Out indices coming before In indices
7272
# Resulting tensor acts like a normal matrix (no extra signs
@@ -95,7 +95,7 @@ function exp(A::ITensor, Linds, Rinds; ishermitian=false)
9595
expA = (itensor(expAT) * dag(CR)) * dag(CL)
9696

9797
# <fermions>
98-
if auto_fermion_enabled
98+
if fermionic_itensor
9999
# Ensure expA indices in "matrix" form before re-enabling fermion system
100100
expA = permute(expA, ordered_inds)
101101
enable_auto_fermion()

test/base/test_fermions.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@eval module $(gensym())
12
using ITensors, Test
23
import ITensors: Out, In
34
using ITensors.SiteTypes: op, siteind, siteinds
@@ -801,7 +802,32 @@ using ITensors.SiteTypes: op, siteind, siteinds
801802

802803
# Check wrong index ordering fails (i.e. we are actually paying attention to it)
803804
@test norm(id_tensor - exp(0.0 * id_tensor, (dag(s[1]), dag(s[2])), (s[2]', s[1]'))) > 1
805+
806+
# Test a different, random tensor
807+
T = random_itensor(s[1]', dag(s[1]))
808+
T = 1/2*(T+swapprime(dag(T), 0=>1))
809+
t = 0.01
810+
eT = exp(t*T)
811+
eT_taylor = (op("I", s[1])+t*T+t^2*apply(T, T)/2)
812+
@test norm(eT - eT_taylor) < 1E-5
813+
814+
#
815+
# Test that bosonic tensor exp works with auto fermion enabled
816+
#
817+
j1 = Index([QN("Nb", 0)=>2, QN("Nb", 1)=>2])
818+
j2 = Index([QN("Nb", 0)=>2, QN("Nb", 1)=>2])
819+
id_tensor = op("I", j1) * op("I", j2)
820+
@test id_tensor exp(0.0 * id_tensor)
821+
822+
T = random_itensor(j1', dag(j1))
823+
T = 1/2*(T+swapprime(dag(T), 0=>1))
824+
t = 0.01
825+
eT = exp(t*T)
826+
eT_taylor = (op("I", j1)+t*T+t^2*apply(T, T)/2)
827+
@test norm(eT - eT_taylor) < 1E-5
804828
end
805829

806830
ITensors.disable_auto_fermion()
807831
end
832+
833+
end # module

test/base/test_qnitensor.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
@eval module $(gensym())
12
using ITensors
23
using ITensors.NDTensors
3-
using ITensors.SiteTypes: siteind, siteinds
4+
using ITensors.SiteTypes: op, siteind, siteinds
45
using LinearAlgebra
56
using Random
67
using Test
@@ -1915,3 +1916,5 @@ Random.seed!(1234)
19151916
@test !ITensors.have_same_qns([QN(0) => 1, QN(0) => 2, QN(("Sz", 2)) => 1])
19161917
end
19171918
end
1919+
1920+
end # module

0 commit comments

Comments
 (0)