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: Add CircuitDiff #773

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
116 changes: 108 additions & 8 deletions Cargo.lock

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

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ missing_docs = "warn"
[patch.crates-io]

# Uncomment to use unreleased versions of hugr
# hugr = { git = "https://github.com/CQCL/hugr", rev = "09bd748398ce5afb39f5628d838756ccc03fc775" }
# hugr-core = { git = "https://github.com/CQCL/hugr", rev = "09bd748398ce5afb39f5628d838756ccc03fc775" }
# hugr-passes = { git = "https://github.com/CQCL/hugr", rev = "09bd748398ce5afb39f5628d838756ccc03fc775" }
# hugr-cli = { git = "https://github.com/CQCL/hugr", rev = "09bd748398ce5afb39f5628d838756ccc03fc775" }
# hugr-model = { git = "https://github.com/CQCL/hugr", rev = "09bd748398ce5afb39f5628d838756ccc03fc775" }
hugr = { git = "https://github.com/CQCL/hugr", rev = "0efc806f" }
hugr-core = { git = "https://github.com/CQCL/hugr", rev = "0efc806f" }
hugr-passes = { git = "https://github.com/CQCL/hugr", rev = "0efc806f" }
hugr-cli = { git = "https://github.com/CQCL/hugr", rev = "0efc806f" }
hugr-model = { git = "https://github.com/CQCL/hugr", rev = "0efc806f" }
# portgraph = { git = "https://github.com/CQCL/portgraph", rev = "68b96ac737e0c285d8c543b2d74a7aa80a18202c" }

[workspace.dependencies]
Expand All @@ -53,9 +53,11 @@ crossbeam-channel = "0.5.14"
csv = "1.3.1"
delegate = "0.13.2"
derive_more = "1.0.0"
derive-where = "1.2.7"
downcast-rs = "2.0.1"
fxhash = "0.2.1"
indexmap = "2.7.1"
insta = "1.42.1"
lazy_static = "1.5.0"
num-complex = "0.4"
num-rational = "0.4"
Expand All @@ -66,6 +68,7 @@ pest_derive = "2.7.15"
petgraph = { version = "0.7.1", default-features = false }
priority-queue = "2.1.2"
rayon = "1.5"
relrc = "0.3.0"
rmp-serde = "1.1.2"
rstest = "0.23.0"
serde = "1.0"
Expand Down
4 changes: 3 additions & 1 deletion tket2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ tracing = { workspace = true }
pest = { workspace = true }
pest_derive = { workspace = true }
zstd = { workspace = true, optional = true }

derive-where = { workspace = true }
relrc = { workspace = true }

[dev-dependencies]
rstest = { workspace = true }
cool_asserts = { workspace = true }
insta = { workspace = true }

# Defined here so it can be overridden by the codspeed CI job
# using `cargo add`.
Expand Down
2 changes: 1 addition & 1 deletion tket2/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::collections::HashSet;
use std::iter::Sum;

pub use command::{Command, CommandIterator};
pub use hash::CircuitHash;
pub use hash::{CircuitHash, HashError, HashedCircuit};
use hugr::extension::prelude::{LiftDef, NoopDef, TupleOpDef};
use hugr::hugr::views::{DescendantsGraph, ExtractHugr, HierarchyView};
use itertools::Either::{Left, Right};
Expand Down
59 changes: 58 additions & 1 deletion tket2/src/circuit/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use derive_more::{Display, Error};
use fxhash::{FxHashMap, FxHasher64};
use hugr::hugr::views::{HierarchyView, SiblingGraph};
use hugr::ops::{NamedOp, OpType};
use hugr::{HugrView, Node};
use hugr::{Hugr, HugrView, Node};
use petgraph::visit::{self as pg, Walker};

use super::Circuit;
Expand Down Expand Up @@ -62,6 +62,46 @@ where
}
}

/// A hashed circuit.
///
/// Unlike [`Circuit`], this wrapper type implements [`Hash`] by precomputing
/// the hash and caching it.
///
/// Converting a [`Circuit`] to a [`HashedCircuit`] is done using its [`TryFrom`]
/// implementation.
#[derive(Clone, Debug)]
pub struct HashedCircuit<H = Hugr> {
circuit: Circuit<H>,
hash: u64,
}

impl<H: HugrView> HashedCircuit<H> {
/// Get the underlying circuit.
pub fn circuit(&self) -> &Circuit<H> {
&self.circuit
}

/// Get the hash of the circuit.
pub fn hash(&self) -> u64 {
self.hash
}
}

impl<H: HugrView> TryFrom<Circuit<H>> for HashedCircuit<H> {
type Error = HashError;

fn try_from(circuit: Circuit<H>) -> Result<Self, Self::Error> {
let hash = circuit.circuit_hash()?;
Ok(Self { circuit, hash })
}
}

impl<H: HugrView> Hash for HashedCircuit<H> {
fn hash<Hasher: std::hash::Hasher>(&self, state: &mut Hasher) {
state.write_u64(self.hash);
}
}

/// Auxiliary data for circuit hashing.
///
/// Contains previously computed hashes.
Expand Down Expand Up @@ -224,4 +264,21 @@ mod test {
}
assert_ne!(all_hashes[0], all_hashes[1]);
}

#[test]
fn hashed_circuit_hash() {
let circ = build_simple_circuit(2, |circ| {
circ.append(Tk2Op::H, [0])?;
circ.append(Tk2Op::CX, [0, 1])?;
circ.append(Tk2Op::T, [1])?;
Ok(())
})
.unwrap();

let direct_hash = circ.circuit_hash().unwrap();
let hashed_circ = HashedCircuit::try_from(circ).unwrap();

// Check the cached hash matches direct computation
assert_eq!(direct_hash, hashed_circ.hash());
}
}
Loading
Loading