Skip to content

Commit

Permalink
Update to 0.9, use some 0.9 features (microsoft#228)
Browse files Browse the repository at this point in the history
* Updated Dockerfile to 0.9 image.

* Updated phase estimation sample a bit.

* Fix SHA sum for devcontainer, update simple algorithms.

* Update a few samples, add host.py to CHSH game.

* Updated database search.

* Add editorconfig to devcontainer.

* Recommend VSCode extensions.

* Force editorconfig to treat workspace as a root.

* Update F# sample.

* Adapted rest of Q# projects in standard samples.

* Adapting notebooks as well.

* Updated Numerics as well.

* Begin updating chemistry samples.

* Addressing feedback.

* Finally remembering to ignore symbol cache.

* Update Numerics/CustomModAdd/CustomModAdd.qs

Co-Authored-By: bettinaheim <[email protected]>

* Update Numerics/CustomModAdd/CustomModAdd.qs

Co-Authored-By: bettinaheim <[email protected]>
  • Loading branch information
Chris Granade and bettinaheim authored Sep 30, 2019
1 parent 1ff82ea commit b29c290
Show file tree
Hide file tree
Showing 63 changed files with 762 additions and 807 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"dockerFile": "../Dockerfile",
"extensions": [
"quantum.quantum-devkit-vscode",
"ms-vscode.csharp"
"ms-vscode.csharp",
"editorconfig.editorconfig",
"ms-python.python"
],
"runArgs": ["-e","IQSHARP_HOSTING_ENV=SAMPLES_DEVCONTAINER"]
}
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,6 @@ __pycache__/
*.btm.cs
*.odx.cs
*.xsd.cs

# Common VS Code extensions
.ionide/symbolCache.db
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"quantum.quantum-devkit-vscode",
"ms-vscode.csharp",
"ms-python.python",
"EditorConfig.EditorConfig"
]
}
4 changes: 2 additions & 2 deletions Chemistry/AnalyzeHamiltonian/1-AnalyzeHamiltonian.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
<PackageReference Include="Microsoft.Quantum.Chemistry" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Chemistry" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.9.1908.2906" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# https://github.com/microsoft/iqsharp/blob/master/images/iqsharp-base/Dockerfile.
# As per Binder documentation, we choose to use an SHA sum here instead of a
# tag.
FROM mcr.microsoft.com/quantum/iqsharp-base@sha256:f8a1d03beccd419ed4155c60a355948ec7c7887566a63a36e9fbdace77918b36
FROM mcr.microsoft.com/quantum/iqsharp-base@sha256:305e27693dfe62b03793a01bbacd387a2d21fe8d3d68deb29ed3542e071aa876

