-
Notifications
You must be signed in to change notification settings - Fork 18
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe related #25 ?
There was a problem hiding this comment.
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.