Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/better-circuit-api
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Sep 16, 2023
2 parents 9a7d253 + b8a34b7 commit 2b210dc
Show file tree
Hide file tree
Showing 24 changed files with 10,494 additions and 441 deletions.
12 changes: 8 additions & 4 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,7 +39,10 @@ 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" }
chrono = { version ="0.4.30" }
bytemuck = "1.14.0"
stringreader = "0.1.1"

[features]
pyo3 = [
Expand All @@ -66,11 +67,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",
] }
2 changes: 1 addition & 1 deletion compile-matcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use itertools::Itertools;

use tket2::json::load_tk1_json_file;
// Import the PatternMatcher struct and its methods
use tket2::passes::taso::load_eccs_json_file;
use tket2::optimiser::taso::load_eccs_json_file;
use tket2::portmatching::{CircuitPattern, PatternMatcher};

/// Program to precompile patterns from files into a PatternMatcher stored as binary file.
Expand Down
1 change: 1 addition & 0 deletions src/passes.rs → 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
46 changes: 37 additions & 9 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use hugr::std_extensions::arithmetic::float_types::ConstF64;
use hugr::values::Value;
use hugr::Hugr;

use stringreader::StringReader;
use thiserror::Error;
use tket_json_rs::circuit_json::SerialCircuit;
use tket_json_rs::optype::OpType as JsonOpType;
Expand Down Expand Up @@ -85,22 +86,49 @@ pub enum OpConvertError {
}

/// Load a TKET1 circuit from a JSON file.
pub fn load_tk1_json_file(path: impl AsRef<Path>) -> Result<Hugr, TK1LoadError> {
pub fn load_tk1_json_file(path: impl AsRef<Path>) -> Result<Hugr, TK1ConvertError> {
let file = fs::File::open(path)?;
let reader = io::BufReader::new(file);
let ser: SerialCircuit = serde_json::from_reader(reader)?;
load_tk1_json_reader(reader)
}

/// Load a TKET1 circuit from a JSON reader.
pub fn load_tk1_json_reader(json: impl io::Read) -> Result<Hugr, TK1ConvertError> {
let ser: SerialCircuit = serde_json::from_reader(json)?;
Ok(ser.decode()?)
}

/// Load a TKET1 circuit from a JSON string.
pub fn load_tk1_json_str(json: &str) -> Result<Hugr, TK1LoadError> {
let ser: SerialCircuit = serde_json::from_str(json)?;
Ok(ser.decode()?)
pub fn load_tk1_json_str(json: &str) -> Result<Hugr, TK1ConvertError> {
let reader = StringReader::new(json);
load_tk1_json_reader(reader)
}

/// Save a circuit to file in TK1 JSON format.
pub fn save_tk1_json_file(path: impl AsRef<Path>, circ: &Hugr) -> Result<(), TK1ConvertError> {
let file = fs::File::create(path)?;
let writer = io::BufWriter::new(file);
save_tk1_json_writer(circ, writer)
}

/// Save a circuit in TK1 JSON format to a writer.
pub fn save_tk1_json_writer(circ: &Hugr, w: impl io::Write) -> Result<(), TK1ConvertError> {
let serial_circ = SerialCircuit::encode(circ)?;
serde_json::to_writer(w, &serial_circ)?;
Ok(())
}

/// Save a circuit in TK1 JSON format to a String.
pub fn save_tk1_json_str(circ: &Hugr) -> Result<String, TK1ConvertError> {
let mut buf = io::BufWriter::new(Vec::new());
save_tk1_json_writer(circ, &mut buf)?;
let bytes = buf.into_inner().unwrap();
String::from_utf8(bytes).map_err(|_| TK1ConvertError::InvalidJson)
}

/// Error type for conversion between `Op` and `OpType`.
#[derive(Debug, Error)]
pub enum TK1LoadError {
pub enum TK1ConvertError {
/// The serialized operation is not supported.
#[error("unsupported serialized operation: {0:?}")]
UnsupportedSerializedOp(JsonOpType),
Expand All @@ -118,19 +146,19 @@ pub enum TK1LoadError {
FileLoadError,
}

impl From<serde_json::Error> for TK1LoadError {
impl From<serde_json::Error> for TK1ConvertError {
fn from(_: serde_json::Error) -> Self {
Self::InvalidJson
}
}

impl From<io::Error> for TK1LoadError {
impl From<io::Error> for TK1ConvertError {
fn from(_: io::Error) -> Self {
Self::FileLoadError
}
}

impl From<OpConvertError> for TK1LoadError {
impl From<OpConvertError> for TK1ConvertError {
fn from(value: OpConvertError) -> Self {
match value {
OpConvertError::UnsupportedSerializedOp(op) => Self::UnsupportedSerializedOp(op),
Expand Down
12 changes: 9 additions & 3 deletions src/json/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,24 @@ 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,
// TODO: Use a TK2 opaque op once we update the tket-json-rs dependency.
T2Op::AngleAdd => JsonOpType::AngleAdd,
T2Op::TK1 => JsonOpType::TK1,
T2Op::PhasedX => JsonOpType::PhasedX,
T2Op::ZZMax => JsonOpType::ZZMax,
T2Op::ZZPhase => JsonOpType::ZZPhase,
_ => return Err(err()),
}
} else if let LeafOp::CustomOp(b) = leaf {
let ext = (*b).as_ref();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod circuit;
pub mod extension;
pub mod json;
pub(crate) mod ops;
pub mod passes;
pub mod optimiser;
pub mod rewrite;
pub use ops::{symbolic_constant_op, Pauli, T2Op};

Expand Down
6 changes: 6 additions & 0 deletions src/optimiser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Optimisers for circuit rewriting.
//!
//! Currently, the only optimiser is TASO
pub mod taso;
pub use taso::TasoOptimiser;
Loading

0 comments on commit 2b210dc

Please sign in to comment.