-
Notifications
You must be signed in to change notification settings - Fork 11
Allow custom ExternArgument in extern declaration #83
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
Changes from 4 commits
a855a77
87fe92f
8c587b4
9f65eae
f2d0e4d
418f556
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
|
||
import functools | ||
import inspect | ||
from typing import Any, Callable, Optional, Sequence, TypeVar, get_type_hints | ||
from dataclasses import dataclass | ||
from typing import Any, Callable, Literal, Optional, Sequence, TypeVar, get_type_hints | ||
|
||
from mypy_extensions import VarArg | ||
from openpulse import ast | ||
|
@@ -30,13 +31,26 @@ | |
from oqpy.quantum_types import Qubit | ||
from oqpy.timing import convert_float_to_duration | ||
|
||
__all__ = ["subroutine", "declare_extern", "declare_waveform_generator"] | ||
__all__ = ["subroutine", "declare_extern", "declare_waveform_generator", "OqpyArgument"] | ||
|
||
SubroutineParams = [oqpy.Program, VarArg(AstConvertible)] | ||
|
||
FnType = TypeVar("FnType", bound=Callable[..., Any]) | ||
|
||
|
||
@dataclass | ||
class OqpyArgument: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep the style that we use for other classes? -> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
"""An oqpy argument to extern declaration..""" | ||
|
||
name: str | ||
dtype: ast.ClassicalType | ||
access: Literal["readonly", "mutable"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
def unzip(self) -> tuple[str, ast.ClassicalType, ast.AccessControl]: | ||
"""Returns the three values, name, dtype and access as a tuple.""" | ||
return self.name, self.dtype, ast.AccessControl[self.access] | ||
|
||
|
||
def enable_decorator_arguments(f: FnType) -> Callable[..., FnType]: | ||
@functools.wraps(f) | ||
def decorator(*args, **kwargs): # type: ignore[no-untyped-def] | ||
|
@@ -173,7 +187,7 @@ def wrapper( | |
|
||
def declare_extern( | ||
name: str, | ||
args: list[tuple[str, ast.ClassicalType]], | ||
args: list[tuple[str, ast.ClassicalType] | OqpyArgument], | ||
return_type: Optional[ast.ClassicalType] = None, | ||
annotations: Sequence[str | tuple[str, str]] = (), | ||
) -> Callable[..., OQFunctionCall]: | ||
|
@@ -190,11 +204,28 @@ def declare_extern( | |
program.set(var, sqrt(0.5)) | ||
|
||
""" | ||
arg_names = list(zip(*(args)))[0] if args else [] | ||
arg_types = list(zip(*(args)))[1] if args else [] | ||
arg_names: list[str] = [] | ||
arg_types: list[ast.ClassicalType] = [] | ||
arg_access: list[ast.AccessControl | None] = [] | ||
|
||
for arg in args: | ||
if isinstance(arg, tuple): | ||
arg_name, arg_type = arg | ||
access = None | ||
elif isinstance(arg, OqpyArgument): | ||
arg_name, arg_type, access = arg.unzip() | ||
else: | ||
raise Exception(f"Argument {arg} should have a proper type") | ||
arg_names.append(arg_name) | ||
arg_types.append(arg_type) | ||
arg_access.append(access) | ||
|
||
extern_decl = ast.ExternDeclaration( | ||
ast.Identifier(name), | ||
[ast.ExternArgument(type=t) for t in arg_types], | ||
[ | ||
ast.ExternArgument(type=ctype, access=access) | ||
for ctype, access in zip(arg_types, arg_access) | ||
], | ||
return_type, | ||
) | ||
extern_decl.annotations = make_annotations(annotations) | ||
|
@@ -236,7 +267,7 @@ def call_extern(*call_args: AstConvertible, **call_kwargs: AstConvertible) -> OQ | |
|
||
def declare_waveform_generator( | ||
name: str, | ||
argtypes: list[tuple[str, ast.ClassicalType]], | ||
argtypes: list[tuple[str, ast.ClassicalType] | OqpyArgument], | ||
annotations: Sequence[str | tuple[str, str]] = (), | ||
) -> Callable[..., OQFunctionCall]: | ||
"""Create a function which generates waveforms using a specified name and argument signature.""" | ||
|
Uh oh!
There was an error while loading. Please reload this page.