Skip to content

Commit

Permalink
Add: rust-native method to obtain Operstion objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
raynelfss committed Jun 14, 2024
1 parent a359071 commit 3406c8b
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions crates/accelerate/src/target_transpiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,10 @@ impl Target {
/// name. This also can also be the class for globally defined variable with
/// operations.
#[pyo3(text_signature = "(instruction, /)")]
pub fn operation_from_name(&self, py: Python<'_>, instruction: String) -> PyResult<PyObject> {
if let Some(gate_obj) = self._gate_name_map.get(&instruction) {
Ok(gate_obj.to_object(py))
} else {
Err(PyKeyError::new_err(format!(
"Instruction {:?} not in target",
instruction
)))
fn operation_from_name(&self, instruction: String) -> PyResult<TargetOperation> {
match self.get_operation_from_name(&instruction) {
Ok(instruction) => Ok(instruction.to_owned()),
Err(e) => Err(PyKeyError::new_err(e.message)),
}
}

Expand All @@ -543,16 +539,12 @@ impl Target {
/// Raises:
/// KeyError: If qargs is not in target
#[pyo3(text_signature = "(/, qargs=None)")]
fn operations_for_qargs(
&self,
py: Python<'_>,
qargs: Option<Qargs>,
) -> PyResult<Vec<PyObject>> {
fn operations_for_qargs(&self, qargs: Option<Qargs>) -> PyResult<Vec<TargetOperation>> {
// Move to rust native once Gates are in rust
Ok(self
.operation_names_for_qargs(qargs)?
.into_iter()
.map(|x| self._gate_name_map[x].to_object(py))
.map(|x| self._gate_name_map[x].to_owned())
.collect())
}

Expand Down Expand Up @@ -1075,6 +1067,23 @@ impl Target {
Ok(res)
}

/// Returns rust-native operation instances present in the Target that affect the provided qargs.
pub fn ops_from_qargs(
&self,
qargs: &Option<Qargs>,
) -> Result<Vec<&NormalOperation>, TargetKeyError> {
match self.op_names_for_qargs(qargs) {
Ok(operations) => Ok(operations
.into_iter()
.filter_map(|oper| match &self._gate_name_map[oper] {
TargetOperation::Normal(normal) => Some(normal),
_ => None,
})
.collect()),
Err(e) => Err(e),
}
}

/// Gets all the qargs used by the specified operation name. Rust native equivalent of ``BaseTarget.qargs_for_operation_name()``
pub fn qargs_for_op_name(
&self,
Expand All @@ -1093,6 +1102,21 @@ impl Target {
}
}

/// Gets the instruction object based on the operation name
pub fn get_operation_from_name(
&self,
instruction: &String,
) -> Result<&TargetOperation, TargetKeyError> {
if let Some(gate_obj) = self._gate_name_map.get(instruction) {
Ok(gate_obj)
} else {
Err(TargetKeyError::new_err(format!(
"Instruction {:?} not in target",
instruction
)))
}
}

/// Rust-native method to get all the qargs of a specific Target object
pub fn get_qargs(&self) -> Option<IndexSet<&Option<Qargs>>> {
let qargs: IndexSet<&Option<Qargs>> = self.qarg_gate_map.keys().collect();
Expand Down

0 comments on commit 3406c8b

Please sign in to comment.