Skip to content

Commit

Permalink
feat: Use ensure_routine to streamline conversion from Qref to Bartiq (
Browse files Browse the repository at this point in the history
…#125)

* Use ensure_routine to streamline conversion from Qref to Bartiq

* Bump QREF version

* Add missing docstrings
  • Loading branch information
dexter2206 authored Oct 9, 2024
1 parent 1976ae9 commit c03e5f0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ python = "^3.10"
pydantic = "^2.7"
sympy = "^1.12"
pyparsing ="~3.1.2"
qref = "0.7.0"
qref = "0.8.0"

# A list of all of the optional dependencies, some of which are included in the
# below `extras`. They can be opted into by apps.
Expand Down
17 changes: 9 additions & 8 deletions src/bartiq/_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Generic, Literal, TypeVar, cast

from qref import SchemaV1
from qref.functools import AnyQrefType, ensure_routine
from qref.schema_v1 import PortV1, ResourceV1, RoutineV1
from typing_extensions import Self, TypedDict

Expand Down Expand Up @@ -120,8 +121,9 @@ def filter_ports(self, directions: Iterable[str]) -> dict[str, Port[T]]:
return {port_name: port for port_name, port in self.ports.items() if port.direction in directions}

@classmethod
def from_qref(cls, qref_obj: SchemaV1 | RoutineV1, backend: SymbolicBackend[T]) -> Routine[T]:
program = qref_obj.program if isinstance(qref_obj, SchemaV1) else qref_obj
def from_qref(cls, qref_obj: AnyQrefType, backend: SymbolicBackend[T]) -> Routine[T]:
"""Load Routine from a QREF definition, using specified backend for parsing expressions."""
program = ensure_routine(qref_obj)
return Routine[T](
children={child.name: cls.from_qref(child, backend) for child in program.children},
local_variables={var: backend.as_expression(expr) for var, expr in program.local_variables.items()},
Expand All @@ -145,18 +147,17 @@ class CompiledRoutine(Generic[T]):
constraints: Iterable[Constraint[T]] = ()

@classmethod
def from_qref(cls, qref_obj: SchemaV1 | RoutineV1, backend: SymbolicBackend[T]) -> CompiledRoutine[T]:
program = qref_obj.program if isinstance(qref_obj, SchemaV1) else qref_obj
def from_qref(cls, qref_obj: AnyQrefType, backend: SymbolicBackend[T]) -> CompiledRoutine[T]:
"""Load CompiledRoutine from a QREF definition, using specified backend for parsing expressions."""
program = ensure_routine(qref_obj)
return CompiledRoutine[T](
children={child.name: cls.from_qref(child, backend) for child in program.children},
**_common_routine_dict_from_qref(qref_obj, backend),
)


def _common_routine_dict_from_qref(
qref_obj: SchemaV1 | RoutineV1, backend: SymbolicBackend[T]
) -> _CommonRoutineParams[T]:
program = qref_obj.program if isinstance(qref_obj, SchemaV1) else qref_obj
def _common_routine_dict_from_qref(qref_obj: AnyQrefType, backend: SymbolicBackend[T]) -> _CommonRoutineParams[T]:
program = ensure_routine(qref_obj)
return {
"name": program.name,
"type": program.type,
Expand Down
6 changes: 2 additions & 4 deletions src/bartiq/compilation/_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Generic, TypeVar

from qref import SchemaV1
from qref.functools import ensure_routine
from qref.schema_v1 import RoutineV1
from qref.verification import verify_topology

Expand Down Expand Up @@ -101,10 +102,7 @@ def compile_routine(
raise BartiqCompilationError(
f"Found the following issues with the provided routine before the compilation started: {problems}",
)
if isinstance(routine, Routine):
root = routine
else:
root = Routine[T].from_qref(routine, backend)
root = routine if isinstance(routine, Routine) else Routine[T].from_qref(ensure_routine(routine), backend)

for stage in preprocessing_stages:
root = stage(root, backend)
Expand Down

0 comments on commit c03e5f0

Please sign in to comment.