From a68f6b4b2a86a7d1dbac3fab9879eed4a259e075 Mon Sep 17 00:00:00 2001 From: Kris Date: Sat, 9 Jul 2022 12:44:55 +0300 Subject: [PATCH 01/12] Examples PR --- qip/examples/cswap.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 qip/examples/cswap.rs diff --git a/qip/examples/cswap.rs b/qip/examples/cswap.rs new file mode 100644 index 0000000..539ee7a --- /dev/null +++ b/qip/examples/cswap.rs @@ -0,0 +1,31 @@ +use qip::prelude::*; +use std::num::NonZeroUsize; + + +fn main() -> Result<(), CircuitError> { + + let mut b = LocalBuilder::::default(); + let n = NonZeroUsize::new(3).unwrap(); + + let q = b.qubit(); + let ra = b.register(n); + let rb = b.register(n); + + let q = b.h(q); + + let mut cb = b.condition_with(q); + let (ra, rb) = cb.swap(ra, rb).unwrap(); + let q = cb.dissolve(); + + let q = b.h(q); + + let (q, m_handle) = b.measure(q); + + + let (_, measured) = b.calculate_state_with_init([(&ra, 0b000), (&rb, 0b001)]); + let (result, p) = measured.get_measurement(m_handle); + println!("Measured: {:?} (with chance {:?})", result, p); + + Ok(()) + +} \ No newline at end of file From 159fa4a7ff7997dca98f0f3d07e37b6990729606 Mon Sep 17 00:00:00 2001 From: Kris Date: Sun, 10 Jul 2022 21:53:15 +0300 Subject: [PATCH 02/12] Examples PR: Dense_coding --- qip/examples/dense_coding.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 qip/examples/dense_coding.rs diff --git a/qip/examples/dense_coding.rs b/qip/examples/dense_coding.rs new file mode 100644 index 0000000..41313ce --- /dev/null +++ b/qip/examples/dense_coding.rs @@ -0,0 +1,24 @@ +use qip::prelude::*; +use std::num::NonZeroUsize; + +fn run_alice(b: &mut LocalBuilder

, epr_alice: CB::Register , bit_a: bool, bit_b: bool) { + + match (bit_a, bit_b) { + (false, false) => epr_alice, + (false, true) => b.x(epr_alice), + (true, false) => b.z(epr_alice), + (true, true) => b.y(epr_alice), + + } + +} + + + + +fn main() -> Result<(), CircuitError> { + + let bits_a = vec![true, false, true, false]; + let bits_b = vec![true, true, false, false]; + +} \ No newline at end of file From fc55645d02d531764b2607b714072dd4f0ad7ea0 Mon Sep 17 00:00:00 2001 From: Kris Date: Tue, 12 Jul 2022 23:56:44 +0300 Subject: [PATCH 03/12] Examples PR: run_alice fix + run_bob --- qip/examples/dense_coding.rs | 48 +++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/qip/examples/dense_coding.rs b/qip/examples/dense_coding.rs index 41313ce..9a87602 100644 --- a/qip/examples/dense_coding.rs +++ b/qip/examples/dense_coding.rs @@ -1,7 +1,19 @@ use qip::prelude::*; use std::num::NonZeroUsize; +use qip::builder::Qudit; -fn run_alice(b: &mut LocalBuilder

, epr_alice: CB::Register , bit_a: bool, bit_b: bool) { +/// Encode the two classical bits Alice wants to communicate to Bob. +/// +/// Depending on the classical bits combination a different gate is applied: +/// 00: Do nothing (or apply Identity gate) +/// 01: Apply Pauli-X gate +/// 10: Apply Pauli-Z gate +/// 11: Apply Pauli-Y gate (or apply Pauli-Z gate followed by a Pauli-X gate) +/// +/// Returns Alice qubit with applied gate. +/// +/// https://en.wikipedia.org/wiki/Superdense_coding#Encoding +fn run_alice>(b: &mut CB, epr_alice: CB::Register , bit_a: bool, bit_b: bool) -> CB::Register { match (bit_a, bit_b) { (false, false) => epr_alice, @@ -14,6 +26,40 @@ fn run_alice(b: &mut LocalBuilder

, epr_alic } +/// Decode the message Alice transmitted to Bob. +/// +/// Bob applies the restoration operation on his qubit and the one transmitted +/// by Alice to decode the original message. After restoration: +/// |00>: 00 +/// |10>: 10 +/// |01>: 01 +/// |11>: 11 +/// +/// Returns a pair of classical bits. +/// +/// https://en.wikipedia.org/wiki/Superdense_coding#Decoding +fn run_bob(b: &mut LocalBuilder

