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 Taso optimiser #104

Merged
merged 18 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ lazy_static = "1.4.0"
cgmath = "0.18.0"
num-rational = "0.4"
num-complex = { version = "0.4", optional = true }
tket-json-rs = { git = "https://github.com/CQCL/tket-json-rs", rev = "619db15d3", features = [
"tket2ops",
] }
tket-json-rs = { workspace = true }
rayon = "1.5"
tket-rs = { optional = true, git = "https://github.com/CQCL-DEV/tket-rs", rev = "bd7e8e04" }
thiserror = "1.0.28"
Expand All @@ -41,6 +39,8 @@ strum = "0.25.0"
fxhash = "0.2.1"
rmp-serde = { version = "1.1.2", optional = true }
delegate = "0.10.0"
csv = { version = "1.2.2", optional = true }
chrono = { version ="0.4.30", optional = true }
bytemuck = "1.14.0"

[features]
Expand All @@ -52,7 +52,7 @@ pyo3 = [
"quantinuum-hugr/pyo3",
]
tkcxx = ["dep:tket-rs", "dep:num-complex"]
portmatching = ["dep:portmatching", "dep:rmp-serde"]
portmatching = ["dep:portmatching", "dep:rmp-serde", "dep:csv", "dep:chrono"]

[dev-dependencies]
rstest = "0.18.1"
Expand All @@ -66,11 +66,14 @@ harness = false

[workspace]

members = ["pyrs", "compile-matcher"]
members = ["pyrs", "compile-matcher", "taso-optimiser"]

[workspace.dependencies]

quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "bc9692b" }
portgraph = { version = "0.9", features = ["serde"] }
pyo3 = { version = "0.19" }
itertools = { version = "0.11.0" }
tket-json-rs = { git = "https://github.com/CQCL/tket-json-rs", rev = "619db15d3", features = [
"tket2ops",
] }
29 changes: 27 additions & 2 deletions src/circuit/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl HashState {
fn set_node(&mut self, circ: &impl HugrView, node: Node, hash: u64) {
let optype = circ.get_optype(node);
let signature = optype.signature();
let mut any_nonlinear = false;
// Defaults to false in most cases, except for OpType::Const.
let mut any_nonlinear = optype.tag() <= OpTag::Const;
for (port_type, port) in signature
.output_types()
.iter()
Expand Down Expand Up @@ -148,8 +149,10 @@ fn hash_node(circ: &impl HugrView, node: Node, state: &mut HashState) -> u64 {
#[cfg(test)]
mod test {
use hugr::hugr::views::{HierarchyView, SiblingGraph};
use hugr::HugrView;
use hugr::{Hugr, HugrView};
use tket_json_rs::circuit_json;

use crate::json::TKETDecode;
use crate::utils::build_simple_circuit;
use crate::T2Op;

Expand Down Expand Up @@ -193,4 +196,26 @@ mod test {

assert_ne!(hash1, hash3);
}

#[test]
fn hash_constants() {
let c_str = r#"{"bits": [], "commands": [{"args": [["q", [0]]], "op": {"params": ["0.5"], "type": "Rz"}}], "created_qubits": [], "discarded_qubits": [], "implicit_permutation": [[["q", [0]], ["q", [0]]]], "phase": "0.0", "qubits": [["q", [0]]]}"#;
let ser: circuit_json::SerialCircuit = serde_json::from_str(c_str).unwrap();
let circ: Hugr = ser.decode().unwrap();
circ.circuit_hash();
}

#[test]
fn hash_constants_neq() {
let c_str1 = r#"{"bits": [], "commands": [{"args": [["q", [0]]], "op": {"params": ["0.5"], "type": "Rz"}}], "created_qubits": [], "discarded_qubits": [], "implicit_permutation": [[["q", [0]], ["q", [0]]]], "phase": "0.0", "qubits": [["q", [0]]]}"#;
let c_str2 = r#"{"bits": [], "commands": [{"args": [["q", [0]]], "op": {"params": ["1.0"], "type": "Rz"}}], "created_qubits": [], "discarded_qubits": [], "implicit_permutation": [[["q", [0]], ["q", [0]]]], "phase": "0.0", "qubits": [["q", [0]]]}"#;

let mut all_hashes = Vec::with_capacity(2);
for c_str in [c_str1, c_str2] {
let ser: circuit_json::SerialCircuit = serde_json::from_str(c_str).unwrap();
let circ: Hugr = ser.decode().unwrap();
all_hashes.push(circ.circuit_hash());
}
assert_ne!(all_hashes[0], all_hashes[1]);
}
}
14 changes: 10 additions & 4 deletions src/json/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,22 @@ impl TryFrom<&OpType> for JsonOp {

let json_optype = if let Ok(t2op) = leaf.clone().try_into() {
match t2op {
T2Op::CX => JsonOpType::CX,
T2Op::H => JsonOpType::H,
T2Op::CX => JsonOpType::CX,
T2Op::T => JsonOpType::T,
T2Op::S => JsonOpType::S,
T2Op::X => JsonOpType::X,
T2Op::Y => JsonOpType::Y,
T2Op::Z => JsonOpType::Z,
T2Op::Tdg => JsonOpType::Tdg,
T2Op::Sdg => JsonOpType::Sdg,
T2Op::ZZMax => JsonOpType::ZZMax,
T2Op::Measure => JsonOpType::Measure,
T2Op::RzF64 => JsonOpType::Rz,
T2Op::RxF64 => JsonOpType::Rx,
T2Op::TK1 => JsonOpType::TK1,
T2Op::PhasedX => JsonOpType::PhasedX,
T2Op::ZZMax => JsonOpType::ZZMax,
T2Op::ZZPhase => JsonOpType::ZZPhase,
_ => return Err(err()),
T2Op::TK1 => JsonOpType::TK1,
}
} else if let LeafOp::CustomOp(b) = leaf {
let ext = (*b).as_ref();
Expand Down
1 change: 1 addition & 0 deletions src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// pub mod redundancy;
// pub mod pattern;
// pub mod squash;
#[cfg(feature = "portmatching")]
pub mod taso;
// use rayon::prelude::*;

Expand Down
Loading