Skip to content

Commit

Permalink
fix: add cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abidart committed Jun 12, 2024
1 parent d64bfad commit 638d3dd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/autoqasm/converters/typecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/autoqasm/operators/typecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
54 changes: 28 additions & 26 deletions test/unit_tests/autoqasm/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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

0 comments on commit 638d3dd

Please sign in to comment.