From c633b08cbd269bfadc503e38a9b2ee10c23a90da Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 11 Jun 2018 09:41:09 -0700 Subject: [PATCH] Add first QC experiments. Bell states in the Microsoft Quantum SDK and Rigetti's pyQuil. --- .gitignore | 4 +++ qc/msqdk/Bell/Bell.csproj | 14 +++++++++++ qc/msqdk/Bell/Bell.qs | 52 +++++++++++++++++++++++++++++++++++++++ qc/msqdk/Bell/Driver.cs | 23 +++++++++++++++++ qc/msqdk/README.md | 4 +++ qc/quil/bell/bell.py | 35 ++++++++++++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 qc/msqdk/Bell/Bell.csproj create mode 100644 qc/msqdk/Bell/Bell.qs create mode 100644 qc/msqdk/Bell/Driver.cs create mode 100644 qc/msqdk/README.md create mode 100755 qc/quil/bell/bell.py diff --git a/.gitignore b/.gitignore index f153588..cb607c2 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,7 @@ bitwise/ion/ion # django db /django/*/db.sqlite3 + +# Quantum experiments +/qc/msqdk/*/obj +/qc/msqdk/*/bin diff --git a/qc/msqdk/Bell/Bell.csproj b/qc/msqdk/Bell/Bell.csproj new file mode 100644 index 0000000..6ce78eb --- /dev/null +++ b/qc/msqdk/Bell/Bell.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.0 + x64 + + + + + + + + diff --git a/qc/msqdk/Bell/Bell.qs b/qc/msqdk/Bell/Bell.qs new file mode 100644 index 0000000..c462ec9 --- /dev/null +++ b/qc/msqdk/Bell/Bell.qs @@ -0,0 +1,52 @@ +namespace Quantum.Bell +{ + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Primitive; + + operation Set (desired: Result, q1: Qubit) : () + { + body + { + let current = M(q1); + + if (desired != current) { + X(q1); + } + } + } + + operation BellTest (count: Int, initial: Result) : (Int, Int, Int) + { + body { + mutable numOnes = 0; // by default in Q#, variables are immutable. + // Q# doesn't require type annotations for variables. + mutable agree = 0; + + // using allocates an array of qubits for use in a block of code. + // all qubits are dynamically allocated and released. + using (qubits = Qubit[2]) + { + for (test in 1..count) { + Set(initial, qubits[0]); + Set(Zero, qubits[1]); + + H(qubits[0]); + CNOT(qubits[0], qubits[1]); + let res = M(qubits[0]); + + if (res == M(qubits[1])) { + set agree = agree + 1; + } + + // Count the number of ones we've seen. + if (res == One) { + set numOnes = numOnes + 1; + } + Set(Zero, qubits[0]); + Set(Zero, qubits[1]); + } + } + return (count-numOnes, numOnes, agree); + } + } +} diff --git a/qc/msqdk/Bell/Driver.cs b/qc/msqdk/Bell/Driver.cs new file mode 100644 index 0000000..134fe23 --- /dev/null +++ b/qc/msqdk/Bell/Driver.cs @@ -0,0 +1,23 @@ +using Microsoft.Quantum.Simulation.Core; +using Microsoft.Quantum.Simulation.Simulators; + +namespace Quantum.Bell +{ + class Driver + { + static void Main(string[] args) + { + using (var sim = new QuantumSimulator()) { + // Try initial values. + Result[] initials = new Result[] { Result.Zero, Result.One }; + foreach (Result initial in initials) { + var res = BellTest.Run(sim, 1000, initial).Result; + var (numZeroes, numOnes, agree) = res; + System.Console.WriteLine($"Init:{initial,-4} |0>={numZeroes,-4}, |1>={numOnes,-4}, agree={agree,-4}"); + } + } + System.Console.WriteLine("Press any key to continue."); + System.Console.ReadKey(); + } + } +} \ No newline at end of file diff --git a/qc/msqdk/README.md b/qc/msqdk/README.md new file mode 100644 index 0000000..3fd8174 --- /dev/null +++ b/qc/msqdk/README.md @@ -0,0 +1,4 @@ +MSQDK +----- + +Experiments with the Microsoft Quantum SDK. diff --git a/qc/quil/bell/bell.py b/qc/quil/bell/bell.py new file mode 100755 index 0000000..4aa1b32 --- /dev/null +++ b/qc/quil/bell/bell.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# bell.py tries to run the same Bell state program demonstrating simple +# entanglement of qubits. +from pyquil.quil import Program +from pyquil.api import QVMConnection +from pyquil.gates import CNOT, H + +def main(): + qvm = QVMConnection() + + print('Constructing program.') + p = Program() + p.inst(H(0)).inst(CNOT(0, 1)).measure(0, 0).measure(1, 1) + print('Constructed program:') + print('--------------------') + print(p) + print('--------------------') + + print('\nRunning program on simulator.') + results = qvm.run(p, trials=1000) + + stats = {0: 0, 1: 0, 'agreement': 0} + for result in results: + if result[0] == 0: + stats[0] += 1 + if result[0] == 1: + stats[1] += 1 + if result[0] == result[1]: + stats['agreement'] += 1 + + print('Results: |0>: {:d} |1>: {:d} agree: {:d}'.format( + stats[0], stats[1], stats['agreement'])) + +if __name__ == '__main__': + main() \ No newline at end of file