Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions crates/transpiler/src/passes/basis_translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ type PhysicalQargs = SmallVec<[PhysicalQubit; 2]>;
#[pyfunction(name = "base_run", signature = (dag, equiv_lib, qargs_with_non_global_operation, min_qubits, target_basis=None, target=None, non_global_operations=None))]
pub fn run_basis_translator(
py: Python<'_>,
dag: DAGCircuit,
dag: &DAGCircuit,
equiv_lib: &mut EquivalenceLibrary,
qargs_with_non_global_operation: HashMap<Qargs, HashSet<String>>,
min_qubits: usize,
target_basis: Option<HashSet<String>>,
target: Option<&Target>,
non_global_operations: Option<HashSet<String>>,
) -> PyResult<DAGCircuit> {
) -> PyResult<Option<DAGCircuit>> {
if target_basis.is_none() && target.is_none() {
return Ok(dag);
return Ok(None);
}

let qargs_with_non_global_operation: IndexMap<
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn run_basis_translator(
.collect();
extract_basis_target(
py,
&dag,
dag,
&mut source_basis,
&mut qargs_local_source_basis,
min_qubits,
Expand All @@ -119,7 +119,7 @@ pub fn run_basis_translator(
.into_iter()
.map(|x| x.to_string())
.collect();
source_basis = extract_basis(py, &dag, min_qubits)?;
source_basis = extract_basis(py, dag, min_qubits)?;
new_target_basis = target_basis.unwrap().into_iter().collect();
}
new_target_basis = new_target_basis
Expand All @@ -131,7 +131,7 @@ pub fn run_basis_translator(
// translate and we can exit early.
let source_basis_names: IndexSet<String> = source_basis.iter().map(|x| x.0.clone()).collect();
if source_basis_names.is_subset(&new_target_basis) && qargs_local_source_basis.is_empty() {
return Ok(dag);
return Ok(None);
}
let basis_transforms = basis_search(equiv_lib, &source_basis, &new_target_basis);
let mut qarg_local_basis_transforms: IndexMap<
Expand Down Expand Up @@ -198,27 +198,27 @@ pub fn run_basis_translator(
)));
};

let instr_map: InstMap = compose_transforms(py, &basis_transforms, &source_basis, &dag)?;
let instr_map: InstMap = compose_transforms(py, &basis_transforms, &source_basis, dag)?;
let extra_inst_map: ExtraInstructionMap = qarg_local_basis_transforms
.iter()
.map(|(qarg, transform)| -> PyResult<_> {
Ok((
*qarg,
compose_transforms(py, transform, &qargs_local_source_basis[*qarg], &dag)?,
compose_transforms(py, transform, &qargs_local_source_basis[*qarg], dag)?,
))
})
.collect::<PyResult<_>>()?;

let (out_dag, _) = apply_translation(
py,
&dag,
dag,
&new_target_basis,
&instr_map,
&extra_inst_map,
min_qubits,
&qargs_with_non_global_operation,
)?;
Ok(out_dag)
Ok(Some(out_dag))
}

/// Method that extracts all gate instances identifiers from a DAGCircuit.
Expand Down
5 changes: 4 additions & 1 deletion qiskit/transpiler/passes/basis/basis_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def run(self, dag):
DAGCircuit: translated circuit.
"""

return base_run(
out = base_run(
dag,
self._equiv_lib,
self._qargs_with_non_global_operation,
Expand All @@ -136,3 +136,6 @@ def run(self, dag):
self._target,
None if self._non_global_operations is None else set(self._non_global_operations),
)
# If Rust-space basis translation returns `None`, it's because the input DAG is already
# suitable and it didn't need to modify anything.
return dag if out is None else out