Skip to content

Add ITensorNetworkOperator and QuadraticFormNetwork data structures - #195

Draft
jack-dunham wants to merge 14 commits into
mainfrom
jd/quadraticform
Draft

jack-dunham wants to merge 14 commits into
mainfrom
jd/quadraticform

Conversation

@jack-dunham

Copy link
Copy Markdown
Contributor

TBW

jack-dunham and others added 14 commits September 9, 2026 14:03
…alse instead of erroring

This is inline with the `Graphs` behaviour.
… not in dictionary

This is now consistant with the fallback defn of `dimnamevertices`.
would error previously.
Avoids some minor code duplication.
Fix imports in `apply_operators.jl`
`QuadraticFormNetwork` represents `⟨tn|op|tn⟩` lazily, with an operator layer
between the bra and ket layers. A ket index name is renamed in the bra layer
when it is a link name or appears in the operator's `inputnames`, so a ket index
the operator does not act on still contracts straight onto the bra.
`operatortensor` renames each operator output name to the bra name of the input
name it is paired with, leaving the input names to meet the ket. The constructor
throws unless `vertices(operator)` and `vertices(ket)` are set-equal.

`AbstractBilinearFormNetwork` is the new supertype of `NormNetwork` and
`QuadraticFormNetwork`. A subtype implements `braname`, `kettensor` and, where it
has an operator layer, `operatortensor`; `bratensor`, `conj_bratensor` and
`indmap` are defined once in terms of those, so the two concrete files now share
them instead of each carrying a copy. `AbstractBilinearFormNetworkView` does the
same for `KetView`, `BraView` and the new `OperatorView`: each implements
`Base.parent` and `get_vertex_data`, and takes its graph structure, assignment
and mutability from the parent network.

`test/test_quadraticformnetwork.jl` covers the name map, the three views, and
three contractions: an identity operator layer reproduces
`contract_network(NormNetwork(tn))`, a random on-site product operator matches a
dense `⟨ψ|O|ψ⟩` reference, and a `2I` layer on three vertices scales the norm by
8.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The five files are byte-identical at their new paths; only the `include` paths in
`src/ITensorNetworksNext.jl` change. `src/normnetworkview.jl` becomes
`bilinearformnetworkview.jl`, since it now holds the views of both `NormNetwork`
and `QuadraticFormNetwork`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
`OperatorView` now accepts any `AbstractBilinearFormNetwork` rather than only a
`QuadraticFormNetwork`, and a second constructor throws an `ArgumentError` for a
`NormNetwork`, which reports the reason rather than surfacing a `MethodError`.

A `NormNetwork` maps every site index name to itself in its bra layer, so an
identity operator between the layers would need its output name to equal its
input name, and `ITensor` rejects that: "Dimension names must be distinct".

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
`encoded_vertex`, `decoded_vertex` and `encoded_graph` move from a
`for View in (:KetView, :BraView)` loop in `bilinearformnetworkview.jl` to
`AbstractBilinearFormNetworkView` in `abstractbilinearformnetworkview.jl`,
replacing the `vertex_positions`/`ordered_vertices`/`position_graph` methods that
NamedGraphs 0.13 removed. `OperatorView` is now covered as well, as it was by the
methods being replaced. The loop also `@eval`ed against `BraView` above that
type's own definition, so including the file threw `UndefVarError: BraView`.

`QuadraticFormNetwork` gets the same rename; it still defined the three removed
names, so the package did not load. `test_quadraticformnetwork.jl` takes
`incident_edges`, `named_grid` and `named_path_graph` from `NamedGraphs` itself,
since the `GraphsExtensions` and `NamedGraphGenerators` submodules are gone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
`ITensorNetworkOperator` is the network equivalent of `ITensorBase.ITensorOperator`:
a tensor network together with a positional pairing of its dangling index names,
`outputnames[i]` with `inputnames[i]`. The constructor, reached through
`ITensorBase.operator(tn, out, in)`, throws an `ArgumentError` when the two name
lists differ in length and when a named index is not dangling, since
`dimnamevertices` must return exactly one vertex for an operator leg.

