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: Emit infallible floatrotation conversions #560

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 5 additions & 13 deletions guppylang/prelude/_internal/compiler/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from guppylang.definition.custom import CustomInoutCallCompiler
from guppylang.definition.value import CallReturnWires
from guppylang.prelude._internal.compiler.prelude import build_error, build_panic
from guppylang.prelude._internal.json_defs import load_extension

# ----------------------------------------------
Expand All @@ -35,10 +34,10 @@
]


def from_halfturns() -> ops.ExtOp:
def from_halfturns_unchecked() -> ops.ExtOp:
return ops.ExtOp(
ROTATION_EXTENSION.get_op("from_halfturns"),
ht.FunctionType([FLOAT_T], [ht.Sum([[], [ROTATION_T]])]),
ROTATION_EXTENSION.get_op("from_halfturns_unchecked"),
ht.FunctionType([FLOAT_T], [ROTATION_T]),
)


Expand Down Expand Up @@ -72,14 +71,7 @@ def compile_with_inouts(self, args: list[Wire]) -> CallReturnWires:

[*qs, angle] = args
[halfturns] = self.builder.add_op(ops.UnpackTuple([FLOAT_T]), angle)
[mb_rotation] = self.builder.add_op(from_halfturns(), halfturns)

conditional = self.builder.add_conditional(mb_rotation)
with conditional.add_case(0) as case:
error = build_error(case, 1, "Non-finite number of half-turns")
case.set_outputs(build_panic(case, [], [ROTATION_T], error))
with conditional.add_case(1) as case:
case.set_outputs(*case.inputs())
[rotation] = self.builder.add_op(from_halfturns_unchecked(), halfturns)

qs = self.builder.add_op(
quantum_op(self.opname)(
Expand All @@ -89,6 +81,6 @@ def compile_with_inouts(self, args: list[Wire]) -> CallReturnWires:
[],
),
*qs,
conditional,
rotation,
)
return CallReturnWires(regular_returns=[], inout_returns=list(qs))
36 changes: 33 additions & 3 deletions guppylang/prelude/_internal/json_defs/tket2/rotation.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"from_halfturns": {
"extension": "tket2.rotation",
"name": "from_halfturns",
"description": "Construct rotation from number of half-turns (would be multiples of π in radians).",
"description": "Construct rotation from number of half-turns (would be multiples of π in radians). Returns None if the float is non-finite.",
"signature": {
"params": [],
"body": {
Expand Down Expand Up @@ -55,10 +55,40 @@
},
"binary": false
},
"from_halfturns_unchecked": {
"extension": "tket2.rotation",
"name": "from_halfturns_unchecked",
"description": "Construct rotation from number of half-turns (would be multiples of π in radians). Panics if the float is non-finite.",
"signature": {
"params": [],
"body": {
"input": [
{
"t": "Opaque",
"extension": "arithmetic.float.types",
"id": "float64",
"args": [],
"bound": "C"
}
],
"output": [
{
"t": "Opaque",
"extension": "tket2.rotation",
"id": "rotation",
"args": [],
"bound": "C"
}
],
"extension_reqs": []
}
},
"binary": false
},
"radd": {
"extension": "tket2.rotation",
"name": "radd",
"description": "Add two rotations together (experimental).",
"description": "Add two angles together (experimental).",
"signature": {
"params": [],
"body": {
Expand Down Expand Up @@ -123,4 +153,4 @@
"binary": false
}
}
}
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies = [
]

[project.optional-dependencies]
pytket = ["pytket >=1.30.0,<2", "tket2 >=0.3.0,<0.4"]
pytket = ["pytket >=1.30.0,<2", "tket2 >=0.4.1,<0.5"]
docs = ["sphinx >=7.2.6,<9", "sphinx-book-theme >=1.1.2,<2"]
execution = ["execute-llvm"]

Expand Down
Loading
Loading