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

[1.0] upgrade to symengine>=0.11 #11315

Merged
merged 9 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 3 additions & 11 deletions qiskit/circuit/parameterexpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,24 +476,16 @@ def __eq__(self, other):

def is_real(self):
"""Return whether the expression is real"""

# workaround for symengine behavior that const * (0 + 1 * I) is not real
# see https://github.com/symengine/symengine.py/issues/414
if self._symbol_expr.is_real is None:
symbol_expr = self._symbol_expr.evalf()
else:
symbol_expr = self._symbol_expr

if not symbol_expr.is_real and symbol_expr.is_real is not None:
if not self._symbol_expr.is_real and self._symbol_expr.is_real is not None:
# Symengine returns false for is_real on the expression if
# there is a imaginary component (even if that component is 0),
# but the parameter will evaluate as real. Check that if the
# expression's is_real attribute returns false that we have a
# non-zero imaginary
if symbol_expr.imag == 0.0:
if self._symbol_expr.imag == 0.0:
return True
return False
return symbol_expr.is_real
return self._symbol_expr.is_real

def numeric(self) -> int | float | complex:
"""Return a Python number representing this object, using the most restrictive of
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/symengine_1-c907ed541eeb9a02.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
upgrade:
- |
The minimum version required for symengine was bumped to >=0.11.
This allows to simplify some code in :meth:`.ParameterExpression.is_real`.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ dill>=0.3
python-dateutil>=2.8.0
stevedore>=3.0.0
typing-extensions
symengine>=0.9,!=0.10.0
symengine>=0.11
52 changes: 25 additions & 27 deletions test/python/circuit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,38 +1523,36 @@ def test_raise_if_subbing_in_parameter_name_conflict(self):
with self.assertRaisesRegex(CircuitError, "Name conflict"):
expr.subs({x: y_second})

def test_expressions_of_parameter_with_constant(self):
@data(2, 1.3, 0, -1, -1.0, numpy.pi, 1j)
def test_expressions_of_parameter_with_constant(self, const):
"""Verify operating on a Parameter with a constant."""

good_constants = [2, 1.3, 0, -1, -1.0, numpy.pi, 1j]

x = Parameter("x")

for op, rel_tol in self.supported_operations.items():
for const in good_constants:
expr = op(const, x)
bound_expr = expr.bind({x: 2.3})

res = complex(bound_expr)
expected = op(const, 2.3)
if rel_tol > 0:
self.assertTrue(
cmath.isclose(res, expected, rel_tol=rel_tol), f"{res} != {expected}"
)
else:
self.assertEqual(res, expected)

# Division by zero will raise. Tested elsewhere.
if const == 0 and op == truediv:
continue

# Repeat above, swapping position of Parameter and constant.
expr = op(x, const)
bound_expr = expr.bind({x: 2.3})

res = complex(bound_expr)
expected = op(2.3, const)
self.assertTrue(cmath.isclose(res, expected), f"{res} != {expected}")
expr = op(const, x)
bound_expr = expr.bind({x: 2.3})

res = complex(bound_expr)
expected = op(const, 2.3)
if rel_tol > 0:
self.assertTrue(
cmath.isclose(res, expected, rel_tol=rel_tol), f"{res} != {expected}"
)
else:
self.assertEqual(res, expected)

# Division by zero will raise. Tested elsewhere.
if const == 0 and op == truediv:
continue

# Repeat above, swapping position of Parameter and constant.
expr = op(x, const)
bound_expr = expr.bind({x: 2.3})

res = complex(bound_expr)
expected = op(2.3, const)
self.assertTrue(cmath.isclose(res, expected), f"{res} != {expected}")

def test_complex_parameter_bound_to_real(self):
"""Test a complex parameter expression can be real if bound correctly."""
Expand Down