From 5a19f8a191220671f7748cb4ce4115bd37805b3d Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Thu, 23 Mar 2023 13:57:23 +0000 Subject: [PATCH] Fix missing handling of no-synthesis case in `UnitarySynthesis` (#9843) Since moving `Optimize1qDecomposition` down to the Rust level, some of its use in the default `UnitarySynthesis` plugin was allowing `None` to be passed to an internal function unchecked. This correctly returns the "could not synthesise" value when appropriate. --- qiskit/transpiler/passes/synthesis/unitary_synthesis.py | 7 ++++--- test/python/transpiler/test_unitary_synthesis.py | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/qiskit/transpiler/passes/synthesis/unitary_synthesis.py b/qiskit/transpiler/passes/synthesis/unitary_synthesis.py index 48bd0b0f8b38..9b42567b8697 100644 --- a/qiskit/transpiler/passes/synthesis/unitary_synthesis.py +++ b/qiskit/transpiler/passes/synthesis/unitary_synthesis.py @@ -740,9 +740,10 @@ def run(self, unitary, **options): if unitary.shape == (2, 2): _decomposer1q = Optimize1qGatesDecomposition(basis_gates, target) - return _decomposer1q._gate_sequence_to_dag( - _decomposer1q._resynthesize_run(unitary, qubits[0]) - ) + sequence = _decomposer1q._resynthesize_run(unitary, qubits[0]) + if sequence is None: + return None + return _decomposer1q._gate_sequence_to_dag(sequence) elif unitary.shape == (4, 4): # select synthesizers that can lower to the target if target is not None: diff --git a/test/python/transpiler/test_unitary_synthesis.py b/test/python/transpiler/test_unitary_synthesis.py index a51f7a420734..0fba49dc3861 100644 --- a/test/python/transpiler/test_unitary_synthesis.py +++ b/test/python/transpiler/test_unitary_synthesis.py @@ -857,6 +857,12 @@ def __init__(self): result_qc = dag_to_circuit(result_dag) self.assertEqual(result_qc, QuantumCircuit(2)) + def test_default_does_not_fail_on_no_syntheses(self): + qc = QuantumCircuit(1) + qc.unitary(np.eye(2), [0]) + pass_ = UnitarySynthesis(["unknown", "gates"]) + self.assertEqual(qc, pass_(qc)) + if __name__ == "__main__": unittest.main()