Skip to content

Commit

Permalink
Add pow for Parameter
Browse files Browse the repository at this point in the history
Adds powering for qiskit.circuit.ParameterExpression allowing to use a**n, n**a, a**b, pow(a,b) etc. for ParameterExpressions a, b and numbers n. Minimal change using default support of pow by Sympy/Symengine.
Added pow to list of operators in TestParameterExpressions test case for unit testing.
fixes Qiskit#8959
Changelog: New Feature
  • Loading branch information
grossardt committed Nov 12, 2023
1 parent 7ef9547 commit 5048020
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions qiskit/circuit/parameterexpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ def __truediv__(self, other):
def __rtruediv__(self, other):
return self._apply_operation(operator.truediv, other, reflected=True)

def __pow__(self, other):
return self._apply_operation(pow, other)

def __rpow__(self, other):
return self._apply_operation(pow, other, reflected=True)

def _call(self, ufunc):
return ParameterExpression(self._parameter_symbols, ufunc(self._symbol_expr))

Expand Down
7 changes: 7 additions & 0 deletions releasenotes/notes/add-parameter-pow-ff5f8d10813f5733.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
features:
- |
:class:`~qiskit.circuit.ParameterExpression` (and thus also
:class:`~qiskit.circuit.Parameter`) now support powering: :code:`x**y`
where :code:`x` and :code:`y` can be any combination of
:class:`~qiskit.circuit.ParameterExpression` and number types.
2 changes: 1 addition & 1 deletion test/python/circuit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ def _paramvec_names(prefix, length):
class TestParameterExpressions(QiskitTestCase):
"""Test expressions of Parameters."""

supported_operations = [add, sub, mul, truediv]
supported_operations = [add, sub, mul, truediv, pow]

def setUp(self):
super().setUp()
Expand Down

0 comments on commit 5048020

Please sign in to comment.