From e7fc5a73ede95acfd3c47f7eba37c47069e24599 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Tue, 29 Oct 2024 14:31:32 +0000 Subject: [PATCH] Avoid unnecessary matrix construction in `Split2qUnitaries` (#13378) During the transition of this pass to Rust, the matrix creation was accidentally moved before the check of whether the pass would operate on the node. This wastes time and allocations on creating matrices that will not be further examined. --- crates/accelerate/src/split_2q_unitaries.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/accelerate/src/split_2q_unitaries.rs b/crates/accelerate/src/split_2q_unitaries.rs index f3a3d9454980..6950ea590212 100644 --- a/crates/accelerate/src/split_2q_unitaries.rs +++ b/crates/accelerate/src/split_2q_unitaries.rs @@ -33,15 +33,18 @@ pub fn split_2q_unitaries( for node in nodes { if let NodeType::Operation(inst) = &dag.dag()[node] { let qubits = dag.get_qargs(inst.qubits).to_vec(); - let matrix = inst.op.matrix(inst.params_view()); // We only attempt to split UnitaryGate objects, but this could be extended in future // -- however we need to ensure that we can compile the resulting single-qubit unitaries // to the supported basis gate set. if qubits.len() != 2 || inst.op.name() != "unitary" { continue; } + let matrix = inst + .op + .matrix(inst.params_view()) + .expect("'unitary' gates should always have a matrix form"); let decomp = TwoQubitWeylDecomposition::new_inner( - matrix.unwrap().view(), + matrix.view(), Some(requested_fidelity), None, )?;