From 9f112b3db1650af917d6701d834fbcfca3a09994 Mon Sep 17 00:00:00 2001 From: Matthew Fishman Date: Thu, 28 May 2026 14:55:32 -0400 Subject: [PATCH] Loosen `linkdim(::AbstractITensorNetwork, ::AbstractEdge)` signature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- src/abstractitensornetwork.jl | 2 +- test/test_itensornetwork.jl | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/abstractitensornetwork.jl b/src/abstractitensornetwork.jl index 4cee74b1..8f2daa30 100644 --- a/src/abstractitensornetwork.jl +++ b/src/abstractitensornetwork.jl @@ -556,7 +556,7 @@ function linkdim(tn::AbstractITensorNetwork, edge::Pair) return linkdim(tn, edgetype(tn)(edge)) end -function linkdim(tn::AbstractITensorNetwork{V}, edge::AbstractEdge{V}) where {V} +function linkdim(tn::AbstractITensorNetwork, edge::AbstractEdge) ls = linkinds(tn, edge) return prod([isnothing(l) ? 1 : dim(l) for l in ls]) end diff --git a/test/test_itensornetwork.jl b/test/test_itensornetwork.jl index 95336570..0ef33803 100644 --- a/test/test_itensornetwork.jl +++ b/test/test_itensornetwork.jl @@ -250,4 +250,16 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) tn = random_tensornetwork(rng, is; link_space = 3) @test_broken swapprime(tn, 0, 2) end + + # Regression test for https://github.com/ITensor/ITensorNetworks.jl/pull/373. + @testset "linkdim accepts an AbstractEdge whose vertex type is a subtype" begin + i = Index(2) + A = ITensor(i) + B = ITensor(i) + tn = ITensorNetwork{Any}(Dictionary([1, 2], [A, B])) + @test tn isa ITensorNetwork{Any} + e = NamedEdge(1 => 2) + @test e isa NamedEdge{Int} + @test ITensorNetworks.linkdim(tn, e) == 2 + end end