Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions MachineLearning/src/Runtime/Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<AssemblyName>Microsoft.Quantum.MachineLearning.Runtime</AssemblyName>
</PropertyGroup>

<PropertyGroup>
<RunQDocGen>True</RunQDocGen>
</PropertyGroup>

<ItemGroup>
Expand Down
52 changes: 32 additions & 20 deletions Standard/src/Arithmetic/Reflections.qs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,39 @@ namespace Microsoft.Quantum.Arithmetic {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Logical;

///Flip the sign of just one amplitude
operation ReflectAboutInteger(index : Int, reg : LittleEndian): Unit is Adj + Ctl {
let nQubits = Length(reg!);
let bitstring = IntAsBoolArray(index, nQubits);
if (nQubits < 2) {
within {
if (not bitstring[0]) {
X(reg![0]);
}
} apply {
Z(reg![0]);
}
} else {
within {
ApplyToEachCA(CControlledCA(X), Zip(bitstring, reg!));
} apply {
(Controlled Z)(Most(reg!), Tail(reg!)); //The true complexity of this operation is in O(nQubits)
}
/// # Summary
/// Reflects a quantum register about a given classical integer.
///
/// # Description
/// Given a quantum register initially in the state $\sum_i \alpha_i \ket{i}$,
/// where each $\ket{i}$ is a basis state representing an integer $i$,
/// reflects the state of the register about the basis state for a given
/// integer $\ket{j}$,
/// $$
/// \sum_i (-1)^{ \delta_{ij} } \alpha_i \ket{i}
/// $$
///
/// # Input
/// ## index
/// The classical integer indexing the basis state about which to reflect.
///
/// # Remarks
/// This operation is implemented in-place, without explicit allocation of
/// additional auxillary qubits.
operation ReflectAboutInteger(index : Int, reg : LittleEndian) : Unit is Adj + Ctl {
within {
// We want to reduce to the problem of reflecting about the all-ones
// state. To do that, we apply our reflection within an application
// of X instructions that flip all the zeros in our index.
ApplyToEachCA(
CControlledCA(X),
Zip(Mapped(Not, IntAsBoolArray(index, Length(reg!))), reg!)
);
} apply {
(Controlled Z)(Most(reg!), Tail(reg!));
}
} //_amplitudeSignFlip

}

}
14 changes: 7 additions & 7 deletions Standard/src/Math/Functions.qs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ namespace Microsoft.Quantum.Math {
}


/// # Summary
/// # Summary
/// Returns the squared 2-norm of a vector.
///
/// # Description
Expand All @@ -625,12 +625,12 @@ namespace Microsoft.Quantum.Math {
/// # Output
/// The squared 2-norm of `array`.
function SquaredNorm(array : Double[]) : Double {
mutable ret = 0.0;
for (element in array) {
set ret += element * element;
}
return ret;
}
mutable ret = 0.0;
for (element in array) {
set ret += element * element;
}
return ret;
}


/// # Summary
Expand Down
4 changes: 2 additions & 2 deletions Standard/src/Measurement/Reset.qs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Quantum.Measurement {
}


/// # Summary
/// # Summary
/// Sets a qubit to a given computational basis state by measuring the
/// qubit and applying a bit flip if needed.
///
Expand All @@ -30,7 +30,7 @@ namespace Microsoft.Quantum.Measurement {
/// # Remarks
/// As an invariant of this operation, calling `M(q)` immediately
/// after `SetToBasisState(result, q)` will return `result`.
operation SetToBasisState(desired : Result, target : Qubit) : Unit {
operation SetToBasisState(desired : Result, target : Qubit) : Unit {
if (desired != M(target)) {
X(target);
}
Expand Down
1 change: 1 addition & 0 deletions Standard/tests/AmplitudeAmplificationTests.qs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
Expand Down
29 changes: 29 additions & 0 deletions Standard/tests/Arithmetic/ReflectionTests.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Diagnostics;

operation ManuallyReflectAboutFive(register : Qubit[]) : Unit is Adj + Ctl {
within {
X(register[1]);
} apply {
Controlled Z(register[0..1], register[2]);
}
}

operation ReflectAboutFiveUsingLibrary(register : Qubit[]) : Unit is Adj + Ctl {
let littleEndian = LittleEndian(register);
ReflectAboutInteger(5, littleEndian);
}

operation ReflectAboutIntegerTest() : Unit {
AssertOperationsEqualReferenced(3,
ReflectAboutFiveUsingLibrary,
ManuallyReflectAboutFive
);
}

}
8 changes: 8 additions & 0 deletions Standard/tests/ArrayTests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ namespace Microsoft.Quantum.Tests {
}
}

function IsEmptyTest() : Unit {
Fact(IsEmpty(new Int[0]), "Empty array marked as non-empty.");
Fact(IsEmpty(new Qubit[0]), "Empty array marked as non-empty.");
Fact(IsEmpty(new (Double, (Int -> String))[0]), "Empty array marked as non-empty.");
Fact(not IsEmpty([PauliX, PauliZ]), "Non-empty array marked as empty.");
Fact(not IsEmpty([""]), "Non-empty array marked as empty.");
}

}


27 changes: 27 additions & 0 deletions Standard/tests/CombinatorTests.qs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Canon;
Expand Down Expand Up @@ -349,4 +350,30 @@ namespace Microsoft.Quantum.Tests {
AssertOperationsEqualReferenced(2, ApplyIfElseBCACase(false, _), ApplyToEachA(X, _));
}

operation ApplyXToSecondQubit(qubits : Qubit[]) : Unit is Adj + Ctl {
X(qubits[1]);
}

operation ApplyToElementTest() : Unit {
AssertOperationsEqualReferenced(3,
ApplyToElement(X, 1, _),
ApplyXToSecondQubit
);

AssertOperationsEqualReferenced(3,
ApplyToElementC(X, 1, _),
ApplyXToSecondQubit
);

AssertOperationsEqualReferenced(3,
ApplyToElementA(X, 1, _),
ApplyXToSecondQubit
);

AssertOperationsEqualReferenced(3,
ApplyToElementCA(X, 1, _),
ApplyXToSecondQubit
);
}

}
6 changes: 6 additions & 0 deletions Standard/tests/Logical/PredicateTests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ namespace Microsoft.Quantum.Tests {
Fact(not LessThanOrEqualL(32L, -13L), "LessThanOrEqualL returned wrong output.");
}

