qsharp-runtime icon indicating copy to clipboard operation
qsharp-runtime copied to clipboard

Ceiling(NaN()) causes an overflow

Open weucode opened this issue 3 years ago • 2 comments

Describe the bug The result of Lg with a non-positive parameter is NaN, so running the following program will cause overflow. The part of the output is shown below:

Unhandled exception. System.OverflowException: Arithmetic operation resulted in an overflow.

To Reproduce

namespace NameSpace {
	open Microsoft.Quantum.Intrinsic;
	open Microsoft.Quantum.Math;


	@EntryPoint()
	operation main() : Unit {
		let nIdxRegQubits = Ceiling(Lg(-1.0));
		// same as:let nIdxRegQubits = Ceiling(NaN());
		Message($"{nIdxRegQubits}");
	}
}

Expected behavior Maybe a range check can be used to avoid it.

System information operating system : Ubuntu 22.04 LTS

dotnet version : 6.0.400

QDK : 0.25.228311

weucode avatar Oct 24 '22 13:10 weucode

It seems this API effect other APIs, one can be seen in https://github.com/microsoft/QuantumLibraries/issues/570; if give targetError a value of 0.0, an overflow behavior will occur. It might be better to fix Ceiling rather than other high-level APIs.

weucode avatar Nov 14 '22 13:11 weucode

Thanks @weucode for filing this. It would be better to return NaN in this case. The function is implemented in Microsoft/qsharp-runtime (see https://github.com/microsoft/qsharp-runtime/blob/15a54935a2f9ac5cac798385bb3b4952deae3496/src/Simulation/QSharpFoundation/Math/Math.qs#L506), so I am transferring the issue to that repository.

msoeken avatar Nov 15 '22 07:11 msoeken