Skip to content

Commit 28b2a91

Browse files
committed
Support swap gate for MPOs
1 parent bbb2bec commit 28b2a91

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/operators/mpo.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,35 @@ function Base.isapprox(
382382
# don't take square roots to avoid precision loss
383383
return norm₁₂² max(atol^2, rtol^2 * max(norm₁², norm₂²))
384384
end
385+
386+
"""
387+
swap(mpo::FiniteMPO, i::Integer; inv::Bool=false, alg=SDD(), trscheme)
388+
swap!(mpo::FiniteMPO, i::Integer; inv::Bool=false, alg=SDD(), trscheme)
389+
390+
Compose the mpo with a swap gate applied to indices `i` and `i + 1`, effectively creating an
391+
operator that acts on the Hilbert spaces with those factors swapped.
392+
The keyword arguments `alg` and `trscheme` can be used to control how the resulting tensor
393+
is truncated again.
394+
"""
395+
swap(mpo::FiniteMPO{<:MPOTensor}, i::Integer; kwargs...) = swap!(copy(mpo), i; kwargs...)
396+
function swap!(
397+
mpo::FiniteMPO{<:MPOTensor}, i::Integer;
398+
inv::Bool = false,
399+
alg = SDD(), trscheme = truncbelow(eps(real(scalartype(mpo)))^(4 / 5))
400+
)
401+
O₁, O₂ = mpo[i], mpo[i + 1]
402+
403+
if inv
404+
@plansor O₂₁[-1 -2 -3; -4 -5 -6] :=
405+
τ'[-3 -6; 4 5] * O₁[-2 4; 2 1] * O₂[1 5; 3 -5] * τ[2 3; -1 -4]
406+
else
407+
@plansor O₂₁[-1 -2 -3; -4 -5 -6] :=
408+
τ[-3 -6; 4 5] * O₁[-2 4; 2 1] * O₂[1 5; 3 -5] * τ'[2 3; -1 -4]
409+
end
410+
411+
U, S, Vᴴ, = tsvd!(O₂₁; alg, trunc = trscheme)
412+
sqrtS = sqrt(S)
413+
@plansor mpo[i][-1 -2; -3 -4] := U[-3 -1 -2; 1] * sqrtS[1; -4]
414+
@plansor mpo[i + 1][-1 -2; -3 -4] := sqrtS[-1; 1] * Vᴴ[1; -3 -4 -2]
415+
return mpo
416+
end

0 commit comments

Comments
 (0)