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!: depth optimisation via commutation pass #74

Merged
merged 90 commits into from
Sep 27, 2023
Merged

Conversation

ss2165
Copy link
Member

@ss2165 ss2165 commented Sep 1, 2023

Forms slices of topologically sorted commands, then greedily moves them forwards if a command can moved to an earlier slice through commutation.

Can be used with pytket through pyo3 binding, does not change the operations and respects device connectivity so can be run as a final pass after all existing pytket optimisations.

I benchmarked on all the circuits in software_benchmarking, metric is CZ depth
so ~1100 circuits total
248 qiskit wins
752 pytket wins + tket-2 does not improve on pytket
106 tket-2 improves on pytket

twice tket-2 + pytket outperforms qiskit even though qiskit was outperforming pytket

for total depth:
111 qiskit wins
834 pytket wins + tket-2 does not improve on pytket
161 tket-2 improves on pytket

BREAKING CHANGE: LinearUnit is now a newtype struct

@ss2165 ss2165 requested a review from aborgna-q September 20, 2023 15:21
@aborgna-q aborgna-q mentioned this pull request Sep 21, 2023
Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

👍 as a first tket2 pass.

As expected, it shows some missing spots in our API that we should improve.

pyrs/test/test_bindings.py Outdated Show resolved Hide resolved
pyrs/src/lib.rs Outdated
Comment on lines 12 to 17
let s_c = SerialCircuit::_from_tket1(py_c.clone());
let mut h: Hugr = s_c.decode()?;
let n_moves = apply_greedy_commutation(&mut h)?;

let s_c = SerialCircuit::encode(&h)?;
Ok((s_c.to_tket1()?, n_moves))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let s_c = SerialCircuit::_from_tket1(py_c.clone());
let mut h: Hugr = s_c.decode()?;
let n_moves = apply_greedy_commutation(&mut h)?;
let s_c = SerialCircuit::encode(&h)?;
Ok((s_c.to_tket1()?, n_moves))
try_with_hugr(py_c, |mut h| {
let n_moves = apply_greedy_commutation(&mut h)?;
let py_c = SerialCircuit::encode(&h)?.to_tket1()?;
PyResult::Ok((py_c, n_moves))
})

(and import try_with_hugr).

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is using to_hugr_dot, to_hugr and check_soundness, which are still not being exported in pyrs.

Also, you're loading test files from your home. Maybe put some in tket1/test_files instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

i think I can just remove this file from version control

src/circuit.rs Outdated
@@ -14,7 +14,7 @@ use hugr::types::FunctionType;
pub use hugr::types::{EdgeKind, Signature, Type, TypeRow};
pub use hugr::{Node, Port, Wire};

use self::units::{filter, FilteredUnits, Units};
pub(crate) use self::units::{filter, FilteredUnits, Units};
Copy link
Collaborator

Choose a reason for hiding this comment

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

circuit::filter may be confusing.
Make mod units public instead (reexporting FilteredUnits and Units is ok).

use thiserror::Error;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct Qb(usize);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the same as ::circuit::units::LinearUnit. Can you use a type alias instead?

type Qb = LinearUnit;

src/passes/commutation.rs Show resolved Hide resolved
src/passes/commutation.rs Show resolved Hide resolved
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct ComCommand {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It'd be nice to avoid the extra wrapper, but it's not possible with our current API (see #126).

For the moment I say use this, and we'll simplify it once that issue is solved.

aborgna-q and others added 16 commits September 25, 2023 11:52
Simplifies the multithreading logic by using a multi-consumer channel.
Includes some small improvements to the TASO bin.


Now, why is this a draft:

I'm testing with `barenco_tof_5_rm` and `Nam_6_3_complete_ECC_set` on my
12-core laptop. Here are some results with a timeout of `10` seconds:

|            | circuits seen after 10s | best size after 10s |
| ----------------- | ------ | ---- |
| single-threaded | 7244 | 178 |
| -j 2 | 31391 | 204 |
| -j 10 | 42690 | 204 |

There's something wrong somewhere :P
(note that `-j N` means `N` worker threads, plus the master)

---------

Co-authored-by: Luca Mondada <[email protected]>
Co-authored-by: Luca Mondada <[email protected]>
- qubits units iterators
- linear_unit_port query
- check if a port is linear
- implement traits without requiring bounds on Circ

Also, publicly export `circuit::units` (this required adding a missing
doc).

Closes #124. Closes #123
- Adds some minimal `tracing` instrumentation. The idea is to do some
multithreaded perf debugging in the future based on that.
- Leverages the same infrastructure to refactor the TASO logger, now
events are logged into the global logger, and the executable subscribes
to either general events (for stdout), or more granular data (for the
logfile).
`Port::index()` now requires importing a trait
@ss2165 ss2165 requested a review from aborgna-q September 25, 2023 10:52
@ss2165 ss2165 changed the title feat: depth optimisation via commutation pass feat!: depth optimisation via commutation pass Sep 27, 2023
@ss2165 ss2165 merged commit 16824ac into main Sep 27, 2023
9 checks passed
@ss2165 ss2165 deleted the feat/commutation branch September 27, 2023 10:16
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.

3 participants