Skip to content

Commit

Permalink
Fixes to CP and RZ parallel versions
Browse files Browse the repository at this point in the history
  • Loading branch information
smu160 committed Jul 24, 2023
1 parent 7ef2682 commit 3045708
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
21 changes: 17 additions & 4 deletions spinoza/examples/h.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ use spinoza::{
fn h(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
let elapsed = now.elapsed().as_micros();
println!(
"created state of {} qubits in {} us",
n,
pretty_print_int(elapsed)
);

for i in 0..n {
apply(Gate::H, &mut state, i);
for t in 0..n {
let now = std::time::Instant::now();
apply(Gate::H, &mut state, t);
let elapsed = now.elapsed().as_micros();
println!(
"applied H to target {} in {} us",
t,
pretty_print_int(elapsed)
);
}

let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
// let elapsed = now.elapsed().as_micros();
// println!("{}", pretty_print_int(elapsed));
if show_results {
to_table(&state);
}
Expand Down
34 changes: 24 additions & 10 deletions spinoza/src/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,28 @@ fn p_c_apply(state: &mut State, control: usize, target: usize, angle: Float) {
let end = state.len() >> 2;
let marks = (target.min(control), target.max(control));

for i in 0..end {
let x = i + (1 << (marks.1 - 1)) + ((i >> (marks.1 - 1)) << (marks.1 - 1));
let s1 = x + (1 << marks.0) + ((x >> marks.0) << marks.0);
let z_re = state.reals[s1];
let z_im = state.imags[s1];
state.reals[s1] = z_re.mul_add(cos, -z_im * sin);
state.imags[s1] = z_im.mul_add(cos, z_re * sin);
if Config::global().threads < 2 {
for i in 0..end {
let x = i + (1 << (marks.1 - 1)) + ((i >> (marks.1 - 1)) << (marks.1 - 1));
let s1 = x + (1 << marks.0) + ((x >> marks.0) << marks.0);
let z_re = state.reals[s1];
let z_im = state.imags[s1];
state.reals[s1] = z_re.mul_add(cos, -z_im * sin);
state.imags[s1] = z_im.mul_add(cos, z_re * sin);
}
} else {
let state_re = SendPtr(state.reals.as_mut_ptr());
let state_im = SendPtr(state.imags.as_mut_ptr());
(0..end).into_par_iter().for_each(|i| {
let x = i + (1 << (marks.1 - 1)) + ((i >> (marks.1 - 1)) << (marks.1 - 1));
let s1 = x + (1 << marks.0) + ((x >> marks.0) << marks.0);
unsafe {
let z_re = *state_re.get().add(s1);
let z_im = *state_im.get().add(s1);
*state_re.get().add(s1) = z_re.mul_add(cos, -z_im * sin);
*state_im.get().add(s1) = z_im.mul_add(cos, z_re * sin);
}
});
}
}

Expand Down Expand Up @@ -538,9 +553,8 @@ fn rz_apply_strategy1(state: &mut State, target: usize, diag_matrix: &[Amplitude
let m = diag_matrix[i & 1];
let c = *a;
let d = *b;

*a = c * m.re - d * m.im;
*b = c * m.im + d * m.re;
*a = c.mul_add(m.re, -d * m.im);
*b = c.mul_add(m.im, d * m.re);
});
});
}
Expand Down

0 comments on commit 3045708

Please sign in to comment.