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: Configure benchmarking, add hashing benches #67

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ name = "tket2"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
bench = false
name = "tket2"
path = "src/lib.rs"

[dependencies]
lazy_static = "1.4.0"
Expand Down Expand Up @@ -52,16 +55,16 @@ criterion = { version = "0.5.1", features = ["html_reports"] }
webbrowser = "0.8.10"
urlencoding = "2.1.2"

#[[bench]]
#name = "tket2_bench"
#harness = false
[[bench]]
name = "bench_main"
harness = false

[workspace]

members = ["pyrs"]

[workspace.dependencies]

quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "2b05270" }
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "d376eeeee80ac" }
portgraph = "0.8"
pyo3 = { version = "0.19" }
8 changes: 8 additions & 0 deletions benches/bench_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[allow(dead_code)]
mod benchmarks;

use criterion::criterion_main;

criterion_main! {
benchmarks::hash::benches,
}
56 changes: 56 additions & 0 deletions benches/benchmarks/generators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use hugr::builder::{BuildError, CircuitBuilder, DFGBuilder, Dataflow, DataflowHugr};
use hugr::extension::prelude::QB_T;
use hugr::extension::PRELUDE_REGISTRY;
use hugr::types::FunctionType;
use hugr::Hugr;
use tket2::T2Op;

/// Helper function for building circuits.
///
/// TODO: Extracted from tket2::ops::test. Should we expose that instead?
pub fn build_simple_circuit(
num_qubits: usize,
f: impl FnOnce(&mut CircuitBuilder<DFGBuilder<Hugr>>) -> Result<(), BuildError>,
) -> Result<Hugr, BuildError> {
let qb_row = vec![QB_T; num_qubits];
let mut h = DFGBuilder::new(FunctionType::new(qb_row.clone(), qb_row))?;

let qbs = h.input_wires();

let mut circ = h.as_circuit(qbs.into_iter().collect());

f(&mut circ)?;

let qbs = circ.finish();
h.finish_hugr_with_outputs(qbs, &PRELUDE_REGISTRY)
}

/// Create a circuit with layers of CNOTs.
///
/// - In each odd layer, we apply CNOTs between qubits `2i` and `2i+1` for each possible `i`.
/// - In each even layer, we apply CNOTs between qubits `2i+1` and `2i+2` for each possible `i`.
///
/// For example, for 4 qubits and 5 layers, we get the following circuit:
/// ```text
/// --*-----*-----*--
/// | | |
/// --x--*--x--*--x--
/// | |
/// --*--x--*--x--*--
/// | | |
/// --x-----x-----x--
/// ```
pub fn make_cnot_layers(num_qubits: usize, layers: usize) -> Hugr {
build_simple_circuit(num_qubits, |circ| {
for layer in 0..layers {
let start = layer % 2;
let cnot_count = (num_qubits - start) / 2;
for i in 0..cnot_count {
let q = i * 2 + start;
circ.append(T2Op::CX, [q, q + 1])?;
}
}
Ok(())
})
.unwrap()
}
27 changes: 27 additions & 0 deletions benches/benchmarks/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use criterion::{black_box, criterion_group, AxisScale, BenchmarkId, Criterion, PlotConfiguration};
use hugr::hugr::views::SiblingGraph;
use hugr::HugrView;
use tket2::circuit::{CircuitHash, HierarchyView};

use super::generators::make_cnot_layers;

fn bench_hash_simple(c: &mut Criterion) {
let mut g = c.benchmark_group("hash a simple circuit");
g.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));

for size in [10, 100, 1_000] {
g.bench_with_input(BenchmarkId::new("hash_simple", size), &size, |b, size| {
let hugr = make_cnot_layers(8, *size);
let circ: SiblingGraph<'_> = SiblingGraph::new(&hugr, hugr.root());
b.iter(|| black_box(circ.circuit_hash()))
});
}
g.finish();
}

criterion_group! {
name = benches;
config = Criterion::default();
targets =
bench_hash_simple,
}
3 changes: 3 additions & 0 deletions benches/benchmarks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod generators;

pub mod hash;
219 changes: 0 additions & 219 deletions benches/tket2_bench.rs

This file was deleted.

5 changes: 3 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Utility functions for the library.

use hugr::extension::PRELUDE_REGISTRY;
use hugr::{
builder::{BuildError, CircuitBuilder, DFGBuilder, Dataflow, DataflowHugr},
extension::{prelude::QB_T, prelude_registry},
extension::prelude::QB_T,
types::FunctionType,
Hugr,
};
Expand All @@ -23,7 +24,7 @@ pub(crate) fn build_simple_circuit(
f(&mut circ)?;

let qbs = circ.finish();
h.finish_hugr_with_outputs(qbs, &prelude_registry())
h.finish_hugr_with_outputs(qbs, &PRELUDE_REGISTRY)
}

// Test only utils
Expand Down