Skip to content

Commit

Permalink
support gate powering with ** (#9030)
Browse files Browse the repository at this point in the history
* support gate powering with **

* separate test

Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 11, 2023
1 parent 804665b commit 7ed5de3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions qiskit/circuit/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ def power(self, exponent: float):
unitary_power = unitary @ np.diag(decomposition_power) @ unitary.conj().T
return UnitaryGate(unitary_power, label=f"{self.name}^{exponent}")

def __pow__(self, exponent: float) -> "Gate":
return self.power(exponent)

def _return_repeat(self, exponent: float) -> "Gate":
return Gate(name=f"{self.name}*{exponent}", num_qubits=self.num_qubits, params=self.params)

Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/gate-power-6f97f9db5c36def3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
Powering gates using ``**`` is now supported.
Example: ``sqrt_x = XGate() ** 0.5``
11 changes: 11 additions & 0 deletions test/python/circuit/test_gate_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ def test_invariant2(self, n):
self.assertEqual(result, expected)


@ddt
class TestGatePow(QiskitTestCase):
"""Test gate __pow__ method."""

@data(2, 3, 4, 5)
def test_gate_pow(self, degree):
"""Test gate __pow__ method."""
self.assertEqual(SGate() ** (1 / degree), SGate().power(1 / degree))
self.assertEqual(CXGate() ** (1 / degree), CXGate().power(1 / degree))


@ddt
class TestEfficientGatePowering(QiskitTestCase):
"""Test gate powering is efficient where expected."""
Expand Down

0 comments on commit 7ed5de3

Please sign in to comment.