guppylang icon indicating copy to clipboard operation
guppylang copied to clipboard

Support generic comptime functions

Open mark-koch opened this issue 8 months ago • 5 comments

mark-koch avatar Mar 26 '25 12:03 mark-koch

Would be cool to have this. I can imagine cases where you would want to write generic functions whilst having full Python expressivity via comptime.

For instance encode_integer(n: int) -> array[qubit, k]. We could just take n as input and use the bin builtin to get a string of 0,1 and apply x gates for the binary encoding. I don't think we can interate through strings in guppy atm.

EDIT: I suppose you could just take an array of bools as an argument in this case instead of an int.

CalMacCQ avatar May 23 '25 14:05 CalMacCQ

Would be very useful to have this.

bhayat-quantinuum avatar Jun 18 '25 13:06 bhayat-quantinuum

I need this

NathanCQC avatar Jul 14 '25 08:07 NathanCQC

I also need this.

CQSpranger avatar Nov 19 '25 16:11 CQSpranger

There is a workaround of sorts that may not be satisfactory for your use case.

def build_ladder_prog(n_qb: int) -> GuppyFunctionDefinition:
    @guppy.comptime
    def ladder(qs: array[qubit, comptime(n_qb)]) -> None:
        for q1, q2 in zip(qs[1:], qs[:-1]):
            print("Applying CX")
            cx(q1, q2)
    
    return ladder

ladder_func5 = build_ladder_prog(5)
ladder_func5.compile_function()

Here I am defining my Guppy comptime function inside a Python function where the number of qubits is passed as a parameter.

Would be nicer to have generic comptime functions of course.

CalMacCQ avatar Nov 19 '25 17:11 CalMacCQ