, r_alice: Qudit, epr_bob: Qudit) -> (bool, bool) { + let (r_alice, r_bob) = b.cnot(r_alice, epr_bob).unwrap(); + let r_alice = b.h(r_alice); + let r = b.merge_two_registers(r_bob, r_alice); + let (r, m) = b.measure(r); + let (_, measurements) = b.calculate_state(); + let (m, _) = measurements.get_measurement(m); + ((m & 2) == 2, (m & 1) == 1) + +} + + +// let r = b.merge(vec![r_bob, r_alice]).unwrap(); +// let (r, m) = b.measure(r); + +// let (_, measurements) = run_local::(&r).unwrap(); +// let (m, _) = measurements.get_measurement(&m).unwrap(); + +// ((m & 2) == 2, (m & 1) == 1) + + + fn main() -> Result<(), CircuitError> { From e93e12c8947d29a99ba89191b9ea033ca636d889 Mon Sep 17 00:00:00 2001 From: Kris Date: Fri, 15 Jul 2022 10:04:11 +0300 Subject: [PATCH 04/12] Examples PR: main for dense_coding done --- qip/examples/dense_coding.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/qip/examples/dense_coding.rs b/qip/examples/dense_coding.rs index 9a87602..db18608 100644 --- a/qip/examples/dense_coding.rs +++ b/qip/examples/dense_coding.rs @@ -50,21 +50,25 @@ fn run_bob(b: &mut LocalBuilder

