Skip to content

Commit

Permalink
Fix: missing return value in operations_for_args
Browse files Browse the repository at this point in the history
- Fix wrong name for function operation_for_qargs.
- Fix missing return value in the python side.
- Other tweaks and fixes.
  • Loading branch information
raynelfss committed Apr 12, 2024
1 parent 04bca9f commit bc089cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
23 changes: 10 additions & 13 deletions crates/accelerate/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use pyo3::{
types::{PyDict, PyList, PyTuple},
};

// This struct allows qargs and any vec to become hashable
#[derive(Eq, PartialEq, Clone, Debug)]
struct HashableVec<T> {
pub vec: Vec<T>,
Expand Down Expand Up @@ -414,9 +415,14 @@ impl Target {
py: Python<'_>,
isclass: &Bound<PyAny>,
qargs: Option<HashableVec<u32>>,
) -> PyResult<PyObject> {
) -> PyResult<Py<PyList>> {
let res = PyList::empty_bound(py);
if let Some(qargs) = qargs.clone() {
for op in self.gate_name_map.values() {
if isclass.call1((op,))?.extract::<bool>()? {
res.append(op)?;
}
}
if let Some(qargs) = qargs {
if qargs
.vec
.iter()
Expand All @@ -437,23 +443,14 @@ impl Target {
res.append(arg)?;
}
}
}
for op in self.gate_name_map.values() {
if isclass.call1((op,))?.extract::<bool>()? {
res.append(op)?;
}
}
if res.is_empty() {
if let Some(qargs) = qargs {
if res.is_empty() {
return Err(PyTypeError::new_err(format!(
"{:?} not in target",
qargs.vec
)));
} else {
return Err(PyTypeError::new_err(format!("{:?} not in target", qargs)));
}
}
Ok(res.to_object(py))
Ok(res.into())
}
}

Expand Down
20 changes: 10 additions & 10 deletions qiskit/transpiler/_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,21 +467,21 @@ def operation_from_name(self, instruction):
operations.
"""
return self._Target.gate_name_map[instruction]
def operation_names_for_qargs(self, qargs):
"""Get the operation names for a specified qargs tuple

def operations_for_qargs(self, qargs):
"""Get the operation class object for a specified qargs tuple
Args:
qargs (tuple): A ``qargs`` tuple of the qubits to get the gates that apply
qargs (tuple): A qargs tuple of the qubits to get the gates that apply
to it. For example, ``(0,)`` will return the set of all
instructions that apply to qubit 0. If set to ``None`` this will
return the names for any globally defined operations in the target.
return any globally defined operations in the target.
Returns:
set: The set of operation names that apply to the specified ``qargs``.
list: The list of :class:`~qiskit.circuit.Instruction` instances
that apply to the specified qarg. This may also be a class if
a variable width operation is globally defined.
Raises:
KeyError: If ``qargs`` is not in target
KeyError: If qargs is not in target
"""
self._Target.operation_names_for_qargs(inspect.isclass, qargs)


return self._Target.operations_for_qargs(inspect.isclass, qargs)

0 comments on commit bc089cc

Please sign in to comment.