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
14 changes: 7 additions & 7 deletions Standard/src/Math/Functions.qs
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,6 @@ namespace Microsoft.Quantum.Math {
return AccumulatedBitsizeI(a, 0);
}

/// # Summary
/// Helper function used to recursively calculate the bitsize of a value.
internal function AccumulatedBitsizeL(val : BigInt, bitsize : Int) : Int {
return val == 0L ? bitsize | AccumulatedBitsizeL(val / 2L, bitsize + 1);
}


/// # Summary
/// For a non-negative integer `a`, returns the number of bits required to represent `a`.
Expand All @@ -572,7 +566,13 @@ namespace Microsoft.Quantum.Math {
/// The bit-size of `a`.
function BitSizeL(a : BigInt) : Int {
Fact(a >= 0L, $"`a` must be non-negative");
return AccumulatedBitsizeL(a, 0);
mutable bitsize = 0;
mutable val = a;
while (val != 0L) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
while (val != 0L) {
while val != 0L {

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the current libraries still builds against an older version of the QDK.

set bitsize += 1;
set val /= 2L;
}
return bitsize;
}


Expand Down
8 changes: 8 additions & 0 deletions Standard/tests/Math/MathTests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ namespace Microsoft.Quantum.Canon {
EqualityFactI(BitSizeI(3), 2, $"BitSizeI(3) must be 2");
EqualityFactI(BitSizeI(7), 3, $"BitSizeI(7) must be 2");
}

@Test("QuantumSimulator")
function CanComputeBitSizeFromLargeNumbers () : Unit {
for (k in 1 .. 100) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (k in 1 .. 100) {
for k in 1 .. 100 {

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the current libraries still builds against an older version of the QDK.

let exp = 128 * k;
EqualityFactI(BitSizeL(1L <<< exp), exp + 1, $"unexpected bitsize for exponent {exp} (k = {k})");
}
}

@Test("QuantumSimulator")
function ExpModIsCorrect () : Unit {
Expand Down