Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelizing scheduling of multiple circuits via parallel_map. #8110

Merged
merged 10 commits into from
Jun 8, 2022
3 changes: 2 additions & 1 deletion qiskit/compiler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from qiskit.providers.backend import Backend
from qiskit.scheduler import ScheduleConfig
from qiskit.scheduler.schedule_circuit import schedule_circuit
from qiskit.tools.parallel import parallel_map

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,7 +98,7 @@ def schedule(

schedule_config = ScheduleConfig(inst_map=inst_map, meas_map=meas_map, dt=dt)
circuits = circuits if isinstance(circuits, list) else [circuits]
schedules = [schedule_circuit(circuit, schedule_config, method) for circuit in circuits]
schedules = parallel_map(schedule_circuit, circuits, (schedule_config, method))
end_time = time()
_log_schedule_time(start_time, end_time)
if arg_circuits_list:
Expand Down
91 changes: 91 additions & 0 deletions test/python/compiler/test_scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Scheduler Test."""

import unittest

from qiskit.circuit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.exceptions import QiskitError
from qiskit.pulse import InstructionScheduleMap, Schedule
from qiskit.test import QiskitTestCase
from qiskit.test.mock import FakeOpenPulse3Q
from qiskit.compiler.scheduler import schedule


class TestCircuitScheduler(QiskitTestCase):
"""Tests for scheduling."""

def setUp(self):
super().setUp()
qr = QuantumRegister(2, name="q")
cr = ClassicalRegister(2, name="c")
self.circ = QuantumCircuit(qr, cr, name="circ")
self.circ.cx(qr[0], qr[1])
self.circ.measure(qr, cr)

qr2 = QuantumRegister(2, name="q")
cr2 = ClassicalRegister(2, name="c")
self.circ2 = QuantumCircuit(qr2, cr2, name="circ2")
self.circ2.cx(qr2[0], qr2[1])
self.circ2.measure(qr2, cr2)

self.backend = FakeOpenPulse3Q()
self.backend_config = self.backend.configuration()
self.num_qubits = self.backend_config.n_qubits

def test_instruction_map_and_backend_not_supplied(self):
"""Test instruction map and backend not supplied."""
with self.assertRaisesRegex(
QiskitError,
r"Must supply either a backend or InstructionScheduleMap for scheduling passes.",
):
schedule(self.circ)

def test_instruction_map_and_backend_defaults_unavailable(self):
"""Test backend defaults unavailable when backend is provided, but instruction map is not."""
self.backend._defaults = None
with self.assertRaisesRegex(
QiskitError, r"The backend defaults are unavailable. The backend may not support pulse."
):
schedule(self.circ, self.backend)

def test_measurement_map_and_backend_not_supplied(self):
"""Test measurement map and backend not supplied."""
with self.assertRaisesRegex(
QiskitError,
r"Must supply either a backend or a meas_map for scheduling passes.",
):
schedule(self.circ, inst_map=InstructionScheduleMap())

def test_schedules_single_circuit(self):
"""Test scheduling of a single circuit."""
circuit_schedule = schedule(self.circ, self.backend)

self.assertIsInstance(circuit_schedule, Schedule)
self.assertEqual(circuit_schedule.name, "circ")

def test_schedules_multiple_circuits(self):
"""Test scheduling of multiple circuits."""
circuits = [self.circ, self.circ2]
circuit_schedules = schedule(circuits, self.backend)
upsideon marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(len(circuit_schedules), len(circuits))

circuit_one_schedule = circuit_schedules[0]
circuit_two_schedule = circuit_schedules[1]

self.assertEqual(circuit_one_schedule.name, "circ")
self.assertEqual(circuit_two_schedule.name, "circ2")
upsideon marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
unittest.main(verbosity=2)
upsideon marked this conversation as resolved.
Show resolved Hide resolved