Skip to content

Commit

Permalink
ensure expval returns normalized values
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Dec 8, 2024
1 parent 1ad24a5 commit d0b0f25
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/algorithms/expval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,38 @@ function expectation_value(ψ::AbstractMPS, (inds, O)::Pair)
# some special cases that avoid using transfer matrices
if length(sites) == 1
AC = ψ.AC[sites[1]]
return @plansor conj(AC[4 5; 6]) *
conj(Ut[1]) * local_mpo[1][1 5; 3 2] * Ut[2] *
AC[4 3; 6]
end
if length(sites) == 2 && (sites[1] + 1 == sites[2])
E = @plansor conj(AC[4 5; 6]) *

Check warning on line 58 in src/algorithms/expval.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/expval.jl#L58

Added line #L58 was not covered by tests
conj(Ut[1]) * local_mpo[1][1 5; 3 2] * Ut[2] *
AC[4 3; 6]
elseif length(sites) == 2 && (sites[1] + 1 == sites[2])
AC = ψ.AC[sites[1]]
AR = ψ.AR[sites[2]]
O1, O2 = local_mpo
return @plansor conj(AC[4 5; 10]) * conj(Ut[1]) * O1[1 5; 3 8] * AC[4 3; 6] *
conj(AR[10 9; 11]) * Ut[2] * O2[8 9; 7 2] * AR[6 7; 11]
end

# generic case: write as Vl * T^N * Vr
# left side
T = storagetype(site_type(ψ))
@plansor Vl[-1 -2; -3] := isomorphism(T,
left_virtualspace(ψ, sites[1] - 1),
left_virtualspace(ψ, sites[1] - 1))[-1; -3] *
conj(Ut[-2])

# middle
M = foldl(zip(sites, local_mpo); init=Vl) do v, (site, o)
if o isa Number
return scale!(v * TransferMatrix.AL[site], ψ.AL[site]), o)
else
return v * TransferMatrix.AL[site], o, ψ.AL[site])
E = @plansor conj(AC[4 5; 10]) * conj(Ut[1]) * O1[1 5; 3 8] * AC[4 3; 6] *
conj(AR[10 9; 11]) * Ut[2] * O2[8 9; 7 2] * AR[6 7; 11]
else
# generic case: write as Vl * T^N * Vr
# left side
T = storagetype(site_type(ψ))
@plansor Vl[-1 -2; -3] := isomorphism(T,

Check warning on line 71 in src/algorithms/expval.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/expval.jl#L70-L71

Added lines #L70 - L71 were not covered by tests
left_virtualspace(ψ, sites[1] - 1),
left_virtualspace(ψ, sites[1] - 1))[-1; -3] *
conj(Ut[-2])

# middle
M = foldl(zip(sites, local_mpo); init=Vl) do v, (site, o)
if o isa Number
return scale!(v * TransferMatrix.AL[site], ψ.AL[site]), o)

Check warning on line 79 in src/algorithms/expval.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/expval.jl#L77-L79

Added lines #L77 - L79 were not covered by tests
else
return v * TransferMatrix.AL[site], o, ψ.AL[site])

Check warning on line 81 in src/algorithms/expval.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/expval.jl#L81

Added line #L81 was not covered by tests
end
end

# right side
E = @plansor M[1 2; 3] * Ut[2] * ψ.CR[sites[end]][3; 4] *

Check warning on line 86 in src/algorithms/expval.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/expval.jl#L86

Added line #L86 was not covered by tests
conj.CR[sites[end]][1; 4])
end

# right side
E = @plansor M[1 2; 3] * Ut[2] * ψ.CR[sites[end]][3; 4] *
conj.CR[sites[end]][1; 4])
return E / norm(ψ)^2
end

Expand Down

0 comments on commit d0b0f25

Please sign in to comment.