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

Stack overflow when using Microsoft.Quantum.Math.BitSizeL #403

@vadym-kl

Description

@vadym-kl

Describe the bug
Stack overflow when using Microsoft.Quantum.Math.BitSizeL with moderate size big integers.

To Reproduce
Create project consisting of single Q# file:

namespace Stackoverflow {

    open  Microsoft.Quantum.Math;
    open Microsoft.Quantum.Intrinsic;

    @EntryPoint()
    operation HelloQ() : Unit
    {
        for k in 1 .. 2048 {
            Message($"Trying 1L <<< {128*k}");
            let b = BitSizeL(1L <<< (128*k));
            Message($"BitSizeL(1 <<< {128*k}) is {b}");
        }
    }
}

Output :

...
BitSizeL(1 <<< 2688) is 2689
Trying 1L <<< 2816
BitSizeL(1 <<< 2816) is 2817
Trying 1L <<< 2944
Stack overflow.

Expected behavior
Q# BigInt type has similar performance and uses to C# BigInt type.

System information

  • OS: Windows
  • Microsoft.Quantum.Sdk/0.15.2101125897

Additional context
Try the following C# code

using System;
using System.Numerics;

namespace bigint
{
    class Program
    {
        static void Main(string[] args)
        {
            for( int k = 0; k < 31; ++k )
            {
                Console.WriteLine($"Trying 1 << {1 << k}");
                long b = ((new BigInteger(1)) << (1 << k)).GetBitLength();
                Console.WriteLine($"Bitsize of 1 << {(1 << k)} is {b}");
            }
        }
    }
}

that runs successfully and outputs

.... 
Trying 1 << 67108864
Bitsize of 1 << 67108864 is 67108865
Trying 1 << 134217728
Bitsize of 1 << 134217728 is 134217729
Trying 1 << 268435456
Bitsize of 1 << 268435456 is 268435457
Trying 1 << 536870912
Bitsize of 1 << 536870912 is 536870913
Trying 1 << 1073741824
Bitsize of 1 << 1073741824 is 1073741825

Metadata

Metadata

Assignees

Labels

Kind-BugSomething isn't workingPkg-StandardIssue relates to the Microsoft.Quantum.Standard package.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions