From aa393c96e8311019a0da84de8619a74952ebe571 Mon Sep 17 00:00:00 2001 From: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com> Date: Mon, 11 Nov 2024 18:01:06 +0900 Subject: [PATCH] Fix the parameter expression case for RZZ validation (#2035) * fix parameter expression case for RZZ * Update qiskit_ibm_runtime/utils/utils.py Co-authored-by: Yael Ben-Haim * black * simplify test * lint --------- Co-authored-by: Yael Ben-Haim --- qiskit_ibm_runtime/utils/utils.py | 15 +++++++-------- test/unit/test_sampler.py | 16 +++++++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/qiskit_ibm_runtime/utils/utils.py b/qiskit_ibm_runtime/utils/utils.py index 6a686915a..608ee7b84 100644 --- a/qiskit_ibm_runtime/utils/utils.py +++ b/qiskit_ibm_runtime/utils/utils.py @@ -29,7 +29,7 @@ IAMAuthenticator, ) from ibm_platform_services import ResourceControllerV2 # pylint: disable=import-error -from qiskit.circuit import QuantumCircuit, ControlFlowOp, Parameter +from qiskit.circuit import QuantumCircuit, ControlFlowOp, ParameterExpression from qiskit.transpiler import Target from qiskit.providers.backend import BackendV1, BackendV2 from .deprecation import deprecate_function @@ -74,14 +74,13 @@ def _is_isa_circuit_helper(circuit: QuantumCircuit, target: Target, qubit_map: D # accurate). if ( name == "rzz" - and not isinstance(instruction.operation.params[0], Parameter) - and ( - instruction.operation.params[0] < 0.0 - or instruction.operation.params[0] > 1.001 * np.pi / 2 - ) + and not isinstance((param := instruction.operation.params[0]), ParameterExpression) + and (param < 0.0 or param > 1.001 * np.pi / 2) ): - return f"The instruction {name} on qubits {qargs} is supported only for angles in the \ - range [0, pi/2], but an angle of {instruction.operation.params[0]} has been provided." + return ( + f"The instruction {name} on qubits {qargs} is supported only for angles in the " + f"range [0, pi/2], but an angle of {param} has been provided." + ) if isinstance(operation, ControlFlowOp): for sub_circ in operation.blocks: diff --git a/test/unit/test_sampler.py b/test/unit/test_sampler.py index 02d4fa222..07ff895ae 100644 --- a/test/unit/test_sampler.py +++ b/test/unit/test_sampler.py @@ -304,8 +304,14 @@ def test_rzz_validates_only_for_fixed_angles(self): backend = FakeFractionalBackend() param = Parameter("p") - circ = QuantumCircuit(2) - circ.rzz(param, 0, 1) - - # Should run without an error - SamplerV2(backend).run(pubs=[(circ, [1])]) + with self.subTest("parameter"): + circ = QuantumCircuit(2) + circ.rzz(param, 0, 1) + # Should run without an error + SamplerV2(backend).run(pubs=[(circ, [1])]) + + with self.subTest("parameter expression"): + circ = QuantumCircuit(2) + circ.rzz(2 * param, 0, 1) + # Should run without an error + SamplerV2(backend).run(pubs=[(circ, [0.5])])