Skip to content

Commit

Permalink
Fix: Bugs when fetching qargs or operations
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
raynelfss committed Apr 18, 2024
1 parent fc3b07e commit c345397
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
45 changes: 27 additions & 18 deletions crates/accelerate/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<HashableVec<u32>>> = gate_map_op.into_keys().collect();
Ok(Some(qargs))
} else {
Ok(None)
}

let qargs: Vec<Option<HashableVec<u32>>> =
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)")]
Expand All @@ -416,7 +421,7 @@ impl Target {
res.append(op)?;
}
}
if let Some(qargs) = qargs {
if let Some(qargs) = &qargs {
if qargs
.vec
.iter()
Expand All @@ -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())?;
}
}

Expand All @@ -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())
}
Expand Down Expand Up @@ -829,7 +836,9 @@ impl Target {
fn qargs(&self) -> PyResult<Option<HashSet<Option<HashableVec<u32>>>>> {
let qargs: HashSet<Option<HashableVec<u32>>> =
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))
Expand Down
6 changes: 4 additions & 2 deletions qiskit/transpiler/_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c345397

Please sign in to comment.