, r_alice: Qudit, epr_bob: Qudit } -// let r = b.merge(vec![r_bob, r_alice]).unwrap(); -// let (r, m) = b.measure(r); - -// let (_, measurements) = run_local::(&r).unwrap(); -// let (m, _) = measurements.get_measurement(&m).unwrap(); - -// ((m & 2) == 2, (m & 1) == 1) - +fn main() { + let n = NonZeroUsize::new(1).unwrap(); + let bits_a = vec![true, false, true, false]; + let bits_b = vec![true, true, false, false]; + for (bit_a, bit_b) in bits_a.into_iter().zip(bits_b.into_iter()) { + let mut b = LocalBuilder::::default(); + let epr_alice = b.register(n); + let epr_bob = b.register(n); + let r_alice = run_alice(&mut b, epr_alice, bit_a, bit_b); + let (bob_a, bob_b) = run_bob(&mut b, r_alice, epr_bob); -fn main() -> Result<(), CircuitError> { + println!( + "Alice: ({:?},{:?}) \tBob: ({:?}, {:?})", + bit_a, bit_b, bob_a, bob_b + ); - let bits_a = vec![true, false, true, false]; - let bits_b = vec![true, true, false, false]; + } } \ No newline at end of file From f0473b66f5d15d2180ab97cb21b300d2335de9d8 Mon Sep 17 00:00:00 2001 From: Kris Date: Fri, 15 Jul 2022 10:36:10 +0300 Subject: [PATCH 05/12] Examples PR: Start bell_inequalities --- qip/examples/bell_inequalities.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 qip/examples/bell_inequalities.rs diff --git a/qip/examples/bell_inequalities.rs b/qip/examples/bell_inequalities.rs new file mode 100644 index 0000000..b121beb --- /dev/null +++ b/qip/examples/bell_inequalities.rs @@ -0,0 +1,12 @@ +use qip::prelude::*; +use std::num::NonZeroUsize; + +fn circuit1() -> Vec { + + let mut b = LocalBuilder::::default(); + let n = NonZeroUsize::new(2).unwrap(); + + let q = b.qubit(); + let ra = b.register(n); + let r = program!() //Not sure what to use here +} \ No newline at end of file From cd008c596c7f3cccf832176ddce160c01e1b50a1 Mon Sep 17 00:00:00 2001 From: Kris Date: Sat, 16 Jul 2022 01:04:06 +0300 Subject: [PATCH 06/12] Examples PR --- qip/examples/bell_inequalities.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/qip/examples/bell_inequalities.rs b/qip/examples/bell_inequalities.rs index b121beb..2cead5c 100644 --- a/qip/examples/bell_inequalities.rs +++ b/qip/examples/bell_inequalities.rs @@ -1,12 +1,32 @@ use qip::prelude::*; +#[cfg(feature = "macros")] +use qip_macros::*; use std::num::NonZeroUsize; +#[cfg(feature = "macros")] fn circuit1() -> Vec { - let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); let q = b.qubit(); let ra = b.register(n); - let r = program!() //Not sure what to use here -} \ No newline at end of file + let r = program!(&mut b, r; + not r; + h r[0]; + control not r[0], r[1]; + rz(std::f64::consts::FRAC_PI_3) r[1]; + h r; + ) + .unwrap(); + let (r, m_handle) = b.stochastic_measure(r); + + //run and get probabilities + let (_, mut measured) = run_local::(&r).unwrap(); + measured.pop_stochastic_measurements(m_handle).unwrap() +} + + + +#[cfg(not(feature = "macros"))] +fn main() {} + From dec9ce6e2947a0fa433ea1cd07fcc3ae0d2cb047 Mon Sep 17 00:00:00 2001 From: Kris Date: Sat, 16 Jul 2022 18:03:26 +0300 Subject: [PATCH 07/12] Examples PR: bell_inequalities + formatting --- qip/examples/bell_inequalities.rs | 76 ++++++++++++++++++++++++++++++- qip/examples/cswap.rs | 6 +-- qip/examples/dense_coding.rs | 22 ++++----- 3 files changed, 85 insertions(+), 19 deletions(-) diff --git a/qip/examples/bell_inequalities.rs b/qip/examples/bell_inequalities.rs index 2cead5c..d3fc9c9 100644 --- a/qip/examples/bell_inequalities.rs +++ b/qip/examples/bell_inequalities.rs @@ -1,6 +1,8 @@ +#[cfg(feature = "macros")] use qip::prelude::*; #[cfg(feature = "macros")] use qip_macros::*; +#[cfg(feature = "macros")] use std::num::NonZeroUsize; #[cfg(feature = "macros")] @@ -25,8 +27,80 @@ fn circuit1() -> Vec { measured.pop_stochastic_measurements(m_handle).unwrap() } +#[cfg(feature = "macros")] +fn circuit2() -> Vec { + let mut b = LocalBuilder::::default(); + let n = NonZeroUsize::new(2).unwrap(); + + let r = b.register(n); + let r = program!( &mut b, r; + not r; + h r[0]; + control not r[0], r[1]; + rz(2. * std::f64::consts::FRAC_PI_3) r[1]; + h r; + ) + .unwrap(); + let (r, m_handle) = b.stochastic_measure(r); + + // run and get probabilities + let (_, mut measured) = run_local::(&r).unwrap(); + measured.pop_stochastic_measurements(m_handle).unwrap() +} +#[cfg(feature = "macros")] +fn circuit3() -> Vec { + let mut b = LocalBuilder::::default(); + let n = NonZeroUsize::new(2).unwrap(); + let r = b.register(n); + + let r = program!(&mut b, r; + not r; + h r[0]; + control not r[0], r[1]; + rz(std::f64::consts::FRAC_PI_3) r[0]; + rz(2. * std::f64::consts::FRAC_PI_3) r[1]; + h r; + ) + .unwrap(); + let (r, m_handle) = b.stochastic_measure(r); + // run and get probabilities + let (_, mut measured) = run_local::(&r).unwrap(); + measured.pop_stochastic_measurements(m_handle).unwrap() +} #[cfg(not(feature = "macros"))] -fn main() {} +fn main() -> () {} + +/// Bell inequality: +/// |P(a, b) - P(a, c)| - P(b, c) <= 1 +/// To get P(a, b), P(a, c) and P(b, c) we use the 3 circuits presented in: +/// https://arxiv.org/pdf/1712.05642.pdf - II.IMPLEMENTED EXPERIMENTS -> 3. Bell's inequality +/// +/// A violation of the inequality is expected in any quantum computer or simulator. +#[cfg(feature = "macros")] +fn main() -> Result<(), CircuitError> { + println!("Bell inequality: |P(a, b) - P(a, c)| - P(b, c) <= 1"); + + let a_b = circuit1(); + let p_of_a_b = (a_b[0] + a_b[3]) - (a_b[1] + a_b[2]); + println!("P(a, b) = {:.2}", p_of_a_b); + let a_c = circuit2(); + let p_of_a_c = (a_c[0] + a_c[3]) - (a_c[1] + a_c[2]); + println!("P(a, c) = {:.2}", p_of_a_c); + + let b_c = circuit3(); + let p_of_b_c = (b_c[0] + b_c[3]) - (b_c[1] + b_c[2]); + println!("P(b, c) = {:.2}", p_of_b_c); + + let left_side = num::abs(p_of_a_b - p_of_a_c) - p_of_b_c; + println!( + "|{:.2} - {:.2}| - ({:.2}) = {:.2} IS NOT <= 1", + p_of_a_b, p_of_a_c, p_of_b_c, left_side + ); + + assert!(left_side > 1.0); + + Ok(()) +} diff --git a/qip/examples/cswap.rs b/qip/examples/cswap.rs index 539ee7a..d15e273 100644 --- a/qip/examples/cswap.rs +++ b/qip/examples/cswap.rs @@ -1,9 +1,7 @@ use qip::prelude::*; use std::num::NonZeroUsize; - fn main() -> Result<(), CircuitError> { - let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(3).unwrap(); @@ -21,11 +19,9 @@ fn main() -> Result<(), CircuitError> { let (q, m_handle) = b.measure(q); - let (_, measured) = b.calculate_state_with_init([(&ra, 0b000), (&rb, 0b001)]); let (result, p) = measured.get_measurement(m_handle); println!("Measured: {:?} (with chance {:?})", result, p); Ok(()) - -} \ No newline at end of file +} diff --git a/qip/examples/dense_coding.rs b/qip/examples/dense_coding.rs index db18608..d69bc5e 100644 --- a/qip/examples/dense_coding.rs +++ b/qip/examples/dense_coding.rs @@ -1,6 +1,6 @@ +use qip::builder::Qudit; use qip::prelude::*; use std::num::NonZeroUsize; -use qip::builder::Qudit; /// Encode the two classical bits Alice wants to communicate to Bob. /// @@ -13,19 +13,20 @@ use qip::builder::Qudit; /// Returns Alice qubit with applied gate. /// /// https://en.wikipedia.org/wiki/Superdense_coding#Encoding -fn run_alice>(b: &mut CB, epr_alice: CB::Register , bit_a: bool, bit_b: bool) -> CB::Register { - +fn run_alice>( + b: &mut CB, + epr_alice: CB::Register, + bit_a: bool, + bit_b: bool, +) -> CB::Register { match (bit_a, bit_b) { (false, false) => epr_alice, (false, true) => b.x(epr_alice), (true, false) => b.z(epr_alice), (true, true) => b.y(epr_alice), - } - } - /// Decode the message Alice transmitted to Bob. /// /// Bob applies the restoration operation on his qubit and the one transmitted @@ -45,13 +46,10 @@ fn run_bob(b: &mut LocalBuilder

