Skip to content

docs: Add arrays, mem_swap and with_owned to ownership chapter#180

Open
mark-koch wants to merge 1 commit into
mainfrom
mk/with-owned
Open

docs: Add arrays, mem_swap and with_owned to ownership chapter#180
mark-koch wants to merge 1 commit into
mainfrom
mk/with-owned

Conversation

@mark-koch

Copy link
Copy Markdown

Closes #25

While adding with_owned, I noticed that we also didn't talk about arrays or mem_swap in the ownership chapter. I grouped them all together under an "Advanced Topics" section that should hopefully feel cohesive

@mark-koch mark-koch requested a review from CalMacCQ as a code owner July 13, 2026 13:52
```

To obtain ownership of an array element, we should use the [``array.take``](guppylang.std.builtins.array.take) or [``array.try_take``](guppylang.std.builtins.array.try_take) methods instead of the subscript syntax.
See TODO for how to manipulate arrays using ``take``.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

TODO: Link #170 once merged

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

#170 is now merged so you should be able to crosslink link like so

[SOME TEXT HERE](data_types/arrays.md#Extracting-individual-elements)

@CalMacCQ CalMacCQ left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great! Thanks Mark. A few minor comments and questions but generally pretty happy with this.

my_measure_and_reset.check()
```

The downside of this implementation is that it requires allocating an additional qubit, which could potential fail if none are available.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
The downside of this implementation is that it requires allocating an additional qubit, which could potential fail if none are available.
The downside of this implementation is that it requires allocating an additional qubit, which could potentially fail if none are available.

```

To obtain ownership of an array element, we should use the [``array.take``](guppylang.std.builtins.array.take) or [``array.try_take``](guppylang.std.builtins.array.try_take) methods instead of the subscript syntax.
See TODO for how to manipulate arrays using ``take``.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

#170 is now merged so you should be able to crosslink link like so

[SOME TEXT HERE](data_types/arrays.md#Extracting-individual-elements)

from guppylang.std.mem import with_owned

@guppy
def f(q: qubit @owned) -> tuple[Measurement, qubit]:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just a question here... Since Measurement is the only v1 feature here, is there a version of this "advanced features" section that you think would make sense to backport to the 0.21.x docs? All of the other functionality relates to 1.x. We will deploy the 0.21.x docs side-by-side with the 1.x docs for a long while I think.

Otherwise I'm fine with just having this merged into main as part of the v1 docs.


### Swapping of borrowed values via ``mem_swap``

Since subscripting only borrows a given element, we also cannot use it to swap elements of an array:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe it would be worth saying explicitly why we cannot swap two elements of an array here.

apply_subscript(qs, 0)
discard_array(qs)

try:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Was this try-except a temporary workaround until we have a better error message to show?

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.

Show with_owned usage in "Ownership and linear types" section

2 participants