Skip to content

docs: document generic comptime functions#175

Merged
CalMacCQ merged 6 commits into
mainfrom
cm/generic_comptime
Jul 21, 2026
Merged

docs: document generic comptime functions#175
CalMacCQ merged 6 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
Comment thread sphinx/language_guide/comptime.md Outdated

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
Contributor

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
Contributor

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?

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.

I think its fine to call out version numbers for major features.

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
Contributor

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?

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.

I guess its helpful for building intuition on comptime. When compiling the function for k=7 we see k-1=7 prints.

Comment thread sphinx/language_guide/comptime.md Outdated

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
Contributor

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 and others added 2 commits July 13, 2026 11:57
Co-authored-by: Mark Koch <48097969+mark-koch@users.noreply.github.com>
@CalMacCQ
CalMacCQ requested a review from tatiana-s July 20, 2026 09:51
@CalMacCQ

Copy link
Copy Markdown
Collaborator Author

Maybe I should add something here about monomorphisation?

Also maybe "generics" should be its own section.

@tatiana-s

Copy link
Copy Markdown
Contributor

@CalMacCQ We have a generics section in the "Static compilation and typing" doc - there might be enough to say about generics to pull it out into a separate doc at this point?

@CalMacCQ
CalMacCQ merged commit 1559a68 into main Jul 21, 2026
3 checks passed
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

3 participants