Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Feature branch cleanup (#582)
Browse files Browse the repository at this point in the history
* Fixes #570. (#572)

* Fixes #571. (#573)

* Add DevSkim scanning (#576)

* Examples in fixed-point conversion functions.

* API review April 2022 (#561)

* API review April 2022

* Apply suggestions from code review

Co-authored-by: Mariia Mykhailova <mamykhai@microsoft.com>

* Update api-design-2022-04.md

* Update api-design-2022-04.md

Co-authored-by: Mariia Mykhailova <mamykhai@microsoft.com>

* Fixes #580. (#581)

* Address reviewer's feedback.

Co-authored-by: Angela Burton <anjbur@users.noreply.github.com>
Co-authored-by: Mariia Mykhailova <mamykhai@microsoft.com>
  • Loading branch information
3 people committed May 12, 2022
1 parent 81fe6de commit 690d742
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Numerics/src/FixedPoint/Convert.qs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace Microsoft.Quantum.Convert {
/// Assumed number of fractional bits.
/// ## value
/// Value to be approximated.
///
/// # Example
/// Note that the first element in the Boolean array is the least-significant bit.
/// ```qsharp
/// let bits = FixedPointAsBoolArray(2, 2, 1.25); // bits = [true, false, true, false]
/// let bits = FixedPointAsBoolArray(2, 2, 1.3); // bits = [true, false, true, false], approximated
/// let bits = FixedPointAsBoolArray(2, 2, -1.75); // bits = [true, false, false, true], two's complement
/// ```
function FixedPointAsBoolArray(integerBits : Int, fractionalBits : Int, value : Double) : Bool[] {
let numBits = integerBits + fractionalBits;
let sign = value < 0.0;
Expand Down Expand Up @@ -47,6 +55,13 @@ namespace Microsoft.Quantum.Convert {
/// Assumed number of integer bits (including the sign bit).
/// ## bits
/// Bit-string representation of approximated number.
///
/// # Example
/// Note that the first element in the Boolean array is the least-significant bit.
/// ```qsharp
/// let value = BoolArrayAsFixedPoint(2, [true, false, true, false]); // value = 1.25
/// let value = BoolArrayAsFixedPoint(2, [true, false, false, true]); // value = -1.75
/// ```
function BoolArrayAsFixedPoint(integerBits : Int, bits : Bool[]) : Double {
let numBits = Length(bits);
let intPart = (Tail(bits) ? -(1 <<< (numBits - 1)) | 0) + BoolArrayAsInt(Most(bits));
Expand All @@ -63,6 +78,12 @@ namespace Microsoft.Quantum.Convert {
/// Assumed number of fractional bits.
/// ## value
/// Value to be approximated.
///
/// # Example
/// ```qsharp
/// let value = DoubleAsFixedPoint(2, 2, 1.3); // value = 1.25
/// let value = DoubleAsFixedPoint(2, 2, 0.8); // value = 0.75
/// ```
function DoubleAsFixedPoint(integerBits : Int, fractionalBits : Int, value : Double) : Double {
return BoolArrayAsFixedPoint(integerBits, FixedPointAsBoolArray(integerBits, fractionalBits, value));
}
Expand Down
13 changes: 13 additions & 0 deletions Numerics/tests/FixedPointTests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ namespace Microsoft.Quantum.Tests {
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1111, qsFxP), -0.25);
}

@Test("QuantumSimulator")
operation FixedPointConversionTest() : Unit {
AllEqualityFactB(FixedPointAsBoolArray(2, 2, 1.25), [true, false, true, false], "FixedPointAsBoolArray failed");
AllEqualityFactB(FixedPointAsBoolArray(2, 2, 1.3), [true, false, true, false], "FixedPointAsBoolArray failed");
AllEqualityFactB(FixedPointAsBoolArray(2, 2, -1.75), [true, false, false, true], "FixedPointAsBoolArray failed");

NearEqualityFactD(BoolArrayAsFixedPoint(2, [true, false, true, false]), 1.25);
NearEqualityFactD(BoolArrayAsFixedPoint(2, [true, false, false, true]), -1.75);

NearEqualityFactD(DoubleAsFixedPoint(2, 2, 1.3), 1.25);
NearEqualityFactD(DoubleAsFixedPoint(2, 2, 0.8), 0.75);
}

@Test("ToffoliSimulator")
operation CompareGreaterThanFxPTest() : Unit {
for a in [1.2, 3.9, 3.14159, -0.6, -4.5, -3.1931, 0.0] {
Expand Down

0 comments on commit 690d742

Please sign in to comment.