diff --git a/qiskit/circuit/parameterexpression.py b/qiskit/circuit/parameterexpression.py index 4f6453f90f4c..2b81ddd769f6 100644 --- a/qiskit/circuit/parameterexpression.py +++ b/qiskit/circuit/parameterexpression.py @@ -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) diff --git a/releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml b/releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml new file mode 100644 index 000000000000..92fc63d49892 --- /dev/null +++ b/releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml @@ -0,0 +1,4 @@ +--- +features_circuits: + - | + :class:`.ParameterExpression` now supports the unary ``+`` operator. diff --git a/test/python/circuit/test_parameters.py b/test/python/circuit/test_parameters.py index 7bcc2cd35f3a..ed82f33eac97 100644 --- a/test/python/circuit/test_parameters.py +++ b/test/python/circuit/test_parameters.py @@ -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