A pair may straddle two vertices, as it does for a swap or a translation, so
`getindex` wraps each vertex tensor as an `ITensorOperator` carrying only the pairs
whose two halves both sit on that vertex; the remaining legs are dangling on the
wrapper, which `NamedTensorOperator` permits for names in neither list. The wrapper
is built even when no pair is local, so `eltype` is `NamedTensorOperator{I, T}` for
every vertex.

`QuadraticFormNetwork` takes one as its operator layer. Its `acted` set is now
`Set{I}(inputnames(operator))` rather than a loop over per-vertex input names, and
`operatortensor` reads the network-level pairing: reading `qf.operator[vertex]`
would skip a crossing pair's output leg, which is dangling on that wrapper, and the
bra layer would not meet it. `operatortensor` therefore returns a plain tensor and
`get_vertex_data` drops its `state` call.

`supportof` gains a method for the new type. Its body moves to
`supportof_dimnames(tn, opnames)` in `abstracttensornetwork.jl` so both methods
share it; a `Union` signature is not possible there because
`ITensorNetworkOperator` is not yet defined at that point in the include order.

`replacedimnames` is not implemented for the new type: `ITensorNetwork` has no
`replacedimnames` method to delegate to.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ITensorBot

Copy link
Copy Markdown
Member

Your PR requires formatting changes to meet the project's style guidelines.
Please run the ITensorFormatter to apply these changes.

Click here to view the suggested changes.
diff --git a/src/apply/apply_operators.jl b/src/apply/apply_operators.jl
index 9edf9ba..92edcd8 100644
--- a/src/apply/apply_operators.jl
+++ b/src/apply/apply_operators.jl
@@ -2,8 +2,8 @@ using .AlgorithmsInterfaceExtensions: AlgorithmsInterfaceExtensions as AIE
 using AlgorithmsInterface: AlgorithmsInterface as AI
 using Base: @kwdef
 using Graphs: dst, src, vertices
-using ITensorBase: ITensorBase as ITB, AbstractITensor, apply, dimnames, inputnames, operator,
-    outputnames, replacedimnames
+using ITensorBase: ITensorBase as ITB, AbstractITensor, apply, dimnames, inputnames,
+    operator, outputnames, replacedimnames
 using LinearAlgebra: norm, normalize!
 using MatrixAlgebraKit: eigh_full, project_hermitian, qr_compact, svd_trunc
 using NamedGraphs: boundary_edges

@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.62791% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.97%. Comparing base (e0b1e67) to head (e6c55df).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/itensornetworkoperator.jl 81.81% 8 Missing ⚠️
...c/bilinearforms/abstractbilinearformnetworkview.jl 54.54% 5 Missing ⚠️
src/bilinearforms/quadraticformnetwork.jl 87.80% 5 Missing ⚠️
src/tensornetwork.jl 89.28% 3 Missing ⚠️
src/bilinearforms/abstractbilinearformnetwork.jl 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #195      +/-   ##
==========================================
+ Coverage   80.17%   81.97%   +1.80%     
==========================================
  Files          13       17       +4     
  Lines         681      799     +118     
==========================================
+ Hits          546      655     +109     
- Misses        135      144       +9     
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.

struct ITensorNetworkOperator{T, V, I, P <: AbstractITensorNetwork{T, V}} <:
AbstractITensorNetwork{T, V}
parent::P
outputnames::Vector{I}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Consider fast lookup of this.

Comment on lines +5 to +11
struct KetView{T, V, I, P <: AbstractBilinearFormNetwork{T, V, I}} <:
AbstractBilinearFormNetworkView{T, V, I}
parent::P
function KetView(parent::AbstractBilinearFormNetwork{T, V, I}) where {T, V, I}
return new{T, V, I, typeof(parent)}(parent)
end
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reconsider

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.

2 participants