# Mark that this Dockerfile is used with the samples repository.
ENV IQSHARP_HOSTING_ENV=SAMPLES_DOCKERFILE
Expand Down
6 changes: 3 additions & 3 deletions Numerics/CustomModAdd/CustomModAdd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Numerics" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Numerics" Version="0.9.1908.2906" />
</ItemGroup>
</Project>
16 changes: 9 additions & 7 deletions Numerics/CustomModAdd/CustomModAdd.qs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ namespace Microsoft.Quantum.Numerics.Samples {
// we have to subtract `modulus` from the result:
let yc = LittleEndian(yQubits + [tmp]);
(Adjoint AddI)(m, yc);
CNOT(tmp, ctrl);
// we should not have subtracted m if there is overflow:
(Controlled AddI)([ctrl], (m, yc));
// now, uncompute temporary qubits:
(Adjoint AddI)(x, yc);
CNOT(tmp, ctrl);
within {
CNOT(tmp, ctrl);
} apply {
// we should not have subtracted m if there is overflow:
Controlled AddI([ctrl], (m, yc));
// now, uncompute temporary qubits:
Adjoint AddI(x, yc);
}
AddI(x, yc);
X(ctrl);

Expand All @@ -59,4 +61,4 @@ namespace Microsoft.Quantum.Numerics.Samples {
}
return results;
}
}
}
6 changes: 3 additions & 3 deletions Numerics/EvaluatingFunctions/EvaluatingFunctions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Numerics" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Numerics" Version="0.9.1908.2906" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions Numerics/ResourceCounting/ResourceCounting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Numerics" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Numerics" Version="0.9.1908.2906" />
</ItemGroup>
</Project>
15 changes: 8 additions & 7 deletions Samples/src/AdiabaticIsing/AdiabaticIsing.qs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ namespace Microsoft.Quantum.Samples.Ising {
/// A `GeneratorSystem` representing the interpolated Hamiltonian H(s) of
/// the Ising model.
function IsingEvolutionScheduleImpl (nSites : Int, hXInitial : Double, hXFinal : Double, jFinal : Double, schedule : Double) : GeneratorSystem {
let hX = GenerateUniformHCoupling(hXFinal * schedule + hXInitial * (1.0 - schedule), _);
let jZ = GenerateUniform1DJCoupling(nSites, schedule * jFinal, _);
let hX = UniformHCoupling(hXFinal * schedule + hXInitial * (1.0 - schedule), _);
let jZ = Uniform1DJCoupling(nSites, schedule * jFinal, _);
let (evolutionSet, generatorSystem) = (Ising1DEvolutionGenerator(nSites, hX, jZ))!;
return generatorSystem;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ namespace Microsoft.Quantum.Samples.Ising {
/// A unitary operator implementing time-dependent evolution by the
/// Hamiltonian H(s) when s is varied uniformly between 0 and 1 over time
/// `adiabaticTime`.
function IsingAdiabaticEvolutionManual (nSites : Int, hXInitial : Double, hXFinal : Double, jFinal : Double, adiabaticTime : Double, trotterStepSize : Double, trotterOrder : Int) : (Qubit[] => Unit is Adj + Ctl) {
function IsingAdiabaticEvolutionManual(nSites : Int, hXInitial : Double, hXFinal : Double, jFinal : Double, adiabaticTime : Double, trotterStepSize : Double, trotterOrder : Int) : (Qubit[] => Unit is Adj + Ctl) {
let timeDependentSimulationAlgorithm = TimeDependentTrotterSimulationAlgorithm(trotterStepSize, trotterOrder);
return IsingAdiabaticEvolutionManualImpl(nSites, hXInitial, hXFinal, jFinal, adiabaticTime, timeDependentSimulationAlgorithm, _);
}
Expand Down Expand Up @@ -344,15 +344,16 @@ namespace Microsoft.Quantum.Samples.Ising {
/// A `Result[]` storing the outcome of Z basis measurements on each site
/// of the Ising model.
operation Ising1DAdiabaticAndMeasureBuiltIn (nSites : Int, hXInitial : Double, jFinal : Double, adiabaticTime : Double, trotterStepSize : Double, trotterOrder : Int) : Result[] {

let hXCoupling = GenerateUniformHCoupling(hXInitial, _);
let hXCoupling = UniformHCoupling(hXInitial, _);

// For antiferromagnetic coupling, choose jFinal to be negative.
let jCoupling = GenerateUniform1DJCoupling(nSites, jFinal, _);
let jCoupling = Uniform1DJCoupling(nSites, jFinal, _);

using (qubits = Qubit[nSites]) {
Prepare1DIsingState(qubits);
(IsingAdiabaticEvolutionBuiltIn(nSites, adiabaticTime, trotterStepSize, trotterOrder, hXCoupling, jCoupling))(qubits);
(IsingAdiabaticEvolutionBuiltIn(
nSites, adiabaticTime, trotterStepSize, trotterOrder, hXCoupling, jCoupling
))(qubits);
return ForEach(MResetZ, qubits);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Samples/src/AdiabaticIsing/AdiabaticIsingSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.9.1908.2906" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions Samples/src/Arithmetic/AdderExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,14 @@
"outputs": [
{
"data": {
"application/json": "{\"rows\":[{\"@type\":\"@tuple\",\"Item1\":\"iqsharp\",\"Item2\":\"0.8.1907.1701\"},{\"@type\":\"@tuple\",\"Item1\":\"Jupyter Core\",\"Item2\":\"1.1.14623.0\"}]}",
"application/json": "{\"rows\":[{\"@type\":\"@tuple\",\"Item1\":\"iqsharp\",\"Item2\":\"0.9.1908.2906\"},{\"@type\":\"@tuple\",\"Item1\":\"Jupyter Core\",\"Item2\":\"1.1.14623.0\"}]}",
"text/html": [
"<table><thead><tr><th>Component</th><th>Version</th></tr></thead><tbody><tr><td>iqsharp</td><td>0.8.1907.1701</td></tr><tr><td>Jupyter Core</td><td>1.1.14623.0</td></tr></tbody></table>"
"<table><thead><tr><th>Component</th><th>Version</th></tr></thead><tbody><tr><td>iqsharp</td><td>0.9.1908.2906</td></tr><tr><td>Jupyter Core</td><td>1.1.14623.0</td></tr></tbody></table>"
],
"text/plain": [
"Component Version\n",
"------------ -------------\n",
"iqsharp 0.8.1907.1701\n",
"iqsharp 0.9.1908.2906\n",
"Jupyter Core 1.1.14623.0\n"
]
},
Expand Down
4 changes: 2 additions & 2 deletions Samples/src/BitFlipCode/BitFlipCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.9.1908.2906" />
</ItemGroup>
</Project>
28 changes: 10 additions & 18 deletions Samples/src/BitFlipCode/BitFlipCode.qs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ namespace Microsoft.Quantum.Samples.BitFlipCode {
// We start by preparing R_x(π / 3) |0〉 as our
// test state, along with two auxiliary qubits in the |00〉
// state that we can use to encode.
using (register = Qubit[3]) {
let data = register[0];
let auxiliaryQubits = register[1 .. 2];
using ((data, auxiliaryQubits) = (Qubit(), Qubit[2])) {
let register = [data] + auxiliaryQubits;
Rx(PI() / 3.0, data);

// Next, we encode our test state.
Expand Down Expand Up @@ -170,14 +169,12 @@ namespace Microsoft.Quantum.Samples.BitFlipCode {
/// This operation will fail when the error correction step fails
/// if run on a target machine which supports assertions, and thus
/// can be used as a unit test of error-correction functionality.
operation CheckBitFlipCodeCorrectsError (error : (Qubit[] => Unit)) : Unit {

using (register = Qubit[3]) {
operation CheckBitFlipCodeCorrectsError(error : (Qubit[] => Unit)) : Unit {
using ((data, auxiliaryQubits) = (Qubit(), Qubit[2])) {
let register = [data] + auxiliaryQubits;

// We start by proceeding the same way as above
// in order to obtain the code block state |̅ψ〉.
let data = register[0];
let auxiliaryQubits = register[1 .. 2];
Rx(PI() / 3.0, data);
EncodeIntoBitFlipCode(data, auxiliaryQubits);

Expand Down Expand Up @@ -256,8 +253,7 @@ namespace Microsoft.Quantum.Samples.BitFlipCode {
/// This operation will fail when error correction fails
/// if run on a target machine which supports assertions, and thus
/// can be used as a unit test of error-correction functionality.
operation CheckBitFlipCodeCorrectsBitFlipErrors () : Unit {

operation CheckBitFlipCodeCorrectsBitFlipErrors() : Unit {
// First, we declare our errors using the notation
// described above.
let X0 = ApplyPauli([PauliX, PauliI, PauliI], _);
Expand Down Expand Up @@ -300,17 +296,16 @@ namespace Microsoft.Quantum.Samples.BitFlipCode {
/// This operation will fail when the error correction step fails
/// if run on a target machine which supports assertions, and thus
/// can be used as a unit test of error-correction functionality.
operation CheckCodeCorrectsError (code : QECC, nScratch : Int, fn : RecoveryFn, error : (Qubit[] => Unit)) : Unit {
operation CheckCodeCorrectsError(code : QECC, nScratch : Int, fn : RecoveryFn, error : (Qubit[] => Unit)) : Unit {

// We once again begin by allocating some qubits to use as data
// and auxiliary qubits, and by preparing a test state on the
// data qubit.
using (register = Qubit[1 + nScratch]) {

using ((data, auxiliaryQubits) = (Qubit(), Qubit[nScratch])) {
// We start by proceeding the same way as above
// in order to obtain the code block state |̅ψ〉.
let data = register[0];
let auxiliaryQubits = register[1 .. nScratch];
let register = [data] + auxiliaryQubits;

Rx(PI() / 3.0, data);

// We differ this time, however, in how we perform the
Expand Down Expand Up @@ -364,7 +359,6 @@ namespace Microsoft.Quantum.Samples.BitFlipCode {
/// if run on a target machine which supports assertions, and thus
/// can be used as a unit test of error-correction functionality.
operation CheckCanonBitFlipCodeCorrectsBitFlipErrors () : Unit {

let code = BitFlipCode();
let recoveryFn = BitFlipRecoveryFn();
let X0 = ApplyPauli([PauliX, PauliI, PauliI], _);
Expand All @@ -381,5 +375,3 @@ namespace Microsoft.Quantum.Samples.BitFlipCode {
}

}


4 changes: 2 additions & 2 deletions Samples/src/CHSHGame/CHSHGame.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.9.1908.2906" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.9.1908.2906" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions Samples/src/CHSHGame/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class Driver
static void Main(string[] args)
{
const int trialCount = 10000;
Random generator = new Random();
var generator = new Random();
using (QuantumSimulator sim = new QuantumSimulator())
{
int classicalWinCount = 0;
int quantumWinCount = 0;
var classicalWinCount = 0;
var quantumWinCount = 0;
for (int i = 0; i < trialCount; i++)
{
bool aliceBit = GetRandomBit(generator);
Expand Down
49 changes: 49 additions & 0 deletions Samples/src/CHSHGame/host.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

import numpy as np
import random
from typing import Tuple

import qsharp
from Microsoft.Quantum.Samples.CHSHGame import PlayQuantumStrategy

def get_random_bits(n_bits=1):
return [
bool(random.getrandbits(1))
for _ in range(n_bits)
] if n_bits > 1 else bool(random.getrandbits(1))

def referee_single_round() -> bool:
"""
Play a single round of the CHSH game and referee to see if the quantum
strategy won the round.
"""

# Generate random inpus for each player.
alice_input, bob_input = get_random_bits(2)

# Check whether Alice or Bob should go first.
alice_measures_first = get_random_bits(1)

# Run the Q# program to get the parity of Alice's and Bob's answers.
output_parity = PlayQuantumStrategy.simulate(
aliceBit=alice_input,
bobBit=bob_input,
aliceMeasuresFirst=alice_measures_first
)

# Check if Alice and Bob won the round.
return output_parity == (not (alice_input and bob_input))

def estimate_quantum_win_probability(n_trials : int) -> Tuple[float, float]:
est = np.mean([
referee_single_round()
for _ in range(n_trials)
])

return est, np.sqrt(est * (1 - est) / n_trials)

if __name__ == "__main__":
est_win_pr, error = estimate_quantum_win_probability(400)
print(f"Estimated quantum win probability: {est_win_pr:%} ± {error:%}")
Loading

0 comments on commit b29c290

Please sign in to comment.