From 638d3ddfe0357f06eccdf8b6e53d9938075d4a15 Mon Sep 17 00:00:00 2001 From: Alan Bidart Date: Wed, 12 Jun 2024 19:01:02 +0200 Subject: [PATCH] fix: add cosmetic changes --- src/autoqasm/converters/typecast.py | 4 +- src/autoqasm/operators/typecast.py | 18 ++++---- test/unit_tests/autoqasm/test_operators.py | 54 +++++++++++----------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/autoqasm/converters/typecast.py b/src/autoqasm/converters/typecast.py index 582376b..0d8ff29 100644 --- a/src/autoqasm/converters/typecast.py +++ b/src/autoqasm/converters/typecast.py @@ -41,10 +41,10 @@ def visit_Call(self, node: ast.stmt) -> ast.stmt: and hasattr(node.func, "id") and node.func.id in TYPECASTING_OPERATORS ): - template = f"{TYPECASTING_OPERATORS[node.func.id]}(argument_)" + template = f"{TYPECASTING_OPERATORS[node.func.id]}(argument)" new_node = templates.replace( template, - argument_=node.args, + argument=node.args, original=node, ) new_node = new_node[0].value diff --git a/src/autoqasm/operators/typecast.py b/src/autoqasm/operators/typecast.py index 37f610b..ef808c2 100644 --- a/src/autoqasm/operators/typecast.py +++ b/src/autoqasm/operators/typecast.py @@ -21,28 +21,28 @@ from autoqasm import types as aq_types -def int_(argument_: Any, *args, **kwargs) -> aq_types.IntVar | int: +def int_(argument: Any, *args, **kwargs) -> aq_types.IntVar | int: """Functional form of "int". Args: - argument_ (Any): object to be converted into an int. + argument (Any): object to be converted into an int. Returns: IntVar | int : IntVar object if argument is QASM type, else int. """ - if aq_types.is_qasm_type(argument_): - return _oqpy_int(argument_) + if aq_types.is_qasm_type(argument): + return _oqpy_int(argument) else: - return _py_int(argument_, *args, **kwargs) + return _py_int(argument, *args, **kwargs) -def _oqpy_int(argument_: Any) -> aq_types.IntVar: +def _oqpy_int(argument: Any) -> aq_types.IntVar: oqpy_program = program.get_program_conversion_context().get_oqpy_program() result = aq_types.IntVar() oqpy_program.declare(result) - oqpy_program.set(result, argument_) + oqpy_program.set(result, argument) return result -def _py_int(argument_: Any, *args, **kwargs) -> int: - return int(argument_, *args, **kwargs) +def _py_int(argument: Any, *args, **kwargs) -> int: + return int(argument, *args, **kwargs) diff --git a/test/unit_tests/autoqasm/test_operators.py b/test/unit_tests/autoqasm/test_operators.py index be0ee5f..99dd137 100644 --- a/test/unit_tests/autoqasm/test_operators.py +++ b/test/unit_tests/autoqasm/test_operators.py @@ -932,13 +932,12 @@ def test_list_ops(): assert test_list_ops.build().to_ir() -class TestTypecasting: - def test_int_typecasting_on_measure(self): - @aq.main(num_qubits=2) - def main(): - test = int(measure([0, 1])) # noqa: F841 +def test_int_typecasting_on_measure(): + @aq.main(num_qubits=2) + def main(): + test = int(measure([0, 1])) # noqa: F841 - expected_ir = """OPENQASM 3.0; + expected_ir = """OPENQASM 3.0; int[32] test; qubit[2] __qubits__; bit[2] __bit_0__ = "00"; @@ -947,38 +946,41 @@ def main(): int[32] __int_1__; __int_1__ = __bit_0__; test = __int_1__;""" - assert main.build().to_ir() == expected_ir + assert main.build().to_ir() == expected_ir - def test_int_typecasting_on_string(self): - @aq.main(num_qubits=2) - def main(): - test = int("101", 2) # noqa: F841 - expected_ir = """OPENQASM 3.0; +def test_int_typecasting_on_string(): + @aq.main(num_qubits=2) + def main(): + test = int("101", 2) # noqa: F841 + + expected_ir = """OPENQASM 3.0; qubit[2] __qubits__;""" - assert main.build().to_ir() == expected_ir + assert main.build().to_ir() == expected_ir + - def test_nested_int_typecasting_without_return(self): - @aq.main(num_qubits=2) - def main(): - test = 2 * int(measure([0, 1])) # noqa: F841 +def test_nested_int_typecasting_without_return(): + @aq.main(num_qubits=2) + def main(): + test = 2 * int(measure([0, 1])) # noqa: F841 - expected_ir = """OPENQASM 3.0; + expected_ir = """OPENQASM 3.0; qubit[2] __qubits__; bit[2] __bit_0__ = "00"; __bit_0__[0] = measure __qubits__[0]; __bit_0__[1] = measure __qubits__[1]; int[32] __int_1__; __int_1__ = __bit_0__;""" - assert main.build().to_ir() == expected_ir + assert main.build().to_ir() == expected_ir + - def test_nested_int_typecasting_with_return(self): - @aq.main(num_qubits=2) - def main(): - test = 2 * int(measure([0, 1])) # noqa: F841 - return test +def test_nested_int_typecasting_with_return(): + @aq.main(num_qubits=2) + def main(): + test = 2 * int(measure([0, 1])) # noqa: F841 + return test - expected_ir = """OPENQASM 3.0; + expected_ir = """OPENQASM 3.0; output int[32] test; qubit[2] __qubits__; bit[2] __bit_0__ = "00"; @@ -987,4 +989,4 @@ def main(): int[32] __int_1__; __int_1__ = __bit_0__; test = 2 * __int_1__;""" - assert main.build().to_ir() == expected_ir + assert main.build().to_ir() == expected_ir