From a3122341b941a7e22c5f3d7d4f858bc65cf29409 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 3 Jun 2024 18:08:55 +0200 Subject: [PATCH 1/7] Add __pos__ for ParameterExpression --- qiskit/circuit/parameterexpression.py | 3 +++ test/python/circuit/test_parameters.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/qiskit/circuit/parameterexpression.py b/qiskit/circuit/parameterexpression.py index 4f6453f90f4c..465cfbac160c 100644 --- a/qiskit/circuit/parameterexpression.py +++ b/qiskit/circuit/parameterexpression.py @@ -327,6 +327,9 @@ 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.0) + def __neg__(self): return self._apply_operation(operator.mul, -1.0) diff --git a/test/python/circuit/test_parameters.py b/test/python/circuit/test_parameters.py index 7bcc2cd35f3a..6502ba31750f 100644 --- a/test/python/circuit/test_parameters.py +++ b/test/python/circuit/test_parameters.py @@ -1755,6 +1755,12 @@ def test_negated_expression(self): self.assertEqual(float(bound_expr2), 3) + def test_positive_expression(self): + x = Parameter("x") + y = +x + self.assertEqual(float(y.bind({x: 1})), 1.) + 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 From 9515634c4b56d26b744055925e7a52d7bba15797 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 3 Jun 2024 18:09:33 +0200 Subject: [PATCH 2/7] docstring --- test/python/circuit/test_parameters.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/python/circuit/test_parameters.py b/test/python/circuit/test_parameters.py index 6502ba31750f..a476d5eb2743 100644 --- a/test/python/circuit/test_parameters.py +++ b/test/python/circuit/test_parameters.py @@ -1756,10 +1756,11 @@ def test_negated_expression(self): self.assertEqual(float(bound_expr2), 3) def test_positive_expression(self): - x = Parameter("x") - y = +x - self.assertEqual(float(y.bind({x: 1})), 1.) - self.assertIsInstance(+x, type(-x)) + """This tests parameter unary plus.""" + x = Parameter("x") + y = +x + self.assertEqual(float(y.bind({x: 1})), 1.) + self.assertIsInstance(+x, type(-x)) def test_standard_cu3(self): """This tests parameter negation in standard extension gate cu3.""" From 47cce308d061fd04f9557b2149ac6ab15c9e3efc Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 3 Jun 2024 18:35:35 +0200 Subject: [PATCH 3/7] Update qiskit/circuit/parameterexpression.py Co-authored-by: Jake Lishman --- qiskit/circuit/parameterexpression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit/circuit/parameterexpression.py b/qiskit/circuit/parameterexpression.py index 465cfbac160c..2de6cb5be0c9 100644 --- a/qiskit/circuit/parameterexpression.py +++ b/qiskit/circuit/parameterexpression.py @@ -327,7 +327,7 @@ def __rsub__(self, other): def __mul__(self, other): return self._apply_operation(operator.mul, other) - def _pos__(self): + def __pos__(self): return self._apply_operation(operator.mul, 1.0) def __neg__(self): From bf8e4c59ef296d112cd8555f42f8062213f78eb3 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 3 Jun 2024 18:40:56 +0200 Subject: [PATCH 4/7] Add release note --- .../unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml 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..93cae847f87b --- /dev/null +++ b/releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml @@ -0,0 +1,4 @@ +--- +features_circuits: + - | + Added `__pos__` for the `qiskit.circuit.ParameterExpression` class From bdfc6402b2992abe36f07f11fc4eea88f568ff3c Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 3 Jun 2024 21:32:51 +0200 Subject: [PATCH 5/7] replace 1.0 by 1 --- qiskit/circuit/parameterexpression.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit/circuit/parameterexpression.py b/qiskit/circuit/parameterexpression.py index 2de6cb5be0c9..2b81ddd769f6 100644 --- a/qiskit/circuit/parameterexpression.py +++ b/qiskit/circuit/parameterexpression.py @@ -328,10 +328,10 @@ def __mul__(self, other): return self._apply_operation(operator.mul, other) def __pos__(self): - return self._apply_operation(operator.mul, 1.0) + 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) From 58b10b0daa96b2b44735e7067e3a129201b06d07 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 3 Jun 2024 22:01:32 +0200 Subject: [PATCH 6/7] black --- test/python/circuit/test_parameters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/python/circuit/test_parameters.py b/test/python/circuit/test_parameters.py index a476d5eb2743..ed82f33eac97 100644 --- a/test/python/circuit/test_parameters.py +++ b/test/python/circuit/test_parameters.py @@ -1759,7 +1759,7 @@ def test_positive_expression(self): """This tests parameter unary plus.""" x = Parameter("x") y = +x - self.assertEqual(float(y.bind({x: 1})), 1.) + self.assertEqual(float(y.bind({x: 1})), 1.0) self.assertIsInstance(+x, type(-x)) def test_standard_cu3(self): From 42cc5eb353d4fd8a98ace2536c2ce6989b09add0 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Mon, 3 Jun 2024 23:43:11 +0100 Subject: [PATCH 7/7] Reword release note --- .../unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml b/releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml index 93cae847f87b..92fc63d49892 100644 --- a/releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml +++ b/releasenotes/notes/unary_pos_for_parameterexpression-6421421b6dc20fbb.yaml @@ -1,4 +1,4 @@ --- features_circuits: - | - Added `__pos__` for the `qiskit.circuit.ParameterExpression` class + :class:`.ParameterExpression` now supports the unary ``+`` operator.