-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Parallelize path simulations #372
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
base: main
Are you sure you want to change the base?
Changes from all commits
fbe395f
cc95178
d8d4bc7
c3d0b6a
d27ef51
7ca7959
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import warnings | ||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Callable | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
|
|
@@ -163,6 +164,11 @@ def run( | |
| return self.run_program_set(circuit_ir, *args, **kwargs) | ||
| return self.run_jaqcd(circuit_ir, *args, **kwargs) | ||
|
|
||
| @property | ||
| def parallelize_paths(self) -> bool: | ||
| """bool: Whether to run path simulations in parallel.""" | ||
| return False | ||
|
|
||
| def create_program_context(self) -> AbstractProgramContext: | ||
| return ProgramContext(simulator=self) | ||
|
|
||
|
|
@@ -878,14 +884,29 @@ def _run_branched( | |
| if circuit.qubit_set: | ||
| sim_qubit_count = max(sim_qubit_count, max(circuit.qubit_set) + 1) | ||
|
|
||
| # Aggregate samples across all active paths | ||
| paths = list(context.active_paths) | ||
| if self.parallelize_paths and len(paths) > 1: | ||
| with ThreadPoolExecutor() as pool: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the performance impact of this verse letting the simulator parallelize each serial simulation?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have the same question. In the beginning there is only one path, and the paths are only added during a path simulation. Would it cause a conflict? Is there really a performance gain? |
||
| per_path_samples = list( | ||
| pool.map( | ||
| _evolve_path_and_sample, | ||
| [self] * len(paths), | ||
| [path.instructions for path in paths], | ||
| [sim_qubit_count] * len(paths), | ||
| [path.shots for path in paths], | ||
| [batch_size] * len(paths), | ||
| ) | ||
| ) | ||
| else: | ||
| per_path_samples = [ | ||
| _evolve_path_and_sample( | ||
| self, path.instructions, sim_qubit_count, path.shots, batch_size | ||
| ) | ||
| for path in paths | ||
| ] | ||
| all_samples = [] | ||
| for path in context.active_paths: | ||
| sim = self.initialize_simulation( | ||
| qubit_count=sim_qubit_count, shots=path.shots, batch_size=batch_size | ||
| ) | ||
| sim.evolve(path.instructions) | ||
| all_samples.extend(sim.retrieve_samples()) | ||
| for samples in per_path_samples: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use |
||
| all_samples.extend(samples) | ||
|
|
||
| # Build measurements in the same format as _formatted_measurements | ||
| measurements = [ | ||
|
|
@@ -995,3 +1016,17 @@ def run_jaqcd( | |
| ) | ||
|
|
||
| return self._create_results_obj(results, circuit_ir, simulation) | ||
|
|
||
|
|
||
| def _evolve_path_and_sample( | ||
| simulator: BaseLocalSimulator, | ||
| instructions, | ||
| qubit_count: int, | ||
| shots: int, | ||
| batch_size: int, | ||
| ): | ||
| sim = simulator.initialize_simulation( | ||
| qubit_count=qubit_count, shots=shots, batch_size=batch_size | ||
| ) | ||
| sim.evolve(instructions) | ||
| return sim.retrieve_samples() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an
intto allow for configurable thread count?