From ba74a04f5836d8754f5ce2409d5a3efee734c4a1 Mon Sep 17 00:00:00 2001 From: Raynel Sanchez <87539502+raynelfss@users.noreply.github.com> Date: Fri, 19 Apr 2024 16:38:06 -0400 Subject: [PATCH] Fix: Recognize None in `operation_for_qargs`. - Fix module labels for each class in target.rs. - Use py.is_instance instead of passing isinstance to `instruction_supported`. - Modify `operations_for_qargs` to accept optional values less aggressively. Allow it to find instructions with no qargs. (NoneType). - Other tweaks and fixes. --- crates/accelerate/src/target.rs | 51 ++++++++++++++++----------------- qiskit/transpiler/target.py | 1 - 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/crates/accelerate/src/target.rs b/crates/accelerate/src/target.rs index 49e211f4bf2d..5f9e5bfec90e 100644 --- a/crates/accelerate/src/target.rs +++ b/crates/accelerate/src/target.rs @@ -20,7 +20,7 @@ use pyo3::{ exceptions::{PyAttributeError, PyIndexError, PyKeyError}, prelude::*, pyclass, - types::{IntoPyDict, PyDict, PyList, PySequence, PyTuple, PyType}, + types::{IntoPyDict, PyDict, PyList, PyTuple, PyType}, }; use self::exceptions::TranspilerError; @@ -68,7 +68,7 @@ mod exceptions { } // Subclassable or Python Wrapping. -#[pyclass(module = "qiskit._accelerate.target.InstructionProperties")] +#[pyclass(module = "qiskit._accelerate.target")] #[derive(Clone, Debug)] pub struct InstructionProperties { #[pyo3(get)] @@ -212,7 +212,7 @@ type GateMapType = IndexMap>, Option>>>; type TargetValue = Option>, Option>>; -#[pyclass(mapping, subclass, module = "qiskit._accelerate.target.Target")] +#[pyclass(mapping, subclass, module = "qiskit._accelerate.target")] #[derive(Clone, Debug)] pub struct Target { #[pyo3(get, set)] @@ -503,12 +503,7 @@ impl Target { qargs: Option>, ) -> PyResult> { let res = PyList::empty_bound(py); - for op in self.gate_name_map.values() { - if isclass.call1((op,))?.extract::()? { - res.append(op)?; - } - } - if let Some(qargs) = &qargs { + if let Some(qargs) = qargs.as_ref() { if qargs .vec .iter() @@ -520,19 +515,24 @@ impl Target { qargs.vec ))); } - - if let Some(gate_map_qarg) = &self.qarg_gate_map[&Some(qargs.to_owned())] { - for x in gate_map_qarg { - res.append(self.gate_name_map[x].clone())?; - } + } + if let Some(Some(gate_map_qarg)) = self.qarg_gate_map.get(&qargs) { + for x in gate_map_qarg { + res.append(self.gate_name_map[x].clone())?; } - + } + if let Some(qargs) = qargs.as_ref() { if let Some(qarg) = self.global_operations.get(&qargs.vec.len()) { for arg in qarg { res.append(arg)?; } } } + for op in self.gate_name_map.values() { + if isclass.call1((op,))?.extract::()? { + res.append(op)?; + } + } if res.is_empty() { return Err(PyKeyError::new_err(format!("{:?} not in target", { match &qargs { @@ -590,8 +590,7 @@ impl Target { &self, py: Python<'_>, isclass: &Bound, - isinstance: &Bound, - parameter_class: &Bound, + parameter_class: &Bound, check_obj_params: &Bound, operation_name: Option, qargs: Option>, @@ -626,16 +625,16 @@ impl Target { } } - if isinstance - .call1((obj, operation_class))? - .extract::()? + if obj + .bind_borrowed(py) + .is_instance(operation_class.downcast::()?)? { if let Some(parameters) = parameters { if parameters.len() != obj .getattr(py, "params")? - .downcast_bound::(py)? - .len()? + .downcast_bound::(py)? + .len() { continue; } @@ -653,7 +652,7 @@ impl Target { if self.gate_map[op_name].is_none() || self.gate_map[op_name] .as_ref() - .unwrap_or(&IndexMap::from_iter([(None, None)].into_iter())) + .unwrap_or(&IndexMap::from_iter([].into_iter())) .contains_key(&None) { let qubit_comparison = self.gate_name_map[op_name] @@ -701,9 +700,7 @@ impl Target { } for (index, param) in parameters.iter().enumerate() { let mut matching_param = false; - if isinstance - .call1((obj_params.get_item(index)?, parameter_class))? - .extract::()? + if obj_params.get_item(index)?.is_instance(parameter_class)? || param.eq(obj_params.get_item(index)?)? { matching_param = true; @@ -727,7 +724,7 @@ impl Target { if self.gate_map[&operation_name].is_none() || self.gate_map[&operation_name] .as_ref() - .unwrap_or(&IndexMap::from_iter([(None, None)].into_iter())) + .unwrap_or(&IndexMap::from_iter([].into_iter())) .contains_key(&None) { obj = &self.gate_name_map[&operation_name]; diff --git a/qiskit/transpiler/target.py b/qiskit/transpiler/target.py index f24d4c1e000a..9ea3b7bd0732 100644 --- a/qiskit/transpiler/target.py +++ b/qiskit/transpiler/target.py @@ -509,7 +509,6 @@ def check_obj_params(parameters, obj): return super().instruction_supported( inspect.isclass, - isinstance, Parameter, check_obj_params, operation_name,