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
22 changes: 22 additions & 0 deletions Numerics/src/FixedPoint/Convert.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Convert {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Math;

/// # Summary
/// Returns the double value of a fixed-point approximation from of a `Bool` array.
///
/// # Input
/// ## integerBits
/// Assumed number of integerBits (including the sign big)
/// ## bits
/// Bit-string representation of approximated number
internal function BoolArrayAsFixedPoint(integerBits : Int, bits : Bool[]) : Double {
let numBits = Length(bits);
let intPart = (Tail(bits) ? -(1 <<< (numBits - 1)) | 0) + BoolArrayAsInt(Most(bits));
return IntAsDouble(intPart) / PowD(2.0, IntAsDouble(numBits - integerBits));
}

}
21 changes: 6 additions & 15 deletions Numerics/src/FixedPoint/Measurement.qs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Licensed under the MIT License.

namespace Microsoft.Quantum.Arithmetic {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Math;

/// # Summary
/// Measure a fixed-point number, returns its value as Double, and resets
Expand All @@ -15,18 +16,8 @@ namespace Microsoft.Quantum.Arithmetic {
/// Fixed-point number to measure.
operation MeasureFxP(fp : FixedPoint) : Double {
let (p, xs) = fp!;
let n = Length(xs);
let sign = MResetZ(xs[n-1]) == One;
mutable keepAdding = sign;
mutable fpAsDouble = 0.;
for i in 0..n - 2 {
mutable currentRes = MResetZ(xs[i]) == (sign ? Zero | One);
if keepAdding {
set keepAdding = currentRes;
set currentRes = not currentRes;
}
set fpAsDouble = fpAsDouble * 0.5 + (currentRes == true ? 1. | 0.);
}
return (sign ? -1.0 | 1.0) * fpAsDouble * PowD(2.0, IntAsDouble(p-2));

let bits = ForEach(q => MResetZ(q) == One, xs);
return BoolArrayAsFixedPoint(p, bits);
}
}
}
28 changes: 28 additions & 0 deletions Numerics/tests/FixedPointTests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Microsoft.Quantum.Numerics.ToffoliTests {
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Intrinsic;
Expand All @@ -21,6 +22,33 @@ namespace Microsoft.Quantum.Numerics.ToffoliTests {
}
}

internal operation PrepareAsSignedAndMeasure(value : Int, fxp : FixedPoint) : Double {
ApplyXorInPlace(value, LittleEndian(Snd(fxp!)));
return MeasureFxP(fxp);
}

operation MeasureFxPTest() : Unit {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a test attribute?

Suggested change
operation MeasureFxPTest() : Unit {
@Test("QuantumSimulator")
operation TestMeasureFxP() : Unit {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests in the Numeric package are still executed via a C# test driver. I want to convert all tests to use the test attribute and remove the C# test driver in a separate PR.

use qs = Qubit[4];
let qsFxP = FixedPoint(2, qs);

NearEqualityFactD(PrepareAsSignedAndMeasure(0b0000, qsFxP), 0.0);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0001, qsFxP), 0.25);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0010, qsFxP), 0.5);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0011, qsFxP), 0.75);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0100, qsFxP), 1.0);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0101, qsFxP), 1.25);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0110, qsFxP), 1.5);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0111, qsFxP), 1.75);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1000, qsFxP), -2.0);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1001, qsFxP), -1.75);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1010, qsFxP), -1.5);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1011, qsFxP), -1.25);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1100, qsFxP), -1.00);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1101, qsFxP), -0.75);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1110, qsFxP), -0.5);
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1111, qsFxP), -0.25);
}

operation CompareGreaterThanFxPTest() : Unit {
for a in [1.2, 3.9, 3.14159, -0.6, -4.5, -3.1931, 0.0] {
for b in [1.1, 3.95, 3.14259, -0.4, -4.6, -3.931, 0.1] {
Expand Down