Last active
March 10, 2016 16:24
-
-
Save jjcollinge/0662b2757975993b224b to your computer and use it in GitHub Desktop.
A basic example of using the LiQu|> quantum simulation for basic quantum operations
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
namespace Microsoft.Research.Liquid | |
module UserSample = | |
open System | |
open Util | |
open Operations | |
//open Native // Support for Native Interop | |
//open HamiltonianGates // Extra gates for doing Hamiltonian simulations | |
//open Tests // All the built-in tests | |
/// Note: | |
/// a Qubit is modelled as a vector on a sphere with radius of 1 radian | |
/// (x,y,z) = sin(theta)cos(phi), sin(theta)sin(phi), cos(theta) | |
/// <summary> | |
/// Performs an arbitrary rotation around X. | |
/// </summary> | |
/// <param name="theta">Angle to rotate by</param> | |
/// <param name="qs">The head qubit of this list is operated on.</param> | |
let rotX (theta:float) (qs:Qubits) = | |
let gate (theta:float) = | |
let nam = "Rx" + theta.ToString("F2") | |
// Create a 2x2 complex matrix for rotation around the x axis | |
new Gate( | |
Name = nam, | |
Help = sprintf "Rotate in X by: %f" theta, | |
Mat = ( | |
let phi = theta / 2.0 | |
let c = Math.Cos phi | |
let s = Math.Sin phi | |
CSMat(2,[0,0,c,0.;0,1,-s,0.;1,0,s,0.;1,1,c,0.])), | |
Draw = "\\gate{" + nam + "}" | |
) | |
(gate theta).Run qs | |
// SUPERPOSITION | |
// let qfunc (qs:Qubits) = | |
// H qs // H(hadamard gate):Superimpose the bit to a 50:50 position | |
// M qs // M:Measurement (First qubit) | |
// ENTANGLEMENT | |
let qfunc (qs:Qubits) = | |
rotX (Math.PI/4.) qs // Rotate the first qubit in qs | |
for q in qs.Tail do CNOT [qs.Head;q] // Entangle with all the other qubits | |
M >< qs // Map a Measurement to all the qubits | |
[<LQD>] // The following function should be callable from the command line | |
let __UserSample(n:int) = | |
let stats = Array.create 2 0 | |
let k = Ket(n) // |n> = col(n1,n2...nn) | |
let circ = Circuit.Compile qfunc k.Qubits // Create a circuit | |
let circ = circ.GrowGates(k) // Collapse the quantum gates (optimisation) | |
show "Test1:" // | |
circ.Dump() // Dump circuit to log | |
circ.RenderHT("Test1") // Render circuit as SVG and Tikz (TeX) | |
show "Test2:" // | |
circ.Dump() // Dump circuit to log | |
circ.RenderHT("Test2") // Render circuit as SVG and Tikz (TeX) | |
for i in 0..9999 do | |
let qs = k.Reset(n) | |
circ.Run qs // Run the circuit (this will operate on the first qubit) | |
let v = qs.Head.Bit.v // Read the value of the first qubit | |
stats.[v] <- stats.[v] + 1 | |
for q in qs.Tail do | |
if q.Bit <> qs.Head.Bit then // Check that all Qubits are equal (as they are entangled) | |
failwith "Exception" | |
show "Measured: 0=%d 1=%d" stats.[0] stats.[1] // Display | |
module Main = | |
open App | |
/// <summary> | |
/// The main entry point for Liquid. | |
/// Define function to run in VS properties > Debug > Command Line Args | |
/// </summary> | |
[<EntryPoint>] | |
let Main _ = | |
RunLiquid () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment