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(badger): cx and rz const functions and strategies for LexicographicCostFunction #625

Merged
merged 6 commits into from
Oct 1, 2024

Conversation

lmondada
Copy link
Contributor

@lmondada lmondada commented Oct 1, 2024

A couple of points to note:

  1. I've made some minor breaking changes to the Rust API of LexicographicCostFunction. I think it is cleaner now.
  2. I had the choice between keeping fn pointers as the cost function type within LexicographicCostFunction or moving to Box<Fn>. I've stuck to the former for the moment, but I didn't figure out a simple way to reuse the same code for Tk2Op::CX and Tk2Op::RzF64 without using closures. The current code has some duplication as a result, but I think it's bearable.
  3. I've tried running badger with cost_fn='rz', but the Rz gate count does not decrease at all. I've looked for an obvious bug but I don't think it is within these changes...

Let me know if you disagree with 1. or 2 and what you think we should do about 3.


Changelog metadata

BEGIN_COMMIT_OVERRIDE
feat: BadgerOptimiser.load_precompiled, BadgerOptimiser.compile_eccs and passes.badger_pass now take an optional cost_fn parameter to specify the cost function to minimise. Supported values are 'cx' (default behaviour) and 'rz'.

END_COMMIT_OVERRIDE

@lmondada lmondada changed the title feat[badger]!: Add Rz cost function feat(badger)!: Add Rz cost function Oct 1, 2024
@lmondada
Copy link
Contributor Author

lmondada commented Oct 1, 2024

Here's some code that shows no optimisation is happening:

from tket2.passes import badger_pass
from pytket import Circuit, OpType

import json

with open("test_files/barenco_tof_10.json", "r") as f:
    circ = Circuit.from_dict(json.load(f))

p = badger_pass(cost_fn="rz", log_dir="badger_logs", timeout=10)

print('Rz count before:', circ.n_gates_of_type(OpType.Rz))
p.apply(circ)
print('Rz count after:', circ.n_gates_of_type(OpType.Rz))

output:

Rz count before: 100
Rz count after: 100

