Add first QC experiments.

Bell states in the Microsoft Quantum SDK and Rigetti's pyQuil.
This commit is contained in:
Kyle Isom 2018-06-11 09:41:09 -07:00
parent 6ad979d28f
commit c633b08cbd
6 changed files with 132 additions and 0 deletions

4
.gitignore vendored
View File

@ -49,3 +49,7 @@ bitwise/ion/ion
# django db # django db
/django/*/db.sqlite3 /django/*/db.sqlite3
# Quantum experiments
/qc/msqdk/*/obj
/qc/msqdk/*/bin

14
qc/msqdk/Bell/Bell.csproj Normal file
View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Canon" Version="0.2.1802.1603-preview" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.2.1802.1603-preview" />
</ItemGroup>
</Project>

52
qc/msqdk/Bell/Bell.qs Normal file
View File

@ -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);
}
}
}

23
qc/msqdk/Bell/Driver.cs Normal file
View File

@ -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();
}
}
}

4
qc/msqdk/README.md Normal file
View File

@ -0,0 +1,4 @@
MSQDK
-----
Experiments with the Microsoft Quantum SDK.

35
qc/quil/bell/bell.py Executable file
View File

@ -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()