Fix return type from schedule() with a list of one entry (#7885)

This commit fixes the return type when calling schedule() with a list of
a single entry. Previously this would return a single Schedule object
instead of the expected list return. A similar issue was already fixed
in transpile() in #5298 but schedule still had the incorrect behavior.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Matthew Treinish 2022-04-04 19:46:02 -04:00 committed by GitHub
parent d430e4d259
commit 6192bfd529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View File

@ -65,6 +65,7 @@ def schedule(
Raises:
QiskitError: If ``inst_map`` and ``meas_map`` are not passed and ``backend`` is not passed
"""
arg_circuits_list = isinstance(circuits, list)
start_time = time()
if backend and getattr(backend, "version", 0) > 1:
if inst_map is None:
@ -100,4 +101,7 @@ def schedule(
schedules = [schedule_circuit(circuit, schedule_config, method) for circuit in circuits]
end_time = time()
_log_schedule_time(start_time, end_time)
return schedules[0] if len(schedules) == 1 else schedules
if arg_circuits_list:
return schedules
else:
return schedules[0]

View File

@ -0,0 +1,7 @@
---
fixes:
- |
Fixed an issue with the :class:`~qiskit.compiler.schedule` function where
previously passing in a ``list`` of :class:`~qiskit.circuit.QuantumCircuit`
objects with a single entry would incorrectly return a single
:class:`~.Schedule` object instead of the expected ``list`` return type.

View File

@ -82,6 +82,16 @@ class TestBasicSchedule(QiskitTestCase):
self.assertEqual(actual[0], expected[0])
self.assertEqual(actual[1], expected[1])
def test_single_circuit_list_schedule(self):
"""Test that passing a single circuit list to schedule() returns a list."""
q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q, c)
sched = schedule([qc], self.backend, method="alap")
expected = Schedule()
self.assertIsInstance(sched, list)
self.assertEqual(sched[0].instructions, expected.instructions)
def test_alap_with_barriers(self):
"""Test that ALAP respects barriers on new qubits."""
q = QuantumRegister(2)