Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TensorNetwork to new definition #55

Merged
merged 59 commits into from
Jun 6, 2023
Merged

Refactor TensorNetwork to new definition #55

merged 59 commits into from
Jun 6, 2023

Conversation

mofeing
Copy link
Member

@mofeing mofeing commented May 2, 2023

This PR redefines the internals of the TensorNetwork type, removing the problematic Index type and link methods.

The TensorNetwork fields now contain:

  • a vector of Tensors
  • a mapping from index to list of Tensors
  • some metadata

Since Tensors contain the list of indices they are connected to, and so does the indices mapping, it is equally efficient to access a TensorNetwork graph and its dual1 graph.

This improves code complexity, composability, ...

Footnotes

  1. Indices as nodes and tensors as (hyper)edges.

@mofeing mofeing requested a review from jofrevalles May 2, 2023 22:35
@mofeing mofeing self-assigned this May 2, 2023
@codecov
Copy link

codecov bot commented May 2, 2023

Codecov Report

Merging #55 (c353ccb) into master (26a1bdc) will decrease coverage by 0.59%.
The diff coverage is 84.47%.

@@            Coverage Diff             @@
##           master      #55      +/-   ##
==========================================
- Coverage   84.89%   84.30%   -0.59%     
==========================================
  Files          12        9       -3     
  Lines         748      599     -149     
==========================================
- Hits          635      505     -130     
+ Misses        113       94      -19     
Impacted Files Coverage Δ
src/Tenet.jl 25.00% <0.00%> (-75.00%) ⬇️
src/Quantum/MP.jl 65.90% <65.90%> (ø)
src/TensorNetwork.jl 84.47% <88.11%> (-1.81%) ⬇️
src/Quantum/Quantum.jl 75.78% <91.42%> (+38.08%) ⬆️
ext/TenetMakieExt.jl 100.00% <100.00%> (ø)
ext/TenetQuacExt.jl 86.95% <100.00%> (ø)
src/Helpers.jl 77.41% <100.00%> (+5.41%) ⬆️
src/Transformations.jl 97.76% <100.00%> (-0.32%) ⬇️

@mofeing mofeing added this to the 0.2 milestone May 3, 2023
@mofeing mofeing added enhancement New feature or request help wanted Extra attention is needed labels May 3, 2023
@mofeing mofeing requested review from emapuljak and a team May 4, 2023 16:57
@mofeing mofeing linked an issue May 6, 2023 that may be closed by this pull request
Co-authored-by: Jofre Vallès Muns <61060572+jofrevalles@users.noreply.github.com>
src/Quantum/Quantum.jl Outdated Show resolved Hide resolved
Copy link
Member

@jofrevalles jofrevalles left a comment

Choose a reason for hiding this comment

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

With these two changes we don't get errors with norm and, in general, the contraction seems ok.

julia> ψ = rand(MatrixProduct{State,Open}, n = 4, p = 2, χ = 32)
TensorNetwork{MatrixProduct{State, Open}}(#tensors=4, #labels=7)

julia> norm(ψ)
0.9999999999999939

julia> A = Tensor(rand(2, 2, 2), (:i, :j, :k)); B = Tensor(rand(2, 2), (:j, :l))
2×2 Tensor{Float64, 2, Matrix{Float64}}:
 0.433555  0.834031
 0.459739  0.646734

julia> C = contract(A, B)
2×2×2 Tensor{Float64, 3, Array{Float64, 3}}:
[:, :, 1] =
 0.559003  0.0637526
 0.198716  0.576309

[:, :, 2] =
 1.00909   0.107564
 0.354096  1.01502

julia> labels(C)
(:i, :k, :l)

src/Quantum/Quantum.jl Outdated Show resolved Hide resolved
src/Quantum/Quantum.jl Outdated Show resolved Hide resolved
@mofeing
Copy link
Member Author

mofeing commented May 8, 2023

@jofrevalles would you mind writing a couple of tests of the errors you are finding? I'm unable to see where the problems are without code examples.

@emapuljak
Copy link
Contributor

Found this error. Needs to be fixed before I explore more for MPO.
image

src/Quantum/MP.jl Outdated Show resolved Hide resolved
mofeing and others added 5 commits May 10, 2023 18:51
Co-authored-by: Jofre Vallès Muns <61060572+jofrevalles@users.noreply.github.com>
@mofeing mofeing requested a review from jofrevalles May 11, 2023 15:28
mofeing and others added 8 commits June 2, 2023 20:18
…e` type (#57)

* Refactor `TensorNetwork` metadata to new parametric definition

* Test `merge`, `superansatzes`

* Fix `TensorNetwork` tests

* Replace `metadata` access with implicit `getproperty` calls

* Fix `NamedTuple`'s field access on `getproperty(::TensorNetwork)`

* Fix typos

* Fix `NamedTuple` initialization with `Pairs`

* Refactor `checkmeta(Quantum)`

* Refactor `TensorNetwork` args and checks in constructors

* Implement `fieldnames` for `TensorNetwork` object

* Fix `plug` mutation

* Move `hcat` test to `Quac` integration tests

* Test `metadata`,`sites`,`labels`,`adjoint` on `Quantum` ansatz

* Init `Composite` ansatz

* Precompute `indices` mapping on `TensorNetwork`'s inner constructor

* Refactor `Quantum` metadata to interlayers alas multi-layer graphs

* Format code

* Fix `hcat` method between `Quantum` TNs

* Apply @mofeing suggestions from code review

Co-authored-by: Sergio Sánchez Ramírez <15837247+mofeing@users.noreply.github.com>

* Store `Plug` info in metadata

* Update `Quac` extension to new `TensorNetwork` definition

* Set `Bijections` compat constraint to v0.1

* Add `MPO` tests

* Add `MPS` tests

* Test `Quantum` code on `Operator` plug

* Specialize `replace!` on Quantum TN

* Fix `layers`, `hcat`

* Fix `@invoke` call in Julia 1.8

* Fix `@invoke` call on ambiguous method

---------

Co-authored-by: Sergio Sánchez Ramírez <sergio.sanchez.ramirez+github@bsc.es>
Co-authored-by: Sergio Sánchez Ramírez <sergio.sanchez.ramirez+git@bsc.es>
Co-authored-by: Jofre Vallès Muns <61060572+jofrevalles@users.noreply.github.com>
Co-authored-by: Ema Puljak <ema.puljak@cern.ch>
@mofeing mofeing merged commit 6d73147 into master Jun 6, 2023
@mofeing mofeing deleted the refactor/new-tn branch June 6, 2023 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider integration with MetaGraphs.jl
3 participants