Skip to content

Commit

Permalink
Add numeric check for operators
Browse files Browse the repository at this point in the history
  • Loading branch information
MucTepDayH16 committed May 30, 2021
1 parent aba8220 commit 64d62ba
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qvnt"
version = "0.1.1"
version = "0.1.2"
authors = ["Denis Drozhzhin <[email protected]>"]
edition = "2018"
repository = "https://github.com/MucTepDayH16/qvnt/"
Expand Down
13 changes: 9 additions & 4 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ macro_rules! rotate_operator_definition {
let mut ops = Op::id();

if ang != ANGLE_TABLE[0] {
let mut real_mask = 0;
for mask in crate::bits_iter::BitsIter::from($mask) {
ops *= Operator {
name: format!("{}{}({})", $name, mask, $phase),
control: Arc::new(0),
func: Box::new(move |psi, idx| $operation(psi, idx, mask, ang))
real_mask |= mask;
if count_bits(real_mask) == $dim {
ops *= Operator {
name: format!("{}{}({})", $name, real_mask, $phase),
control: Arc::new(0),
func: Box::new(move |psi, idx| $operation(psi, idx, real_mask, ang))
};
real_mask = 0;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const MIN_QREG_LEN: usize = 8;

#[derive(Default, Clone)]
pub struct QReg {
#[cfg(test)]
pub(crate) psi: Vec<C>,
#[cfg(not(test))]
psi: Vec<C>,
q_num: N,
q_mask: N,
Expand Down
162 changes: 85 additions & 77 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,91 +161,99 @@ fn operator_from_matrix() {
println!("{:?}", Op::if_b_then_u1_else_u0(matrix, matrix, 0b0001, 0b1000))
}

#[test]
fn rotate_operators() {
const PHASE: R = 1.1;
fn get_matrix_2x2(ops: &Op) -> [[C; 2]; 2] {
let mut matrix = [[C::from(0.); 2]; 2];

println!("{:?}", (PHASE * 0.5).sin_cos());
for b in 0..2 {
let mut reg = QReg::new(1).init_state(b);
reg.apply(ops);
matrix[0][b] = reg.psi[0];
matrix[1][b] = reg.psi[1];
}

// rx
let op = Op::rx(PHASE, 0b1);
matrix
}

let mut reg0 = QReg::new(1).init_state(0b0);
reg0.apply(&op);
let mut reg1 = QReg::new(1).init_state(0b1);
reg1.apply(&op);
println!("{:?}\n{:?}\n", reg0, reg1);
fn get_matrix_4x4(ops: &Op) -> [[C; 4]; 4] {
let mut matrix = [[C::from(0.); 4]; 4];

// ry
let op = Op::ry(PHASE, 0b1);
for b in 0..4 {
let mut reg = QReg::new(2).init_state(b);
reg.apply(ops);
matrix[0][b] = reg.psi[0];
matrix[1][b] = reg.psi[1];
matrix[2][b] = reg.psi[2];
matrix[3][b] = reg.psi[3];
}

let mut reg0 = QReg::new(1).init_state(0b0);
reg0.apply(&op);
let mut reg1 = QReg::new(1).init_state(0b1);
reg1.apply(&op);
println!("{:?}\n{:?}\n", reg0, reg1);
matrix
}

#[test]
fn simple_operators() {
use num::{One, Zero};
const _1: C = C{ re: 1.0, im: 0.0 };
const _0: C = C{ re: 0.0, im: 0.0 };
const _i: C = C{ re: 0.0, im: 1.0 };

//x
assert_eq!(
get_matrix_2x2(&Op::x(0b1)),
[ [_0, _1],
[_1, _0]]
);
//y
assert_eq!(
get_matrix_2x2(&Op::y(0b1)),
[ [_0, -_i],
[_i, _0]]
);
//z
assert_eq!(
get_matrix_2x2(&Op::z(0b1)),
[ [_1, _0],
[_0, -_1]]
);
//s
assert_eq!(
get_matrix_2x2(&Op::s(0b1)),
[ [_1, _0],
[_0, _i]]
);
//t
assert_eq!(
get_matrix_2x2(&Op::t(0b1)),
[ [_1, _0],
[_0, C::new(SQRT_2 * 0.5, SQRT_2 * 0.5)]]
);

// rz
let op = Op::rz(PHASE, 0b1);
//swap
println!("{:?}", get_matrix_4x4(&Op::swap(0b11)));
//i_swap
println!("{:?}", get_matrix_4x4(&Op::i_swap(0b11)));
//sqrt_swap
println!("{:?}", get_matrix_4x4(&Op::sqrt_swap(0b11)));
//sqrt_i_swap
println!("{:?}", get_matrix_4x4(&Op::sqrt_i_swap(0b11)));
}

let mut reg0 = QReg::new(1).init_state(0b0);
reg0.apply(&op);
let mut reg1 = QReg::new(1).init_state(0b1);
reg1.apply(&op);
println!("{:?}\n{:?}\n", reg0, reg1);
#[test]
fn rotate_operators() {
const PHASE: R = 1.1;

// rxx
let op = Op::rxx(PHASE, 0b11);
println!("{:?}", (PHASE * 0.5).sin_cos());

let mut reg0 = QReg::new(2).init_state(0b00);
reg0.apply(&op);
let mut reg1 = QReg::new(2).init_state(0b01);
reg1.apply(&op);
let mut reg2 = QReg::new(2).init_state(0b10);
reg2.apply(&op);
let mut reg3 = QReg::new(2).init_state(0b11);
reg3.apply(&op);
println!("{:?}\n{:?}\n{:?}\n{:?}\n", reg0, reg1, reg2, reg3);

// ryy
let op = Op::ryy(PHASE, 0b11);

let mut reg0 = QReg::new(2).init_state(0b00);
reg0.apply(&op);
let mut reg1 = QReg::new(2).init_state(0b01);
reg1.apply(&op);
let mut reg2 = QReg::new(2).init_state(0b10);
reg2.apply(&op);
let mut reg3 = QReg::new(2).init_state(0b11);
reg3.apply(&op);
println!("{:?}\n{:?}\n{:?}\n{:?}\n", reg0, reg1, reg2, reg3);

// rzz
let op = Op::rzz(PHASE, 0b11);

let mut reg0 = QReg::new(2).init_state(0b00);
reg0.apply(&op);
let mut reg1 = QReg::new(2).init_state(0b01);
reg1.apply(&op);
let mut reg2 = QReg::new(2).init_state(0b10);
reg2.apply(&op);
let mut reg3 = QReg::new(2).init_state(0b11);
reg3.apply(&op);
println!("{:?}\n{:?}\n{:?}\n{:?}\n", reg0, reg1, reg2, reg3);

// swap
let op =
Op::sqrt_swap(0b11)
* Op::sqrt_swap(0b11);

let mut reg0 = QReg::new(2).init_state(0b00);
reg0.apply(&op);
let mut reg1 = QReg::new(2).init_state(0b01);
reg1.apply(&op);
let mut reg2 = QReg::new(2).init_state(0b10);
reg2.apply(&op);
let mut reg3 = QReg::new(2).init_state(0b11);
reg3.apply(&op);
println!("{:?}\n{:?}\n{:?}\n{:?}\n", reg0, reg1, reg2, reg3);
//rx
println!("{:?}", get_matrix_2x2(&Op::rx(PHASE, 0b1)));
//ry
println!("{:?}", get_matrix_2x2(&Op::ry(PHASE, 0b1)));
//rz
println!("{:?}", get_matrix_2x2(&Op::rz(PHASE, 0b1)));

//rxx
println!("{:?}", get_matrix_4x4(&Op::rxx(PHASE, 0b11)));
//ryy
println!("{:?}", get_matrix_4x4(&Op::ryy(PHASE, 0b11)));
//rzz
println!("{:?}", get_matrix_4x4(&Op::rzz(PHASE, 0b11)));
}

0 comments on commit 64d62ba

Please sign in to comment.