Repository: Qiskit/qiskit

Issue:
transpile() always returns a circuit not in a list whether or not the input is a circuit in a list
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**:
- **Python version**:
- **Operating system**:

### What is the current behavior?
When you run transpile function with a circuit list of only one circuit, then the output is the circuit, not the circuit in a list.


### Steps to reproduce the problem
from qiskit import *
new_qc = transpile([qc], backend) #for some circuit qc and backend
new_qc


### What is the expected behavior?
Right now, new_qc would produce a circuit not in a list, but I think it would be helpful if it would result as a list.


### Suggested solutions
One suggested solution is to make a new variable in the transpile function called list_boolean, or something else, which would keep track of whether the input is a list or not, and in the conditional where the input list is length 1, one can check the value of list_boolean and if it is true, it returns the transpiled circuit in a list, and if false, it just returns the circuit.




Discussion:
It seems to me the current behavior is intentional:
https://github.com/Qiskit/qiskit-terra/blob/397320a8faff018cc9e338aed98d2d73e33a5b54/qiskit/compiler/transpile.py#L200-L206

However, I agree with you that it is counterintuitive. Fixing it is kind of trivial. But maybe we should try to understand deeper why it is currently like this. @ajavadia @kdk ?
From #2166, it looks like the intent was to support both
```
transpiled_qc = transpile(qc, backend)
```
and
```
[transpiled_qc1, transpiled_qc2, ...] = transpile([qc1, qc2, ...], backend)
```
so I think returning a length-1 list for a length-1 list input is consistent.
I think the problem is that `transpiled_qc = transpile([qc], backend)` instead of `[transpiled_qc] = transpile([qc], backend)`

Resolving patch:
diff --git a/qiskit/compiler/transpile.py b/qiskit/compiler/transpile.py
--- 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):


Test patch:
diff --git a/test/python/compiler/test_transpiler.py b/test/python/compiler/test_transpiler.py
--- 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.
         """