Skip to content

Commit

Permalink
feat: add maybe_qubit stdlib function (#705)
Browse files Browse the repository at this point in the history
Closes #627
  • Loading branch information
ss2165 authored Dec 10, 2024
1 parent df4745b commit a49f70e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
8 changes: 8 additions & 0 deletions guppylang/std/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from guppylang.std._internal.util import quantum_op
from guppylang.std.angles import angle
from guppylang.std.builtins import owned
from guppylang.std.option import Option


@guppy.type(ht.Qubit, linear=True)
Expand All @@ -38,6 +39,13 @@ def discard(self: "qubit" @ owned) -> None:
discard(self)


@guppy.hugr_op(quantum_op("TryQAlloc"))
@no_type_check
def maybe_qubit() -> Option[qubit]:
"""Try to allocate a qubit, returning `some(qubit)`
if allocation succeeds or `nothing` if it fails."""


@guppy.hugr_op(quantum_op("H"))
@no_type_check
def h(q: qubit) -> None: ...
Expand Down
22 changes: 0 additions & 22 deletions tests/integration/test_qalloc.py

This file was deleted.

6 changes: 3 additions & 3 deletions tests/integration/test_quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from guppylang.std.builtins import owned

from guppylang.std.quantum import discard, measure, qubit
from guppylang.std.quantum import discard, measure, qubit, maybe_qubit
from guppylang.std.quantum_functional import (
cx,
cy,
Expand Down Expand Up @@ -43,7 +43,7 @@ def compile_quantum_guppy(fn) -> ModulePointer:
), "`@compile_quantum_guppy` does not support extra arguments."

module = GuppyModule("module")
module.load(angle, qubit, discard, measure)
module.load(angle, qubit, discard, measure, maybe_qubit)
module.load_all(quantum_functional)
guppylang.decorator.guppy(module)(fn)
return module.compile()
Expand All @@ -52,7 +52,7 @@ def compile_quantum_guppy(fn) -> ModulePointer:
def test_alloc(validate):
@compile_quantum_guppy
def test() -> tuple[bool, bool]:
q1, q2 = qubit(), qubit()
q1, q2 = qubit(), maybe_qubit().unwrap()
q1, q2 = cx(q1, q2)
return (measure(q1), measure(q2))

Expand Down

0 comments on commit a49f70e

Please sign in to comment.