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

Draft: Add make_sparse_matrix_op bench #40

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
56 changes: 54 additions & 2 deletions qip/benches/state_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use num_complex::Complex;
use qip::state_ops::iterators::UnitaryOp;
use qip::state_ops::matrix_ops::{apply_op, from_reals};
use qip::state_ops::matrix_ops::{apply_op, from_reals};
use num_traits::One;
use qip::types::Representation;

/// Make the full op matrix from `ops`.
/// Not very efficient, use only for debugging.
Expand All @@ -29,7 +31,7 @@ mod tests {
extern crate test;

use qip::state_ops::iterators::UnitaryOp::*;
use qip::state_ops::matrix_ops::{apply_ops, make_control_op, make_matrix_op};
use qip::state_ops::matrix_ops::{apply_ops, make_control_op, make_matrix_op, make_sparse_matrix_op};
use test::Bencher;

#[bench]
Expand Down Expand Up @@ -366,4 +368,54 @@ mod tests {

b.iter(|| apply_op(n, &op, &input, &mut output, 0, 0));
}

#[bench]
fn bench_sparse_matrix(b: &mut Bencher) {

let n = 8;
let one = Complex::<f64>::one();
let expected_dat = vec![
vec![(1, one)],
vec![(0, one)],
vec![(3, one)],
vec![(2, one)],
];

// let mat: Vec<f64> = from_reals(&[1.0, 0.0, 0.0, 1.0]);
let op = make_sparse_matrix_op(vec![0,1], expected_dat.clone(),Representation::BigEndian).unwrap();
let op = make_control_op((0..n - 1).collect(), op).unwrap();

let base_vector: Vec<f64> = (0..1 << n).map(|_| 0.0).collect();
let input = from_reals(&base_vector);
let mut output = from_reals(&base_vector);

b.iter(|| apply_op(n, &op, &input, &mut output, 0, 0));
}

#[bench]
fn bench_apply_two_sparse_swaps(b: &mut Bencher) {
let n = 3;
let one = Complex::<f64>::one();

let expected_dat = vec![
vec![(1, one)],
vec![(0, one)],
vec![(3, one)],
vec![(2, one)],
];

// This is still work in progress
// let op1 = make_sparse_matrix_op(vec![0,1], expected_dat.clone(), Representation::BigEndian).unwrap();
// let op2 = make_sparse_matrix_op(vec![0,1], expected_dat, Representation::BigEndian).unwrap();

// let base_vector: Vec<f32> = (0..1 << n).map(|_| 0.0).collect();
// let input = from_reals(&base_vector);
// let output = from_reals(&base_vector);
// b.iter(|| {
// apply_op(n, &op1, &input, &mut output,0,0 );
// apply_op(n, &op2,&input, &mut output, 0, 0);
// })
}


}