From 4308fa28420d5d73ff393a356456c89f48b302a0 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Fri, 27 Feb 2026 15:15:09 +0100 Subject: [PATCH 1/6] update expval with styles --- src/algorithms/expval.jl | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/algorithms/expval.jl b/src/algorithms/expval.jl index a9e25f16d..5da35f700 100644 --- a/src/algorithms/expval.jl +++ b/src/algorithms/expval.jl @@ -121,16 +121,24 @@ function contract_mpo_expval2( τ[3 4; 9 10] * A1[8 1 2; 5] * A2[5 7 13; 14] * O[10 12; 6 7] end +function expectation_value(ψ::AbstractMPS, H::AbstractMPO) + return expectation_value(GeometryStyle(ψ, H), OperatorStyle(H), ψ, H) +end function expectation_value( - ψ::FiniteMPS, H::FiniteMPOHamiltonian, - envs::AbstractMPSEnvironments = environments(ψ, H) + ψ::AbstractMPS, H::AbstractMPO, + envs::AbstractMPSEnvironments + ) + return expectation_value(GeometryStyle(ψ, H, envs), OperatorStyle(H), ψ, H, envs) +end + +function expectation_value(::FiniteChainStyle, ::HamiltonianStyle, + ψ, H, envs = environments(ψ, H) ) return dot(ψ, H, ψ, envs) / dot(ψ, ψ) end -function expectation_value( - ψ::InfiniteMPS, H::InfiniteMPOHamiltonian, - envs::AbstractMPSEnvironments = environments(ψ, H) +function expectation_value(::InfiniteChainStyle, ::HamiltonianStyle, + ψ, H, envs = environments(ψ, H) ) return sum(1:length(ψ)) do site return contract_mpo_expval( @@ -152,13 +160,13 @@ end # DenseMPO # -------- -function expectation_value(ψ::FiniteMPS, mpo::FiniteMPO) +function expectation_value(::FiniteChainStyle, ::MPOStyle, ψ, mpo) return dot(ψ, mpo, ψ) / dot(ψ, ψ) end function expectation_value(ψ::FiniteQP, mpo::FiniteMPO) return expectation_value(convert(FiniteMPS, ψ), mpo) end -function expectation_value(ψ::InfiniteMPS, mpo::InfiniteMPO, envs...) +function expectation_value(ψ::InfiniteMPS, mpo::InfiniteMPO, envs...) # TODO: Discuss style convention for multiline! return expectation_value(convert(MultilineMPS, ψ), convert(MultilineMPO, mpo), envs...) end function expectation_value( @@ -176,7 +184,9 @@ function expectation_value(ψ::MultilineMPS, mpo::MultilineMPO, envs...) return prod(x -> expectation_value(x...), zip(parent(ψ), parent(mpo))) end # fallback -function expectation_value(ψ::AbstractMPS, mpo::AbstractMPO, envs...) +function expectation_value(::GeometryStyle, ::OperatorStyle, + ψ::AbstractMPS, mpo::AbstractMPO, envs... + ) return dot(ψ, mpo, ψ) / dot(ψ, ψ) end From bc3e44974ee1363f7d59d7445817385d59f3061d Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Fri, 27 Feb 2026 15:22:12 +0100 Subject: [PATCH 2/6] for now there are no styles for environments, so I will not dispatch on it --- src/algorithms/expval.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/expval.jl b/src/algorithms/expval.jl index 5da35f700..9d4593ade 100644 --- a/src/algorithms/expval.jl +++ b/src/algorithms/expval.jl @@ -128,7 +128,7 @@ function expectation_value( ψ::AbstractMPS, H::AbstractMPO, envs::AbstractMPSEnvironments ) - return expectation_value(GeometryStyle(ψ, H, envs), OperatorStyle(H), ψ, H, envs) + return expectation_value(GeometryStyle(ψ, H), OperatorStyle(H), ψ, H, envs) end function expectation_value(::FiniteChainStyle, ::HamiltonianStyle, From 4e0eebb09307126b9c13ef8156bbbcab94ca367c Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Fri, 27 Feb 2026 15:28:12 +0100 Subject: [PATCH 3/6] format --- src/algorithms/expval.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/algorithms/expval.jl b/src/algorithms/expval.jl index 9d4593ade..f68baf643 100644 --- a/src/algorithms/expval.jl +++ b/src/algorithms/expval.jl @@ -131,13 +131,15 @@ function expectation_value( return expectation_value(GeometryStyle(ψ, H), OperatorStyle(H), ψ, H, envs) end -function expectation_value(::FiniteChainStyle, ::HamiltonianStyle, +function expectation_value( + ::FiniteChainStyle, ::HamiltonianStyle, ψ, H, envs = environments(ψ, H) ) return dot(ψ, H, ψ, envs) / dot(ψ, ψ) end -function expectation_value(::InfiniteChainStyle, ::HamiltonianStyle, +function expectation_value( + ::InfiniteChainStyle, ::HamiltonianStyle, ψ, H, envs = environments(ψ, H) ) return sum(1:length(ψ)) do site @@ -184,7 +186,8 @@ function expectation_value(ψ::MultilineMPS, mpo::MultilineMPO, envs...) return prod(x -> expectation_value(x...), zip(parent(ψ), parent(mpo))) end # fallback -function expectation_value(::GeometryStyle, ::OperatorStyle, +function expectation_value( + ::GeometryStyle, ::OperatorStyle, ψ::AbstractMPS, mpo::AbstractMPO, envs... ) return dot(ψ, mpo, ψ) / dot(ψ, ψ) From edf69a3fd47874e907852ff6045e575fb1e18abb Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Fri, 27 Feb 2026 17:15:59 +0100 Subject: [PATCH 4/6] fix dispatch ambiguity --- src/algorithms/expval.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/expval.jl b/src/algorithms/expval.jl index f68baf643..faa98b917 100644 --- a/src/algorithms/expval.jl +++ b/src/algorithms/expval.jl @@ -188,7 +188,7 @@ end # fallback function expectation_value( ::GeometryStyle, ::OperatorStyle, - ψ::AbstractMPS, mpo::AbstractMPO, envs... + ψ, mpo, envs... ) return dot(ψ, mpo, ψ) / dot(ψ, ψ) end From a8d30102e81416883f93b221535ec5c9bef0b090 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Fri, 27 Feb 2026 18:28:11 +0100 Subject: [PATCH 5/6] fix --- src/algorithms/expval.jl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/algorithms/expval.jl b/src/algorithms/expval.jl index faa98b917..1721b75ab 100644 --- a/src/algorithms/expval.jl +++ b/src/algorithms/expval.jl @@ -121,14 +121,8 @@ function contract_mpo_expval2( τ[3 4; 9 10] * A1[8 1 2; 5] * A2[5 7 13; 14] * O[10 12; 6 7] end -function expectation_value(ψ::AbstractMPS, H::AbstractMPO) - return expectation_value(GeometryStyle(ψ, H), OperatorStyle(H), ψ, H) -end -function expectation_value( - ψ::AbstractMPS, H::AbstractMPO, - envs::AbstractMPSEnvironments - ) - return expectation_value(GeometryStyle(ψ, H), OperatorStyle(H), ψ, H, envs) +function expectation_value(ψ::AbstractMPS, H::AbstractMPO, envs...) + return expectation_value(GeometryStyle(ψ, H), OperatorStyle(H), ψ, H, envs...) end function expectation_value( @@ -168,7 +162,10 @@ end function expectation_value(ψ::FiniteQP, mpo::FiniteMPO) return expectation_value(convert(FiniteMPS, ψ), mpo) end -function expectation_value(ψ::InfiniteMPS, mpo::InfiniteMPO, envs...) # TODO: Discuss style convention for multiline! +function expectation_value( + ::InfiniteChainStyle, ::MPOStyle, + ψ::InfiniteMPS, mpo::InfiniteMPO, envs... + ) # TODO: Discuss style convention for multiline! return expectation_value(convert(MultilineMPS, ψ), convert(MultilineMPO, mpo), envs...) end function expectation_value( From 611a7e493f687945d97344597b4ec1f1a0a5b6a2 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Fri, 27 Feb 2026 18:35:29 +0100 Subject: [PATCH 6/6] format --- src/algorithms/expval.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/expval.jl b/src/algorithms/expval.jl index 1721b75ab..a56bd207e 100644 --- a/src/algorithms/expval.jl +++ b/src/algorithms/expval.jl @@ -163,7 +163,7 @@ function expectation_value(ψ::FiniteQP, mpo::FiniteMPO) return expectation_value(convert(FiniteMPS, ψ), mpo) end function expectation_value( - ::InfiniteChainStyle, ::MPOStyle, + ::InfiniteChainStyle, ::MPOStyle, ψ::InfiniteMPS, mpo::InfiniteMPO, envs... ) # TODO: Discuss style convention for multiline! return expectation_value(convert(MultilineMPS, ψ), convert(MultilineMPO, mpo), envs...)