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
2 changes: 1 addition & 1 deletion MachineLearning/src/Structure.qs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Quantum.MachineLearning {
: Int {
mutable nQubitsRequired = 0;
for (gate in model::Structure) {
set nQubitsRequired = Fold(
set nQubitsRequired = 1 + Fold(
MaxI, 0,
gate::ControlIndices + [
gate::TargetIndex,
Expand Down
25 changes: 25 additions & 0 deletions MachineLearning/tests/ClassificationTests.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.MachineLearning.Tests {
open Microsoft.Quantum.Logical;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.MachineLearning as ML;

@Test("QuantumSimulator")
function InferredLabelFact() : Unit {
EqualityFactI(ML.InferredLabel(0.25, 0.26), 1, "InferredLabel returned wrong class.");
}

@Test("QuantumSimulator")
function InferredLabelsFact() : Unit {
AllEqualityFactI(
ML.InferredLabels(0.25, [0.23, 0.26, 0.1]),
[0, 1, 0],
"InferredLabels returned at least one wrong class."
);
}

}
187 changes: 187 additions & 0 deletions MachineLearning/tests/StructureTests.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.MachineLearning.Tests {
open Microsoft.Quantum.Logical;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.MachineLearning as ML;

@Test("QuantumSimulator")
function NQubitsRequiredFact() : Unit {
let model = Default<ML.SequentialModel>()
w/ Structure <- [
ML.ControlledRotation((3, [7, 9]), PauliX, 0)
];
let actual = ML.NQubitsRequired(model);
EqualityFactI(actual, 10, "Wrong output from NQubitsRequired.");
}

function ExampleModel() : ML.SequentialModel {
return Default<ML.SequentialModel>()
w/ Structure <- [
Default<ML.ControlledRotation>()
w/ TargetIndex <- 2
w/ ControlIndices <- [0]
w/ Axis <- PauliX
w/ ParameterIndex <- 0,
Default<ML.ControlledRotation>()
w/ TargetIndex <- 0
w/ ControlIndices <- [1, 2]
w/ Axis <- PauliZ
w/ ParameterIndex <- 1
]
w/ Parameters <- [
1.234,
2.345
];
}

operation ApplyExampleModelManually(register : Qubit[]) : Unit is Adj + Ctl {
Controlled R([register[0]], (PauliX, 1.234, register[2]));
Controlled R([register[1], register[2]], (PauliZ, 2.345, register[0]));
}

@Test("QuantumSimulator")
operation TestApplySequentialClassifier() : Unit {
AssertOperationsEqualReferenced(ML.NQubitsRequired(ExampleModel()),
ML.ApplySequentialClassifier(ExampleModel(), _),
ApplyExampleModelManually
);
}

function EqualCR(x : ML.ControlledRotation, y : ML.ControlledRotation) : Bool {
return x::Axis == y::Axis and
All(EqualI, Zip(x::ControlIndices, y::ControlIndices)) and
x::TargetIndex == y::TargetIndex and
x::ParameterIndex == y::ParameterIndex;
}

@Test("QuantumSimulator")
function LocalRotationsLayerFact() : Unit {
Fact(All(EqualCR, Zip(
ML.LocalRotationsLayer(3, PauliY),
[
Default<ML.ControlledRotation>()
w/ TargetIndex <- 0
w/ ControlIndices <- new Int[0]
w/ Axis <- PauliY
w/ ParameterIndex <- 0,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 1
w/ ControlIndices <- new Int[0]
w/ Axis <- PauliY
w/ ParameterIndex <- 1,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 2
w/ ControlIndices <- new Int[0]
w/ Axis <- PauliY
w/ ParameterIndex <- 2
]
)), "LocalRotationsLayer returned wrong output.");
}

@Test("QuantumSimulator")
function PartialRotationsLayerFact() : Unit {
Fact(All(EqualCR, Zip(
ML.PartialRotationsLayer([4, 5, 6], PauliY),
[
Default<ML.ControlledRotation>()
w/ TargetIndex <- 4
w/ ControlIndices <- new Int[0]
w/ Axis <- PauliY
w/ ParameterIndex <- 0,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 5
w/ ControlIndices <- new Int[0]
w/ Axis <- PauliY
w/ ParameterIndex <- 1,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 6
w/ ControlIndices <- new Int[0]
w/ Axis <- PauliY
w/ ParameterIndex <- 2
]
)), "PartialRotationsLayer returned wrong output.");
}

@Test("QuantumSimulator")
function CyclicEntanglingLayerFact() : Unit {
Fact(All(EqualCR, Zip(
ML.CyclicEntanglingLayer(3, PauliX, 2),
[
Default<ML.ControlledRotation>()
w/ TargetIndex <- 0
w/ ControlIndices <- [2]
w/ Axis <- PauliX
w/ ParameterIndex <- 0,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 1
w/ ControlIndices <- [0]
w/ Axis <- PauliX
w/ ParameterIndex <- 1,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 2
w/ ControlIndices <- [1]
w/ Axis <- PauliX
w/ ParameterIndex <- 2
]
)), "CyclicEntanglingLayer returned wrong output.");
}

@Test("QuantumSimulator")
function CombinedStructureFact() : Unit {
let combined = ML.CombinedStructure([
[
Default<ML.ControlledRotation>()
w/ TargetIndex <- 0
w/ ControlIndices <- [2]
w/ Axis <- PauliX
w/ ParameterIndex <- 0,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 1
w/ ControlIndices <- [0]
w/ Axis <- PauliX
w/ ParameterIndex <- 1
],
[
Default<ML.ControlledRotation>()
w/ TargetIndex <- 2
w/ ControlIndices <- [1]
w/ Axis <- PauliZ
w/ ParameterIndex <- 0
]
]);
Fact(All(EqualCR, Zip(
combined,
[
Default<ML.ControlledRotation>()
w/ TargetIndex <- 0
w/ ControlIndices <- [2]
w/ Axis <- PauliX
w/ ParameterIndex <- 0,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 1
w/ ControlIndices <- [0]
w/ Axis <- PauliX
w/ ParameterIndex <- 1,

Default<ML.ControlledRotation>()
w/ TargetIndex <- 2
w/ ControlIndices <- [1]
w/ Axis <- PauliZ
w/ ParameterIndex <- 2
]
)), "CombinedStructure returned wrong output.");
}

}
20 changes: 20 additions & 0 deletions MachineLearning/tests/TypesTests.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.MachineLearning.Tests {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.MachineLearning as ML;

@Test("QuantumSimulator")
function ScheduleLengthFact() : Unit {
let actualLength = ML.ScheduleLength(ML.SamplingSchedule([0..4, 1..2..5]));
EqualityFactI(actualLength, 5 + 3, "Wrong output from ScheduleLength.");
}

@Test("QuantumSimulator")
function SampledFact() : Unit {
let actuallySampled = ML.Sampled(ML.SamplingSchedule([0..4, 1..2..5]), [0, 10, 20, 30, 40, 50, 60, 70]);
AllEqualityFactI(actuallySampled, [0, 10, 20, 30, 40, 10, 30, 50], "Wrong output from Sampled.");
}

}
19 changes: 19 additions & 0 deletions MachineLearning/tests/ValidationTests.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.MachineLearning.Tests {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.MachineLearning as ML;

@Test("QuantumSimulator")
function MisclassificationsFact() : Unit {
let misclassifications = ML.Misclassifications([0, 1, 0, 0], [0, 1, 1, 0]);
AllEqualityFactI(misclassifications, [2], "Wrong output from Misclassifications.");
}

@Test("QuantumSimulator")
function NMisclassificationsFact() : Unit {
let nMisclassifications = ML.NMisclassifications([0, 1, 0, 0, 1], [0, 1, 1, 0, 0]);
EqualityFactI(nMisclassifications, 2, "Wrong output from NMisclassifications.");
}
}
18 changes: 7 additions & 11 deletions Standard/src/Arithmetic/Comparators.qs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,15 @@ namespace Microsoft.Quantum.Arithmetic {
}

// Implementation step of `ApplyRippleCarryComparatorLE`.
operation _ApplyRippleCarryComparatorLE(x: LittleEndian, y: LittleEndian, auxiliary: Qubit[], output: Qubit) : Unit {
body (...) {
let nQubitsX = Length(x!);
operation _ApplyRippleCarryComparatorLE(x: LittleEndian, y: LittleEndian, auxiliary: Qubit[], output: Qubit)
: Unit is Adj + Ctl {
let nQubitsX = Length(x!);

// Take 2's complement
ApplyToEachCA(X, x! + auxiliary);
// Take 2's complement
ApplyToEachCA(X, x! + auxiliary);

InPlaceMajority(x![0], [y![0], auxiliary[0]]);
ApplyToEachCA(MAJ, Zip3(Most(x!), Rest(y!), Rest(x!)));
}
adjoint auto;
controlled auto;
adjoint controlled auto;
ApplyMajorityInPlace(x![0], [y![0], auxiliary[0]]);
ApplyToEachCA(MAJ, Zip3(Most(x!), Rest(y!), Rest(x!)));
}

}
24 changes: 13 additions & 11 deletions Standard/src/Arithmetic/Integer.qs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ namespace Microsoft.Quantum.Arithmetic {
"Input registers must have the same number of qubits." );

ApplyToEachCA(CNOT, Zip(Rest(xs!), Rest(ys!)));
(Adjoint CascadeCNOT) (Rest(xs!));
Adjoint ApplyCNOTChain(Rest(xs!));
}

/// # Summary
Expand Down Expand Up @@ -583,16 +583,18 @@ namespace Microsoft.Quantum.Arithmetic {
X(ys![0]);
}
else {
ApplyToEachCA(X, ys!);
ApplyToEachCA(CNOT, Zip(Rest(xs!),Rest(ys!)));
(Adjoint CascadeCNOT) (Rest(xs!));
CascadeCCNOT (Most(ys!), xs!);
(Controlled CCNOT) (controls, (xs![nQubits-1], ys![nQubits-1], result));
(Adjoint CascadeCCNOT) (Most(ys!), xs!);
CascadeCNOT(Rest(xs!));
(Controlled CNOT) (controls, (xs![nQubits-1], result));
ApplyToEachCA(CNOT, Zip(Rest(xs!), Rest(ys!)));
ApplyToEachCA(X, ys!);
within {
ApplyToEachCA(X, ys!);
ApplyToEachCA(CNOT, Zip(Rest(xs!), Rest(ys!)));
} apply {
within {
(Adjoint ApplyCNOTChain) (Rest(xs!));
CascadeCCNOT (Most(ys!), xs!);
} apply {
(Controlled CCNOT) (controls, (xs![nQubits-1], ys![nQubits-1], result));
}
(Controlled CNOT) (controls, (xs![nQubits-1], result));
}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions Standard/src/Diagnostics/Facts.qs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ namespace Microsoft.Quantum.Diagnostics {
/// The array that is expected from a test case of interest.
/// ## message
/// A message to be printed if the arrays are not equal.
///
/// # See Also
/// Microsoft.Quantum.Diagnostics.AllEqualityFactI
function AllEqualityFactB(actual : Bool[], expected : Bool[], message : String) : Unit {
let n = Length(actual);
if (n != Length(expected)) {
Expand All @@ -209,4 +212,26 @@ namespace Microsoft.Quantum.Diagnostics {
Ignore(Mapped(EqualityFactB(_, _, message), Zip(actual, expected)));
}

/// # Summary
/// Asserts that two arrays of integer values are equal.
///
/// # Input
/// ## actual
/// The array that is produced by a test case of interest.
/// ## expected
/// The array that is expected from a test case of interest.
/// ## message
/// A message to be printed if the arrays are not equal.
///
/// # See Also
/// Microsoft.Quantum.Diagnostics.AllEqualityFactB
function AllEqualityFactI(actual : Int[], expected : Int[], message : String) : Unit {
let n = Length(actual);
if (n != Length(expected)) {
fail message;
}

Ignore(Mapped(EqualityFactI(_, _, message), Zip(actual, expected)));
}

}
Loading