From a57f513237dc4b809cde664d9218771b325e2d3f Mon Sep 17 00:00:00 2001 From: Yash-10 Date: Thu, 30 May 2024 17:24:37 +0530 Subject: [PATCH 1/5] change: get dimensions for aq.ArrayVar automatically reformat --- src/autoqasm/types/types.py | 6 ++++- test/unit_tests/autoqasm/test_types.py | 32 ++++++++++++++++++++------ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/autoqasm/types/types.py b/src/autoqasm/types/types.py index 3739740..c17fdd6 100644 --- a/src/autoqasm/types/types.py +++ b/src/autoqasm/types/types.py @@ -104,8 +104,12 @@ def __init__(self, *args, annotations: str | Iterable[str] | None = None, **kwar raise errors.InvalidArrayDeclaration( "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.") + dimensions = [len(args[0])] super(ArrayVar, self).__init__( - *args, annotations=make_annotations_list(annotations), **kwargs + *args, annotations=make_annotations_list(annotations), dimensions=dimensions, **kwargs ) self.name = program.get_program_conversion_context().next_var_name(oqpy.ArrayVar) diff --git a/test/unit_tests/autoqasm/test_types.py b/test/unit_tests/autoqasm/test_types.py index 5b63dc4..839701c 100644 --- a/test/unit_tests/autoqasm/test_types.py +++ b/test/unit_tests/autoqasm/test_types.py @@ -186,9 +186,9 @@ def test_declare_array(): @aq.main def declare_array(): - a = aq.ArrayVar([1, 2, 3], base_type=aq.IntVar, dimensions=[3]) + a = aq.ArrayVar([1, 2, 3], base_type=aq.IntVar) a[0] = 11 - b = aq.ArrayVar([4, 5, 6], base_type=aq.IntVar, dimensions=[3]) + b = aq.ArrayVar([4, 5, 6], base_type=aq.IntVar) b[2] = 14 b = a @@ -207,8 +207,8 @@ def test_invalid_array_assignment(): @aq.main def invalid(): - a = aq.ArrayVar([1, 2, 3], base_type=aq.IntVar, dimensions=[3]) - b = aq.ArrayVar([4, 5], base_type=aq.IntVar, dimensions=[2]) + a = aq.ArrayVar([1, 2, 3], base_type=aq.IntVar) + b = aq.ArrayVar([4, 5], base_type=aq.IntVar) a = b # noqa: F841 with pytest.raises(aq.errors.InvalidAssignmentStatement): @@ -221,7 +221,7 @@ def test_declare_array_in_local_scope(): @aq.main def declare_array(): if aq.BoolVar(True): - _ = aq.ArrayVar([1, 2, 3], base_type=aq.IntVar, dimensions=[3]) + _ = aq.ArrayVar([1, 2, 3], base_type=aq.IntVar) with pytest.raises(aq.errors.InvalidArrayDeclaration): declare_array.build() @@ -236,7 +236,7 @@ def main() -> list[int]: @aq.subroutine def declare_array(): - _ = aq.ArrayVar([1, 2, 3], dimensions=[3]) + _ = aq.ArrayVar([1, 2, 3]) with pytest.raises(aq.errors.InvalidArrayDeclaration): main.build() @@ -383,7 +383,7 @@ def annotation_test(input: list[int]): @aq.main def main(): - a = aq.ArrayVar([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dimensions=[10]) + a = aq.ArrayVar([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) annotation_test(a) with pytest.raises(aq.errors.ParameterTypeError): @@ -724,3 +724,21 @@ def main(): with pytest.raises(aq.errors.ParameterTypeError): main.build() + + +def test_ArrayVar_does_not_need_dimensions_argument(): + @aq.main + def declare_array(): + aq.ArrayVar([1, 2, 3], base_type=aq.IntVar, dimensions=[3]) + + with pytest.raises(TypeError): + declare_array.build() + + +def test_ArrayVar_requires_init_expression(): + @aq.main + def declare_array(): + aq.ArrayVar() + + with pytest.raises(aq.errors.InvalidArrayDeclaration): + declare_array.build() From d823ac97fc0f1b17d99334d5f3d060370bfd426a Mon Sep 17 00:00:00 2001 From: Yash-10 Date: Fri, 31 May 2024 08:19:54 +0530 Subject: [PATCH 2/5] change: add init_expression to ArrayVar explicitly --- src/autoqasm/types/types.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/autoqasm/types/types.py b/src/autoqasm/types/types.py index c17fdd6..47ee200 100644 --- a/src/autoqasm/types/types.py +++ b/src/autoqasm/types/types.py @@ -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 + ): if ( program.get_program_conversion_context().subroutines_processing or not program.get_program_conversion_context().at_function_root_scope @@ -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.") - 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) From 1c42c17117e43a7658b7d8a3d1b95925c92dc329 Mon Sep 17 00:00:00 2001 From: Yash-10 Date: Fri, 31 May 2024 08:20:11 +0530 Subject: [PATCH 3/5] Add test for multidimensional array support remove print --- test/unit_tests/autoqasm/test_types.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/unit_tests/autoqasm/test_types.py b/test/unit_tests/autoqasm/test_types.py index 839701c..0e6ad74 100644 --- a/test/unit_tests/autoqasm/test_types.py +++ b/test/unit_tests/autoqasm/test_types.py @@ -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]) @@ -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 From da3f9738a7da54f8e99089d0a89f901bcaf33554 Mon Sep 17 00:00:00 2001 From: Yash-10 Date: Fri, 31 May 2024 22:18:13 +0530 Subject: [PATCH 4/5] Add type hint for init_expression --- src/autoqasm/types/types.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/autoqasm/types/types.py b/src/autoqasm/types/types.py index 47ee200..1963049 100644 --- a/src/autoqasm/types/types.py +++ b/src/autoqasm/types/types.py @@ -97,7 +97,11 @@ def __init__( class ArrayVar(oqpy.ArrayVar): def __init__( - self, init_expression, *args, annotations: str | Iterable[str] | None = None, **kwargs + self, + init_expression: Iterable, + *args, + annotations: str | Iterable[str] | None = None, + **kwargs, ): if ( program.get_program_conversion_context().subroutines_processing @@ -107,6 +111,9 @@ def __init__( "Arrays may only be declared at the root scope of an AutoQASM main function." ) + if not isinstance(init_expression, Iterable): + raise errors.InvalidArrayDeclaration("init_expression must be an iterable type.") + dimensions = [len(init_expression)] super(ArrayVar, self).__init__( init_expression=init_expression, From 1b854466c987e7eb744965d05685fc3d86e5ac43 Mon Sep 17 00:00:00 2001 From: Yash-10 Date: Fri, 31 May 2024 22:18:34 +0530 Subject: [PATCH 5/5] Add type hint test for init_expression --- test/unit_tests/autoqasm/test_types.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/unit_tests/autoqasm/test_types.py b/test/unit_tests/autoqasm/test_types.py index 0e6ad74..ec4ff93 100644 --- a/test/unit_tests/autoqasm/test_types.py +++ b/test/unit_tests/autoqasm/test_types.py @@ -744,6 +744,15 @@ def declare_array(): declare_array.build() +def test_array_init_expression_type(): + @aq.main + def declare_array(): + aq.ArrayVar(1) + + with pytest.raises(aq.errors.InvalidArrayDeclaration): + declare_array.build() + + def test_array_supports_multidimensional_arrays(): @aq.main def declare_array():