diff --git a/qiskit/compiler/transpile.py b/qiskit/compiler/transpile.py index fcfe66a2ab9a..258464f26f8d 100644 --- a/qiskit/compiler/transpile.py +++ b/qiskit/compiler/transpile.py @@ -191,19 +191,19 @@ def callback_func(**kwargs): TranspilerError: in case of bad inputs to transpiler (like conflicting parameters) or errors in passes """ - circuits = circuits if isinstance(circuits, list) else [circuits] + arg_circuits_list = isinstance(circuits, list) + circuits = circuits if arg_circuits_list else [circuits] # transpiling schedules is not supported yet. start_time = time() if all(isinstance(c, Schedule) for c in circuits): warnings.warn("Transpiling schedules is not supported yet.", UserWarning) - if len(circuits) == 1: - end_time = time() - _log_transpile_time(start_time, end_time) - return circuits[0] end_time = time() _log_transpile_time(start_time, end_time) - return circuits + if arg_circuits_list: + return circuits + else: + return circuits[0] if pass_manager is not None: _check_conflicting_argument(optimization_level=optimization_level, basis_gates=basis_gates, @@ -242,13 +242,13 @@ def callback_func(**kwargs): # Transpile circuits in parallel circuits = parallel_map(_transpile_circuit, list(zip(circuits, transpile_args))) - if len(circuits) == 1: - end_time = time() - _log_transpile_time(start_time, end_time) - return circuits[0] end_time = time() _log_transpile_time(start_time, end_time) - return circuits + + if arg_circuits_list: + return circuits + else: + return circuits[0] def _check_conflicting_argument(**kargs): diff --git a/releasenotes/notes/transpile_singleton-61755c5cf1970111.yaml b/releasenotes/notes/transpile_singleton-61755c5cf1970111.yaml new file mode 100644 index 000000000000..03757c78de72 --- /dev/null +++ b/releasenotes/notes/transpile_singleton-61755c5cf1970111.yaml @@ -0,0 +1,23 @@ +--- +fixes: + - | + When running :func:`qiskit.compiler.transpile` on a list of circuits with a + single element, the function used to return a circuit instead of a list. Now, + when :func:`qiskit.compiler.transpile` is called with a list, it will return a + list if when that list has a single element. See + `#5260 `__. + + .. code-block:: python + + from qiskit import * + + qc = QuantumCircuit(2) + qc.h(0) + qc.cx(0, 1) + qc.measure_all() + + transpiled = transpile([qc]) + print(type(transpiled), len(transpiled)) + + .. parsed-literal:: + 1 \ No newline at end of file diff --git a/test/python/compiler/test_transpiler.py b/test/python/compiler/test_transpiler.py index 35388affd911..f29928f716cd 100644 --- a/test/python/compiler/test_transpiler.py +++ b/test/python/compiler/test_transpiler.py @@ -257,6 +257,22 @@ def test_transpile_two(self): self.assertIsInstance(circuits[0], QuantumCircuit) self.assertIsInstance(circuits[1], QuantumCircuit) + def test_transpile_singleton(self): + """Test transpile a single-element list with a circuit. + See https://github.com/Qiskit/qiskit-terra/issues/5260""" + backend = BasicAer.get_backend('qasm_simulator') + + qubit_reg = QuantumRegister(2) + clbit_reg = ClassicalRegister(2) + qc = QuantumCircuit(qubit_reg, clbit_reg, name="bell") + qc.h(qubit_reg[0]) + qc.cx(qubit_reg[0], qubit_reg[1]) + qc.measure(qubit_reg, clbit_reg) + + circuits = transpile([qc], backend) + self.assertEqual(len(circuits), 1) + self.assertIsInstance(circuits[0], QuantumCircuit) + def test_mapping_correction(self): """Test mapping works in previous failed case. """