From af86deef80040944543f1d70826fa3bf20134bfe Mon Sep 17 00:00:00 2001 From: luffy-orf Date: Fri, 30 May 2025 14:51:38 +0530 Subject: [PATCH] fix: Raise TypeError for string exponents in EigenGate and add test --- cirq-core/cirq/ops/common_gates_test.py | 5 +++++ cirq-core/cirq/ops/eigen_gate.py | 2 ++ 2 files changed, 7 insertions(+) diff --git a/cirq-core/cirq/ops/common_gates_test.py b/cirq-core/cirq/ops/common_gates_test.py index d615ddb29b1..ec71917d878 100644 --- a/cirq-core/cirq/ops/common_gates_test.py +++ b/cirq-core/cirq/ops/common_gates_test.py @@ -1322,3 +1322,8 @@ def test_parameterized_pauli_expansion(gate_type, exponent): gate_resolved = cirq.resolve_parameters(gate, {'s': 0.5}) pauli_resolved = cirq.resolve_parameters(pauli, {'s': 0.5}) assert cirq.approx_eq(pauli_resolved, cirq.pauli_expansion(gate_resolved)) + + +def test_xpowgate_string_exponent_raises(): + with pytest.raises(TypeError, match="Exponent must be a float or sympy expression, not str"): + _ = cirq.X ** "text" diff --git a/cirq-core/cirq/ops/eigen_gate.py b/cirq-core/cirq/ops/eigen_gate.py index 7c9b57706ba..b070e8fd8c9 100644 --- a/cirq-core/cirq/ops/eigen_gate.py +++ b/cirq-core/cirq/ops/eigen_gate.py @@ -289,6 +289,8 @@ def _period(self) -> float | None: return _approximate_common_period(real_periods) def __pow__(self, exponent: float | sympy.Symbol) -> EigenGate: + if isinstance(exponent, str): + raise TypeError("Exponent must be a float or sympy expression, not str. String exponents are not supported.") new_exponent = protocols.mul(self._exponent, exponent, NotImplemented) if new_exponent is NotImplemented: return NotImplemented # pragma: no cover