diff --git a/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py b/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py index f96ed999061d..9eb31b8f8134 100644 --- a/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py +++ b/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py @@ -173,10 +173,16 @@ def _substitution_checks(self, dag, old_run, new_circ, basis, qubit): # if we're outside of the basis set, we're obligated to logically decompose. # if we're outside of the set of gates for which we have physical definitions, # then we _try_ to decompose, using the results if we see improvement. + new_error = 0.0 + old_error = 0.0 + if not uncalibrated_and_not_basis_p: + new_error = self._error(new_circ, qubit) + old_error = self._error(old_run, qubit) + return ( uncalibrated_and_not_basis_p - or (uncalibrated_p and self._error(new_circ, qubit) < self._error(old_run, qubit)) - or math.isclose(self._error(new_circ, qubit)[0], 0) + or (uncalibrated_p and new_error < old_error) + or (math.isclose(new_error[0], 0) and not math.isclose(old_error[0], 0)) ) @control_flow.trivial_recurse diff --git a/releasenotes/notes/fix-optimize-1q-sim-407b88e45e6062b6.yaml b/releasenotes/notes/fix-optimize-1q-sim-407b88e45e6062b6.yaml new file mode 100644 index 000000000000..f531561789e0 --- /dev/null +++ b/releasenotes/notes/fix-optimize-1q-sim-407b88e45e6062b6.yaml @@ -0,0 +1,10 @@ +--- +fixes: + - | + Fixed an issue with the :class:`.Optimize1qGatesDecomposition` transpiler + pass where it would potentially resynthesize a single ideal (meaning the + error rate is ``0.0``) gate which was present in the :class:`.Target`. This + is now fixed so the pass :class:`.Optimize1qGatesDecomposition` will defer + to the circuit's gate if the error rate (which includes number of gates) + are the same. + Fixed `#10568 `__ diff --git a/test/python/transpiler/test_optimize_1q_decomposition.py b/test/python/transpiler/test_optimize_1q_decomposition.py index 5d3ebb3d7245..72dbae0d3115 100644 --- a/test/python/transpiler/test_optimize_1q_decomposition.py +++ b/test/python/transpiler/test_optimize_1q_decomposition.py @@ -745,6 +745,20 @@ def test_nested_control_flow(self): result = passmanager.run(test) self.assertEqual(result, expected) + def test_prefer_no_substitution_if_all_ideal(self): + """Test that gates are not substituted if all our ideal gates in basis.""" + target = Target(num_qubits=1) + target.add_instruction(HGate(), {(0,): InstructionProperties(error=0)}) + target.add_instruction( + UGate(Parameter("a"), Parameter("b"), Parameter("c")), + {(0,): InstructionProperties(error=0)}, + ) + qc = QuantumCircuit(1) + qc.h(0) + opt_pass = Optimize1qGatesDecomposition(target) + res = opt_pass(qc) + self.assertEqual(res, qc) + if __name__ == "__main__": unittest.main()