Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions qiskit_ibm_runtime/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _is_valid_rzz_pub_helper(circuit: QuantumCircuit) -> str | set[Parameter]:
angle = instruction.operation.params[0]
if isinstance(angle, ParameterExpression):
angle_params.add(angle)
elif angle < 0.0 or angle > np.pi / 2 + 1e-10:
elif np.isnan(angle) or angle < 0.0 or angle > np.pi / 2 + 1e-10:
return (
"The instruction rzz is supported only for angles in the "
f"range [0, pi/2], but an angle ({angle}) outside of this "
Expand Down Expand Up @@ -218,8 +218,8 @@ def is_valid_rzz_pub(pub: EstimatorPub | SamplerPub) -> str:
projected_arr = arr[:, col_indices]

for row in projected_arr:
angle = float(param_exp.bind(dict(zip(param_exp.parameters, row))))
if angle < 0.0 or angle > np.pi / 2 + 1e-10:
angle = float(param_exp.bind_all(dict(zip(param_exp.parameters, row))))
if np.isnan(angle) or angle < 0.0 or angle > np.pi / 2 + 1e-10:
vals_msg = ", ".join(
[f"{param_name}={param_val}" for param_name, param_val in zip(param_names, row)]
)
Expand Down
3 changes: 3 additions & 0 deletions release-notes/unreleased/2452.bug.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Reject fixed and parameterized ``RZZ`` angles that evaluate to ``NaN`` during
primitive input validation, instead of accepting the value or leaking a raw
Qiskit binding error.
15 changes: 13 additions & 2 deletions test/unit/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_isa_inside_condition_block_body_in_separate_circuit(self, backend):
with self.assertRaises(IBMInputValueError):
SamplerV2(backend).run(pubs=[circ])

@data(-1, 1, 2)
@data(-1, 1, 2, np.nan)
def test_rzz_fixed_angle_validation(self, angle):
"""Test exception when rzz gate is used with an angle outside the range [0, pi/2]."""
backend = FakeFractionalBackend()
Expand All @@ -314,7 +314,7 @@ def test_rzz_fixed_angle_validation(self, angle):
with self.assertRaisesRegex(IBMInputValueError, f"{angle}"):
SamplerV2(backend).run(pubs=[circ])

@data(-1, 1, 2)
@data(-1, 1, 2, np.nan)
def test_rzz_parametrized_angle_validation(self, angle):
"""Test rzz gate with parameter outside range.

Expand All @@ -334,6 +334,17 @@ def test_rzz_parametrized_angle_validation(self, angle):
with self.assertRaisesRegex(IBMInputValueError, f"p={angle}"):
SamplerV2(backend).run(pubs=[(circ, [angle])])

def test_rzz_parametrized_angle_allows_eliminated_nan(self):
"""Test that a NaN parameter is allowed when it is eliminated from the RZZ angle."""
backend = FakeFractionalBackend()
param = Parameter("p")

circ = QuantumCircuit(2)
circ.rzz(0 * param + 1, 0, 1)
circ.measure_all()

SamplerV2(backend).run(pubs=[(circ, [np.nan])])

@data([1.0, 2.0], [1.0, 0.0])
@unpack
def test_rzz_validation_param_exp(self, val1, val2):
Expand Down