From 86150a74c891f1e8c9ae0f0503f2635db710c4b8 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Fri, 6 Oct 2023 08:49:43 -0400 Subject: [PATCH 1/2] Reuse paramterless gates in Optimize1qGatesDecomposition This commit is a small optimization inside the Optimize1qGatesDecomposition pass. The last stage of the pass is taking a circuit sequence and using it to construct an equivalent 1q dag. To do this the pass iterates over the returned circuit sequence from the 1q synthesis routine and looks up the gate name in a mapping to gate classes, and creates a new object of that class with any angles provided. However, for XGate and SXGate there are no angles, and since #10314 merged there is extra overhead with the repeated construction of these gate classes (see #10867 for more details). Since these gates are now singletons since #10314 it is safe to just reuse the same instance because calling XGate() will return that instance anyway. This commit updates the DAGCircuit construction to just reuse the same instance if the gate in circuit sequence is for x or sx. --- .../passes/optimization/optimize_1q_decomposition.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py b/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py index 854fe8204551..9ea6907bd7b5 100644 --- a/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py +++ b/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py @@ -51,11 +51,12 @@ "ry": RYGate, "rz": RZGate, "r": RGate, - "sx": SXGate, - "x": XGate, + "sx": SXGate(), + "x": XGate(), } + class Optimize1qGatesDecomposition(TransformationPass): """Optimize chains of single-qubit gates by combining them into a single gate. @@ -146,7 +147,10 @@ def _gate_sequence_to_dag(self, best_synth_circuit): out_dag.global_phase = best_synth_circuit.global_phase for gate_name, angles in best_synth_circuit: - out_dag.apply_operation_back(NAME_MAP[gate_name](*angles), qubits, check=False) + if gate_name == "x" or gate_name == "sx": + out_dag.apply_operation_back(NAME_MAP[gate_name], qubits, check=False) + else: + out_dag.apply_operation_back(NAME_MAP[gate_name](*angles), qubits, check=False) return out_dag def _substitution_checks(self, dag, old_run, new_circ, basis, qubit): From 0d8d4b4c13547a87055dc6351cfcd4074cf3db60 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 9 Oct 2023 14:26:37 -0400 Subject: [PATCH 2/2] Fix lint --- .../passes/optimization/optimize_1q_decomposition.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py b/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py index 9ea6907bd7b5..8788d00a6612 100644 --- a/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py +++ b/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py @@ -55,6 +55,7 @@ "x": XGate(), } +PARAMETER_FREE_GATE_NAMES = {"x", "sx"} class Optimize1qGatesDecomposition(TransformationPass): @@ -147,7 +148,7 @@ def _gate_sequence_to_dag(self, best_synth_circuit): out_dag.global_phase = best_synth_circuit.global_phase for gate_name, angles in best_synth_circuit: - if gate_name == "x" or gate_name == "sx": + if gate_name in PARAMETER_FREE_GATE_NAMES: out_dag.apply_operation_back(NAME_MAP[gate_name], qubits, check=False) else: out_dag.apply_operation_back(NAME_MAP[gate_name](*angles), qubits, check=False)