Skip to content

Commit

Permalink
Limit maximum multiprocessing pool size in xeb tests (#6713)
Browse files Browse the repository at this point in the history
Problem: `multiprocessing.Pool()` uses all available cores which can
cause problems on hardware with many cores.

Solution: Limit the maximum pool size to 4.
  • Loading branch information
pavoljuhas authored Aug 30, 2024
1 parent e16d886 commit f0ead96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions cirq-core/cirq/experiments/xeb_fitting_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import itertools
import multiprocessing
from typing import Iterable
Expand All @@ -36,6 +37,8 @@
)
from cirq.experiments.xeb_sampling import sample_2q_xeb_circuits

_POOL_NUM_PROCESSES = min(4, multiprocessing.cpu_count())


@pytest.fixture(scope='module')
def circuits_cycle_depths_sampled_df():
Expand Down Expand Up @@ -229,7 +232,7 @@ def test_characterize_phased_fsim_parameters_with_xeb():
characterize_phi=False,
)
p_circuits = [parameterize_circuit(circuit, options) for circuit in circuits]
with multiprocessing.Pool() as pool:
with multiprocessing.Pool(_POOL_NUM_PROCESSES) as pool:
result = characterize_phased_fsim_parameters_with_xeb(
sampled_df=sampled_df,
parameterized_circuits=p_circuits,
Expand Down Expand Up @@ -270,7 +273,7 @@ def test_parallel_full_workflow(use_pool):
)

if use_pool:
pool = multiprocessing.Pool()
pool = multiprocessing.Pool(_POOL_NUM_PROCESSES)
else:
pool = None

Expand Down
7 changes: 5 additions & 2 deletions cirq-core/cirq/experiments/xeb_simulation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import multiprocessing
from typing import Dict, Any, Optional
from typing import Sequence
Expand All @@ -23,6 +24,8 @@
import cirq.experiments.random_quantum_circuit_generation as rqcg
from cirq.experiments.xeb_simulation import simulate_2q_xeb_circuits

_POOL_NUM_PROCESSES = min(4, multiprocessing.cpu_count())


def test_simulate_2q_xeb_circuits():
q0, q1 = cirq.LineQubit.range(2)
Expand All @@ -42,7 +45,7 @@ def test_simulate_2q_xeb_circuits():
assert len(row['pure_probs']) == 4
assert np.isclose(np.sum(row['pure_probs']), 1)

with multiprocessing.Pool() as pool:
with multiprocessing.Pool(_POOL_NUM_PROCESSES) as pool:
df2 = simulate_2q_xeb_circuits(circuits, cycle_depths, pool=pool)

pd.testing.assert_frame_equal(df, df2)
Expand Down Expand Up @@ -130,7 +133,7 @@ def test_incremental_simulate(multiprocess):
cycle_depths = np.arange(3, 100, 9, dtype=np.int64)

if multiprocess:
pool = multiprocessing.Pool()
pool = multiprocessing.Pool(_POOL_NUM_PROCESSES)
else:
pool = None

Expand Down

0 comments on commit f0ead96

Please sign in to comment.