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: PortDiff for StaticSizeCircuit #561

Draft
wants to merge 57 commits into
base: feat/badgerv2
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
dd083ee
feat: Add StaticSizeCircuit
lmondada Aug 19, 2024
67d6d90
Add linked_op method
lmondada Aug 20, 2024
c710541
feat: Fast hashing for StaticSizeCircuit
lmondada Aug 20, 2024
e3f773d
chore: Move `pyproject.toml` to `tket2-py` (#546)
aborgna-q Aug 22, 2024
58a5015
Implement portmatching
lmondada Aug 23, 2024
dd29e3a
chore: Use portmatching pre-release
lmondada Aug 26, 2024
5038474
chore(deps-py): bump the dev group across 1 directory with 3 updates …
dependabot[bot] Aug 27, 2024
c1210cc
feat: PortDiff support
lmondada Aug 27, 2024
685b4f7
chore: Replace `poetry` with `uv` (#557)
aborgna-q Aug 27, 2024
3d32130
chore(deps-rs): bump the patch group with 2 updates (#558)
dependabot[bot] Aug 27, 2024
486fd29
Finish PortDiff impl
lmondada Aug 27, 2024
7c83ba8
feat: Extend Command::optype lifetime (#562)
lmondada Aug 28, 2024
17644ef
chore(deps-rs): bump bytemuck from 1.17.0 to 1.17.1 in the patch grou…
dependabot[bot] Sep 2, 2024
258a7c5
feat!: Update rust hugr dependency to `0.12.0`, and python hugr to `0…
aborgna-q Sep 2, 2024
8503cc9
feat: Badger on PortDiff
lmondada Sep 3, 2024
42cc82f
feat!: remove Tk2Op::AngleAdd (#567)
ss2165 Sep 3, 2024
fb2e4e0
fix: Deactivate Sentinels, check owner in SameOp predicate
lmondada Sep 3, 2024
ee4dc88
docs: Add static_circ docs
lmondada Sep 3, 2024
0147a35
chore: Upgarde to portmatching rc2
lmondada Sep 3, 2024
9d7ef43
feat: Support empty wire
lmondada Sep 3, 2024
1a617aa
feat: Update search strategy
lmondada Sep 3, 2024
a231905
feat(badger): Fix qubit order
lmondada Sep 3, 2024
62351fb
feat!: angle type no longer parametric. (#577)
ss2165 Sep 4, 2024
e6acc88
feat!: HSeries ops (#573)
ss2165 Sep 4, 2024
cc41081
feat: Better cost heuristic
lmondada Sep 4, 2024
d14631f
feat!: replace f64 with angle type for tk2 ops (#578)
ss2165 Sep 4, 2024
bf73511
feat: more angle ops (construct, deconstruct, radians, equality) (#581)
ss2165 Sep 4, 2024
06a6838
fix!: quantum extension name wrong way round (#582)
ss2165 Sep 4, 2024
6fe8807
chore: Update to portdiff v0.3 and activate salient squashing
lmondada Sep 4, 2024
eb833a4
chore: release v0.2.0 (#553)
Sep 4, 2024
d9538d3
chore(py): release tket2-py 0.3.0 (#570)
Sep 4, 2024
e3f9da1
ci: Fix incorrect wheels name on publish (#587)
aborgna-q Sep 5, 2024
f0884cc
feat!: move angle types + and ops to new "tket2.angle" extension (#591)
ss2165 Sep 5, 2024
09a7b8b
ci: Fix wheels upload on different dir (#590)
aborgna-q Sep 5, 2024
7591c08
fix: extension ops checking against incorrect name (#593)
ss2165 Sep 5, 2024
5ca29af
fix!: remove TryFrom for extension ops use `cast` (#592)
ss2165 Sep 5, 2024
dd9592f
fix: don't load angle extensions on to quantum (#597)
ss2165 Sep 5, 2024
dcc562d
feat: dataflow builder methods for angle ops (#596)
ss2165 Sep 6, 2024
cd27d46
fix: badger-optimiser produces Diff json output
lmondada Sep 6, 2024
7ac0206
feat: lowering tk2ops -> hseriesops (#579)
ss2165 Sep 6, 2024
304471b
feat: Store rewrite value in serialised format
lmondada Sep 9, 2024
9358142
feat: Enable compile-rewriter
lmondada Sep 9, 2024
9ed57c1
chore: Upgrade portdiff to v0.3.1
lmondada Sep 9, 2024
58e1626
chore(deps-rs): bump the minor group with 2 updates (#600)
dependabot[bot] Sep 9, 2024
d9deacb
chore(deps-rs): bump the patch group with 3 updates (#599)
dependabot[bot] Sep 9, 2024
191c86f
WIP Python bindings for StaticSizeCircuit
lmondada Sep 10, 2024
aed9d73
Merge branch 'main' into feat/portdiff
ABorgna Sep 10, 2024
f537bb5
Solve some warnings
lmondada Sep 10, 2024
7c5de17
Use portdiff's MergeStrategy
lmondada Sep 9, 2024
4ba3e8c
just fix
ABorgna Sep 10, 2024
3b61397
Wrap PortDiffGraph
ABorgna Sep 10, 2024
34c70da
Add python all_edges
lmondada Sep 11, 2024
469a5f2
Working badgerv2 prototype
lmondada Sep 20, 2024
1cee039
Update portdiff
lmondada Sep 20, 2024
970e782
feat: PortDiff rendering in jupyter
lmondada Sep 20, 2024
b99fa90
docs: Add badger v2 demo
lmondada Sep 20, 2024
52d33b2
Use https
lmondada Sep 23, 2024
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
Prev Previous commit
Next Next commit
Solve some warnings
lmondada committed Sep 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit f537bb56a9ace8a0c53a0a2e419793cac136ab01
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion badger-optimiser/src/main.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ use tket2::optimiser::badger::log::BadgerLogger;
use tket2::optimiser::badger::BadgerOptions;
use tket2::optimiser::BadgerOptimiser;
use tket2::optimiser::DiffBadgerOptimiser;
use tket2::serialize::{load_tk1_json_file, save_tk1_json_file};
use tket2::serialize::load_tk1_json_file;
use tket2::static_circ::StaticSizeCircuit;

#[cfg(feature = "peak_alloc")]
4 changes: 2 additions & 2 deletions tket2-py/src/optimiser.rs
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@ use portdiff::PortDiffGraph;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use tket2::optimiser::badger::BadgerOptions;
use tket2::optimiser::{BadgerLogger, DefaultBadgerOptimiser, DiffBadgerOptimiser};
use tket2::optimiser::{BadgerLogger, DiffBadgerOptimiser};
use tket2::static_circ::StaticSizeCircuit;
use tket2::Circuit;

use crate::circuit::{try_with_circ, update_circ};
use crate::circuit::try_with_circ;

/// The module definition
pub fn module(py: Python<'_>) -> PyResult<Bound<'_, PyModule>> {
12 changes: 6 additions & 6 deletions tket2-py/src/passes.rs
Original file line number Diff line number Diff line change
@@ -2,18 +2,18 @@

pub mod chunks;

use std::{cmp::min, convert::TryInto, fs, num::NonZeroUsize, path::PathBuf};
// use std::{cmp::min, convert::TryInto, fs, num::NonZeroUsize, path::PathBuf};

use pyo3::{prelude::*, types::IntoPyDict};
use tket2::optimiser::badger::BadgerOptions;
// use tket2::optimiser::badger::BadgerOptions;
use tket2::passes;
use tket2::{op_matches, Tk2Op};
// use tket2::{op_matches, Tk2Op};

use crate::circuit::CircuitType;
use crate::utils::{create_py_exception, ConvertPyErr};
use crate::{
circuit::{try_update_circ, try_with_circ},
optimiser::PyBadgerOptimiser,
circuit::try_with_circ,
// optimiser::PyBadgerOptimiser,
};

/// The module definition
@@ -62,7 +62,7 @@ fn greedy_depth_reduce<'py>(circ: &Bound<'py, PyAny>) -> PyResult<(Bound<'py, Py
/// from pytket.passes.auto_rebase import auto_rebase_pass
/// from pytket import OpType
/// auto_rebase_pass({OpType.CX, OpType.Rz, OpType.H}).apply(circ)"
// ```
/// ```
fn rebase_nam(circ: &Bound<PyAny>) -> PyResult<()> {
let py = circ.py();
let auto_rebase = py
1 change: 0 additions & 1 deletion tket2/src/optimiser.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,5 @@

#[cfg(feature = "portmatching")]
pub mod badger;

#[cfg(feature = "portmatching")]
pub use badger::{BadgerLogger, BadgerOptimiser, DefaultBadgerOptimiser, DiffBadgerOptimiser};
8 changes: 3 additions & 5 deletions tket2/src/optimiser/badger.rs
Original file line number Diff line number Diff line change
@@ -20,11 +20,10 @@ mod qtz_circuit;

pub use eq_circ_class::{load_eccs_json_file, EqCircClass};
use fxhash::{FxHashMap, FxHashSet};
use itertools::Itertools;
pub use log::BadgerLogger;
use portdiff::{PortDiff, PortDiffGraph};

use std::collections::{BTreeMap, BTreeSet};
use std::collections::BTreeMap;
use std::iter::Sum;
use std::num::NonZeroUsize;
use std::ops::{AddAssign, Deref};
@@ -37,7 +36,7 @@ use crate::optimiser::badger::hugr_pqueue::{Entry, HugrPQ};
use crate::portdiff::{DiffCircuit, DiffRewrite};
use crate::rewrite::strategy::StrategyCost;
use crate::rewrite::Rewriter;
use crate::static_circ::{StaticSizeCircuit, UpdatableHash};
use crate::static_circ::StaticSizeCircuit;

/// Configuration options for the Badger optimiser.
#[derive(Copy, Clone, Debug)]
@@ -832,12 +831,11 @@ mod tests {
extension::prelude::QB_T,
types::Signature,
};
use rstest::{fixture, rstest};
use rstest::fixture;

use crate::serialize::load_tk1_json_str;
use crate::{extension::angle::ANGLE_TYPE, optimiser::badger::BadgerOptions};
use crate::{extension::REGISTRY, Circuit, Tk2Op};
use crate::{optimiser::badger::BadgerOptions, static_circ::StaticSizeCircuit};

use super::{BadgerOptimiser, DefaultBadgerOptimiser};

2 changes: 0 additions & 2 deletions tket2/src/optimiser/badger/hugr_pqueue.rs
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@ use delegate::delegate;
use fxhash::FxHashMap;
use priority_queue::DoublePriorityQueue;

use crate::circuit::CircuitHash;

/// A min-priority queue for Hugrs.
///
/// The cost function provided will be used as the priority of the Hugrs.
17 changes: 16 additions & 1 deletion tket2/src/portdiff.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ pub use rewrite::DiffRewrite;

use derive_more::Into;
use hugr::{Direction, IncomingPort, OutgoingPort, Port, PortIndex};
use itertools::Itertools;
use portdiff::{self as pd, port_diff::Owned, BoundPort, EdgeEnd, Site};

use crate::static_circ::{OpId, OpPosition, StaticSizeCircuit};
@@ -129,6 +128,7 @@ impl pd::Graph for StaticSizeCircuit {
}
}

/// An edge in a StaticSizeCircuit.
#[derive(
Debug,
Clone,
@@ -149,6 +149,7 @@ pub struct EdgeId {
}

impl EdgeId {
/// Create an edge from a site in the circuit.
pub fn try_from_site(site: Site<OpId, Port>, circuit: &StaticSizeCircuit) -> Option<Self> {
let (opp_port, opp_node) = circuit.linked_op(site.node, site.port)?;
let (out_port, source) = match site.port.direction() {
@@ -159,34 +160,48 @@ impl EdgeId {
Some(Self { source, out_port })
}

/// Get the source of the edge.
pub fn source(&self) -> OpId {
self.source
}

/// Get the source port of the edge.
pub fn source_port(&self) -> OutgoingPort {
self.out_port
}

/// Get the source position of the edge.
///
/// This requires the `circuit` the edge belongs to.
pub fn source_position(&self, circuit: &StaticSizeCircuit) -> OpPosition {
circuit
.get_position(self.source(), self.source_port().index())
.expect("invalid edge ID")
}

/// Get the target of the edge.
///
/// This requires the `circuit` the edge belongs to.
pub fn target(&self, circuit: &StaticSizeCircuit) -> OpId {
let (_, target) = circuit
.linked_op(self.source().into(), self.source_port().into())
.expect("invalid edge ID");
target
}

/// Get the target port of the edge.
///
/// This requires the `circuit` the edge belongs to.
pub fn target_port(&self, circuit: &StaticSizeCircuit) -> IncomingPort {
let (target_port, _) = circuit
.linked_op(self.source().into(), self.source_port().into())
.expect("invalid edge ID");
target_port.as_incoming().expect("invalid edge ID")
}

/// Get the target position of the edge.
///
/// This requires the `circuit` the edge belongs to.
pub fn target_position(&self, circuit: &StaticSizeCircuit) -> OpPosition {
circuit
.get_position(self.target(circuit), self.target_port(circuit).index())
1 change: 1 addition & 0 deletions tket2/src/portmatching/constraint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Constraints for port matching.
use std::collections::BTreeSet;

use super::{indexing::PatternOpPosition, predicate::Predicate};
2 changes: 2 additions & 0 deletions tket2/src/portmatching/indexing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Indexing scheme into StaticSizeCircuit.

mod pattern;

pub use pattern::{DisconnectedCircuit, PatternOpPosition};
6 changes: 5 additions & 1 deletion tket2/src/portmatching/indexing/pattern.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

use std::collections::VecDeque;

use crate::static_circ::{OpPosition, StaticQubitIndex, StaticSizeCircuit};
use crate::static_circ::{OpPosition, StaticSizeCircuit};

use itertools::Itertools;
use thiserror::Error;
@@ -35,20 +35,24 @@ impl Ord for PatternOpPosition {
}

impl PatternOpPosition {
/// Create a new position in a pattern.
pub fn new(qubit: CircuitPath, op_idx: i8) -> Self {
Self { qubit, op_idx }
}

/// Create a new position with a different operation index.
pub fn with_op_idx(self, op_idx: i8) -> Self {
Self { op_idx, ..self }
}

/// Create a new position from an operation position.
pub fn from_position(loc: OpPosition, starts: &[(CircuitPath, usize)]) -> Self {
let (qubit_path, start) = starts[loc.qubit.0];
let offset = (loc.index as i8) - (start as i8);
PatternOpPosition::new(qubit_path, offset)
}

/// Create the root pattern position.
pub fn root() -> Self {
Self {
qubit: CircuitPath([0; MAX_PATH_LEN * 2]),
25 changes: 21 additions & 4 deletions tket2/src/portmatching/predicate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Pattern matching predicates.

use std::collections::BTreeSet;

use itertools::Itertools;
@@ -17,13 +19,28 @@ use super::{indexing::PatternOpPosition, Constraint};
)]
pub enum Predicate {
/// An edge from `out_port` to `in_port`.
Link { out_port: usize, in_port: usize },
Link {
/// The outgoing source port of the edge.
out_port: usize,
/// The incoming target port of the edge.
in_port: usize,
},
/// An operation of type `op`.
IsOp { op: Tk2Op },
IsOp {
/// The operation type to match.
op: Tk2Op,
},
/// All locations are the same operation.
SameOp { arity: usize },
SameOp {
/// The arity of the predicate, i.e. the number of positions
/// that share the same op.
arity: usize,
},
/// Check that the first qubit is distinct from all others.
DistinctQubits { n_qubits: usize },
DistinctQubits {
/// The number of positions to check.
n_qubits: usize,
},
}

impl pm::ArityPredicate for Predicate {
3 changes: 3 additions & 0 deletions tket2/src/rewrite.rs
Original file line number Diff line number Diff line change
@@ -148,11 +148,14 @@ impl CircuitRewrite {
}
}

/// The cost delta type for a rewrite rule.
pub type CostDelta<S> = <<S as StrategyCost>::OpCost as CircuitCost>::CostDelta;

/// Generate rewrite rules for circuits.
pub trait Rewriter<C> {
/// The rewrite object type.
type CircuitRewrite;
/// The error type for rewrite rules.
type Error;

/// Get the rewrite rules for a circuit.