Skip to content

Commit 9f112b3

Browse files
mtfishmanclaude
andcommitted
Loosen linkdim(::AbstractITensorNetwork, ::AbstractEdge) signature
The previous signature `linkdim(tn::AbstractITensorNetwork{V}, edge::AbstractEdge{V}) where V` required the network's vertex-type parameter and the edge's vertex-type parameter to match exactly. For graphs with abstract V (e.g. the `NamedGraph{Tuple}` produced by `named_binary_tree`, where vertices are tuples of varying arity), downstream operations can produce edges whose V parameter is a strict subtype — for example, `subgraph(g, vs)` where `vs` came from a Dict's keys and concretized to `Tuple{Int64, Vararg{Int64}}`. In that situation dispatch failed with `MethodError` even though `linkinds` itself accepts the edge fine. Drop the `{V}` constraint so `linkdim` accepts any `AbstractEdge` alongside any `AbstractITensorNetwork`. Behavior is unchanged for cases that previously dispatched; previously-failing cases now go through. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent c0cb74d commit 9f112b3

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/abstractitensornetwork.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ function linkdim(tn::AbstractITensorNetwork, edge::Pair)
556556
return linkdim(tn, edgetype(tn)(edge))
557557
end
558558

559-
function linkdim(tn::AbstractITensorNetwork{V}, edge::AbstractEdge{V}) where {V}
559+
function linkdim(tn::AbstractITensorNetwork, edge::AbstractEdge)
560560
ls = linkinds(tn, edge)
561561
return prod([isnothing(l) ? 1 : dim(l) for l in ls])
562562
end

test/test_itensornetwork.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,16 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
250250
tn = random_tensornetwork(rng, is; link_space = 3)
251251
@test_broken swapprime(tn, 0, 2)
252252
end
253+
254+
# Regression test for https://github.com/ITensor/ITensorNetworks.jl/pull/373.
255+
@testset "linkdim accepts an AbstractEdge whose vertex type is a subtype" begin
256+
i = Index(2)
257+
A = ITensor(i)
258+
B = ITensor(i)
259+
tn = ITensorNetwork{Any}(Dictionary([1, 2], [A, B]))
260+
@test tn isa ITensorNetwork{Any}
261+
e = NamedEdge(1 => 2)
262+
@test e isa NamedEdge{Int}
263+
@test ITensorNetworks.linkdim(tn, e) == 2
264+
end
253265
end

0 commit comments

Comments
 (0)