Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The imaginary part of a complex number is no longer moved incorrectly to the outside of a binary expression. #1756

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pyquil/quilatom.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,12 @@ def _expression_to_string(expression: ExpressionDesignator) -> str:
right = "(" + right + ")"
# If op2 is a float, it will maybe represented as a multiple
# of pi in right. If that is the case, then we need to take
# extra care to insert parens. See gh-943.
elif isinstance(expression.op2, float) and (("pi" in right and right != "pi")):
# extra care to insert parens. Similarly, complex numbers need
# to be wrapped in params so the imaginary part is captured.
MarquessV marked this conversation as resolved.
Show resolved Hide resolved
# See gh-943,1734.
elif (isinstance(expression.op2, float) and (("pi" in right and right != "pi"))) or isinstance(
expression.op2, complex
):
right = "(" + right + ")"

return left + expression.operator + right
Expand Down
13 changes: 8 additions & 5 deletions test/unit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ def test_expression_to_string():
assert str((x + y) - 2) == "%x + %y - 2"
assert str(x + (y - 2)) == "%x + %y - 2"

assert str(x ** y ** 2) == "%x^%y^2"
assert str(x ** (y ** 2)) == "%x^%y^2"
assert str((x ** y) ** 2) == "(%x^%y)^2"
assert str(x**y**2) == "%x^%y^2"
assert str(x ** (y**2)) == "%x^%y^2"
assert str((x**y) ** 2) == "(%x^%y)^2"

assert str(quil_sin(x)) == "SIN(%x)"
assert str(3 * quil_sin(x + y)) == "3*SIN(%x + %y)"
assert (
str(quil_exp(-1j * x / 2) * np.exp(1j * np.pi / 4)) == "EXP(-i*%x/2)*(0.7071067811865476+0.7071067811865475i)"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)


def test_contained_parameters():
Expand All @@ -80,7 +83,7 @@ def test_contained_parameters():
y = Parameter("y")
assert _contained_parameters(x + y) == {x, y}

assert _contained_parameters(x ** y ** quil_sin(x * y * 4)) == {x, y}
assert _contained_parameters(x**y ** quil_sin(x * y * 4)) == {x, y}


def test_eval():
Expand All @@ -93,7 +96,7 @@ def test_eval():
assert substitute(quil_exp(x), {y: 5}) != np.exp(5)
assert substitute(quil_exp(x), {x: 5}) == np.exp(5)

assert np.isclose(substitute(quil_sin(x * x ** 2 / y), {x: 5.0, y: 10.0}), np.sin(12.5))
assert np.isclose(substitute(quil_sin(x * x**2 / y), {x: 5.0, y: 10.0}), np.sin(12.5))
assert np.isclose(substitute(quil_sqrt(x), {x: 5.0, y: 10.0}), np.sqrt(5.0))
assert np.isclose(substitute(quil_cis(x), {x: 5.0, y: 10.0}), np.exp(1j * 5.0))
assert np.isclose(substitute(x - y, {x: 5.0, y: 10.0}), -5.0)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_rewrite_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_rewrite_arithmetic_duplicate_exprs():

assert response == RewriteArithmeticResponse(
original_memory_descriptors={"theta": ParameterSpec(length=1, type="REAL")},
recalculation_table={ParameterAref(index=0, name="__P1"): "theta[0]*1.5/(2*pi)"},
recalculation_table={ParameterAref(index=0, name="__P1"): "theta[0]*(1.5)/(2*pi)"},
quil=Program("DECLARE __P1 REAL[1]", "DECLARE theta REAL[1]", "RZ(__P1[0]) 0", "RX(__P1[0]) 0").out(),
)

Expand Down
Loading