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 Parameter.is_real() #9664

Merged
merged 5 commits into from
Mar 12, 2023
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
9 changes: 4 additions & 5 deletions qiskit/circuit/parameterexpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,10 @@ def is_real(self):
# expression's is_real attribute returns false that we have a
# non-zero imaginary
if _optionals.HAS_SYMENGINE:
if symbol_expr.imag != 0.0:
return False
else:
return False
return True
if symbol_expr.imag == 0.0:
return True
return False
return symbol_expr.is_real

def sympify(self):
"""Return symbolic expression as a raw Sympy or Symengine object.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed a bug where :meth:`.Parameter.is_real` did not return ``None`` when
the parameter is not bound.
Fixed `#8619 <https://github.com/Qiskit/qiskit-terra/issues/8619>`.
Cryoris marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 15 additions & 1 deletion test/python/circuit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,10 +1759,24 @@ def test_parameter_expression_grad(self):
def test_bound_expression_is_real(self):
"""Test is_real on bound parameters."""
x = Parameter("x")
self.assertEqual(x.is_real(), None)
self.assertEqual((1j * x).is_real(), None)

expr = 1j * x
bound = expr.bind({x: 2})
self.assertEqual(bound.is_real(), False)

bound = x.bind({x: 0 + 0j})
self.assertEqual(bound.is_real(), True)

bound = x.bind({x: 0 + 1j})
self.assertEqual(bound.is_real(), False)

bound = x.bind({x: 1 + 0j})
self.assertEqual(bound.is_real(), True)

self.assertFalse(bound.is_real())
bound = x.bind({x: 1 + 1j})
self.assertEqual(bound.is_real(), False)


class TestParameterEquality(QiskitTestCase):
Expand Down