Skip to content

Commit 1aab0bb

Browse files
Keep the arg names in OQFunctionCall (#75)
* complete test coverage for base.py * simplify tests * store the args dict in OQFunctionCall * keep Iterable * remove useless list() * update type hint
1 parent 895d072 commit 1aab0bb

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

oqpy/classical_types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ class OQFunctionCall(OQPyExpression):
424424
def __init__(
425425
self,
426426
identifier: Union[str, ast.Identifier],
427-
args: Iterable[AstConvertible],
427+
args: Union[Iterable[AstConvertible], dict[Any, AstConvertible]],
428428
return_type: Optional[ast.ClassicalType],
429429
extern_decl: ast.ExternDeclaration | None = None,
430430
subroutine_decl: ast.SubroutineDefinition | None = None,
@@ -433,7 +433,8 @@ def __init__(
433433
434434
Args:
435435
identifier: The function name.
436-
args: The function arguments.
436+
args: The function arguments. If passed as a dict, the values are used when
437+
creating the FunctionCall ast node.
437438
return_type: The type returned by the function call. If none, returns nothing.
438439
extern_decl: An optional extern declaration ast node. If present,
439440
this extern declaration will be added to the top of the program
@@ -457,4 +458,5 @@ def to_ast(self, program: Program) -> ast.Expression:
457458
program.externs[self.identifier.name] = self.extern_decl
458459
if self.subroutine_decl is not None:
459460
program._add_subroutine(self.identifier.name, self.subroutine_decl)
460-
return ast.FunctionCall(self.identifier, map_to_ast(program, self.args))
461+
args = self.args.values() if isinstance(self.args, dict) else self.args
462+
return ast.FunctionCall(self.identifier, map_to_ast(program, args))

oqpy/pulse.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ def __init__(
7575
init_expression = None
7676
else:
7777
assert frequency is not None
78-
init_expression = OQFunctionCall("newframe", [port, frequency, phase], ast.FrameType)
78+
init_expression = OQFunctionCall(
79+
"newframe", {"port": port, "frequency": frequency, "phase": phase}, ast.FrameType
80+
)
7981
super().__init__(
8082
init_expression, name, needs_declaration=needs_declaration, annotations=annotations
8183
)

oqpy/subroutines.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def wrapper(
153153
program.externs.update(inner_prog.externs)
154154
return OQFunctionCall(
155155
identifier,
156-
args,
156+
{k: v for k, v in zip(argnames[1:], args)},
157157
return_type,
158158
subroutine_decl=stmt,
159159
)
@@ -218,7 +218,9 @@ def call_extern(*call_args: AstConvertible, **call_kwargs: AstConvertible) -> OQ
218218
for i, a in enumerate(call_args):
219219
if type(arg_types[i]) == ast.DurationType:
220220
new_args[i] = convert_float_to_duration(a)
221-
return OQFunctionCall(name, new_args, return_type, extern_decl=extern_decl)
221+
return OQFunctionCall(
222+
name, {k: v for k, v in zip(arg_names, new_args)}, return_type, extern_decl=extern_decl
223+
)
222224

223225
return call_extern
224226

tests/test_directives.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ def test_non_trivial_array_access():
328328

329329
assert prog.to_qasm() == expected
330330
_check_respects_type_hints(prog)
331+
assert expr_matches(frame.init_expression.args["port"], port)
331332

332333

333334
def test_non_trivial_variable_declaration():
@@ -941,6 +942,8 @@ def test_box_and_timings():
941942
assert prog.to_qasm() == expected
942943
# Todo: box only currently technically allows QuantumStatements (i.e. gates)
943944
_check_respects_type_hints(prog, ["body"])
945+
assert expr_matches(constant(100e-9, 0.5).args["length"], convert_float_to_duration(100e-9))
946+
assert expr_matches(constant(100e-9, 0.5).args["iq"], 0.5)
944947

945948

946949
def test_play_capture():
@@ -1037,6 +1040,9 @@ def test_declare_extern():
10371040
).strip()
10381041

10391042
assert program.to_qasm() == expected
1043+
assert "x" in arctan(f, f).args
1044+
assert expr_matches(arctan(f, i).args["x"], f)
1045+
assert expr_matches(arctan(f, i).args["y"], i)
10401046

10411047

10421048
def test_defcals():

0 commit comments

Comments
 (0)