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

Add __pos__ for Parameter #12496

Merged
merged 7 commits into from
Jun 4, 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
5 changes: 4 additions & 1 deletion qiskit/circuit/parameterexpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,11 @@ def __rsub__(self, other):
def __mul__(self, other):
return self._apply_operation(operator.mul, other)

def __pos__(self):
return self._apply_operation(operator.mul, 1)

def __neg__(self):
return self._apply_operation(operator.mul, -1.0)
return self._apply_operation(operator.mul, -1)

def __rmul__(self, other):
return self._apply_operation(operator.mul, other, reflected=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features_circuits:
- |
:class:`.ParameterExpression` now supports the unary ``+`` operator.
7 changes: 7 additions & 0 deletions test/python/circuit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,13 @@ def test_negated_expression(self):

self.assertEqual(float(bound_expr2), 3)

def test_positive_expression(self):
"""This tests parameter unary plus."""
x = Parameter("x")
y = +x
self.assertEqual(float(y.bind({x: 1})), 1.0)
self.assertIsInstance(+x, type(-x))

def test_standard_cu3(self):
"""This tests parameter negation in standard extension gate cu3."""
from qiskit.circuit.library import CU3Gate
Expand Down
Loading