Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/commands-api
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Sep 16, 2023
2 parents 2781755 + bfdd86b commit 0986ff9
Show file tree
Hide file tree
Showing 40 changed files with 11,074 additions and 1,221 deletions.
2 changes: 1 addition & 1 deletion .github/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ then
exit 1
fi

if ! cargo clippy --all-targets --features="pyo3,portmatching" -- -D warnings
if ! cargo clippy --all-targets --features="pyo3,portmatching" --workspace -- -D warnings
then
echo "There are some clippy issues."
exit 1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Check formatting
run: cargo fmt -- --check
- name: Run clippy
run: cargo clippy --all-targets --features="$FEATURES" -- -D warnings
run: cargo clippy --all-targets --features="$FEATURES" --workspace -- -D warnings
- name: Build docs
run: cargo doc --no-deps --features="$FEATURES"
env:
Expand Down
15 changes: 10 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,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 @@ -65,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 = "e23323d" }
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",
] }
5 changes: 1 addition & 4 deletions compile-matcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ use std::fs;
use std::path::Path;

use clap::Parser;
use hugr::hugr::views::SiblingGraph;
use hugr::ops::handle::DfgID;
use hugr::HugrView;
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
119 changes: 0 additions & 119 deletions pyrs/src/libcopuy

This file was deleted.

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.
38 changes: 17 additions & 21 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{UnitType, Units};
use self::units::{filter, FilteredUnits, Units};

/// An object behaving like a quantum circuit.
//
Expand Down Expand Up @@ -42,24 +42,23 @@ pub trait Circuit: HugrView {
/// Returns the input node to the circuit.
#[inline]
fn input(&self) -> Node {
return self
.children(self.root())
.next()
.expect("Circuit has no input node");
self.get_io(self.root()).expect("Circuit has no input node")[0]
}

/// Returns the output node to the circuit.
#[inline]
fn output(&self) -> Node {
return self
.children(self.root())
.nth(1)
.expect("Circuit has no output node");
self.get_io(self.root())
.expect("Circuit has no output node")[1]
}

/// The number of gates in the circuit.
/// The number of quantum gates in the circuit.
#[inline]
fn num_gates(&self) -> usize {
fn num_gates(&self) -> usize
where
Self: Sized,
{
// TODO: Implement discern quantum gates in the commands iterator.
self.children(self.root()).count() - 2
}

Expand All @@ -78,34 +77,34 @@ pub trait Circuit: HugrView {
where
Self: Sized,
{
Units::new_circ_input(self, UnitType::All)
Units::new_circ_input(self)
}

/// Get the linear input units of the circuit and their types.
#[inline]
fn linear_units(&self) -> Units
fn linear_units(&self) -> FilteredUnits<filter::Linear>
where
Self: Sized,
{
Units::new_circ_input(self, UnitType::Linear)
self.units().filter_units::<filter::Linear>()
}

/// Get the non-linear input units of the circuit and their types.
#[inline]
fn nonlinear_units(&self) -> Units
fn nonlinear_units(&self) -> FilteredUnits<filter::NonLinear>
where
Self: Sized,
{
Units::new_circ_input(self, UnitType::NonLinear)
self.units().filter_units::<filter::NonLinear>()
}

/// Returns the units corresponding to qubits inputs to the circuit.
#[inline]
fn qubits(&self) -> Units
fn qubits(&self) -> FilteredUnits<filter::Qubits>
where
Self: Sized,
{
Units::new_circ_input(self, UnitType::Qubits)
self.units().filter_units::<filter::Qubits>()
}

/// Returns all the commands in the circuit, in some topological order.
Expand Down Expand Up @@ -160,8 +159,5 @@ mod tests {
assert_eq!(circ.nonlinear_units().count(), 0);
assert_eq!(circ.linear_units().count(), 3);
assert_eq!(circ.qubits().count(), 2);

assert!(circ.linear_units().all(|(unit, _, _)| unit.is_linear()));
assert!(circ.nonlinear_units().all(|(unit, _, _)| unit.is_wire()));
}
}
14 changes: 6 additions & 8 deletions src/circuit/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hugr::ops::{OpTag, OpTrait};
use itertools::Itertools;
use petgraph::visit as pv;

use super::units::{LinearUnit, UnitLabeller, UnitType, Units};
use super::units::{filter, DefaultUnitLabeller, LinearUnit, UnitLabeller, Units};
use super::Circuit;

pub use hugr::hugr::CircuitUnit;
Expand Down Expand Up @@ -54,13 +54,13 @@ impl<'circ, Circ: Circuit> Command<'circ, Circ> {
/// Returns the units of this command in a given direction.
#[inline]
pub fn units(&self, direction: Direction) -> Units<&'_ Self> {
Units::new(self.circ, self.node, direction, UnitType::All, self)
Units::new(self.circ, self.node, direction, self)
}

/// Returns the linear units of this command in a given direction.
#[inline]
pub fn linear_units(&self, direction: Direction) -> Units<&'_ Self> {
Units::new(self.circ, self.node, direction, UnitType::Linear, self)
Units::new(self.circ, self.node, direction, self)
}

/// Returns the units and wires of this command in a given direction.
Expand Down Expand Up @@ -241,11 +241,9 @@ where
//
// Updates the map tracking the last wire of linear units.
let linear_units: Vec<_> =
Units::new(self.circ, node, Direction::Outgoing, UnitType::Linear, ())
.map(|(unit, port, _)| {
let CircuitUnit::Linear(_) = unit else {
panic!("Expected a linear unit");
};
Units::new(self.circ, node, Direction::Outgoing, DefaultUnitLabeller)
.filter_units::<filter::Linear>()
.map(|(_, port, _)| {
// Find the linear unit id for this port.
let linear_id = self
.follow_linear_port(node, port)
Expand Down
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]);
}
}
Loading

0 comments on commit 0986ff9

Please sign in to comment.