Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(qsystem): Add RPC call support #847

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions guppylang/std/qsystem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from guppylang.module import GuppyModule
from guppylang.std._internal.compiler.quantum import QSYSTEM_UTILS_EXTENSION
from guppylang.std._internal.util import external_op
from guppylang.std.builtins import array

qsystem_utils = GuppyModule("qsystem.utils")

Expand All @@ -13,3 +14,15 @@
)
@no_type_check
def get_current_shot() -> int: ...


M = guppy.nat_var("M", module=qsystem_utils)
N = guppy.nat_var("N", module=qsystem_utils)


@guppy.hugr_op(
external_op("RPC", [], ext=QSYSTEM_UTILS_EXTENSION),
module=qsystem_utils,
)
@no_type_check
def rpc(request: array[int, M]) -> array[int, N]: ...
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ execute-llvm = { workspace = true }

# Uncomment these to test the latest dependency version during development
# hugr = { git = "https://github.com/CQCL/hugr", subdirectory = "hugr-py", rev = "e40b6c7" }
# tket2-exts = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-exts", rev = "633ebd7"}
# tket2 = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-py", rev = "259cf88"}
tket2-exts = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-exts", branch = "feat/util-rpc"}
# tket2 = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-py", rev = "c89ced7"}


[build-system]
Expand Down
30 changes: 21 additions & 9 deletions tests/integration/test_qsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
from guppylang.module import GuppyModule
from guppylang.std.angles import angle

from guppylang.std.builtins import nat, owned
from guppylang.std.builtins import array, nat, owned
from guppylang.std.qsystem.random import RNG, maybe_rng
from guppylang.std.qsystem.utils import get_current_shot
from guppylang.std.qsystem.utils import get_current_shot, rpc
from guppylang.std.quantum import qubit
from guppylang.std.qsystem.functional import (
measure_and_reset,
measure,
phased_x,
zz_phase,
qfree,
qsystem_functional,
measure_and_reset,
zz_max,
reset,
rz,
measure,
qfree,
zz_max,
reset,
zz_phase,
)


Expand All @@ -32,7 +33,7 @@ def compile_qsystem_guppy(fn) -> ModulePointer: # type: ignore[no-untyped-def]
), "`@compile_qsystem_guppy` does not support extra arguments."

module = GuppyModule("module")
module.load(angle, qubit, get_current_shot, RNG, maybe_rng) # type: ignore[arg-type]
module.load(angle, qubit, get_current_shot, rpc, RNG, maybe_rng) # type: ignore[arg-type]
module.load_all(qsystem_functional)
guppylang.decorator.guppy(module)(fn)
return module.compile()
Expand All @@ -43,7 +44,6 @@ def test_qsystem(validate): # type: ignore[no-untyped-def]

@compile_qsystem_guppy
def test(q1: qubit @ owned, q2: qubit @ owned, a1: angle) -> bool:
shot = get_current_shot()
q1 = phased_x(q1, a1, a1)
q1, q2 = zz_phase(q1, q2, a1)
q1 = rz(q1, a1)
Expand All @@ -56,6 +56,18 @@ def test(q1: qubit @ owned, q2: qubit @ owned, a1: angle) -> bool:

validate(test)

def test_qsystem_utils(validate): # type: ignore[no-untyped-def]
"""Compile various operations from the qsystem utils extension."""

@compile_qsystem_guppy
def test() -> array[int, 50]:
shot = get_current_shot()
request = array(shot)
response: array[int, 50] = rpc(request)
return response

validate(test)


def test_qsystem_random(validate): # type: ignore[no-untyped-def]
"""Compile various operations from the qsystem random extension."""
Expand Down
Loading