-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
327 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
""" | ||
By default, elementwise multiplication will be performed. | ||
This function is kind of deprecated and will be removed in the future. | ||
""" | ||
function automul( | ||
M1::BlockedMPS, | ||
M2::BlockedMPS; | ||
tag_row::String="", | ||
tag_shared::String="", | ||
tag_col::String="", | ||
alg="naive", | ||
maxdim=typemax(Int), | ||
cutoff=1e-25, | ||
kwargs..., | ||
) | ||
all(length.(siteinds(M1)) .== 1) || error("M1 should have only 1 site index per site") | ||
all(length.(siteinds(M2)) .== 1) || error("M2 should have only 1 site index per site") | ||
|
||
sites_row = _findallsiteinds_by_tag(M1; tag=tag_row) | ||
sites_shared = _findallsiteinds_by_tag(M1; tag=tag_shared) | ||
sites_col = _findallsiteinds_by_tag(M2; tag=tag_col) | ||
sites_matmul = Set(Iterators.flatten([sites_row, sites_shared, sites_col])) | ||
|
||
sites1 = only.(siteinds(M1)) | ||
sites1_ewmul = setdiff(only.(siteinds(M1)), sites_matmul) | ||
sites2_ewmul = setdiff(only.(siteinds(M2)), sites_matmul) | ||
sites2_ewmul == sites1_ewmul || error("Invalid sites for elementwise multiplication") | ||
|
||
M1 = _makesitediagonal(M1, sites1_ewmul; baseplev=1) | ||
M2 = _makesitediagonal(M2, sites2_ewmul; baseplev=0) | ||
|
||
sites_M1_diag = [collect(x) for x in siteinds(M1)] | ||
sites_M2_diag = [collect(x) for x in siteinds(M2)] | ||
|
||
M1 = rearrange_siteinds(M1, combinesites(sites_M1_diag, sites_row, sites_shared)) | ||
|
||
M2 = rearrange_siteinds(M2, combinesites(sites_M2_diag, sites_shared, sites_col)) | ||
|
||
M = contract(M1, M2; alg=alg, kwargs...) | ||
|
||
M = extractdiagonal(M, sites1_ewmul) | ||
|
||
ressites = Vector{eltype(siteinds(M1)[1])}[] | ||
for s in siteinds(M) | ||
s_ = unique(ITensors.noprime.(s)) | ||
if length(s_) == 1 | ||
push!(ressites, s_) | ||
else | ||
if s_[1] ∈ sites1 | ||
push!(ressites, [s_[1]]) | ||
push!(ressites, [s_[2]]) | ||
else | ||
push!(ressites, [s_[2]]) | ||
push!(ressites, [s_[1]]) | ||
end | ||
end | ||
end | ||
return truncate(rearrange_siteinds(M, ressites); cutoff=cutoff, maxdim=maxdim) | ||
end | ||
|
||
function combinesites( | ||
sites::Vector{Vector{Index{IndsT}}}, | ||
site1::AbstractVector{Index{IndsT}}, | ||
site2::AbstractVector{Index{IndsT}}, | ||
) where {IndsT} | ||
length(site1) == length(site2) || error("Length mismatch") | ||
for (s1, s2) in zip(site1, site2) | ||
sites = combinesites(sites, s1, s2) | ||
end | ||
return sites | ||
end | ||
|
||
function combinesites( | ||
sites::Vector{Vector{Index{IndsT}}}, site1::Index, site2::Index | ||
) where {IndsT} | ||
sites = deepcopy(sites) | ||
p1 = findfirst(x -> x[1] == site1, sites) | ||
p2 = findfirst(x -> x[1] == site2, sites) | ||
if p1 === nothing || p2 === nothing | ||
error("Site not found") | ||
end | ||
if abs(p1 - p2) != 1 | ||
error("Sites are not adjacent") | ||
end | ||
deleteat!(sites, min(p1, p2)) | ||
deleteat!(sites, min(p1, p2)) | ||
insert!(sites, min(p1, p2), [site1, site2]) | ||
return sites | ||
end | ||
|
||
function _findallsiteinds_by_tag(M::BlockedMPS; tag=tag) | ||
return findallsiteinds_by_tag(only.(siteinds(M)); tag=tag) | ||
end | ||
|
||
# The following code is copied from Quantics.jl | ||
|
||
function findallsiteinds_by_tag( | ||
sites::AbstractVector{Index{T}}; tag::String="x", maxnsites::Int=1000 | ||
) where {T} | ||
_valid_tag(tag) || error("Invalid tag: $tag") | ||
positions = findallsites_by_tag(sites; tag=tag, maxnsites=maxnsites) | ||
return [sites[p] for p in positions] | ||
end | ||
|
||
function findallsites_by_tag( | ||
sites::Vector{Index{T}}; tag::String="x", maxnsites::Int=1000 | ||
)::Vector{Int} where {T} | ||
_valid_tag(tag) || error("Invalid tag: $tag") | ||
result = Int[] | ||
for n in 1:maxnsites | ||
tag_ = tag * "=$n" | ||
idx = findall(hastags(tag_), sites) | ||
if length(idx) == 0 | ||
break | ||
elseif length(idx) > 1 | ||
error("Found more than one site indices with $(tag_)!") | ||
end | ||
push!(result, idx[1]) | ||
end | ||
return result | ||
end | ||
|
||
_valid_tag(tag::String)::Bool = !occursin("=", tag) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Test | ||
|
||
using ITensors | ||
import ProjMPSs: ProjMPSs, Projector, project, ProjMPS, projcontract, BlockedMPS | ||
import FastMPOContractions as FMPOC | ||
using Quantics: Quantics | ||
|
||
@testset "mul.jl" begin | ||
""" | ||
Reconstruct 3D matrix | ||
""" | ||
function _tomat3(a::MPS) | ||
sites = siteinds(a) | ||
N = length(sites) | ||
Nreduced = N ÷ 3 | ||
sites_ = [sites[1:3:N]..., sites[2:3:N]..., sites[3:3:N]...] | ||
return reshape(Array(reduce(*, a), sites_), 2^Nreduced, 2^Nreduced, 2^Nreduced) | ||
end | ||
|
||
@testset "batchedmatmul" for T in [Float64] | ||
""" | ||
C(x, z, k) = sum_y A(x, y, k) * B(y, z, k) | ||
""" | ||
nbit = 2 | ||
D = 2 | ||
cutoff = 1e-25 | ||
sx = [Index(2, "Qubit,x=$n") for n in 1:nbit] | ||
sy = [Index(2, "Qubit,y=$n") for n in 1:nbit] | ||
sz = [Index(2, "Qubit,z=$n") for n in 1:nbit] | ||
sk = [Index(2, "Qubit,k=$n") for n in 1:nbit] | ||
|
||
sites_a = collect(Iterators.flatten(zip(sx, sy, sk))) | ||
sites_b = collect(Iterators.flatten(zip(sy, sz, sk))) | ||
|
||
a = random_mps(T, sites_a; linkdims=D) | ||
b = random_mps(T, sites_b; linkdims=D) | ||
|
||
# Reference data | ||
a_arr = _tomat3(a) | ||
b_arr = _tomat3(b) | ||
ab_arr = zeros(T, 2^nbit, 2^nbit, 2^nbit) | ||
for k in 1:(2^nbit) | ||
ab_arr[:, :, k] .= a_arr[:, :, k] * b_arr[:, :, k] | ||
end | ||
|
||
a_ = BlockedMPS([ | ||
project(a, Projector(Dict(sx[1] => 1, sy[1] => 1))), | ||
project(a, Projector(Dict(sx[1] => 1, sy[1] => 2))), | ||
project(a, Projector(Dict(sx[1] => 2, sy[1] => 1))), | ||
project(a, Projector(Dict(sx[1] => 2, sy[1] => 2))), | ||
]) | ||
|
||
b_ = BlockedMPS([ | ||
project(b, Projector(Dict(sy[1] => 1, sz[1] => 1))), | ||
project(b, Projector(Dict(sy[1] => 1, sz[1] => 2))), | ||
project(b, Projector(Dict(sy[1] => 2, sz[1] => 1))), | ||
project(b, Projector(Dict(sy[1] => 2, sz[1] => 2))), | ||
]) | ||
|
||
@test a ≈ MPS(a_) | ||
@test b ≈ MPS(b_) | ||
|
||
ab = ProjMPSs.automul( | ||
a_, b_; tag_row="x", tag_shared="y", tag_col="z", alg="fit", cutoff | ||
) | ||
ab_ref = Quantics.automul( | ||
a, b; tag_row="x", tag_shared="y", tag_col="z", alg="fit", cutoff | ||
) | ||
|
||
@test MPS(ab) ≈ ab_ref rtol = 10 * sqrt(cutoff) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters