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

New array syntax fails for arrays within tuples

Open tcNickolas opened this issue 3 years ago • 1 comments

Describe the bug

Replacing the code based on the warning about deprecated syntax (see repro below) produces a runtime error

Unhandled exception. System.InvalidCastException: Unable to cast object of type 
'System.ValueTuple`2[Microsoft.Quantum.Simulation.Core.QArray`1[System.Int64],Microsoft.Quantum.Simulation.Core.QRange]' to type 
'System.ValueTuple`2[Microsoft.Quantum.Simulation.Core.IQArray`1[System.Int64],Microsoft.Quantum.Simulation.Core.QRange]'.

To Reproduce Old code:

function TestRangeAsIntArray() : Unit {
    mutable testCases = new (Int[], Range)[1];
    set testCases w/= 0 <- ([1, 3, 5, 7], 1..2..8);
}

New code that throws exception:

function TestRangeAsIntArray() : Unit {
    mutable testCases = [([0, size = 0], 1..0), size = 1];
    set testCases w/= 0 <- ([1, 3, 5, 7], 1..2..8);
}

Expected behavior

I expected the updated code to run successfully.

System information

  • QDK 0.22.187631

tcNickolas avatar Feb 02 '22 01:02 tcNickolas

The generated code for the array expression is

(IQArray<(IQArray<Int64>,QRange)>)QArray.Filled(() => (QArray.Filled(() => 0L, 0L), new QRange(1L, 0L)), 1L);

The issue is that QArray.Filled returns QArray<T>, not IQArray<T>. QArray<T> implements IQArray<T>, but .NET ValueTuple is invariant.

Either the compiler needs to insert a cast to IQArray<Int64> for the second call to QArray.Filled, or the return type of QArray.Filled needs to change to IQArray<T>.

bamarsha avatar Feb 03 '22 23:02 bamarsha