function NearlyEqualDTest() : Unit {
Fact(NearlyEqualD(1.0, 1.0), "Exactly equal numbers marked as not nearly equal.");
Fact(NearlyEqualD(1.0, 1.0 + 1e-15), "Nearly equal numbers marked as not nearly equal.");
Fact(not NearlyEqualD(1.0, 1000.0), "Not nearly equal numbers marked as nearly equal.");
}

}
6 changes: 6 additions & 0 deletions Standard/tests/Math/MathTests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ namespace Microsoft.Quantum.Canon {
}
}
}

function SquaredNormTest() : Unit {
NearEqualityFactD(SquaredNorm([2.0]), 4.0);
NearEqualityFactD(SquaredNorm([1.0, 1.0]), 2.0);
NearEqualityFactD(SquaredNorm([3.0, 4.0]), 25.0);
}

}

Expand Down
24 changes: 24 additions & 0 deletions Standard/tests/Measurement/ResetTests.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;

operation CheckSetToBasisState(desired : Result) : Unit {
using (q = Qubit()) {
Ry(0.1234, q);
SetToBasisState(desired, q);
AssertQubit(desired, q);
Reset(q);
}
}

operation SetToBasisStateTest() : Unit {
for (desired in [Zero, One]) {
CheckSetToBasisState(desired);
}
}

}