From c3453977d7b5d6df0cb609e85f9b15bd3a28463a Mon Sep 17 00:00:00 2001 From: Raynel Sanchez <87539502+raynelfss@users.noreply.github.com> Date: Thu, 18 Apr 2024 10:27:45 -0400 Subject: [PATCH] Fix: Bugs when fetching qargs or operations - Fix qarg_for_operation_name logic to account for None and throw correct exceptions. - Stringify description before sending in case of numerical descriptors. - Fix qarg to account for None entry. - Other tweaks and fixes. --- crates/accelerate/src/target.rs | 45 ++++++++++++++++++++------------- qiskit/transpiler/_target.py | 6 +++-- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/crates/accelerate/src/target.rs b/crates/accelerate/src/target.rs index 8e9e897cba7c..8ac0214034cb 100644 --- a/crates/accelerate/src/target.rs +++ b/crates/accelerate/src/target.rs @@ -391,16 +391,21 @@ impl Target { Returns: set: The set of qargs the gate instance applies to. */ - if let Some(gate_map_oper) = self.gate_map[&operation].as_ref() { - if gate_map_oper.is_empty() { - return Ok(None); + if let Some(gate_map_oper) = self.gate_map.get(&operation).cloned() { + if let Some(gate_map_op) = gate_map_oper { + if gate_map_op.contains_key(&None) { + return Ok(None); + } + let qargs: Vec>> = gate_map_op.into_keys().collect(); + Ok(Some(qargs)) + } else { + Ok(None) } - - let qargs: Vec>> = - gate_map_oper.to_owned().into_keys().collect(); - return Ok(Some(qargs)); + } else { + Err(PyKeyError::new_err(format!( + "Operation: {operation} not in Target." + ))) } - Ok(Some(vec![])) } #[pyo3(text_signature = "(/, qargs)")] @@ -416,7 +421,7 @@ impl Target { res.append(op)?; } } - if let Some(qargs) = qargs { + if let Some(qargs) = &qargs { if qargs .vec .iter() @@ -429,9 +434,9 @@ impl Target { ))); } - if let Some(gate_map_qarg) = self.qarg_gate_map[&Some(qargs.clone())].clone() { + 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())?; + res.append(self.gate_name_map[x].clone())?; } } @@ -440,12 +445,14 @@ impl Target { res.append(arg)?; } } - if res.is_empty() { - return Err(PyKeyError::new_err(format!( - "{:?} not in target", - qargs.vec - ))); - } + } + if res.is_empty() { + return Err(PyKeyError::new_err(format!("{:?} not in target", { + match &qargs { + Some(qarg) => format!("{:?}", qarg.vec), + None => "None".to_owned(), + } + }))); } Ok(res.into()) } @@ -829,7 +836,9 @@ impl Target { fn qargs(&self) -> PyResult>>>> { let qargs: HashSet>> = self.qarg_gate_map.clone().into_keys().collect(); - if qargs.len() == 1 && qargs.iter().next().is_none() { + // Modify logic to account for the case of {None} + let next_entry = qargs.iter().flatten().next(); + if qargs.len() == 1 && (qargs.iter().next().is_none() || next_entry.is_none()) { return Ok(None); } Ok(Some(qargs)) diff --git a/qiskit/transpiler/_target.py b/qiskit/transpiler/_target.py index 7da4e6df9e83..1df7ac4e7973 100644 --- a/qiskit/transpiler/_target.py +++ b/qiskit/transpiler/_target.py @@ -198,8 +198,9 @@ def __init__( ``qubit_properties``. """ + # Convert descriptions to string in the case of numerical descriptors self._Target = Target2( - description=description, + description=str(description), num_qubits=num_qubits, dt=dt, granularity=granularity, @@ -496,7 +497,8 @@ def qargs_for_operation_name(self, operation): Returns: set: The set of qargs the gate instance applies to. """ - return {x: None for x in self._Target.qargs_for_operation_name(operation)}.keys() + qargs = self._Target.qargs_for_operation_name(operation) + return {x: None for x in qargs}.keys() if qargs else qargs def durations(self): """Get an InstructionDurations object from the target