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

A compactify_interface feature that takes a list of types and a list of functions that have methods for these types and "compactifies" them #28

Open
Krastanov opened this issue May 17, 2023 · 0 comments

Comments

@Krastanov
Copy link

Krastanov commented May 17, 2023

As seen in discussions on slack and discourse:

I want to be able to do:

for g in list_of_different_types_of_gates:
    apply!(state, g)
end

without dynamic dispatch or allocations.

apply! is a function that has a ton of non-allocating fast methods of the form apply!(::State, ::OneOfManyDifferentTypes)::State

I have the following approach:

I made a function that takes

  • a list of types
  • a list of functions that have methods defined for these types

and it creates

  • one single sumtype
  • for all the given functions, it defines a method for the new sumtype
    In pseudo code it looks like
make_sumtype_infrastructure(my_many_types, [:fun1, :fun2]) |> eval
which generates the following code
@sum_type MySumType
    OneOfMyOriginalTypes(...)
    ...
end
function fun1(arg::MySumType)
    @case arg begin
        OneOfMyOriginalTypes(...) => fun1(OneOfMyOriginalTypes(...))
        ...
    end
end

The implementation is at QuantumSavory/QuantumClifford.jl@17cc1ca

It is based on a discussion from https://discourse.julialang.org/t/ann-sumtypes-jl-v0-4/97038/4?u=krastanov

Having some reliable form of this officially supported by the library would be amazing (I suspect what I have written is very prone to breaking, as I am not experienced with meta-programing). Especially if it has some way for expanding the sumtype without a ton of invalidations and recompilation (maybe by "padding" the sumtype so that it can be extended) -- that way we can still keep some of the amazing interoperability of julia packages.

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

No branches or pull requests

1 participant