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

Add some basic enanglement tests #26

Closed
wants to merge 1 commit into from
Closed
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
148 changes: 148 additions & 0 deletions tests/entanglement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
extern crate num;
extern crate qip;

mod utils;

use qip::{state_ops::from_reals, *};

// Classic and quantum representation of two-qubit initial states.
struct InitialState {
first_bit: u8,
first_qubit: Vec<Complex<f64>>,
second_bit: u8,
second_qubit: Vec<Complex<f64>>,
}

// Creates the four computational basis states for two-qubit.
fn initial_states() -> Vec<InitialState> {
let basic_state_zero = from_reals(&[1.0, 0.0]);
let basic_state_one = from_reals(&[0.0, 1.0]);

vec![
// |00>
InitialState {
first_bit: 0,
first_qubit: basic_state_zero.clone(),
second_bit: 0,
second_qubit: basic_state_zero.clone(),
},
// |01>
InitialState {
first_bit: 0,
first_qubit: basic_state_zero.clone(),
second_bit: 1,
second_qubit: basic_state_one.clone(),
},
// |10>
InitialState {
first_bit: 1,
first_qubit: basic_state_one.clone(),
second_bit: 0,
second_qubit: basic_state_zero.clone(),
},
// |11>
InitialState {
first_bit: 1,
first_qubit: basic_state_one.clone(),
second_bit: 1,
second_qubit: basic_state_one.clone(),
},
]
}

#[test]
fn create_entanglement() -> Result<(), CircuitError> {
let basis_inputs = initial_states();

for input in basis_inputs {
let mut b = OpBuilder::new();
let q1 = b.qubit();
let q2 = b.qubit();

let h1 = q1.handle();
let h2 = q2.handle();

let initial_state = [
h1.make_init_from_state(vec![input.first_qubit[0], input.first_qubit[1]])
.unwrap(),
h2.make_init_from_state(vec![input.second_qubit[0], input.second_qubit[1]])
.unwrap(),
];

// entangle q1 and q2
let q1 = b.hadamard(q1);
let (q1, q2) = b.cnot(q1, q2);

// merge
let q = b.merge(vec![q1, q2]).unwrap();

// measure the merged qubit
let (q, m) = b.measure(q);

// run and get measurment
let (_, measurements) = run_local_with_init::<f64>(&q, &initial_state).ok().unwrap();
let (m, l) = measurements.get_measurement(&m).unwrap();

// We can't test the measure result as the qubit is in an entangled state but
// we know the likelihood of getting one of two possible states is 50-50.
assert!(m == 0 || m <= 3);
utils::assert_almost_eq(l, 0.5, 10);
// TODO: Can we do something better here ?
}

Ok(())
}

#[test]
fn measure_entanglement() -> Result<(), CircuitError> {
let basis_inputs = initial_states();

for input in basis_inputs {
let mut b = OpBuilder::new();
let q1 = b.qubit();
let q2 = b.qubit();

let h1 = q1.handle();
let h2 = q2.handle();

let initial_state = [
h1.make_init_from_state(vec![input.first_qubit[0], input.first_qubit[1]])
.unwrap(),
h2.make_init_from_state(vec![input.second_qubit[0], input.second_qubit[1]])
.unwrap(),
];

// entangle q1 and q2
let q1 = b.hadamard(q1);
let (q1, q2) = b.cnot(q1, q2);

// apply the reverse and merge
let (q1, q2) = b.cnot(q1, q2);
let q1 = b.hadamard(q1);
let q = b.merge(vec![q1, q2]).unwrap();

// measure the merged qubit
let (q, m) = b.measure(q);

// run and get measurment
let (_, measurements) = run_local_with_init::<f64>(&q, &initial_state).ok().unwrap();
let (m, l) = measurements.get_measurement(&m).unwrap();

// likelihood is always 1.0
utils::assert_almost_eq(l, 1.0, 10);

// depending on the measurment result we can know with no ambiguity the initial state.
let binary_m = format!("{:02b}", m);
assert_eq!(
binary_m.chars().nth(0).unwrap().to_digit(2).unwrap(),
input.second_bit as u32
);
assert_eq!(
binary_m.chars().nth(1).unwrap().to_digit(2).unwrap(),
input.first_bit as u32
);
// TODO: Positions are reversed, check if there is some sort of endianess happening.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe related #25 ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right that the endianness issue is related to #25 - and the whole thing comes from the original iterators being defined to match how numpy handles outer (/tensor) products. Rewriting the internals may be an undertaking so I'm considering whether it's worthwhile.

}

Ok(())
}