Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -470,24 +470,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 sympify(self):
"""Return symbolic expression as a raw Sympy or Symengine object.
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dill>=0.3
python-dateutil>=2.8.0
stevedore>=3.0.0
typing-extensions; python_version<'3.11'
symengine>=0.9,!=0.10.0
symengine>=0.11
31 changes: 14 additions & 17 deletions test/python/circuit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1497,31 +1497,28 @@ 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 in self.supported_operations:
for const in good_constants:
expr = op(const, x)
bound_expr = expr.bind({x: 2.3})

self.assertEqual(complex(bound_expr), op(const, 2.3))
expr = op(const, x)
bound_expr = expr.bind({x: 2.3})
self.assertEqual(complex(bound_expr), op(const, 2.3))

# Division by zero will raise. Tested elsewhere.
if const == 0 and op == truediv:
continue
# 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})
# 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}")
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