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

Commit e838b63

Browse files
msoekenCassandra Granade
andauthored
Fixes #560 (#562)
* Fixes #560. * Apply suggestions from code review Co-authored-by: Cassandra Granade <chgranad@microsoft.com> Co-authored-by: Cassandra Granade <chgranad@microsoft.com>
1 parent 30d1abd commit e838b63

3 files changed

Lines changed: 56 additions & 15 deletions

File tree

Numerics/src/FixedPoint/Convert.qs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.Quantum.Convert {
5+
open Microsoft.Quantum.Arrays;
6+
open Microsoft.Quantum.Math;
7+
8+
/// # Summary
9+
/// Returns the double value of a fixed-point approximation from of a `Bool` array.
10+
///
11+
/// # Input
12+
/// ## integerBits
13+
/// Assumed number of integerBits (including the sign big)
14+
/// ## bits
15+
/// Bit-string representation of approximated number
16+
internal function BoolArrayAsFixedPoint(integerBits : Int, bits : Bool[]) : Double {
17+
let numBits = Length(bits);
18+
let intPart = (Tail(bits) ? -(1 <<< (numBits - 1)) | 0) + BoolArrayAsInt(Most(bits));
19+
return IntAsDouble(intPart) / PowD(2.0, IntAsDouble(numBits - integerBits));
20+
}
21+
22+
}

Numerics/src/FixedPoint/Measurement.qs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Licensed under the MIT License.
33

44
namespace Microsoft.Quantum.Arithmetic {
5+
open Microsoft.Quantum.Arrays;
6+
open Microsoft.Quantum.Canon;
57
open Microsoft.Quantum.Convert;
68
open Microsoft.Quantum.Measurement;
7-
open Microsoft.Quantum.Math;
89

910
/// # Summary
1011
/// Measure a fixed-point number, returns its value as Double, and resets
@@ -15,18 +16,8 @@ namespace Microsoft.Quantum.Arithmetic {
1516
/// Fixed-point number to measure.
1617
operation MeasureFxP(fp : FixedPoint) : Double {
1718
let (p, xs) = fp!;
18-
let n = Length(xs);
19-
let sign = MResetZ(xs[n-1]) == One;
20-
mutable keepAdding = sign;
21-
mutable fpAsDouble = 0.;
22-
for i in 0..n - 2 {
23-
mutable currentRes = MResetZ(xs[i]) == (sign ? Zero | One);
24-
if keepAdding {
25-
set keepAdding = currentRes;
26-
set currentRes = not currentRes;
27-
}
28-
set fpAsDouble = fpAsDouble * 0.5 + (currentRes == true ? 1. | 0.);
29-
}
30-
return (sign ? -1.0 | 1.0) * fpAsDouble * PowD(2.0, IntAsDouble(p-2));
19+
20+
let bits = ForEach(q => MResetZ(q) == One, xs);
21+
return BoolArrayAsFixedPoint(p, bits);
3122
}
32-
}
23+
}

Numerics/tests/FixedPointTests.qs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Microsoft.Quantum.Numerics.ToffoliTests {
55
open Microsoft.Quantum.Arrays;
6+
open Microsoft.Quantum.Canon;
67
open Microsoft.Quantum.Convert;
78
open Microsoft.Quantum.Math;
89
open Microsoft.Quantum.Intrinsic;
@@ -21,6 +22,33 @@ namespace Microsoft.Quantum.Numerics.ToffoliTests {
2122
}
2223
}
2324

25+
internal operation PrepareAsSignedAndMeasure(value : Int, fxp : FixedPoint) : Double {
26+
ApplyXorInPlace(value, LittleEndian(Snd(fxp!)));
27+
return MeasureFxP(fxp);
28+
}
29+
30+
operation MeasureFxPTest() : Unit {
31+
use qs = Qubit[4];
32+
let qsFxP = FixedPoint(2, qs);
33+
34+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0000, qsFxP), 0.0);
35+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0001, qsFxP), 0.25);
36+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0010, qsFxP), 0.5);
37+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0011, qsFxP), 0.75);
38+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0100, qsFxP), 1.0);
39+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0101, qsFxP), 1.25);
40+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0110, qsFxP), 1.5);
41+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b0111, qsFxP), 1.75);
42+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1000, qsFxP), -2.0);
43+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1001, qsFxP), -1.75);
44+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1010, qsFxP), -1.5);
45+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1011, qsFxP), -1.25);
46+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1100, qsFxP), -1.00);
47+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1101, qsFxP), -0.75);
48+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1110, qsFxP), -0.5);
49+
NearEqualityFactD(PrepareAsSignedAndMeasure(0b1111, qsFxP), -0.25);
50+
}
51+
2452
operation CompareGreaterThanFxPTest() : Unit {
2553
for a in [1.2, 3.9, 3.14159, -0.6, -4.5, -3.1931, 0.0] {
2654
for b in [1.1, 3.95, 3.14259, -0.4, -4.6, -3.931, 0.1] {

0 commit comments

Comments
 (0)