Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ITensorMPS] Bond dimensions produced by randomCircuitMPS #870

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.3.18"
[deps]
BitIntegers = "c3b6d118-76ef-56ca-8cc7-ebb389d030a1"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
Expand Down
19 changes: 16 additions & 3 deletions src/mps/mps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,28 @@ function randomCircuitMPS(

l = Vector{Index}(undef, N)

# Compute the bond dimension for each link.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have this as a separate function, so we could use it other places in the code and simplify the code here.

Maybe linkdims_upper_bound? It doesn't have to be exported, just internal use for now.

# It should be the the minimum of the product of local
# Hilbert space dimensions taken from either end of the chain
# or the `linkdim`.
maxdims = Vector{Int}(undef, N - 1)
maxdims[1] = min(dim(sites[1]), linkdim)
for i in 2:(N - 1)
maxdims[i] = min(maxdims[i - 1] * dim(sites[i]), linkdim)
end
maxdims[N - 1] = min(dim(sites[N]), maxdims[N - 1])
for i in (N - 2):-1:1
maxdims[i] = min(dim(sites[i + 1]) * maxdims[i + 1], maxdims[i])
end

d = dim(sites[N])
chi = min(linkdim, d)
chi = maxdims[N - 1]
l[N - 1] = Index(chi, "Link,l=$(N-1)")
O = NDTensors.random_unitary(ElT, chi, d)
M[N] = itensor(O, l[N - 1], sites[N])

for j in (N - 1):-1:2
chi *= dim(sites[j])
chi = min(linkdim, chi)
chi = maxdims[j - 1]
l[j - 1] = Index(chi, "Link,l=$(j-1)")
O = NDTensors.random_unitary(ElT, chi, dim(sites[j]) * dim(l[j]))
T = reshape(O, (chi, dim(sites[j]), dim(l[j])))
Expand Down
13 changes: 12 additions & 1 deletion test/mps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ include("util.jl")
@test norm(phic[4]) ≈ 1.0
end

@testset "randomMPS bond dimensions" begin
phi = randomMPS(ComplexF64, sites, 50)
expected_dims = [2, 4, 8, 16, 32, 16, 8, 4, 2]

for i in 1:9
l = linkind(phi, i)
@test dim(l) == expected_dims[i]
end
end

@testset "randomMPS with chi>1" for linkdims in [1, 4]
phi = randomMPS(Float32, sites; linkdims)
@test LinearAlgebra.promote_leaf_eltypes(phi) === Float32
Expand Down Expand Up @@ -482,7 +492,8 @@ include("util.jl")

ϕ2 = +(ψ1, ψ2; alg="directsum")
for j in 1:8
@test linkdim(ϕ2, j) == χ1 + χ2
#@test linkdim(ϕ2, j) == χ1 + χ2
@test linkdim(ϕ2, j) == linkdim(ψ1, j) + linkdim(ψ2, j)
end
@test inner(ϕ2, ψ1) + inner(ϕ2, ψ2) ≈ inner(ϕ2, ϕ2)
end
Expand Down