docs: document generic comptime functions#175
Open
CalMacCQ wants to merge 5 commits into
Open
Conversation
CalMacCQ
marked this pull request as draft
July 9, 2026 21:50
CalMacCQ
commented
Jul 9, 2026
|
|
||
| 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. |
Collaborator
Author
There was a problem hiding this comment.
Maybe it'd be instructive to call generic_ladder inside another function and compile it.
There was a problem hiding this comment.
Also I think it would be good to show the compiler error message when you try to compile it
CalMacCQ
marked this pull request as ready for review
July 9, 2026 22:06
mark-koch
reviewed
Jul 13, 2026
| 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. |
There was a problem hiding this comment.
Do we want to specifically call out v1 changes in regular docs sections?
| @guppy.comptime | ||
| def generic_ladder(qs: array[qubit, k]) -> None: | ||
| for q1, q2 in zip(qs[1:], qs[:-1]): | ||
| print("Applying CX") |
|
|
||
| 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. |
There was a problem hiding this comment.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.