super-dense.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //! # Super Dense Coding
  2. //!
  3. //! If Alice and Bob share a pair of entangled qubits, then Alice can encode two classical bits into her one entangled qubit,
  4. //! send it to Bob, and Bob can decode it with the help of his entangled qubit.
  5. extern crate failure;
  6. extern crate qcgpu;
  7. use failure::Error;
  8. use qcgpu::Simulator;
  9. fn superdense(input: &str) -> Result<u64, Error> {
  10. let mut state = Simulator::new_opencl(2)?;
  11. let input_str = String::from(input);
  12. // Prepare the bell state
  13. state.h(0)?;
  14. state.cx(0, 1)?;
  15. // Alice prepares her qubit
  16. let alice = 1;
  17. if input_str.get(0..1) == Some("1") {
  18. state.z(alice)?;
  19. }
  20. if input_str.get(1..2) == Some("1") {
  21. state.x(alice)?;
  22. }
  23. println!("\nState after Alice prepares her qubit: \n{}", state);
  24. // Alice sends her qubit to Bob
  25. let bob = 0;
  26. state.cx(alice, bob)?;
  27. state.h(alice)?;
  28. println!(
  29. "\nState after Bob receives Alice's qubit and 'decodes' it: \n{}",
  30. state
  31. );
  32. state.measure()
  33. }
  34. fn main() {
  35. use std::io;
  36. println!("Two bit string to send:");
  37. let mut input = String::new();
  38. match io::stdin().read_line(&mut input) {
  39. Ok(_n) => {
  40. let result = superdense(input.as_str()).unwrap();
  41. println!("\nDecoded string is: {}", result);
  42. }
  43. Err(error) => println!("error: {}", error),
  44. }
  45. }