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

Fix index dimension mismatch in TensorNetwork construction #219

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/TensorNetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ struct TensorNetwork <: AbstractTensorNetwork
tensormap = IdDict{Tensor,Vector{Symbol}}(tensor => inds(tensor) for tensor in tensors)

indexmap = reduce(tensors; init=Dict{Symbol,Vector{Tensor}}()) do dict, tensor
# TODO check for inconsistent dimensions?
for index in inds(tensor)
# TODO use lambda? `Tensor[]` might be reused
push!(get!(dict, index, Tensor[]), tensor)
end
dict
end

# Check for inconsistent index dimensions
for ind in keys(indexmap)
dims = map(tensor -> size(tensor, ind), indexmap[ind])
length(unique(dims)) == 1 || throw(DimensionMismatch("Index $(ind) has inconsistent dimension: $(dims)"))
end

return new(indexmap, tensormap, CachedField{Vector{Tensor}}())
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/TensorNetwork_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@testset "TensorNetwork with tensors of different dimensions" begin
tensor1 = Tensor(zeros(2, 2), (:i, :j))
tensor2 = Tensor(zeros(3, 3), (:j, :k))
@test_skip @test_throws DimensionMismatch tn = TensorNetwork([tensor1, tensor2])
@test_throws DimensionMismatch tn = TensorNetwork([tensor1, tensor2])
end
end

Expand Down
Loading