|
23 | 23 |
|
24 | 24 | from qiskit.circuit import QuantumCircuit |
25 | 25 | from qiskit.converters import circuit_to_dag |
26 | | -from qiskit.transpiler import PassManager |
| 26 | +from qiskit.transpiler import PassManager, Target, CouplingMap |
27 | 27 | from qiskit.transpiler.passes import UnitarySynthesis |
28 | 28 | from qiskit.transpiler.passes.synthesis.plugin import ( |
29 | 29 | UnitarySynthesisPlugin, |
@@ -304,6 +304,40 @@ def test_config_not_passed_to_default_on_fallback(self): |
304 | 304 | self.MOCK_PLUGINS["_controllable"].run.assert_not_called() |
305 | 305 | self.assertNotIn("config", call_kwargs) |
306 | 306 |
|
| 307 | + def test_target_overrides_basis_gates(self): |
| 308 | + """Test that when both target and basis gates are specified, but the plugin only supports |
| 309 | + basis gates, the basis gates that it gets come from the target.""" |
| 310 | + |
| 311 | + self.MOCK_PLUGINS["_controllable"].support(["basis_gates"]) |
| 312 | + |
| 313 | + qc = QuantumCircuit(2) |
| 314 | + qc.unitary(np.eye(4, dtype=np.complex128), [0, 1]) |
| 315 | + |
| 316 | + target_basis_gates = ["rx", "rz"] |
| 317 | + standalone_basis_gates = ["cx", "u"] |
| 318 | + target = Target.from_configuration( |
| 319 | + num_qubits=2, basis_gates=target_basis_gates, coupling_map=CouplingMap.from_line(2) |
| 320 | + ) |
| 321 | + pm = PassManager( |
| 322 | + [ |
| 323 | + UnitarySynthesis( |
| 324 | + target=target, basis_gates=standalone_basis_gates, method="_controllable" |
| 325 | + ) |
| 326 | + ] |
| 327 | + ) |
| 328 | + |
| 329 | + with self.mock_default_run_method(): |
| 330 | + pm.run(qc) |
| 331 | + self.DEFAULT_PLUGIN.run.assert_not_called() # pylint: disable=no-member |
| 332 | + self.MOCK_PLUGINS["_controllable"].run.assert_called() # pylint: disable=no-member |
| 333 | + call_kwargs = self.MOCK_PLUGINS["_controllable"].run.call_args[ |
| 334 | + 1 |
| 335 | + ] # pylint: disable=no-member |
| 336 | + |
| 337 | + self.assertIn("basis_gates", call_kwargs) |
| 338 | + self.assertEqual(call_kwargs["basis_gates"], set(target_basis_gates)) |
| 339 | + self.assertNotEqual(call_kwargs["basis_gates"], set(standalone_basis_gates)) |
| 340 | + |
307 | 341 |
|
308 | 342 | if __name__ == "__main__": |
309 | 343 | unittest.main() |
0 commit comments