Skip to content

Commit 9cb594d

Browse files
committed
Update map_eigvals to use newer pattern and make more robust
1 parent 01a0824 commit 9cb594d

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/lib/ITensorsExtensions/src/itensor.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ using ITensors:
55
Index,
66
commonind,
77
dag,
8+
dir,
89
hasqns,
910
inds,
1011
isdiag,
1112
itensor,
1213
map_diag,
1314
noncommonind,
1415
noprime,
16+
permute,
1517
replaceind,
1618
replaceinds,
1719
sim,
@@ -63,17 +65,20 @@ function eigendecomp(A::ITensor, linds, rinds; ishermitian=false, kwargs...)
6365
end
6466

6567
function map_eigvals(f::Function, A::ITensor, Linds, Rinds; kws...)
68+
Linds = isa(Linds, Index) ? [Linds] : collect(Linds)
69+
Rinds = isa(Rinds, Index) ? [Rinds] : collect(Rinds)
6670

6771
# <fermions>
68-
auto_fermion_enabled = ITensors.using_auto_fermion()
69-
if auto_fermion_enabled
72+
fermionic_itensor =
73+
ITensors.using_auto_fermion() && ITensors.has_fermionic_subspaces(inds(A))
74+
if fermionic_itensor
7075
# If fermionic, bring indices into i',j',..,dag(j),dag(i)
7176
# ordering with Out indices coming before In indices
7277
# Resulting tensor acts like a normal matrix (no extra signs
7378
# when taking powers A^n)
74-
if all(j->dir(j)==Out, Linds) && all(j->dir(j)==In, Rinds)
79+
if all(j->dir(j)==ITensors.Out, Linds) && all(j->dir(j)==ITensors.In, Rinds)
7580
ordered_inds = [Linds..., reverse(Rinds)...]
76-
elseif all(j->dir(j)==Out, Rinds) && all(j->dir(j)==In, Linds)
81+
elseif all(j->dir(j)==ITensors.Out, Rinds) && all(j->dir(j)==ITensors.In, Linds)
7782
ordered_inds = [Rinds..., reverse(Linds)...]
7883
else
7984
error(
@@ -96,7 +101,7 @@ function map_eigvals(f::Function, A::ITensor, Linds, Rinds; kws...)
96101
end
97102

98103
# <fermions>
99-
if auto_fermion_enabled
104+
if fermionic_itensor
100105
# Ensure expA indices in "matrix" form before re-enabling fermion system
101106
mapped_A = permute(mapped_A, ordered_inds)
102107
ITensors.enable_auto_fermion()

test/test_itensorsextensions.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ using Test: @test, @testset
7979
end
8080

8181
@testset "Fermionic eigendecomp" begin
82+
ITensors.enable_auto_fermion()
8283
s1 = Index([QN("Nf", 0, -1)=>2, QN("Nf", 1, -1)=>2], "Site,Fermion,n=1")
8384
s2 = Index([QN("Nf", 0, -1)=>2, QN("Nf", 1, -1)=>2], "Site,Fermion,n=2")
8485

@@ -90,9 +91,11 @@ using Test: @test, @testset
9091
Ul, D, Ur = eigendecomp(T, [s1', s2'], [dag(s1), dag(s2)]; ishermitian=true)
9192

9293
@test Ul*D*Ur T
94+
ITensors.disable_auto_fermion()
9395
end
9496

9597
@testset "Fermionic map eigvals tests" begin
98+
ITensors.enable_auto_fermion()
9699
s1 = Index([QN("Nf", 0, -1)=>2, QN("Nf", 1, -1)=>2], "Site,Fermion,n=1")
97100
s2 = Index([QN("Nf", 0, -1)=>2, QN("Nf", 1, -1)=>2], "Site,Fermion,n=2")
98101

@@ -125,6 +128,15 @@ using Test: @test, @testset
125128
@test T apply(sqrtT, sqrtT)
126129
sqrtT = map_eigvals(sqrt, T, [dag(s1), dag(s2)], [s1', s2'], ; ishermitian=true)
127130
@test T apply(sqrtT, sqrtT)
131+
132+
# Test bosonic index case while fermion system is enabled
133+
b = Index([QN("Nb", 0)=>2, QN("Nb", 1)=>2])
134+
T = random_itensor(b', dag(b))
135+
T = apply(T, swapprime(dag(T), 0=>1))
136+
sqrtT = map_eigvals(sqrt, T, b', dag(b); ishermitian=true)
137+
@test T apply(sqrtT, sqrtT)
138+
139+
ITensors.disable_auto_fermion()
128140
end
129141
end
130142
end

0 commit comments

Comments
 (0)