docs: Add arrays, mem_swap and with_owned to ownership chapter#180
docs: Add arrays, mem_swap and with_owned to ownership chapter#180mark-koch wants to merge 1 commit into
mem_swap and with_owned to ownership chapter#180Conversation
| ``` | ||
|
|
||
| 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``. |
There was a problem hiding this comment.
#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
left a comment
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
| 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``. |
There was a problem hiding this comment.
#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]: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Was this try-except a temporary workaround until we have a better error message to show?
Closes #25
While adding
with_owned, I noticed that we also didn't talk about arrays ormem_swapin the ownership chapter. I grouped them all together under an "Advanced Topics" section that should hopefully feel cohesive