Skip to content

Commit

Permalink
fix: The imaginary part of a complex number is no longer moved incorr…
Browse files Browse the repository at this point in the history
…ectly to the outside of a binary expression. (#1756)

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

* update comment

* update test

* fix typo

Co-authored-by: Michael Bryant <[email protected]>

---------

Co-authored-by: Michael Bryant <[email protected]>
  • Loading branch information
MarquessV and Shadow53 authored Apr 11, 2024
1 parent 2cabc84 commit 6cccb01
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
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 parens so the imaginary part is captured.
# 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)"
)


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

0 comments on commit 6cccb01

Please sign in to comment.