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

change: get dimensions for aq.ArrayVar automatically #22

Merged
merged 5 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 9 additions & 5 deletions src/autoqasm/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def __init__(


class ArrayVar(oqpy.ArrayVar):
def __init__(self, *args, annotations: str | Iterable[str] | None = None, **kwargs):
def __init__(
self, init_expression, *args, annotations: str | Iterable[str] | None = None, **kwargs
rmshaffer marked this conversation as resolved.
Show resolved Hide resolved
):
if (
program.get_program_conversion_context().subroutines_processing
or not program.get_program_conversion_context().at_function_root_scope
Expand All @@ -105,11 +107,13 @@ def __init__(self, *args, annotations: str | Iterable[str] | None = None, **kwar
"Arrays may only be declared at the root scope of an AutoQASM main function."
)

if not args:
raise errors.InvalidArrayDeclaration("init_expression must at least be provided.")
rmshaffer marked this conversation as resolved.
Show resolved Hide resolved
dimensions = [len(args[0])]
dimensions = [len(init_expression)]
super(ArrayVar, self).__init__(
*args, annotations=make_annotations_list(annotations), dimensions=dimensions, **kwargs
init_expression=init_expression,
*args,
annotations=make_annotations_list(annotations),
dimensions=dimensions,
**kwargs,
)
self.name = program.get_program_conversion_context().next_var_name(oqpy.ArrayVar)

Expand Down
17 changes: 14 additions & 3 deletions test/unit_tests/autoqasm/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ def main():
main.build()


def test_ArrayVar_does_not_need_dimensions_argument():
def test_array_does_not_accept_dimensions_argument():
@aq.main
def declare_array():
aq.ArrayVar([1, 2, 3], base_type=aq.IntVar, dimensions=[3])
Expand All @@ -735,10 +735,21 @@ def declare_array():
declare_array.build()


def test_ArrayVar_requires_init_expression():
def test_array_requires_init_expression():
@aq.main
def declare_array():
aq.ArrayVar()

with pytest.raises(aq.errors.InvalidArrayDeclaration):
with pytest.raises(TypeError):
declare_array.build()


def test_array_supports_multidimensional_arrays():
@aq.main
def declare_array():
aq.ArrayVar([[1, 2], [3, 4]])

expected = """OPENQASM 3.0;
array[int[32], 2, 2] a = {{1, 2}, {3, 4}};"""

declare_array.build().to_ir() == expected
Loading