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 `metadata` field in `TensorNetwork` to parametric `NamedTuple` type #57

Closed mofeing closed 1 year ago

mofeing commented 1 year ago

This PR replaces the metadata dictionary proposed in #55 with a NamedTuple defined as a parameter of the TensorNetwork type.

# before this PR
struct TensorNetwork{A<:Ansatz} ... end

# after this PR
struct TensorNetwork{A<:Ansatz,N<:NamedTuple} ... end

This makes the TensorNetwork type a lil bit more cumbersome, but avoids mutability bugs and makes its use easier. It actually implements a kind of attribute inheritance pattern that perfectly fits our use case.

With this PR, we can:

And because the metadata fields only depends on the Ansatz, it can be compiled without any recompilation or method invalidation problems.

codecov[bot] commented 1 year ago

Codecov Report

Merging #57 (d500e4f) into refactor/new-tn (e91eb89) will increase coverage by 6.65%. The diff coverage is 95.37%.

:exclamation: Current head d500e4f differs from pull request most recent head c05364d. Consider uploading reports for the commit c05364d to get more accurate results

@@                 Coverage Diff                 @@
##           refactor/new-tn      #57      +/-   ##
===================================================
+ Coverage            73.88%   80.53%   +6.65%     
===================================================
  Files                    9        9              
  Lines                  448      483      +35     
===================================================
+ Hits                   331      389      +58     
+ Misses                 117       94      -23     
Impacted Files Coverage Δ
src/Quantum/MP.jl 65.90% <90.00%> (+6.38%) :arrow_up:
src/Quantum/Quantum.jl 75.78% <95.23%> (+36.35%) :arrow_up:
src/TensorNetwork.jl 82.46% <96.00%> (+1.80%) :arrow_up:
ext/TenetQuacExt.jl 86.95% <100.00%> (-1.51%) :arrow_down:
src/Helpers.jl 77.41% <100.00%> (+5.41%) :arrow_up:

... and 1 file with indirect coverage changes

mofeing commented 1 year ago

I've managed to solve the last conceptual barrier: how to merge two TensorNetworks into a Composite ansatz (and preserve the metadata to reconstruct the original TNs).

With this, it should be ready to merge.

@jofrevalles Can you give it a quick review?

mofeing commented 1 year ago

@jofrevalles @emapuljak this PR is finished. Can you give it a quick review?

Once merged, we can work on updating last PRs and doing a new release.

jofrevalles commented 1 year ago

@jofrevalles @emapuljak this PR is finished. Can you give it a quick review?

Once merged, we can work on updating last PRs and doing a new release.

I have tested the code and seems OK for me!

emapuljak commented 1 year ago

@jofrevalles @emapuljak this PR is finished. Can you give it a quick review?

Once merged, we can work on updating last PRs and doing a new release.

Testing it at the moment for us mortals. Finishing asap.

emapuljak commented 1 year ago

image image hcat(mps, mps) working. hcat related with MPO doesn't work. Writting supporting tests.