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: Use ensure_routine to streamline conversion from Qref to Bartiq #125

Merged
merged 3 commits into from
Oct 9, 2024
Merged
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
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]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lack of docstrings?

"""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
Loading