Add ITensorNetworkOperator and QuadraticFormNetwork data structures - #195
Draft
jack-dunham wants to merge 14 commits into
Draft
jack-dunham wants to merge 14 commits into
jack-dunham wants to merge 14 commits into
Conversation
…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.
… on a tensor network.
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>
Member
|
Your PR requires formatting changes to meet the project's style guidelines. 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 Report❌ Patch coverage is 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jack-dunham
commented
Sep 16, 2026
| struct ITensorNetworkOperator{T, V, I, P <: AbstractITensorNetwork{T, V}} <: | ||
| AbstractITensorNetwork{T, V} | ||
| parent::P | ||
| outputnames::Vector{I} |
Contributor
Author
There was a problem hiding this comment.
Consider fast lookup of this.
jack-dunham
commented
Sep 16, 2026
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TBW