, r_alice: Qudit, epr_bob: Qudit let (r, m) = b.measure(r); let (_, measurements) = b.calculate_state(); let (m, _) = measurements.get_measurement(m); - ((m & 2) == 2, (m & 1) == 1) - + ((m & 2) == 2, (m & 1) == 1) } - fn main() { - let n = NonZeroUsize::new(1).unwrap(); let bits_a = vec![true, false, true, false]; let bits_b = vec![true, true, false, false]; @@ -68,7 +66,5 @@ fn main() { "Alice: ({:?},{:?}) \tBob: ({:?}, {:?})", bit_a, bit_b, bob_a, bob_b ); - } - -} \ No newline at end of file +} From 2eb3778f44e7a7bc455f469e0e754e1357d76958 Mon Sep 17 00:00:00 2001 From: Kris Date: Sat, 30 Jul 2022 23:34:27 +0300 Subject: [PATCH 08/12] some_bell_fixes --- qip/examples/bell_inequalities.rs | 42 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/qip/examples/bell_inequalities.rs b/qip/examples/bell_inequalities.rs index d3fc9c9..c5fb60e 100644 --- a/qip/examples/bell_inequalities.rs +++ b/qip/examples/bell_inequalities.rs @@ -1,68 +1,66 @@ #[cfg(feature = "macros")] use qip::prelude::*; +use qip::prelude::{CircuitBuilder, CircuitError}; #[cfg(feature = "macros")] use qip_macros::*; #[cfg(feature = "macros")] use std::num::NonZeroUsize; #[cfg(feature = "macros")] -fn circuit1() -> Vec { +fn circuit1() -> Result, CircuitError>{ let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); let q = b.qubit(); let ra = b.register(n); - let r = program!(&mut b, r; + let r = program!(&mut b; r; not r; h r[0]; control not r[0], r[1]; rz(std::f64::consts::FRAC_PI_3) r[1]; h r; - ) - .unwrap(); - let (r, m_handle) = b.stochastic_measure(r); + )?; + let (r, m_handle) = b.measure_stochastic(r); //run and get probabilities let (_, mut measured) = run_local::(&r).unwrap(); measured.pop_stochastic_measurements(m_handle).unwrap() } -#[cfg(feature = "macros")] -fn circuit2() -> Vec { +// #[cfg(feature = "macros")] +fn circuit2() -> Result, CircuitError>{ let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); let r = b.register(n); - let r = program!( &mut b, r; + let r = program!( &mut b; r; not r; h r[0]; control not r[0], r[1]; rz(2. * std::f64::consts::FRAC_PI_3) r[1]; h r; - ) - .unwrap(); - let (r, m_handle) = b.stochastic_measure(r); + )?; + let (r, m_handle) = b.measure_stochastic(r); // run and get probabilities let (_, mut measured) = run_local::(&r).unwrap(); measured.pop_stochastic_measurements(m_handle).unwrap() } -#[cfg(feature = "macros")] -fn circuit3() -> Vec { +// #[cfg(feature = "macros")] +fn circuit3() -> Result, CircuitError> { let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); let r = b.register(n); - let r = program!(&mut b, r; + let r = program!(&mut b; r; not r; h r[0]; control not r[0], r[1]; rz(std::f64::consts::FRAC_PI_3) r[0]; rz(2. * std::f64::consts::FRAC_PI_3) r[1]; h r; - ) - .unwrap(); - let (r, m_handle) = b.stochastic_measure(r); + )?; + let (r, m_handle) = b.measure_stochastic(r); // run and get probabilities let (_, mut measured) = run_local::(&r).unwrap(); @@ -78,23 +76,23 @@ fn main() -> () {} /// https://arxiv.org/pdf/1712.05642.pdf - II.IMPLEMENTED EXPERIMENTS -> 3. Bell's inequality /// /// A violation of the inequality is expected in any quantum computer or simulator. -#[cfg(feature = "macros")] +// #[cfg(feature = "macros")] fn main() -> Result<(), CircuitError> { println!("Bell inequality: |P(a, b) - P(a, c)| - P(b, c) <= 1"); let a_b = circuit1(); - let p_of_a_b = (a_b[0] + a_b[3]) - (a_b[1] + a_b[2]); + let p_of_a_b = (a_b?[0] + a_b?[3]) - (a_b?[1] + a_b?[2]); println!("P(a, b) = {:.2}", p_of_a_b); let a_c = circuit2(); - let p_of_a_c = (a_c[0] + a_c[3]) - (a_c[1] + a_c[2]); + let p_of_a_c = (a_c?[0] + a_c?[3]) - (a_c?[1] + a_c?[2]); println!("P(a, c) = {:.2}", p_of_a_c); let b_c = circuit3(); - let p_of_b_c = (b_c[0] + b_c[3]) - (b_c[1] + b_c[2]); + let p_of_b_c = (b_c?[0] + b_c?[3]) - (b_c?[1] + b_c?[2]); println!("P(b, c) = {:.2}", p_of_b_c); - let left_side = num::abs(p_of_a_b - p_of_a_c) - p_of_b_c; + let left_side = (p_of_a_b - p_of_a_c).abs() - p_of_b_c; println!( "|{:.2} - {:.2}| - ({:.2}) = {:.2} IS NOT <= 1", p_of_a_b, p_of_a_c, p_of_b_c, left_side From 0f89ef2d798d24b7372778c24e94b2897d8bcf95 Mon Sep 17 00:00:00 2001 From: Kris Date: Sat, 6 Aug 2022 19:08:18 +0300 Subject: [PATCH 09/12] fix circuit3 --- qip/examples/bell_inequalities.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/qip/examples/bell_inequalities.rs b/qip/examples/bell_inequalities.rs index c5fb60e..5399f04 100644 --- a/qip/examples/bell_inequalities.rs +++ b/qip/examples/bell_inequalities.rs @@ -1,9 +1,11 @@ #[cfg(feature = "macros")] use qip::prelude::*; +#[cfg(feature = "macros")] use qip::prelude::{CircuitBuilder, CircuitError}; #[cfg(feature = "macros")] use qip_macros::*; #[cfg(feature = "macros")] +use qip::macros::program_ops::*; use std::num::NonZeroUsize; #[cfg(feature = "macros")] @@ -27,7 +29,7 @@ fn circuit1() -> Result, CircuitError>{ measured.pop_stochastic_measurements(m_handle).unwrap() } -// #[cfg(feature = "macros")] +#[cfg(feature = "macros")] fn circuit2() -> Result, CircuitError>{ let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); @@ -59,12 +61,10 @@ fn circuit3() -> Result, CircuitError> { rz(std::f64::consts::FRAC_PI_3) r[0]; rz(2. * std::f64::consts::FRAC_PI_3) r[1]; h r; - )?; - let (r, m_handle) = b.measure_stochastic(r); - - // run and get probabilities - let (_, mut measured) = run_local::(&r).unwrap(); - measured.pop_stochastic_measurements(m_handle).unwrap() + ).unwrap(); + let (r, m_handle) = b.measure_stochastic(r); //returns (Self::Register, Self::StochasticMeasurementHandle) + let (_, measurements) = b.calculate_state(); + let (m, _) = measurements.get_measurement(m_handle); } #[cfg(not(feature = "macros"))] @@ -76,7 +76,7 @@ fn main() -> () {} /// https://arxiv.org/pdf/1712.05642.pdf - II.IMPLEMENTED EXPERIMENTS -> 3. Bell's inequality /// /// A violation of the inequality is expected in any quantum computer or simulator. -// #[cfg(feature = "macros")] +#[cfg(feature = "macros")] fn main() -> Result<(), CircuitError> { println!("Bell inequality: |P(a, b) - P(a, c)| - P(b, c) <= 1"); From dbdff4ab0c8411ac5f02f046303e13fa8871b5e8 Mon Sep 17 00:00:00 2001 From: Kris Date: Sat, 6 Aug 2022 19:16:48 +0300 Subject: [PATCH 10/12] get_stochastic_measurement --- qip/examples/bell_inequalities.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qip/examples/bell_inequalities.rs b/qip/examples/bell_inequalities.rs index 5399f04..9c81c3c 100644 --- a/qip/examples/bell_inequalities.rs +++ b/qip/examples/bell_inequalities.rs @@ -64,7 +64,7 @@ fn circuit3() -> Result, CircuitError> { ).unwrap(); let (r, m_handle) = b.measure_stochastic(r); //returns (Self::Register, Self::StochasticMeasurementHandle) let (_, measurements) = b.calculate_state(); - let (m, _) = measurements.get_measurement(m_handle); + let (m, _) = measurements.get_stochastic_measurement(m_handle); } #[cfg(not(feature = "macros"))] From 3df9d807e6d0520c171fec0f227e533bfeb4f674 Mon Sep 17 00:00:00 2001 From: Kris Date: Sat, 6 Aug 2022 19:19:54 +0300 Subject: [PATCH 11/12] stochastic_measurement_probability --- qip/examples/bell_inequalities.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/qip/examples/bell_inequalities.rs b/qip/examples/bell_inequalities.rs index 9c81c3c..e1d06ca 100644 --- a/qip/examples/bell_inequalities.rs +++ b/qip/examples/bell_inequalities.rs @@ -1,3 +1,4 @@ +use qip::builder::StochasticMeasurementHandle; #[cfg(feature = "macros")] use qip::prelude::*; #[cfg(feature = "macros")] @@ -49,7 +50,7 @@ fn circuit2() -> Result, CircuitError>{ measured.pop_stochastic_measurements(m_handle).unwrap() } // #[cfg(feature = "macros")] -fn circuit3() -> Result, CircuitError> { +fn circuit3() { let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); let r = b.register(n); @@ -64,7 +65,7 @@ fn circuit3() -> Result, CircuitError> { ).unwrap(); let (r, m_handle) = b.measure_stochastic(r); //returns (Self::Register, Self::StochasticMeasurementHandle) let (_, measurements) = b.calculate_state(); - let (m, _) = measurements.get_stochastic_measurement(m_handle); + let stochastic_measurement_probability = measurements.get_stochastic_measurement(m_handle); } #[cfg(not(feature = "macros"))] From 06d234c3cbea4c25ed64609c07e61a31eeaffa22 Mon Sep 17 00:00:00 2001 From: Kris Date: Thu, 18 Aug 2022 22:35:20 +0300 Subject: [PATCH 12/12] fix the other circuits --- qip/examples/bell_inequalities.rs | 55 ++++++++++++++++--------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/qip/examples/bell_inequalities.rs b/qip/examples/bell_inequalities.rs index e1d06ca..ece8841 100644 --- a/qip/examples/bell_inequalities.rs +++ b/qip/examples/bell_inequalities.rs @@ -10,52 +10,56 @@ use qip::macros::program_ops::*; use std::num::NonZeroUsize; #[cfg(feature = "macros")] -fn circuit1() -> Result, CircuitError>{ +fn circuit1() -> &'static [f64]{ let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); let q = b.qubit(); - let ra = b.register(n); - let r = program!(&mut b; r; + let r1 = b.register(n); + let r = program!(&mut b; r1; not r; h r[0]; control not r[0], r[1]; rz(std::f64::consts::FRAC_PI_3) r[1]; h r; - )?; + ).unwrap(); let (r, m_handle) = b.measure_stochastic(r); //run and get probabilities - let (_, mut measured) = run_local::(&r).unwrap(); - measured.pop_stochastic_measurements(m_handle).unwrap() + let (r, m_handle) = b.measure_stochastic(r); //returns (Self::Register, Self::StochasticMeasurementHandle) + let (_, measurements) = b.calculate_state(); + let stochastic_measurement_probability = measurements.get_stochastic_measurement(m_handle); + return stochastic_measurement_probability; } #[cfg(feature = "macros")] -fn circuit2() -> Result, CircuitError>{ +fn circuit2() -> &'static [f64]{ let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); - let r = b.register(n); - let r = program!( &mut b; r; + let r2 = b.register(n); + let r = program!( &mut b; r2; not r; h r[0]; control not r[0], r[1]; rz(2. * std::f64::consts::FRAC_PI_3) r[1]; h r; - )?; + ).unwrap(); let (r, m_handle) = b.measure_stochastic(r); // run and get probabilities - let (_, mut measured) = run_local::(&r).unwrap(); - measured.pop_stochastic_measurements(m_handle).unwrap() + let (r, m_handle) = b.measure_stochastic(r); //returns (Self::Register, Self::StochasticMeasurementHandle) + let (_, measurements) = b.calculate_state(); + let stochastic_measurement_probability = measurements.get_stochastic_measurement(m_handle); + return stochastic_measurement_probability; } -// #[cfg(feature = "macros")] -fn circuit3() { +#[cfg(feature = "macros")] +fn circuit3() -> &'static [f64] { let mut b = LocalBuilder::::default(); let n = NonZeroUsize::new(2).unwrap(); - let r = b.register(n); + let r3 = b.register(n); - let r = program!(&mut b; r; + let r = program!(&mut b; r3; not r; h r[0]; control not r[0], r[1]; @@ -66,31 +70,30 @@ fn circuit3() { let (r, m_handle) = b.measure_stochastic(r); //returns (Self::Register, Self::StochasticMeasurementHandle) let (_, measurements) = b.calculate_state(); let stochastic_measurement_probability = measurements.get_stochastic_measurement(m_handle); + return stochastic_measurement_probability; + } #[cfg(not(feature = "macros"))] fn main() -> () {} -/// Bell inequality: -/// |P(a, b) - P(a, c)| - P(b, c) <= 1 -/// To get P(a, b), P(a, c) and P(b, c) we use the 3 circuits presented in: -/// https://arxiv.org/pdf/1712.05642.pdf - II.IMPLEMENTED EXPERIMENTS -> 3. Bell's inequality -/// -/// A violation of the inequality is expected in any quantum computer or simulator. + + #[cfg(feature = "macros")] fn main() -> Result<(), CircuitError> { println!("Bell inequality: |P(a, b) - P(a, c)| - P(b, c) <= 1"); let a_b = circuit1(); - let p_of_a_b = (a_b?[0] + a_b?[3]) - (a_b?[1] + a_b?[2]); + let p_of_a_b = (a_b[0] + a_b[3]) - (a_b[1] + a_b[2]); println!("P(a, b) = {:.2}", p_of_a_b); let a_c = circuit2(); - let p_of_a_c = (a_c?[0] + a_c?[3]) - (a_c?[1] + a_c?[2]); + let p_of_a_c = (a_c[0] + a_c[3]) - (a_c[1] + a_c[2]); println!("P(a, c) = {:.2}", p_of_a_c); - + let b_c = circuit3(); - let p_of_b_c = (b_c?[0] + b_c?[3]) - (b_c?[1] + b_c?[2]); + println!("{:?}", b_c); + let p_of_b_c = (b_c[0] + b_c[3]) - (b_c[1] + b_c[2]); println!("P(b, c) = {:.2}", p_of_b_c); let left_side = (p_of_a_b - p_of_a_c).abs() - p_of_b_c;