:(

EDIT: This might just be because barenco_tof_10 has already been optimised for T count!

Copy link

codecov bot commented Oct 1, 2024

Codecov Report

Attention: Patch coverage is 37.50000% with 25 lines in your changes missing coverage. Please review.

Project coverage is 81.69%. Comparing base (295b0df) to head (304d45c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
tket2/src/rewrite/strategy.rs 42.30% 15 Missing ⚠️
tket2/src/optimiser/badger.rs 28.57% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #625      +/-   ##
==========================================
- Coverage   82.06%   81.69%   -0.37%     
==========================================
  Files          48       48              
  Lines        6572     6606      +34     
  Branches     6572     6606      +34     
==========================================
+ Hits         5393     5397       +4     
- Misses        820      850      +30     
  Partials      359      359              
Flag Coverage Δ
rust 81.69% <37.50%> (-0.37%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lmondada
Copy link
Contributor Author

lmondada commented Oct 1, 2024

Update: Running the above script on a different input does something, for example using qf21_n15 and timeout=10 results in a Rz count decrease from 166 to 149.

(Make sure to rebase to CX + H + Rz first)

However using cost_fn='cx' performs equally well, so still not convinced it is doing the right thing.

Running FullPeepholeOptimise followed by Badger (timeout=30) reduces Rz gate 166 -> 122 -> 118.

@aborgna-q aborgna-q self-assigned this Oct 1, 2024
@aborgna-q aborgna-q changed the title feat(badger)!: Add Rz cost function feat(badger): Add Rz cost function Oct 1, 2024
@aborgna-q
Copy link
Collaborator

I edited the changes to make them non-breaking (deprecated the renamed method instead)

@aborgna-q aborgna-q changed the title feat(badger): Add Rz cost function feat(badger): cx and rz const functions and strategies for LexicographicCostFunction Oct 1, 2024
@aborgna-q aborgna-q added this pull request to the merge queue Oct 1, 2024
Merged via the queue into main with commit 83ebfcb Oct 1, 2024
18 of 19 checks passed
@aborgna-q aborgna-q deleted the feat/rzopt branch October 1, 2024 14:10
@hugrbot hugrbot mentioned this pull request Oct 1, 2024
github-merge-queue bot pushed a commit that referenced this pull request Oct 15, 2024
## 🤖 New release
* `tket2`: 0.5.0 -> 0.6.0 (⚠️ API breaking changes)
* `tket2-hseries`: 0.5.0 -> 0.6.0 (✓ API compatible changes)

### ⚠️ `tket2` breaking changes

```
--- failure inherent_method_missing: pub method removed or renamed ---

Description:
A publicly-visible method or associated fn is no longer available under its prior name. It may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.35.0/src/lints/inherent_method_missing.ron

Failed in:
  LexicographicCostFunction::default_cx, previously in file /private/var/folders/3j/ktpgz6yj0gn05q3x3d0qqndw0000gn/T/.tmpwozYGu/tket2/src/rewrite/strategy.rs:349

--- failure pub_module_level_const_missing: pub module-level const is missing ---

Description:
A public const is missing, renamed, or changed from const to static.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.35.0/src/lints/pub_module_level_const_missing.ron

Failed in:
  SYM_OP_ID in file /private/var/folders/3j/ktpgz6yj0gn05q3x3d0qqndw0000gn/T/.tmpwozYGu/tket2/src/extension/sympy.rs:27
  SYM_EXPR_NAME in file /private/var/folders/3j/ktpgz6yj0gn05q3x3d0qqndw0000gn/T/.tmpwozYGu/tket2/src/extension/sympy.rs:24

--- failure struct_missing: pub struct removed or renamed ---

Description:
A publicly-visible struct cannot be imported by its prior path. A `pub use` may have been removed, or the struct itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.35.0/src/lints/struct_missing.ron

Failed in:
  struct tket2::extension::SYM_EXPR_T, previously in file /private/var/folders/3j/ktpgz6yj0gn05q3x3d0qqndw0000gn/T/.tmpwozYGu/tket2/src/extension/sympy.rs:140
```

<details><summary><i><b>Changelog</b></i></summary><p>

## `tket2`
<blockquote>

##
[0.6.0](tket2-v0.5.0...tket2-v0.6.0)
- 2024-10-15

### New Features

- *(badger)* `cx` and `rz` const functions and strategies for
`LexicographicCostFunction`
([#625](#625))
- Add `tket2.rotation.from_halfturns_unchecked` op
([#640](#640))
- [**breaking**] update to hugr 0.13.0
([#645](#645))
- Decode pytket op parameters
([#644](#644))
- re-export hugr crate ([#652](#652))
- Extract pytket parameters to input wires
([#661](#661))

### Refactor

- [**breaking**] Remove deprecated exports
([#662](#662))
</blockquote>

## `tket2-hseries`
<blockquote>

##
[0.4.0](tket2-hseries-v0.3.0...tket2-hseries-v0.4.0)
- 2024-09-16

### New Features

- [**breaking**] `HSeriesPass` lowers `Tk2Op`s into `HSeriesOp`s
([#602](#602))
- [**breaking**] simplify angle extension in to a half turns rotation
type ([#611](#611))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).

Co-authored-by: Agustín Borgna <121866228+aborgna-q@users.noreply.github.com>
github-merge-queue bot pushed a commit that referenced this pull request Oct 15, 2024
## 🤖 New release
* `tket2`: 0.5.0 -> 0.6.0 (⚠️ API breaking changes)
* `tket2-hseries`: 0.5.0 -> 0.6.0 (✓ API compatible changes)

### ⚠️ `tket2` breaking changes

```
--- failure inherent_method_missing: pub method removed or renamed ---

Description:
A publicly-visible method or associated fn is no longer available under its prior name. It may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.35.0/src/lints/inherent_method_missing.ron

Failed in:
  LexicographicCostFunction::default_cx, previously in file /private/var/folders/3j/ktpgz6yj0gn05q3x3d0qqndw0000gn/T/.tmpwozYGu/tket2/src/rewrite/strategy.rs:349

--- failure pub_module_level_const_missing: pub module-level const is missing ---

Description:
A public const is missing, renamed, or changed from const to static.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.35.0/src/lints/pub_module_level_const_missing.ron

Failed in:
  SYM_OP_ID in file /private/var/folders/3j/ktpgz6yj0gn05q3x3d0qqndw0000gn/T/.tmpwozYGu/tket2/src/extension/sympy.rs:27
  SYM_EXPR_NAME in file /private/var/folders/3j/ktpgz6yj0gn05q3x3d0qqndw0000gn/T/.tmpwozYGu/tket2/src/extension/sympy.rs:24

--- failure struct_missing: pub struct removed or renamed ---

Description:
A publicly-visible struct cannot be imported by its prior path. A `pub use` may have been removed, or the struct itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.35.0/src/lints/struct_missing.ron

Failed in:
  struct tket2::extension::SYM_EXPR_T, previously in file /private/var/folders/3j/ktpgz6yj0gn05q3x3d0qqndw0000gn/T/.tmpwozYGu/tket2/src/extension/sympy.rs:140
```

<details><summary><i><b>Changelog</b></i></summary><p>

## `tket2`
<blockquote>

##
[0.6.0](tket2-v0.5.0...tket2-v0.6.0)
- 2024-10-15

### New Features

- *(badger)* `cx` and `rz` const functions and strategies for
`LexicographicCostFunction`
([#625](#625))
- Add `tket2.rotation.from_halfturns_unchecked` op
([#640](#640))
- [**breaking**] update to hugr 0.13.0
([#645](#645))
- Decode pytket op parameters
([#644](#644))
- re-export hugr crate ([#652](#652))
- Extract pytket parameters to input wires
([#661](#661))

### Refactor

- [**breaking**] Remove deprecated exports
([#662](#662))
</blockquote>

## `tket2-hseries`
<blockquote>

##
[0.4.0](tket2-hseries-v0.3.0...tket2-hseries-v0.4.0)
- 2024-09-16

### New Features

- [**breaking**] `HSeriesPass` lowers `Tk2Op`s into `HSeriesOp`s
([#602](#602))
- [**breaking**] simplify angle extension in to a half turns rotation
type ([#611](#611))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).

Co-authored-by: Agustín Borgna <121866228+aborgna-q@users.noreply.github.com>
@hugrbot hugrbot mentioned this pull request Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants