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
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.14.2012122063-beta" />
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.15.210223045-alpha" />
</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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

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

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

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">
<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.14.2012122063-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.14.2012122063-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

<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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">
<Import Project="..\Common\AssemblyCommon.props" />
<Import Project="..\Common\DebugSymbols.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/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

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

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

<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/0.14.2012122063-beta">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210223045-alpha">

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

Expand Down