Skip to content

Refactor TensorNetwork type; add NormNetwork struct. #119

Open
jack-dunham wants to merge 112 commits into
mainfrom
jd/tensornetwork-refactor
Open

Refactor TensorNetwork type; add NormNetwork struct. #119
jack-dunham wants to merge 112 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.
Comment thread src/tensornetwork.jl Outdated
Comment thread src/ITensorNetworksNext.jl
@jack-dunham jack-dunham marked this pull request as ready for review June 25, 2026 15:15
Comment thread src/beliefpropagation/beliefpropagation.jl

"""
similar_norm_message_env(tn) -> MessageCache
ketnames = linknames(KetView(nn), edge)

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.

Suggested change
ketnames = linknames(KetView(nn), edge)
ketnames = linknames(ketview, edge)

Comment thread src/normnetwork.jl

# ====================================== interface ======================================= #

tensornetwork(nn::NormNetwork) = nn.tensornetwork

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.

This doesn't seem like a good definition, my first reaction was that it converts to a flat TN (i.e. would flatten the bra with the ket). Field access or KetView seem like fine alternatives.

Comment thread src/normnetwork.jl
Comment on lines +64 to +69
function namemap(nn::NormNetwork, name)
if !has_dimname(nn.tensornetwork, name)
error("index name $name not found underlying tensor network.")
end
return get(nn.namemap, name, name)
end

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.

Is this supposed to take a ket name and map it to the corresponding bra name? If so I think a more descriptive name like braname would be better. Also, I'm confused by get(nn.namemap, name, name), if the name is already checked to be in the ket, why do we need get? Why not nn.namemap[name]? I.e. what would the fallback be for?

Comment thread src/normnetwork.jl

indmap(nn::NormNetwork, ind) = setname(conj(ind), namemap(nn, name(ind)))

ket(nn::NormNetwork, vertex) = nn.tensornetwork[vertex]

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'd prefer something more descriptive like ket_tensor to make it clearer what it is returning.

# environment-builder interface; the low-level `MessageCache` / `messagecache(...)`
# constructors are used internally. A parallel `*_norm_ctm_env` family is planned for
# CTMRG environments.
function message_environment(::UndefInitializer, nn::NormNetwork)

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.

This is clever syntax, but I'd prefer the style similar_message_environment(nn::NormNetwork), I've only seen undef used for constructors (i.e. constructors that take types).

Comment thread src/normnetwork.jl
the name of the corresponding index in the bra layer.
"""
struct NormNetwork{T, V, I} <: AbstractITensorNetwork{T, V}
tensornetwork::ITensorNetwork{T, V, I}

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 clearer name for this field would be ket or ket_network.

Comment thread test/test_normnetwork.jl
# Contract a (possibly double-layer) network into a single tensor by multiplying all
# of its vertex tensors together. For a `NormNetwork` the vertex data are lazy products
# `ket * conj(bra)`, so the result is a lazy expression that we materialize.
contract(tn) = materialize(prod(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.

Probably should call this contract_network to not have it confused with TensorAlgebra.contract.

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