Skip to content

Commit

Permalink
Fix parallel test setup
Browse files Browse the repository at this point in the history
In the previous patches we were attempting to use
unittest.mock.patch.dict to override the env variable used to control
parallel execution in qiskit. However, the issue with doing this is that
the env variable is read at import time and stored in a global variable
as we don't expect the env variable to change dynamically during the
execution of a script. To workaround this the mocks are removed and
instead a setUp() method is added to the test class to override the
whatever the environment default is and instead hardcode parallel_map to
run in parallel for the test and then switch it back to the earlier
value after the test finishes. This lets us dynamically adjust the
default behavior for parallel execution for this test class.
  • Loading branch information
mtreinish committed Oct 19, 2022
1 parent cb6ea8d commit 9d2cfd2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from qiskit.quantum_info import Operator, random_unitary
from qiskit.transpiler.passmanager_config import PassManagerConfig
from qiskit.transpiler.preset_passmanagers import level_0_pass_manager
from qiskit.tools import parallel


class CustomCX(Gate):
Expand Down Expand Up @@ -1795,6 +1796,18 @@ def test_custom_multiple_circuits(self):
class TestTranspileParallel(QiskitTestCase):
"""Test transpile() in parallel."""

def setUp(self):
super().setUp()

# Force parallel execution to True to test multiprocessing for this class
original_val = parallel.PARALLEL_DEFAULT

def restore_default():
parallel.PARALLEL_DEFAULT = original_val

self.addCleanup(restore_default)
parallel.PARALLEL_DEFAULT = True

@data(0, 1, 2, 3)
def test_parallel_with_target(self, opt_level):
"""Test that parallel dispatch works with a manual target."""
Expand All @@ -1803,8 +1816,7 @@ def test_parallel_with_target(self, opt_level):
qc.cx(0, 1)
qc.measure_all()
target = FakeMumbaiV2().target
with patch.dict("os.environ", {"QISKIT_PARALLEL": "TRUE"}):
res = transpile([qc] * 3, target=target, optimization_level=opt_level)
res = transpile([qc] * 3, target=target, optimization_level=opt_level)
self.assertIsInstance(res, list)
for circ in res:
self.assertIsInstance(circ, QuantumCircuit)

0 comments on commit 9d2cfd2

Please sign in to comment.