Skip to content

Commit 5943587

Browse files
committed
Skip running decomposer if we're going to consolidate
The running of the 2q decomposer to compute the weyl coordinates and the number of 2q gates is the most expensive part of the heuristic used to determine whether we should consolidate or not. It's also the last bit of data collection we run prior to making the determination of whether to consolidate or not. This commit introduces a small optimization where we only run the 2q decomposer when we know the other checks to determine whether we should consolidate or not are false. If any of them are true then we can skip the expensive computation and just consolidate the block.
1 parent 524fc74 commit 5943587

1 file changed

Lines changed: 31 additions & 21 deletions

File tree

crates/transpiler/src/passes/consolidate_blocks.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -413,31 +413,41 @@ fn py_run_consolidate_blocks(
413413
];
414414
let matrix = blocks_to_matrix(dag, &block, block_index_map).ok();
415415
if let Some(matrix) = matrix {
416-
let num_basis_gates = if let Some(ref decomposer) = decomposer {
417-
match decomposer {
418-
DecomposerType::TwoQubitBasis(decomp) => decomp.num_basis_gates_inner(
419-
nalgebra_array_view::<Complex64, U4, U4>(matrix.as_view()),
420-
)?,
421-
DecomposerType::TwoQubitControlledU(decomp) => decomp
422-
.num_basis_gates_inner(nalgebra_array_view::<Complex64, U4, U4>(
423-
matrix.as_view(),
424-
))?,
425-
}
426-
} else {
427-
// If we don't have a decomposer set we have force_consolidate set.
428-
// This value doesn't matter since we're always going to consolidate,
429-
// but usize::MAX is selected as a safeguard against a logic bug since
430-
// with that value we'll only consolidate if force_consolidate is true
431-
// in the absence of a decomposer.
432-
usize::MAX
433-
};
434-
435-
if force_consolidate
436-
|| num_basis_gates < basis_count
416+
let consolidate = if force_consolidate
437417
|| block.len() > MAX_2Q_DEPTH
438418
|| (basis_gates.is_some() && outside_basis)
439419
|| (target.is_some() && outside_basis)
440420
{
421+
true
422+
} else {
423+
let num_basis_gates = if let Some(ref decomposer) = decomposer {
424+
match decomposer {
425+
DecomposerType::TwoQubitBasis(decomp) => {
426+
decomp.num_basis_gates_inner(nalgebra_array_view::<
427+
Complex64,
428+
U4,
429+
U4,
430+
>(
431+
matrix.as_view()
432+
))?
433+
}
434+
DecomposerType::TwoQubitControlledU(decomp) => decomp
435+
.num_basis_gates_inner(nalgebra_array_view::<Complex64, U4, U4>(
436+
matrix.as_view(),
437+
))?,
438+
}
439+
} else {
440+
// If we don't have a decomposer set we have force_consolidate set.
441+
// This value doesn't matter since we're always going to consolidate,
442+
// but usize::MAX is selected as a safeguard against a logic bug since
443+
// with that value we'll only consolidate if force_consolidate is true
444+
// in the absence of a decomposer.
445+
usize::MAX
446+
};
447+
num_basis_gates < basis_count
448+
};
449+
450+
if consolidate {
441451
if approx::abs_diff_eq!(IDENTITY_2Q, matrix) {
442452
for node in block {
443453
dag.remove_op_node(node);

0 commit comments

Comments
 (0)