Skip to content

docs: document generic comptime functions#175

Open
CalMacCQ wants to merge 5 commits into
mainfrom
cm/generic_comptime
Open

docs: document generic comptime functions#175
CalMacCQ wants to merge 5 commits into
mainfrom
cm/generic_comptime

Conversation

@CalMacCQ

@CalMacCQ CalMacCQ commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

closes #94

This is really quite a powerful feature so I'm sure we can think of some cool algorithms to implement for the notebook examples.

As for the language guide, I've stuck with the basic cx ladder for now. We can remove the pre-V1 workaround where we define a comptime Guppy function inside a Python function and just have a generic type in the function signature.

@CalMacCQ
CalMacCQ marked this pull request as draft July 9, 2026 21:50

Note how the input to the `ladder` function is of type `array[qubit, k]` so this comptime function is generic over the number of qubits.

Note that we cannot compile the `generic_ladder` function directly as the value of `k` is unknown at compile time. However we can call `generic_ladder` inside another function with a concrete `k` value.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it'd be instructive to call generic_ladder inside another function and compile it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think it would be good to show the compiler error message when you try to compile it

@CalMacCQ
CalMacCQ requested a review from mark-koch July 9, 2026 22:06
@CalMacCQ
CalMacCQ marked this pull request as ready for review July 9, 2026 22:06
As we can see, the ``print`` statement is executed at compile-time.
We get 9 printed lines, highlighting that the ``for`` loop is compile-time evaluated as well.

With Guppy v1.0 and above, we can use generic variables in the type signatures of generic functions. Let's generalize the `ladder` function defined above to apply a chain of `cx` gates to a qubit array of variable size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to specifically call out v1 changes in regular docs sections?

Comment thread sphinx/language_guide/comptime.md Outdated
@guppy.comptime
def generic_ladder(qs: array[qubit, k]) -> None:
for q1, q2 in zip(qs[1:], qs[:-1]):
print("Applying CX")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why include the print here?


Note how the input to the `ladder` function is of type `array[qubit, k]` so this comptime function is generic over the number of qubits.

Note that we cannot compile the `generic_ladder` function directly as the value of `k` is unknown at compile time. However we can call `generic_ladder` inside another function with a concrete `k` value.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think it would be good to show the compiler error message when you try to compile it

Co-authored-by: Mark Koch <48097969+mark-koch@users.noreply.github.com>
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.

[Docs] Update docs once generic comptime functions are available

2 participants