def test_division_is_converted_into_div_fn_call_instead_of_multiplication_by_reciprocal( sympy_multiplication, expected_args): # Important note about sympy: there is no Div operator (as opposed to # e.g. Mul). The division on sympy expressions actually produces Mul # objects, in which second operand is a reciprocal of the original one. # We need to deal with this case, otherwise converting anything that # contains division will result in very confusing expressions. assert expression_from_sympy(sympy_multiplication) == FunctionCall( "div", expected_args)
def test_sympy_mul_is_converted_to_function_call_with_mul_operation( sympy_mul, expected_args): assert expression_from_sympy(sympy_mul) == FunctionCall( "mul", expected_args)
def test_sympy_add_is_converted_to_function_call_with_add_operation( sympy_add, expected_args): assert expression_from_sympy(sympy_add) == FunctionCall( "add", expected_args)
def test_imaginary_unit_is_converted_to_1j(): assert expression_from_sympy(sympy.I) == 1j
def test_sympy_numbers_are_converted_to_corresponding_native_number( sympy_number, expected_number, expected_class): native_number = expression_from_sympy(sympy_number) assert native_number == expected_number assert isinstance(native_number, expected_class)
def test_symbols_are_converted_to_instance_of_symbol_class( sympy_symbol, expected_symbol): assert expression_from_sympy(sympy_symbol) == expected_symbol
def test_sympy_fn_calls_are_converted_to_fn_call_object_with_appropriate_fn_name( sympy_function_call, expected_function_call): assert expression_from_sympy(sympy_function_call) == expected_function_call
def test_sympy_power_with_negative_one_exponent_gets_converted_to_division( sympy_power, expected_denominator): assert expression_from_sympy(sympy_power) == FunctionCall( "div", (1, expected_denominator))
def test_sympy_pow_is_converted_to_pow_function_call(sympy_power, expected_args): assert expression_from_sympy(sympy_power) == FunctionCall( "pow", expected_args)
def test_add_resulting_from_subtraction_is_converted_to_sub_function_call( sympy_addition, expected_args): assert expression_from_sympy(sympy_addition) == FunctionCall( "sub", expected_args)
def test_native_numbers_are_preserved(number): assert expression_from_sympy(number) == number
def _export_expression(expr: sympy.Expr): return translate_expression(expression_from_sympy(expr), QUIL_DIALECT)
def test_translating_tree_from_sympy_to_quil_gives_expected_result( sympy_expression): expression = expression_from_sympy(sympy_expression) assert translate_expression(expression, SYMPY_DIALECT) - sympy_expression == 0