Adam Kelly 6 years ago
parent
commit
95571b5f91
4 changed files with 129 additions and 1 deletions
  1. 11 1
      Cargo.toml
  2. 6 0
      examples/bell-state.rs
  3. 58 0
      examples/bernstein-vazirani.rs
  4. 54 0
      examples/super-dense.rs

+ 11 - 1
Cargo.toml

@@ -11,4 +11,14 @@ rand = "0.5.1"
 [[example]]
 name = "bell"
 doc = false
-path = "examples/bell-state.rs"
+path = "examples/bell-state.rs"
+
+[[example]]
+name = "super-dense"
+doc = false
+path = "examples/super-dense.rs"
+
+[[example]]
+name="bernstein-vazirani"
+doc = false
+path = "examples/bernstein-vazirani.rs"

+ 6 - 0
examples/bell-state.rs

@@ -1,3 +1,9 @@
+//! # Bell State / EPR Pair
+//!
+//! The Bell State, also known as the EPR pair (after Einstein, Podosky and Rosen)
+//! is the simplest example of entanglement.
+//!
+//! The Bell State is defined as the maximally entangled quantum state of two qubits.
 extern crate qcgpu;
 
 use qcgpu::Simulator;

+ 58 - 0
examples/bernstein-vazirani.rs

@@ -0,0 +1,58 @@
+//! # Bernstein-Vazirani Algorithm
+//!
+//! This algorithm finds a hidden integer $a \in \{ 0, 1\}^n$ from
+//! an oracle $f_a$ which returns a bit $a \cdot x \equiv \sum_i a_i x_i \mod 2$
+//! for an input $x \in \{0,1\}^n$.
+//!
+//! A classical oracle returns $f_a(x) = a \dot x \mod 2$, while the quantum oracle
+//! must be queried with superpositions of input $x$'s.
+//!
+//! To solve this problem classically, the hidden integer can be found by checking the
+//! oracle with the inputs $x = 1,2,/dots,2^i,2^{n-1}$, where each
+//! query reveals the $i$th bit of $a$ ($a_i$).
+//! This is the optimal classical solution, and is O(n). Using a quantum oracle and the
+//! Bernstein-Vazirani algorithm, $a$ can be found with just one query to the oracle.
+//!
+//! ## The Algorithm
+//!
+//! 1. Initialize $n$ qubits in the state $\lvert 0, \dots, 0\rangle$.
+//! 2. Apply the Hadamard gate $H$ to each qubit.
+//! 3. Apply the inner product oracle.
+//! 4. Apply the Hadamard gate $H$ to each qubit.
+//! 5. Measure the register
+//!
+//! From this procedure, we find that the registers measured value is equal to that of
+//! the original hidden integer.
+
+extern crate qcgpu;
+
+use qcgpu::Simulator;
+use qcgpu::gate::h;
+
+fn main() {
+    let num_qubits = 16; // Number of qubits to use
+    let a = 101; // Hidden integer, bitstring is 1100101
+
+    // You should also make sure that a is representable with $n$ qubits,
+    // by settings a as $a mod 2^n$.
+
+    // Bernstein-Vazirani algorithm
+    let mut state = Simulator::new_opencl(num_qubits); // New quantum register, using the GPU.
+
+    // Apply a hadamard gate to each qubit
+    state.apply_all(h());
+
+    // Apply the inner products oracle
+    for i in 0..num_qubits {
+        if a & (1 << i) != 0 {
+            state.z(i);
+        }
+        // Otherwise should apply identity gate, but computationally this doens't change the state.
+    }
+
+    // Apply hadamard gates before measuring
+    state.apply_all(h());
+
+    println!("Measurement Results: {:?}", state.measure_many(1000));
+    // Measurement Results: {"0000000001100101": 1000}
+}

+ 54 - 0
examples/super-dense.rs

@@ -0,0 +1,54 @@
+//! # Super Dense Coding
+//!
+//! If Alice and Bob share a pair of entangled qubits, then Alice can encode two classical bits into her one entangled qubit,
+//! send it to Bob, and Bob can decode it with the help of his entangled qubit.
+
+extern crate qcgpu;
+
+use qcgpu::Simulator;
+
+fn superdense(input: &str) -> u8 {
+    let mut state = Simulator::new_opencl(2);
+    let input_str = String::from(input);
+
+    // Prepare the bell state
+    state.h(0);
+    state.cx(0, 1);
+
+    // Alice prepares her qubit
+    let alice = 1;
+    if input_str.get(0..1) == Some("1") {
+        state.z(alice);
+    }
+    if input_str.get(1..2) == Some("1") {
+        state.x(alice);
+    }
+
+    println!("\nState after Alice prepares her qubit: \n{}", state);
+
+    // Alice sends her qubit to Bob
+    let bob = 0;
+    state.cx(alice, bob);
+    state.h(alice);
+
+    println!(
+        "\nState after Bob receives Alice's qubit and 'decodes' it: \n{}",
+        state
+    );
+
+    state.measure()
+}
+
+fn main() {
+    use std::io;
+
+    println!("Two bit string to send:");
+    let mut input = String::new();
+    match io::stdin().read_line(&mut input) {
+        Ok(_n) => {
+            let result = superdense(input.as_str());
+            println!("\nDecoded string is: {}", result);
+        }
+        Err(error) => println!("error: {}", error),
+    }
+}