-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: qsystem std functions with updated primitives (#679)
Closes #625 Closes #645 - hseries renamed to qsystem BREAKING CHANGE: hseries primitive functions moved to std.qsystem BREAKING CHANGE: measure_return renamed to `project_z`
- Loading branch information
Showing
12 changed files
with
283 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from typing import no_type_check | ||
|
||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std import angles | ||
from guppylang.std._internal.compiler.quantum import ( | ||
QSYSTEM_EXTENSION, | ||
InoutMeasureCompiler, | ||
) | ||
from guppylang.std._internal.util import quantum_op | ||
from guppylang.std.angles import angle | ||
from guppylang.std.builtins import owned | ||
from guppylang.std.quantum import qubit | ||
|
||
qsystem = GuppyModule("qsystem") | ||
|
||
qsystem.load(qubit) | ||
qsystem.load_all(angles) | ||
|
||
|
||
@guppy(qsystem) | ||
@no_type_check | ||
def phased_x(q: qubit, angle1: angle, angle2: angle) -> None: | ||
f1 = float(angle1) | ||
f2 = float(angle2) | ||
_phased_x(q, f1, f2) | ||
|
||
|
||
@guppy.hugr_op(quantum_op("ZZMax", ext=QSYSTEM_EXTENSION), module=qsystem) | ||
@no_type_check | ||
def zz_max(q1: qubit, q2: qubit) -> None: ... | ||
|
||
|
||
@guppy(qsystem) | ||
@no_type_check | ||
def zz_phase(q1: qubit, q2: qubit, angle: angle) -> None: | ||
f = float(angle) | ||
_zz_phase(q1, q2, f) | ||
|
||
|
||
@guppy(qsystem) | ||
@no_type_check | ||
def rz(q: qubit, angle: angle) -> None: | ||
f1 = float(angle) | ||
_rz(q, f1) | ||
|
||
|
||
@guppy.hugr_op(quantum_op("Measure", ext=QSYSTEM_EXTENSION), module=qsystem) | ||
@no_type_check | ||
def measure(q: qubit @ owned) -> bool: ... | ||
|
||
|
||
@guppy.custom(InoutMeasureCompiler("MeasureReset", QSYSTEM_EXTENSION), module=qsystem) | ||
@no_type_check | ||
def measure_and_reset(q: qubit) -> bool: | ||
"""MeasureReset operation from the qsystem extension.""" | ||
|
||
|
||
@guppy.hugr_op(quantum_op("Reset", ext=QSYSTEM_EXTENSION), module=qsystem) | ||
@no_type_check | ||
def reset(q: qubit) -> None: ... | ||
|
||
|
||
# TODO | ||
# @guppy.hugr_op(quantum_op("TryQAlloc", ext=QSYSTEM_EXTENSION), module=qsystem) | ||
# @no_type_check | ||
# def _try_qalloc() -> Option[qubit]: | ||
# .. | ||
|
||
|
||
@guppy.hugr_op(quantum_op("QFree", ext=QSYSTEM_EXTENSION), module=qsystem) | ||
@no_type_check | ||
def qfree(q: qubit @ owned) -> None: ... | ||
|
||
|
||
# ------------------------------------------------------ | ||
# --------- Internal definitions ----------------------- | ||
# ------------------------------------------------------ | ||
|
||
|
||
@guppy.hugr_op(quantum_op("PhasedX", ext=QSYSTEM_EXTENSION), module=qsystem) | ||
@no_type_check | ||
def _phased_x(q: qubit, angle1: float, angle2: float) -> None: | ||
"""PhasedX operation from the qsystem extension. | ||
See `guppylang.std.qsystem.phased_x` for a public definition that | ||
accepts angle parameters. | ||
""" | ||
|
||
|
||
@guppy.hugr_op(quantum_op("ZZPhase", ext=QSYSTEM_EXTENSION), module=qsystem) | ||
@no_type_check | ||
def _zz_phase(q1: qubit, q2: qubit, angle: float) -> None: | ||
"""ZZPhase operation from the qsystem extension. | ||
See `guppylang.std.qsystem.phased_x` for a public definition that | ||
accepts angle parameters. | ||
""" | ||
|
||
|
||
@guppy.hugr_op(quantum_op("Rz", ext=QSYSTEM_EXTENSION), module=qsystem) | ||
@no_type_check | ||
def _rz(q: qubit, angle: float) -> None: | ||
"""Rz operation from the qsystem extension. | ||
See `guppylang.std.qsystem.rz` for a public definition that | ||
accepts angle parameters. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import no_type_check | ||
|
||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std import angles, qsystem | ||
from guppylang.std.angles import angle | ||
from guppylang.std.builtins import owned | ||
from guppylang.std.quantum import qubit | ||
|
||
qsystem_functional = GuppyModule("qsystem_functional") | ||
|
||
qsystem_functional.load(qubit, qsystem) | ||
qsystem_functional.load_all(angles) | ||
|
||
|
||
@guppy(qsystem_functional) | ||
@no_type_check | ||
def phased_x(q: qubit @ owned, angle1: angle, angle2: angle) -> qubit: | ||
qsystem.phased_x(q, angle1, angle2) | ||
return q | ||
|
||
|
||
@guppy(qsystem_functional) | ||
@no_type_check | ||
def zz_phase(q1: qubit @ owned, q2: qubit @ owned, angle: angle) -> tuple[qubit, qubit]: | ||
qsystem.zz_phase(q1, q2, angle) | ||
return q1, q2 | ||
|
||
|
||
@guppy(qsystem_functional) | ||
@no_type_check | ||
def measure_and_reset(q: qubit @ owned) -> tuple[qubit, bool]: | ||
b = qsystem.measure_and_reset(q) | ||
return q, b | ||
|
||
|
||
@guppy(qsystem_functional) | ||
@no_type_check | ||
def zz_max(q1: qubit @ owned, q2: qubit @ owned) -> tuple[qubit, qubit]: | ||
qsystem.zz_max(q1, q2) | ||
return q1, q2 | ||
|
||
|
||
@guppy(qsystem_functional) | ||
@no_type_check | ||
def rz(q: qubit @ owned, angle: angle) -> qubit: | ||
qsystem.rz(q, angle) | ||
return q | ||
|
||
|
||
@guppy(qsystem_functional) | ||
@no_type_check | ||
def measure(q: qubit @ owned) -> bool: | ||
result = qsystem.measure(q) | ||
return result | ||
|
||
|
||
@guppy(qsystem_functional) | ||
@no_type_check | ||
def qfree(q: qubit @ owned) -> None: | ||
qsystem.qfree(q) | ||
|
||
|
||
@guppy(qsystem_functional) | ||
@no_type_check | ||
def reset(q: qubit @ owned) -> qubit: | ||
qsystem.reset(q) | ||
return q |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.