Skip to content

Commit 1fde7ef

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 1fde7ef

2 files changed

Lines changed: 19 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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,22 @@ 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+
# When the network's vertex-type parameter is abstract (e.g. `Any`,
255+
# or `Tuple` for graphs like `named_binary_tree` whose vertices have
256+
# varying arity), it is common to encounter `AbstractEdge`s whose vertex
257+
# type is a concrete subtype. The previous
258+
# `linkdim(::AbstractITensorNetwork{V}, ::AbstractEdge{V}) where V` failed
259+
# to dispatch in that case; the loosened signature accepts any compatible
260+
# edge.
261+
@testset "linkdim accepts an AbstractEdge whose vertex type is a subtype" begin
262+
i = Index(2)
263+
A = ITensor(i)
264+
B = ITensor(i)
265+
tn = ITensorNetwork{Any}(Dictionary([1, 2], [A, B]))
266+
@test tn isa ITensorNetwork{Any}
267+
e = NamedEdge(1 => 2)
268+
@test e isa NamedEdge{Int}
269+
@test ITensorNetworks.linkdim(tn, e) == 2
270+
end
253271
end

0 commit comments

Comments
 (0)