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

Replace KeywordDispatch for an in-house implementation #272

Merged
merged 5 commits into from
Jan 9, 2025

Conversation

mofeing
Copy link
Member

@mofeing mofeing commented Dec 26, 2024

KeywordDispatch.jl is no longer actively developed and doesn't propagate docs. With our own implementation of keyword-dispatching, we have more control over it and can fix the propagation of docs.

The idea is to do sth as Core.kwcall; i.e. Julia lowers calls to methods with keyword-arguments to calls to Core.kwcall where the 1st argument is a NamedTuple with the arguments, the 2nd argument is the type of the function and the rest are the positional arguments. In our case, we call that kwcall manually on the default method without a kwcall function (but I can reconsider), so we implement keyword-dispatched methods as a method of the same function but where the kwargs are a NamedTuple that is passed as the first argument.

For example, for inds, the replacement for @kwdispatch is

inds(tn::AbstractTensorNetwork; kwargs...) = inds(sort_nt(values(kwargs)), tn)

The default @kwmethod is replaced for

inds(::@NamedTuple{}, tn::AbstractTensorNetwork) = inds((; set=:all), tn)

And the set-kwarg method is now

function inds(kwargs::NamedTuple{(:set,)}, tn::AbstractTensorNetwork)
    tn = TensorNetwork(tn)
    if kwargs.set === :all
...

It looks a lil more verbose than before, but we can add some helpers. The nice thing is that we can now put docstrings to these keyword-methods and also point to them.

Note: sort_nt is a function that sorts the keys of a NamedTuple. It's used for making keyword-dispatch invariant to kwarg order.

@mofeing mofeing added this to the 0.8 milestone Dec 26, 2024
Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 0% with 90 lines in your changes missing coverage. Please review.

Project coverage is 0.04%. Comparing base (f2abea6) to head (00deea3).
Report is 31 commits behind head on master.

Files with missing lines Patch % Lines
src/TensorNetwork.jl 0.00% 34 Missing ⚠️
src/MPS.jl 0.00% 18 Missing ⚠️
src/Quantum.jl 0.00% 17 Missing ⚠️
src/Numerics.jl 0.00% 12 Missing ⚠️
src/Ansatz.jl 0.00% 8 Missing ⚠️
src/Helpers.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           master    #272      +/-   ##
=========================================
- Coverage    0.04%   0.04%   -0.01%     
=========================================
  Files          32      32              
  Lines        2146    2159      +13     
=========================================
  Hits            1       1              
- Misses       2145    2158      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mofeing mofeing requested a review from jofrevalles January 8, 2025 11:30
@mofeing mofeing marked this pull request as ready for review January 9, 2025 15:17
@mofeing
Copy link
Member Author

mofeing commented Jan 9, 2025

Currently, tests fail due to Reactant.

I've run core tests in local and they pass all, so I'm merging.

@mofeing mofeing merged commit 14599c8 into master Jan 9, 2025
2 of 5 checks passed
@mofeing mofeing deleted the refactor/kwcall branch January 9, 2025 15:45
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