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

feat: Expose advanced Badger timeout options to tket2-py #506

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions tket2-py/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,27 @@ fn lower_to_pytket<'py>(circ: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>>
/// optimising. This can be deactivated by setting `rebase` to `false`, in which
/// case the circuit is expected to be in the Nam gate set.
///
/// Will use at most `max_threads` threads (plus a constant) and take at most
/// `timeout` seconds (plus a constant). Default to the number of cpus and
/// 15min respectively.
/// Will use at most `max_threads` threads (plus a constant). Defaults to the
/// number of CPUs available.
///
/// The optimisation will terminate at the first of the following timeout
/// criteria, if set:
/// - `timeout` seconds (default: 15min) have elapsed since the start of the
/// optimisation
/// - `progress_timeout` (default: None) seconds have elapsed since progress
/// in the cost function was last made
/// - `max_circuit_cnt` (default: None) circuits have been explored.
///
/// Log files will be written to the directory `log_dir` if specified.
#[pyfunction]
#[allow(clippy::too_many_arguments)]
fn badger_optimise<'py>(
circ: &Bound<'py, PyAny>,
optimiser: &PyBadgerOptimiser,
max_threads: Option<NonZeroUsize>,
timeout: Option<u64>,
progress_timeout: Option<u64>,
max_circuit_cnt: Option<usize>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer max_circuit_count, as marginally less vulgar. you may disagree

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will make that change. Thanks!

log_dir: Option<PathBuf>,
rebase: Option<bool>,
) -> PyResult<Bound<'py, PyAny>> {
Expand Down Expand Up @@ -165,6 +174,7 @@ fn badger_optimise<'py>(
progress_timeout,
n_threads: n_threads.try_into().unwrap(),
split_circuit: true,
max_circuit_cnt,
..Default::default()
};
circ = optimiser.optimise(circ, log_file, options);
Expand Down
14 changes: 11 additions & 3 deletions tket2-py/tket2/_tket2/passes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def badger_optimise(
max_threads: int | None = None,
timeout: int | None = None,
progress_timeout: int | None = None,
max_circuit_cnt: int | None = None,
log_dir: Path | None = None,
rebase: bool | None = False,
) -> CircuitClass:
Expand All @@ -47,9 +48,16 @@ def badger_optimise(
optimising. This can be deactivated by setting `rebase` to `false`, in which
case the circuit is expected to be in the Nam gate set.

Will use at most `max_threads` threads (plus a constant) and take at most
`timeout` seconds (plus a constant). Default to the number of cpus and
15min respectively.
Will use at most `max_threads` threads (plus a constant). Defaults to the
number of CPUs available.

The optimisation will terminate at the first of the following timeout
criteria, if set:
- `timeout` seconds (default: 15min) have elapsed since the start of the
optimisation
- `progress_timeout` (default: None) seconds have elapsed since progress
in the cost function was last made
- `max_circuit_cnt` (default: None) circuits have been explored.

Log files will be written to the directory `log_dir` if specified.
"""
Expand Down
7 changes: 5 additions & 2 deletions tket2-py/tket2/passes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def badger_pass(
max_threads: Optional[int] = None,
timeout: Optional[int] = None,
progress_timeout: Optional[int] = None,
max_circuit_cnt: Optional[int] = None,
log_dir: Optional[Path] = None,
rebase: bool = False,
) -> BasePass:
Expand All @@ -44,8 +45,9 @@ def badger_pass(
`compile-rewriter <https://github.com/CQCL/tket2/tree/main/badger-optimiser>`_
utility. If `rewriter` is not specified, a default one will be used.

The arguments `max_threads`, `timeout`, `log_dir` and `rebase` are optional
and will be passed on to the Badger optimiser if provided."""
The arguments `max_threads`, `timeout`, `progress_timeout`, `max_circuit_cnt`,
`log_dir` and `rebase` are optional and will be passed on to the Badger
optimiser if provided."""
if rewriter is None:
with resources.as_file(
resources.files("tket2").joinpath("data/nam_6_3.rwr")
Expand All @@ -61,6 +63,7 @@ def apply(circuit: Circuit) -> Circuit:
max_threads=max_threads,
timeout=timeout,
progress_timeout=progress_timeout,
max_circuit_cnt=max_circuit_cnt,
log_dir=log_dir,
rebase=rebase,
)
Expand Down
Loading