Skip to content

Commit

Permalink
feat: Emit infallible floatrotation conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Oct 15, 2024
1 parent 3aecbf7 commit a940193
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 192 deletions.
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
}
}
}
}
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "guppylang"
version = "0.12.1"
requires-python = ">=3.10,<3.13"
requires-python = ">=3.10,<3.14"
description = "Pythonic quantum-classical programming language"
license = { file = "LICENCE" }
readme = "quickstart.md"
Expand All @@ -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

0 comments on commit a940193

Please sign in to comment.