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 DimensionMismatch not thrown in TensorNetwork constructor #72

Merged
merged 5 commits into from
Jul 12, 2023
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
6 changes: 6 additions & 0 deletions src/TensorNetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ struct TensorNetwork{A<:Ansatz,M<:NamedTuple}
mergewith(vcat, dict, Dict([index => [i] for index in labels(tensor)]))
end

# Check for inconsistent dimensions
for (index, idxs) in indices
allequal(Iterators.map(i -> size(tensors[i], index), idxs)) ||
throw(DimensionMismatch("Different sizes specified for index $index"))
end

M = Tenet.metadata(A)
metadata = M((; metadata...))

Expand Down
2 changes: 1 addition & 1 deletion test/Quantum_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)

operator = TensorNetwork{MockOperator}(
[Tensor(rand(2, 4, 2), (:a, :c, :d)), Tensor(rand(3, 4, 2, 5), (:b, :c, :e, :f))];
[Tensor(rand(2, 4, 2), (:a, :c, :d)), Tensor(rand(3, 4, 3, 5), (:b, :c, :e, :f))];
plug = Operator,
interlayer = [Bijection(Dict([1 => :a, 2 => :b])), Bijection(Dict([1 => :d, 2 => :e]))],
)
Expand Down
6 changes: 6 additions & 0 deletions test/TensorNetwork_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
@test size(tn) == Dict(:i => 2, :j => 3)
@test issetequal(labels(tn, :open), [:i, :j])
@test isempty(labels(tn, :hyper))

tensor1 = Tensor(zeros(2, 2), (:i, :j))
tensor2 = Tensor(zeros(3, 3), (:j, :k))
@test_throws DimensionMismatch tn = TensorNetwork([tensor1, tensor2])
end
end

Expand All @@ -32,6 +36,8 @@
@test size(tn) == Dict(:i => 2, :j => 2, :k => 2)
@test issetequal(labels(tn, :open), [:i, :j, :k])
@test isempty(labels(tn, :hyper))

@test_throws DimensionMismatch push!(tn, Tensor(zeros(3, 3), (:i, :j)))
end

@test_throws Exception begin
Expand Down
Loading