You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One way would be if we had an intrinsic swap(q1: qubit, q2: qubit) -> None:
@guppy
def foo(q: qubit) -> None:
q2 = qubit()
swap(q,q2) # newly-allocated qubit is now in `q`
measure(q2)
@guppy
def bar(q1: qubit, q2: qubit, a: angle, b: bool) -> None:
if b:
swap(q1, q2)
h(q1)
rz(q2)
cx(q1, q2)
The foo here is a bit awkward - this is akin to Rust std::swap on &muts - but perhaps OK. bar seems fine, but supposes that the swap of q1,q2 should be propagated back to the caller - which makes sense if we realize that borrowing is actually transfer of ownership and back again from the same variable. If we didn't want to do this, we might try:
@guppy(module)
def bar2(q1: qubit, q2: qubit, a: angle, b: bool) -> None:
(r1, r2) = (q1,q2) if b else (q2, q1)
h(r1)
rz(r2, a)
cx(r1, r2)
but this won't compile - you'd have to refactor into an inner function:
@guppy(module)
def test(q1: qubit, q2: qubit, a: angle, b: bool) -> None:
def inner(r1: qubit, r2: qubit, a: angle) -> None:
h(r1)
rz(r2, a)
cx(r1, r2)
if b:
inner(q1, q2, a)
else:
inner(q2, q1, a)
Which works ok. (One could imagine def inner(t: tuple[qubit, qubit], a: angle) and then calling inner((q1,q2) if b else (q2,q1),a) but I'm not sure whether that works, haven't figured out how to use tuples yet.)
A further enhancement would thus be to allow variables r1,r2 in bar2 above to borrowq1/q2, i.e., for borrowing to occur over the lifetime of a local variable (i.e. a section of the CFG) rather than only/always over the lifetime of a function call. This would (significantly) complicate the checker and perhaps also require new syntax, however (in some sense equivalent to defining that local function but without the possibility of indirect call). For discussion, perhaps something like
with ((q1, q2) if b else (q2, q1)) as (r1,r2): # Univariate `with (q1 if b else q2)` straightforward, but tuples??
h(r1)
rz(r2, a)
cx(r1, r2)
# r1,r2 now gone (out-of-scope/consumed); q1,q2 restored
The text was updated successfully, but these errors were encountered:
Note the swap version (where the caller sees the qubits being swapped too) is straightforward via the quantum_functional interface: for example:
def test(q1: qubit @owned, q2: qubit @owned, a: angle, b:bool) -> tuple[qubit, qubit]:
(r1,r2) = (q1,q2) if b else (q2, q1) # Renaming to r1/r2 only for clarity, no need to
r1 = h(r1)
r2 = rz(r2, a)
return cx(r1, r2)
so as an alternative to swap one might consider ways of calling into quantum_functional from the "inout" world (the other way around being easy)
Here are a couple of example programs that fail type-checking at the moment:
One way would be if we had an intrinsic
swap(q1: qubit, q2: qubit) -> None
:The
foo
here is a bit awkward - this is akin to Ruststd::swap
on&mut
s - but perhaps OK.bar
seems fine, but supposes that the swap ofq1,q2
should be propagated back to the caller - which makes sense if we realize that borrowing is actually transfer of ownership and back again from the same variable. If we didn't want to do this, we might try:but this won't compile - you'd have to refactor into an inner function:
Which works ok. (One could imagine
def inner(t: tuple[qubit, qubit], a: angle)
and then callinginner((q1,q2) if b else (q2,q1),a)
but I'm not sure whether that works, haven't figured out how to use tuples yet.)A further enhancement would thus be to allow variables
r1,r2
inbar2
above to borrowq1
/q2
, i.e., for borrowing to occur over the lifetime of a local variable (i.e. a section of the CFG) rather than only/always over the lifetime of a function call. This would (significantly) complicate the checker and perhaps also require new syntax, however (in some sense equivalent to defining that local function but without the possibility of indirect call). For discussion, perhaps something likeThe text was updated successfully, but these errors were encountered: