Skip to content

Refactor TensorNetwork type; add NormNetwork struct. #119

Draft
jack-dunham wants to merge 93 commits into
mainfrom
jd/tensornetwork-refactor
Draft

Refactor TensorNetwork type; add NormNetwork struct. #119
jack-dunham wants to merge 93 commits into
mainfrom
jd/tensornetwork-refactor

Conversation

@jack-dunham

Copy link
Copy Markdown
Contributor

This PR refactors the TensorNetwork data type to include a reverse index map, and to be stricter about it's construction and how it's tensors can be set.

This PR also adds NormNetwork type, as a wrapper around a TensorNetwork, to represent the norm of a tensor network.

JoeyT1994 and others added 30 commits January 6, 2026 09:55
Introduce `BeliefPropagationProblem` wrapper to hold the cache and the
error `diff` field.

Also simplifies some kwargs wrangling.
Also includes some fixes to the way `TensorNetwork` types are
constructed based on index structure.
…instead of trying to operate on existing graphs

The reason for this is:
- One only cares about the edges of the input graph
- A simple graph cannot be used as it "forgets" its edge names resulting
in recursion
- As shown with `TensorNetwork`, removing edges may not always be
defined.
This was caused by the change to the `cache` being backed by a directed
graph.
@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 56.54450% with 83 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.32%. Comparing base (1b5ea47) to head (7c51b20).

Files with missing lines Patch % Lines
src/normnetwork.jl 0.00% 32 Missing ⚠️
src/abstracttensornetwork.jl 5.00% 19 Missing ⚠️
src/tensornetwork.jl 80.61% 19 Missing ⚠️
src/beliefpropagation/messagecache.jl 60.60% 13 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (1b5ea47) and HEAD (7c51b20). Click for more details.

HEAD has 7 uploads less than BASE
Flag BASE (1b5ea47) HEAD (7c51b20)
6 1
docs 3 1
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #119      +/-   ##
==========================================
- Coverage   73.30%   65.32%   -7.98%     
==========================================
  Files          21       19       -2     
  Lines         929      845      -84     
==========================================
- Hits          681      552     -129     
- Misses        248      293      +45     
Flag Coverage Δ
docs 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ITensorBot

ITensorBot commented Jun 8, 2026

Copy link
Copy Markdown
Member

Your PR no longer requires formatting changes. Thank you for your contribution!

@jack-dunham jack-dunham force-pushed the jd/tensornetwork-refactor branch from 1310cc2 to 7c51b20 Compare June 9, 2026 20:34
Comment thread src/normnetwork.jl
Comment on lines +41 to +43
B = conjbra(nn, vertex)
# TODO: implement and use a lazy `conj` via `LazyNamedDimsArrays` here?
return lazy(A) * lazy(conj(B))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can define bra(nn, vertex) = conj(conjbra(nn, vertex))? Agreed we should probably define a lazy conj, it is something that has come up a few places, but eager is ok for now.

Comment thread src/normnetwork.jl
ket(nn::NormNetwork, vertex) = nn.tensornetwork[vertex]
conjbra(nn::NormNetwork, vertex) = replacedimnames(n -> namemap(nn, n), ket(nn, vertex))

lazy_norm(tn::TensorNetwork) = NormNetwork(tn)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we were calling this norm_network in ITensorNetworksNext. I kind of prefer that name since it indicates what kind of lazy representation it is, i.e. one could imagine lazy_norm might output a lazy expression for the norm. I think norm_network is a bit more descriptive.

return tn
end
# Return the vertices associated with an index.
function indsites(tn::AbstractGraph, ind)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe dimnamevertices?

for e in edges(graph)
show(io, mime, e)
println(io)
function has_indname(tn::AbstractGraph, name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has_dimname?

Comment thread src/normnetwork.jl
index_map = Dictionary{I, I}()
for (name, vertices) in pairs(tn.index_locations)
if length(vertices) == 2
insert!(index_map, name, randname(name))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to have an interface where we can specify the ket names (say if you have a set of messages and want a norm network that matches the names of the messages).

Comment thread src/tensornetwork.jl
function TensorNetwork(f::Base.Callable, graph::AbstractGraph)
return TensorNetwork(graph, Dictionary(map(f, vertices(graph))))
end
for ind in dimnames(tensor)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The terminology is definitely kind of tricky. I think if something is just a name, we should refer to it as dimname or name. In my mind, ind would refer to a named axis, though I've gone back and forth about whether we should still use the ind/inds terminology from ITensors.jl or embrace the axis/axes terminology from Base Julia...

Comment thread src/tensornetwork.jl

struct TensorNetwork{T, V, I} <: AbstractTensorNetwork{T, V}
tensors::Dictionary{V, T}
index_locations::Dictionary{I, Set{V}}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a better name would be dimname_vertices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants