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
Show all changes
36 commits
Select commit Hold shift + click to select a range
6eaac54
Add C# gen for sized array
Feb 4, 2021
5ad7f75
Implement QArray.Repeat
Feb 8, 2021
ad01b34
Add more array tests
Feb 8, 2021
b41ed0f
Add doc comments
Feb 9, 2021
d21c679
More efficient array creation
Feb 9, 2021
eeb5715
Remove TODO, leave exception type untested
Feb 9, 2021
3ac7a0b
Formatting
Feb 9, 2021
268c94b
Merge branch 'main' into samarsha/sized-array
bamarsha Feb 9, 2021
5158e4a
Update packages
Feb 10, 2021
0474318
Add tests for ref counts in array-of-arrays
Feb 10, 2021
6fe98bf
Capture value expression
Feb 10, 2021
e7bdfd9
Undo QscVerbosity
Feb 10, 2021
51f1aaf
Clean up QArrayInner.Extend
Feb 11, 2021
588d499
Increment ref count N times
Feb 12, 2021
71102e6
Add comment about exception type
Feb 26, 2021
fa4f62a
Merge pull request #507 from microsoft/samarsha/sized-array
bamarsha Feb 26, 2021
6ec8be9
Merge branch 'main' into samarsha/qep2-main
Mar 15, 2021
20c19d5
Merge pull request #563 from microsoft/samarsha/qep2-main
bamarsha Mar 15, 2021
92ee05e
Update packages
Mar 17, 2021
ccfd394
Fix partial application code gen test
Mar 17, 2021
b9c49cc
Merge branch 'main' into samarsha/qep2-main
Apr 7, 2021
9f0a231
Merge branch 'main' into samarsha/qep2-main
Apr 8, 2021
5efa4e2
Update SDK package
Apr 8, 2021
07ffdc2
Update Microsoft.Quantum.CSharpGeneration.fsproj
bamarsha Apr 8, 2021
da2190a
Merge pull request #611 from microsoft/samarsha/qep2-main
bamarsha Apr 8, 2021
3c87036
Merge branch 'feature/qep2' into samarsha/hm
bamarsha Apr 8, 2021
f85c039
Update Microsoft.Quantum.CSharpGeneration.fsproj
bamarsha Apr 8, 2021
7ae6a9b
Merge pull request #567 from microsoft/samarsha/hm
bamarsha Apr 8, 2021
099a257
Update C# generation tests for microsoft/qsharp-compiler#952
Apr 9, 2021
b309edc
Merge pull request #621 from microsoft/samarsha/update-deprecated-ranges
bamarsha Apr 9, 2021
ef8f96f
Merge branch 'main' into samarsha/qep2-main
Apr 19, 2021
369e775
Merge pull request #641 from microsoft/samarsha/qep2-main
bamarsha Apr 19, 2021
47e0620
Merge branch 'main' into samarsha/qep2
bamarsha Apr 21, 2021
7a68577
Update global.json
bamarsha Apr 21, 2021
80d582e
Merge branch 'main' into samarsha/qep2
bamarsha Apr 21, 2021
28b2d1c
Merge branch 'main' into samarsha/qep2
bettinaheim Apr 21, 2021
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
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"msbuild-sdks": {
"Microsoft.Quantum.Sdk": "0.15.210425594-alpha"
"Microsoft.Quantum.Sdk": "0.15.210425813-alpha"
}
}
61 changes: 34 additions & 27 deletions src/Simulation/CSharpGeneration.Tests/SimulationCodeTests.fs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.7.0" />
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.15.210324735-alpha" />
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.15.210425459-pull" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/Simulation/CSharpGeneration/SimulationCode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ module SimulationCode =
| NamedItem (ex, acc) -> buildNamedItem ex acc
| ArrayItem (a, i) -> buildArrayItem a i
| ValueArray elems -> buildValueArray ex.ResolvedType elems
| SizedArray (value, size) -> buildSizedArray value size
| NewArray (t, expr) -> buildNewArray t expr
| AdjointApplication op -> (buildExpression op) <|.|> (``ident`` "Adjoint")
| ControlledApplication op -> (buildExpression op) <|.|> (``ident`` "Controlled")
Expand Down Expand Up @@ -591,6 +592,10 @@ module SimulationCode =
// TODO: diagnostics.
| _ -> failwith ""

and buildSizedArray value size =
let supplier = ``() =>`` [] (captureExpression value) :> ExpressionSyntax
ident "QArray" <.> (ident "Filled", [ supplier; buildExpression size ])

and buildNewArray b count =
let arrayType = (ArrayType b |> QArrayType).Value
arrayType <.> (``ident`` "Create", [count |> buildExpression])
Expand Down
53 changes: 40 additions & 13 deletions src/Simulation/Core/QArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,22 @@ public void UnsafeSetElement(long index, T value)

public void Extend(long newLength)
{
var newLengthInt = Convert.ToInt32(newLength);
if (storage is null)
{
storage = new List<T>(newLengthInt);
}
else if (storage.Capacity < newLengthInt)
{
storage.Capacity = newLengthInt;
}
storage.AddRange(Enumerable.Repeat(Default.OfType<T>(), newLengthInt - storage.Count));
var value = Default.OfType<T>();
Extend(() => value, Convert.ToInt32(newLength - Length));
}

/// <summary>
/// Extends the length of the array by calling <paramref name="supplier"/> <paramref name="count"/> times to
/// supply a value for each new index.
/// </summary>
internal void Extend(Func<T> supplier, long count)
{
var total = Convert.ToInt32(Length + count);
storage ??= new List<T>(total);
storage.Capacity = Math.Max(storage.Capacity, total);
storage.AddRange(Enumerable
.Repeat<object>(null, Convert.ToInt32(count))
.Select(_ => supplier()));
}
}

Expand Down Expand Up @@ -232,15 +238,26 @@ public QArray(params T[] collection)
}

/// <summary>
/// Creates an array of size given by capacity and default-initializes
/// array elements. Uses C# keyword <code>default</code> to initialize array elements.
/// Creates an array of size given by capacity and initializes each array element to the default value it has in
/// Q#.
/// </summary>
public static QArray<T> Create(long capacity) => new QArray<T>
{
storage = new QArrayInner(capacity),
Length = capacity
};

/// <summary>
/// Creates an array filled by calling <paramref name="supplier"/> <paramref name="count"/> times to supply a
/// value for each index.
/// </summary>
internal static QArray<T> Filled(Func<T> supplier, long count)
{
var array = new QArray<T> { Length = count };
array.storage.Extend(supplier, count);
return array;
}

/// <summary>
/// Creates a copy of this array.
/// </summary>
Expand Down Expand Up @@ -517,6 +534,17 @@ public static implicit operator QArray<object>(QArray<T> arg) =>
arg;
}

/// <summary>
/// Contains static methods for creating <see cref="QArray"/>s.
/// </summary>
public static class QArray
{
/// <summary>
/// Creates an array filled by calling <paramref name="supplier"/> <paramref name="count"/> times to supply a
/// value for each index.
/// </summary>
public static QArray<T> Filled<T>(Func<T> supplier, long count) => QArray<T>.Filled(supplier, count);
}

/// <summary>
/// This JsonConverter converts instances of IQArray['T] as QArray['T]
Expand Down Expand Up @@ -557,4 +585,3 @@ public override bool CanConvert(Type objectType) =>
objectType.GetGenericTypeDefinition() == typeof(QArray<>);
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\Common\AssemblyCommon.props" />
<Import Project="..\Common\Simulators.Dev.props" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">
<Import Project="..\TargetDefinitions\TargetPackages\QSharpCore.Package.props" />

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\Common\AssemblyCommon.props" />
<Import Project="..\Common\DebugSymbols.props" />
Expand Down
77 changes: 77 additions & 0 deletions src/Simulation/Simulators.Tests/Circuits/Arrays.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Intrinsic;

operation MapF<'T, 'U>(mapper : ('T -> 'U), source : 'T[]) : 'U[] {
mutable result = new 'U[Length(source)];
for (i in 0 .. Length(source) - 1) {
let m = mapper(source[i]);
set result = result w/ i <- m;
}

return result;
}

operation LengthTest() : Unit {
let a1 = [One, Zero];
let a2 = [Zero, Zero, Zero];

AssertEqual(2, Length(a1));
AssertEqual(3, Length(a2));

let values = MapF(Length<Result>, [a1, a2]);
AssertEqual(2, values[0]);
AssertEqual(3, values[1]);
}

@Test("QuantumSimulator")
function CreateArrayWithPositiveSize() : Unit {
let xs = [true, size = 3];
AssertEqual([true, true, true], xs);
}

@Test("QuantumSimulator")
function CreateArrayWithZeroSize() : Unit {
let xs = [true, size = 0];
AssertEqual(0, Length(xs));
}

function CreateArrayWithNegativeSize() : Bool[] {
return [true, size = -1];
}

@Test("QuantumSimulator")
function CreateArrayWithSizeExpression() : Unit {
let n = 2;
let xs = [7, size = n + 1];
AssertEqual([7, 7, 7], xs);
}

@Test("QuantumSimulator")
function CreateArrayWithValueExpression() : Unit {
let x = "foo";
let xs = [x + "bar", size = 3];
AssertEqual(["foobar", "foobar", "foobar"], xs);
}

@Test("QuantumSimulator")
function SizedArrayShouldIncrementArrayItemRefCount() : Unit {
mutable item = [1];
let items = [item, size = 2];
set item w/= 0 <- 2;

AssertEqual([2], item);
AssertEqual([[1], [1]], items);
}

@Test("QuantumSimulator")
function ArrayOfArraysShouldCopyOnUpdate() : Unit {
mutable items = [[1], size = 2];
set items w/= 0 <- items[0] w/ 0 <- 2;

AssertEqual([[2], [1]], items);
}
}
33 changes: 0 additions & 33 deletions src/Simulation/Simulators.Tests/Circuits/Length.qs

This file was deleted.

8 changes: 8 additions & 0 deletions src/Simulation/Simulators.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,13 @@ public void CatchFail()
[Fact]
public void InternalCallables() =>
OperationsTestHelper.RunWithMultipleSimulators(s => Circuits.InternalCallablesTest.Run(s).Wait());

[Fact]
public void CreateArrayWithNegativeSize() =>
// TODO: This should throw ArgumentOutOfRangeException, but issue #536 causes the wrong exception type to be
// thrown: https://github.com/microsoft/qsharp-runtime/issues/536
Assert.ThrowsAny<Exception>(() =>
OperationsTestHelper.RunWithMultipleSimulators(simulator =>
Circuits.CreateArrayWithNegativeSize.Run(simulator).Wait()));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<CSharpGeneration>false</CSharpGeneration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\Common\Simulators.Test.props" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\Common\Simulators.Test.props" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\Common\Simulators.Test.props" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\Common\Simulators.Test.props" />

Expand All @@ -19,5 +19,3 @@
</ItemGroup>

</Project>


Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\Common\AssemblyCommon.props" />
<Import Project="..\Common\DebugSymbols.props" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">
<Import Project="..\TargetDefinitions\TargetPackages\Type1.Package.props" />

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\TargetDefinitions\TargetPackages\Type2.Package.props" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\TargetDefinitions\TargetPackages\Type3.Package.props" />

Expand All @@ -8,4 +8,3 @@
</PropertyGroup>

</Project>