bsc-quantic / Tenet.jl

Composable Tensor Network library in Julia
https://bsc-quantic.github.io/Tenet.jl/
Apache License 2.0
17 stars 1 forks source link

Refactor `TensorNetwork` to new definition #55

Closed mofeing closed 1 year ago

mofeing commented 1 year ago

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

The TensorNetwork fields now contain:

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 dual[^1] graph.

[^1]: Indices as nodes and tensors as (hyper)edges.

This improves code complexity, composability, ...

codecov[bot] commented 1 year ago

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%) :arrow_down:
src/Quantum/MP.jl 65.90% <65.90%> (ø)
src/TensorNetwork.jl 84.47% <88.11%> (-1.81%) :arrow_down:
src/Quantum/Quantum.jl 75.78% <91.42%> (+38.08%) :arrow_up:
ext/TenetMakieExt.jl 100.00% <100.00%> (ø)
ext/TenetQuacExt.jl 86.95% <100.00%> (ø)
src/Helpers.jl 77.41% <100.00%> (+5.41%) :arrow_up:
src/Transformations.jl 97.76% <100.00%> (-0.32%) :arrow_down:
jofrevalles commented 1 year ago

I also have noticed strange things in rand:

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

julia> norm(ψ)
ERROR: MethodError: no method matching merge(::Int64, ::Int64)
Closest candidates are:
  merge(::NamedTuple, ::Any) at namedtuple.jl:299
Stacktrace:
 [1] mergewith!(combine::typeof(merge), d1::Dict{Symbol, Any}, d2::Dict{Symbol, Any})
   @ Base ./dict.jl:752
 [2] mergewith(combine::Function, d::Dict{Symbol, Any}, others::Dict{Symbol, Any})
   @ Base ./abstractdict.jl:392
 [3] hcat(A::TensorNetwork{MatrixProduct{State, Open}}, B::TensorNetwork{MatrixProduct{State, Open}})
   @ Tenet ~/git/Tenet.jl/src/Quantum/Quantum.jl:89
 [4] norm(ψ::TensorNetwork{MatrixProduct{State, Open}}, p::Int64; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Tenet ~/git/Tenet.jl/src/Quantum/Quantum.jl:123
 [5] norm (repeats 2 times)
   @ ~/git/Tenet.jl/src/Quantum/Quantum.jl:120 [inlined]
 [6] top-level scope
   @ REPL[19]:1

julia> ψ[:plug]
Dict{Tuple{Int64, Symbol}, Symbol} with 7 entries:
  (7, :out) => Symbol("9753d94a-a45f-485f-ae72-473f6928b69c")
  (2, :out) => Symbol("3839413c-b28e-4665-9c17-ba43b83c2c32")
  (3, :out) => Symbol("7b5e3adb-bf44-4cae-a297-9e5029c0dac5")
  (1, :out) => Symbol("0876d73f-a31c-49b2-8982-7262d8150879")
  (5, :out) => Symbol("74ae16e9-47f4-4f03-a227-0d80c06e6cc2")
  (4, :out) => Symbol("ebad618e-e7fc-4302-a2ea-7aec10f94ba0")
  (6, :out) => Symbol("4155e572-766b-4140-bacd-697d1ac3b94e")

julia> ψ'[:plug]
Dict{Tuple{Int64, Symbol}, Symbol} with 14 entries:
  (6, :in)  => Symbol("4155e572-766b-4140-bacd-697d1ac3b94e")
  (3, :out) => Symbol("7b5e3adb-bf44-4cae-a297-9e5029c0dac5")
  (3, :in)  => Symbol("7b5e3adb-bf44-4cae-a297-9e5029c0dac5")
  (7, :out) => Symbol("9753d94a-a45f-485f-ae72-473f6928b69c")
  (7, :in)  => Symbol("9753d94a-a45f-485f-ae72-473f6928b69c")
  (4, :out) => Symbol("ebad618e-e7fc-4302-a2ea-7aec10f94ba0")
  (2, :out) => Symbol("3839413c-b28e-4665-9c17-ba43b83c2c32")
  (4, :in)  => Symbol("ebad618e-e7fc-4302-a2ea-7aec10f94ba0")
  (2, :in)  => Symbol("3839413c-b28e-4665-9c17-ba43b83c2c32")
  (5, :out) => Symbol("74ae16e9-47f4-4f03-a227-0d80c06e6cc2")
  (5, :in)  => Symbol("74ae16e9-47f4-4f03-a227-0d80c06e6cc2")
  (1, :out) => Symbol("0876d73f-a31c-49b2-8982-7262d8150879")
  (1, :in)  => Symbol("0876d73f-a31c-49b2-8982-7262d8150879")
  (6, :out) => Symbol("4155e572-766b-4140-bacd-697d1ac3b94e")

I suppose this shouldn't happen for the adjoint, I will take a look. Also, we could include a norm test inside MatrixProductState_test.

mofeing commented 1 year ago

@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 commented 1 year ago

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

image