Skip to content

Commit 1326365

Browse files
authored
Fix a bug in Target::py_instruction_supported and remove the py token parameter (Qiskit#14765)
* Remove py token argument from py_instruction_supported This commit removes the py token from py_instruction_supported. `python::with_gil` is still used when the function is called with `operation_class` or with `operation_name` and `parameters`. Also fixed a bug where qargs checking was skipped when the function was called with an operation name and a parameters list that matched an existing instruction, even if the order of qargs differed. * Address review comments
1 parent 8660465 commit 1326365

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

crates/transpiler/src/passes/gate_direction.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ pub fn fix_direction_target(
210210
StandardGate::RXX | StandardGate::RYY | StandardGate::RZZ | StandardGate::RZX => {
211211
return target
212212
.py_instruction_supported(
213-
py,
214213
None,
215214
qargs.into(),
216215
Some(

crates/transpiler/src/target/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,6 @@ impl Target {
522522
)]
523523
pub fn py_instruction_supported(
524524
&self,
525-
py: Python,
526525
operation_name: Option<String>,
527526
qargs: Qargs,
528527
operation_class: Option<&Bound<PyAny>>,
@@ -552,6 +551,7 @@ impl Target {
552551
}
553552
}
554553
TargetOperation::Normal(normal) => {
554+
let py = _operation_class.py();
555555
if normal.into_pyobject(py)?.is_instance(_operation_class)? {
556556
if let Some(parameters) = &parameters {
557557
if parameters.len() != normal.params.len() {
@@ -610,19 +610,21 @@ impl Target {
610610
if parameters.len() != obj_params.len() {
611611
return Ok(false);
612612
}
613+
613614
for (index, params) in parameters.iter().enumerate() {
614-
let mut matching_params = false;
615615
let obj_at_index = &obj_params[index];
616-
if matches!(obj_at_index, Param::ParameterExpression(_))
617-
|| python_compare(py, params, &obj_params[index])?
618-
{
619-
matching_params = true;
620-
}
616+
let matching_params = match (obj_at_index, params) {
617+
(Param::Float(obj_f), Param::Float(param_f)) => obj_f == param_f,
618+
(Param::ParameterExpression(_), _) => true,
619+
_ => Python::with_gil(|py| {
620+
python_compare(py, params, &obj_params[index])
621+
})?,
622+
};
623+
621624
if !matching_params {
622625
return Ok(false);
623626
}
624627
}
625-
return Ok(true);
626628
}
627629
}
628630
Ok(self.instruction_supported(&operation_name, &qargs))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a bug in :meth:`.Target.instruction_supported` where the check of instruction's qubit order
5+
was skipped when the method was called with ``operation_name`` and ``parameters`` arguments
6+
that matched an existing instruction.

test/python/transpiler/test_target.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,9 @@ def test_instruction_supported_parameters(self):
10901090
self.assertTrue(
10911091
mumbai.target.instruction_supported("rzx_45", qargs=(0, 1), parameters=[math.pi / 4])
10921092
)
1093+
self.assertFalse(
1094+
mumbai.target.instruction_supported("rzx_45", qargs=(1, 0), parameters=[math.pi / 4])
1095+
)
10931096
self.assertTrue(mumbai.target.instruction_supported("rzx_45", qargs=(0, 1)))
10941097
self.assertTrue(mumbai.target.instruction_supported("rzx_45", parameters=[math.pi / 4]))
10951098
self.assertFalse(mumbai.target.instruction_supported("rzx_45", parameters=[math.pi / 6]))

0 commit comments

Comments
 (0)