From 9d01de5a0b0bef5d2aee0482c1d42f6a4a56adf5 Mon Sep 17 00:00:00 2001 From: Yael Ben-Haim Date: Mon, 28 Oct 2024 17:28:17 +0200 Subject: [PATCH] Don't validate rzz angle for non-fixed parameters (#1999) * Fix rzz parameter validation on Qiskit 1.1 The rzz validation check was using `CircuitInstruction.params`, an alias for `CircuitInstruction.operation.params` added in Qiskit 1.2. Here the usage is replaced `CircuitInstruction.operation.params` to keep compatibility with Qiskit 1.1. * a test that demonstrates the bug * Don't validate rzz angle for non-fixed parameters * black --------- Co-authored-by: Will Shanks Co-authored-by: Ian Hincks --- qiskit_ibm_runtime/utils/utils.py | 12 ++++++++---- test/unit/test_sampler.py | 13 +++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/qiskit_ibm_runtime/utils/utils.py b/qiskit_ibm_runtime/utils/utils.py index ca1183aaf..6a686915a 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 +from qiskit.circuit import QuantumCircuit, ControlFlowOp, Parameter from qiskit.transpiler import Target from qiskit.providers.backend import BackendV1, BackendV2 from .deprecation import deprecate_function @@ -72,9 +72,13 @@ def _is_isa_circuit_helper(circuit: QuantumCircuit, target: Target, qubit_map: D # We allow an angle value of a bit more than pi/2, to compensate floating point rounding # errors (beyond pi/2 does not trigger an error down the stack, only may become less # accurate). - if name == "rzz" and ( - instruction.operation.params[0] < 0.0 - or instruction.operation.params[0] > 1.001 * np.pi / 2 + 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 + ) ): 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." diff --git a/test/unit/test_sampler.py b/test/unit/test_sampler.py index c54144147..02d4fa222 100644 --- a/test/unit/test_sampler.py +++ b/test/unit/test_sampler.py @@ -19,6 +19,7 @@ from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister from qiskit.primitives.containers.sampler_pub import SamplerPub +from qiskit.circuit import Parameter from qiskit.circuit.library import RealAmplitudes from qiskit_ibm_runtime import Session, SamplerV2, SamplerOptions, IBMInputValueError from qiskit_ibm_runtime.fake_provider import FakeFractionalBackend, FakeSherbrooke, FakeCusco @@ -296,3 +297,15 @@ def test_rzz_angle_validation(self, angle): else: with self.assertRaises(IBMInputValueError): SamplerV2(backend).run(pubs=[(circ)]) + + def test_rzz_validates_only_for_fixed_angles(self): + """Verify that the rzz validation occurs only when the angle is a number, and not a + parameter""" + backend = FakeFractionalBackend() + param = Parameter("p") + + circ = QuantumCircuit(2) + circ.rzz(param, 0, 1) + + # Should run without an error + SamplerV2(backend).run(pubs=[(circ, [1])])