Skip to content

Commit

Permalink
code-review-2-updated testing function
Browse files Browse the repository at this point in the history
  • Loading branch information
daredevil3435 committed Jun 10, 2024
1 parent 0bc9c32 commit ad4a1a2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 45 deletions.
15 changes: 6 additions & 9 deletions src/autoqasm/converters/arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from malt.pyct import templates

ARITHMETIC_OPERATORS = {
gast.FloorDiv: "ag__.fd",
gast.FloorDiv: "ag__.fd_",
}


Expand All @@ -31,13 +31,11 @@ def visit_Arithmetic(self, node: ast.stmt) -> ast.stmt:
"""Transforms a arithmetic node.
Args :
node(ast.stmt) : AST node to transform
node(ast.stmt) : AST node to transform
Returns :
ast.stmt : Transformed node
ast.stmt : Transformed node
"""

node = self.generic_visit(node)

op_type = type(node.ops[0])
Expand All @@ -58,12 +56,11 @@ def transform(node: ast.stmt, ctx: ag_ctx.ControlStatusCtx) -> ast.stmt:
"""Transform arithmetic nodes.
Args:
node(ast.stmt) : AST node to transform
cts (ag_ctx.ControlStatusCtx) : Transformer context
node(ast.stmt) : AST node to transform
ctx (ag_ctx.ControlStatusCtx) : Transformer context.
Returns :
ast.stmt : Transformed node.
ast.stmt : Transformed node.
"""

return ArithmeticTransformer(ctx).visit(node)
2 changes: 1 addition & 1 deletion src/autoqasm/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from malt.impl.api import autograph_artifact # noqa: F401
from malt.operators.variables import Undefined, UndefinedReturnValue, ld, ldu # noqa: F401

from .arithmetics import _aq_fd, fd_
from .arithmetics import _aq_fd, fd_ # noqa: F401
from .assignments import assign_for_output, assign_stmt # noqa: F401
from .comparisons import gt_, gteq_, lt_, lteq_ # noqa: F401
from .conditional_expressions import if_exp # noqa: F401
Expand Down
26 changes: 11 additions & 15 deletions src/autoqasm/operators/arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,35 @@

from __future__ import annotations

from typing import Any

from autoqasm import program
from autoqasm import types as aq_types

from .utils import _register_and_convert_parameters


def fd_(a: int, b: int) -> int | aq_types.IntVar:
def fd_(a: Any, b: Any) -> int | aq_types.IntVar:
"""Functional form of "//"
Args:
a (int) : The first integer
b (int) : The second integer
Returns :
<<<<<<< HEAD
int | IntVar : where the result is floor division of a by b
=======
int | IntVar : where the result if floor division of a by b
Args:
a (Any) : The first operator can be int | IntVar
b (Any) : The second operator can be int | IntVar
>>>>>>> fd954a9 (second code-review)
Returns :
int | IntVar : floor division of a by b
"""
if aq_types.is_qasm_type(a) or aq_types.is_qasm_type(b):
return _aq_fd(a, b)
else:
return a // b
return a / b


def _aq_fd(a: int, b: int) -> aq_types.IntVar:
def _aq_fd(a: Any, b: Any) -> aq_types.IntVar:
a, b = _register_and_convert_parameters(a, b)

oqpy_program = program.get_program_conversion_context().get_oqpy_program()
result = aq_types.IntVar()
oqpy_program.declare(result)
oqpy_program.set(result, a // b)
oqpy_program.set(result, a / b)
return result
3 changes: 2 additions & 1 deletion src/autoqasm/transpiler/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
comparisons,
return_statements,
)
from autoqasm.src.autoqasm.converters import arithmetics

# from autoqasm.src.autoqasm.converters import arithmetics


class PyToOqpy(transpiler.PyToPy):
Expand Down
96 changes: 77 additions & 19 deletions test/unit_tests/autoqasm/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,64 @@ def prog():

assert prog.build().to_ir() == expected

def test_arithmetics_fd() -> None:
"""Tests for integer division operator"""

@aq.main
def prog():
a = 12
b = 3
c = a / b

expected = """OPENQASM 3.0;
int a = 12;
int b = 5;
int c;
c = a /b;"""

assert prog.build().to_ir() == expected

# def test_arithmetic_fd() -> None:
# """Tests for floor division operator"""

# @aq.main
# def prog():
# a = 12
# b = 3
# c = a / b

# expected = """OPENQASM 3.0;
# int a = 12;
# int b = 3;
# int c = a / b;
# """


# assert prog.build().to_ir() == expected




# def test_fd_operator_with_qasm_types() -> None:
# """Tests floor division operator handling with QASM types."""

# @aq.main
# def prog():
# a = aq.IntVar(5)
# b = aq.IntVar(2)
# c = a // b
# assert (c)

# # expected = """OPENQASM 3.0;
# # int a = 5;
# # int b = 2;

# # a // b;"""
# expected = """OPENQASM 3.0;
# """

# assert prog.build().to_ir() == expected


def test_comparison_lt() -> None:
"""Tests less than operator handling."""
Expand Down Expand Up @@ -932,26 +990,26 @@ def test_list_ops():
assert test_list_ops.build().to_ir()


def test_arithmetic_fd() -> None:
"""Tests for floor division operator"""
# def test_arithmetic_fd() -> None:
# """Tests for floor division operator"""

@aq.subroutine
def return_fd(a: int, b: int):
return a // b
# @aq.subroutine
# def return_fd(a: int, b: int):
# return a // b

@aq.main
def prog():
return_fd(10, 3)
# @aq.main
# def prog():
# return return_fd(10, 3)

expected = """OPENQASM 3.0;
def return_fd(int a, int b)-> int{
int ans;
ans = a / b
return ans
}
int solution;
solution = return_fd(10,3);
"""
# expected = """OPENQASM 3.0;
# def return_fd(int a, int b)-> int{
# int ans;
# ans = a // b
# return ans
# }

assert prog.build().to_ir() == expected
# int solution;
# solution = return_fd(10,3);
# """

# assert prog.build().to_ir() == expected

0 comments on commit ad4a1a2

Please sign in to comment.