From 6eaac54ede49977908c27f50b017399e1d764ba7 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Thu, 4 Feb 2021 14:33:12 -0800 Subject: [PATCH 01/14] Add C# gen for sized array --- src/Simulation/CSharpGeneration/SimulationCode.fs | 4 ++++ src/Simulation/Core/QArray.cs | 5 ++++- .../Simulators.Tests/Circuits/{Length.qs => Arrays.qs} | 7 +++++++ .../Tests.Microsoft.Quantum.Simulators.csproj | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) rename src/Simulation/Simulators.Tests/Circuits/{Length.qs => Arrays.qs} (81%) diff --git a/src/Simulation/CSharpGeneration/SimulationCode.fs b/src/Simulation/CSharpGeneration/SimulationCode.fs index 6b4029c0030..9fbd0b2cef7 100644 --- a/src/Simulation/CSharpGeneration/SimulationCode.fs +++ b/src/Simulation/CSharpGeneration/SimulationCode.fs @@ -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") @@ -591,6 +592,9 @@ module SimulationCode = // TODO: diagnostics. | _ -> failwith "" + and buildSizedArray value size = + ident "QArray" <.> (ident "Repeat", [ buildExpression value; buildExpression size ]) + and buildNewArray b count = let arrayType = (ArrayType b |> QArrayType).Value arrayType <.> (``ident`` "Create", [count |> buildExpression]) diff --git a/src/Simulation/Core/QArray.cs b/src/Simulation/Core/QArray.cs index a6e7ac4aac3..ffc8f635161 100644 --- a/src/Simulation/Core/QArray.cs +++ b/src/Simulation/Core/QArray.cs @@ -517,6 +517,10 @@ public static implicit operator QArray(QArray arg) => arg; } + public static class QArray + { + public static QArray Repeat(T element, long count) => throw new NotImplementedException(); + } /// /// This JsonConverter converts instances of IQArray['T] as QArray['T] @@ -557,4 +561,3 @@ public override bool CanConvert(Type objectType) => objectType.GetGenericTypeDefinition() == typeof(QArray<>); } } - diff --git a/src/Simulation/Simulators.Tests/Circuits/Length.qs b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs similarity index 81% rename from src/Simulation/Simulators.Tests/Circuits/Length.qs rename to src/Simulation/Simulators.Tests/Circuits/Arrays.qs index 1cdadf8552b..a9bbafb8024 100644 --- a/src/Simulation/Simulators.Tests/Circuits/Length.qs +++ b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs @@ -3,6 +3,7 @@ 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[] { @@ -30,4 +31,10 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits AssertEqual(2, values[0]); AssertEqual(3, values[1]); } + + @Test("QuantumSimulator") + function SizedArray() : Unit { + let xs = [true, size = 3]; + AssertEqual([true, true, true], xs); + } } diff --git a/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj b/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj index 0db807e3577..f5878dda92c 100644 --- a/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj +++ b/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj @@ -2,6 +2,10 @@ + + detailed + + From 5ad7f7543b973df61a6082e7fa7b208e5fbc203c Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Mon, 8 Feb 2021 15:02:52 -0800 Subject: [PATCH 02/14] Implement QArray.Repeat --- src/Simulation/Core/QArray.cs | 3 ++- src/Simulation/Simulators.Tests/Circuits/Arrays.qs | 12 ++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Simulation/Core/QArray.cs b/src/Simulation/Core/QArray.cs index ffc8f635161..25ff67c8e9f 100644 --- a/src/Simulation/Core/QArray.cs +++ b/src/Simulation/Core/QArray.cs @@ -519,7 +519,8 @@ public static implicit operator QArray(QArray arg) => public static class QArray { - public static QArray Repeat(T element, long count) => throw new NotImplementedException(); + public static QArray Repeat(T element, long count) => + new QArray(Enumerable.Repeat(element, Convert.ToInt32(count))); } /// diff --git a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs index a9bbafb8024..55d4d82be06 100644 --- a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs +++ b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs @@ -1,13 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits -{ +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[] { - + + operation MapF<'T, 'U>(mapper : ('T -> 'U), source : 'T[]) : 'U[] { mutable result = new 'U[Length(source)]; for (i in 0 .. Length(source) - 1) { @@ -17,10 +15,8 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits return result; } - - operation LengthTest () : Unit - { + operation LengthTest() : Unit { let a1 = [One, Zero]; let a2 = [Zero, Zero, Zero]; From ad01b34bd4f9ddcaf6f45d0af5da609a69bdc23b Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Mon, 8 Feb 2021 15:56:43 -0800 Subject: [PATCH 03/14] Add more array tests --- .../Simulators.Tests/Circuits/Arrays.qs | 26 ++++++++++++++++++- src/Simulation/Simulators.Tests/CoreTests.cs | 7 +++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs index 55d4d82be06..e21023d0fdf 100644 --- a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs +++ b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs @@ -29,8 +29,32 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits { } @Test("QuantumSimulator") - function SizedArray() : Unit { + 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); + } } diff --git a/src/Simulation/Simulators.Tests/CoreTests.cs b/src/Simulation/Simulators.Tests/CoreTests.cs index 8432e3c4494..3acaec6a61c 100644 --- a/src/Simulation/Simulators.Tests/CoreTests.cs +++ b/src/Simulation/Simulators.Tests/CoreTests.cs @@ -324,5 +324,12 @@ public void CatchFail() [Fact] public void InternalCallables() => OperationsTestHelper.RunWithMultipleSimulators(s => Circuits.InternalCallablesTest.Run(s).Wait()); + + [Fact] + public void CreateArrayWithNegativeSize() => + // TODO: More specific exception type. + Assert.ThrowsAny(() => + OperationsTestHelper.RunWithMultipleSimulators(simulator => + Circuits.CreateArrayWithNegativeSize.Run(simulator).Wait())); } } From b41ed0f41edf984f8ff11383c23c9f252a0883a0 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Mon, 8 Feb 2021 16:00:43 -0800 Subject: [PATCH 04/14] Add doc comments --- src/Simulation/Core/QArray.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Simulation/Core/QArray.cs b/src/Simulation/Core/QArray.cs index 25ff67c8e9f..7775fb8ec1f 100644 --- a/src/Simulation/Core/QArray.cs +++ b/src/Simulation/Core/QArray.cs @@ -232,8 +232,8 @@ public QArray(params T[] collection) } /// - /// Creates an array of size given by capacity and default-initializes - /// array elements. Uses C# keyword default 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#. /// public static QArray Create(long capacity) => new QArray { @@ -517,8 +517,14 @@ public static implicit operator QArray(QArray arg) => arg; } + /// + /// Contains static methods for creating s. + /// public static class QArray { + /// + /// Creates an array that contains repeated times. + /// public static QArray Repeat(T element, long count) => new QArray(Enumerable.Repeat(element, Convert.ToInt32(count))); } From d21c679e8dbc474837f2ce0f1cbfa67dcde5f2da Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Mon, 8 Feb 2021 17:03:29 -0800 Subject: [PATCH 05/14] More efficient array creation --- src/Simulation/Core/QArray.cs | 36 ++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/Simulation/Core/QArray.cs b/src/Simulation/Core/QArray.cs index 7775fb8ec1f..25b3a1f54d5 100644 --- a/src/Simulation/Core/QArray.cs +++ b/src/Simulation/Core/QArray.cs @@ -133,18 +133,27 @@ public void UnsafeSetElement(long index, T value) public void RemoveReference() => refCount--; - public void Extend(long newLength) + public void Extend(long newLength) => + Extend(Default.OfType(), Convert.ToInt32(newLength - Length)); + + /// + /// Extends the length of the array by adding occurrences of + /// . + /// + internal void Extend(T value, long count) { - var newLengthInt = Convert.ToInt32(newLength); + var intCount = Convert.ToInt32(count); + var total = Convert.ToInt32(Length) + intCount; if (storage is null) { - storage = new List(newLengthInt); + storage = new List(total); } - else if (storage.Capacity < newLengthInt) + else if (storage.Capacity < total) { - storage.Capacity = newLengthInt; + storage.Capacity = total; } - storage.AddRange(Enumerable.Repeat(Default.OfType(), newLengthInt - storage.Count)); + + storage.AddRange(Enumerable.Repeat(value, intCount)); } } @@ -241,6 +250,16 @@ public QArray(params T[] collection) Length = capacity }; + /// + /// Creates an array that contains repeated times. + /// + internal static QArray Repeat(T value, long count) + { + var array = new QArray { Length = count }; + array.storage.Extend(value, count); + return array; + } + /// /// Creates a copy of this array. /// @@ -523,10 +542,9 @@ public static implicit operator QArray(QArray arg) => public static class QArray { /// - /// Creates an array that contains repeated times. + /// Creates an array that contains repeated times. /// - public static QArray Repeat(T element, long count) => - new QArray(Enumerable.Repeat(element, Convert.ToInt32(count))); + public static QArray Repeat(T value, long count) => QArray.Repeat(value, count); } /// From eeb5715313be1fdfdd18a8c90a0ff7513a5e7354 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Mon, 8 Feb 2021 17:09:58 -0800 Subject: [PATCH 06/14] Remove TODO, leave exception type untested --- src/Simulation/Simulators.Tests/CoreTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Simulation/Simulators.Tests/CoreTests.cs b/src/Simulation/Simulators.Tests/CoreTests.cs index 3acaec6a61c..80b6e501cc8 100644 --- a/src/Simulation/Simulators.Tests/CoreTests.cs +++ b/src/Simulation/Simulators.Tests/CoreTests.cs @@ -327,7 +327,6 @@ public void InternalCallables() => [Fact] public void CreateArrayWithNegativeSize() => - // TODO: More specific exception type. Assert.ThrowsAny(() => OperationsTestHelper.RunWithMultipleSimulators(simulator => Circuits.CreateArrayWithNegativeSize.Run(simulator).Wait())); From 3ac7a0b765f63eeab1377c0315794b8e2bba2041 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Mon, 8 Feb 2021 17:11:25 -0800 Subject: [PATCH 07/14] Formatting --- src/Simulation/Simulators.Tests/Circuits/Arrays.qs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs index e21023d0fdf..af7bf7c78f7 100644 --- a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs +++ b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs @@ -7,7 +7,6 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits { 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; From 5158e4a39b375d40cc30dc60f93c3eb71d62274e Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 10 Feb 2021 13:44:27 -0800 Subject: [PATCH 08/14] Update packages --- .../CSharpGeneration/Microsoft.Quantum.CSharpGeneration.fsproj | 2 +- ....Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj | 2 +- src/Simulation/QSharpCore/Microsoft.Quantum.QSharp.Core.csproj | 2 +- .../QSharpFoundation/Microsoft.Quantum.QSharp.Foundation.csproj | 2 +- .../TestProjects/HoneywellExe/HoneywellExe.csproj | 2 +- .../TestProjects/IntrinsicTests/IntrinsicTests.csproj | 2 +- .../Simulators.Tests/TestProjects/IonQExe/IonQExe.csproj | 2 +- .../TestProjects/Library with Spaces/Library with Spaces.csproj | 2 +- .../Simulators.Tests/TestProjects/Library1/Library1.csproj | 2 +- .../Simulators.Tests/TestProjects/Library2/Library2.csproj | 2 +- .../Simulators.Tests/TestProjects/QCIExe/QCIExe.csproj | 2 +- .../Simulators.Tests/TestProjects/QSharpExe/QSharpExe.csproj | 2 +- .../TestProjects/TargetedExe/TargetedExe.csproj | 2 +- .../Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj | 2 +- .../Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj | 2 +- .../Tests.Microsoft.Quantum.Simulators.Type1.csproj | 2 +- .../Tests.Microsoft.Quantum.Simulators.Type2.csproj | 2 +- .../Tests.Microsoft.Quantum.Simulators.Type3.csproj | 2 +- src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj | 2 +- src/Simulation/Type1Core/Microsoft.Quantum.Type1.Core.csproj | 2 +- src/Simulation/Type2Core/Microsoft.Quantum.Type2.Core.csproj | 2 +- src/Simulation/Type3Core/Microsoft.Quantum.Type3.Core.csproj | 2 +- 22 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Simulation/CSharpGeneration/Microsoft.Quantum.CSharpGeneration.fsproj b/src/Simulation/CSharpGeneration/Microsoft.Quantum.CSharpGeneration.fsproj index a78b19dd7c0..0a8cfcfa1c3 100644 --- a/src/Simulation/CSharpGeneration/Microsoft.Quantum.CSharpGeneration.fsproj +++ b/src/Simulation/CSharpGeneration/Microsoft.Quantum.CSharpGeneration.fsproj @@ -22,7 +22,7 @@ - + diff --git a/src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj b/src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj index 7e8e61d22f6..ebe74577f64 100644 --- a/src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj +++ b/src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/QSharpCore/Microsoft.Quantum.QSharp.Core.csproj b/src/Simulation/QSharpCore/Microsoft.Quantum.QSharp.Core.csproj index a80ac901a6b..ac98ed0cbba 100644 --- a/src/Simulation/QSharpCore/Microsoft.Quantum.QSharp.Core.csproj +++ b/src/Simulation/QSharpCore/Microsoft.Quantum.QSharp.Core.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/QSharpFoundation/Microsoft.Quantum.QSharp.Foundation.csproj b/src/Simulation/QSharpFoundation/Microsoft.Quantum.QSharp.Foundation.csproj index 69118c12e1e..0c593afc8a5 100644 --- a/src/Simulation/QSharpFoundation/Microsoft.Quantum.QSharp.Foundation.csproj +++ b/src/Simulation/QSharpFoundation/Microsoft.Quantum.QSharp.Foundation.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Simulators.Tests/TestProjects/HoneywellExe/HoneywellExe.csproj b/src/Simulation/Simulators.Tests/TestProjects/HoneywellExe/HoneywellExe.csproj index 5384a2f6901..305c9c85ef3 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/HoneywellExe/HoneywellExe.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/HoneywellExe/HoneywellExe.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/IntrinsicTests.csproj b/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/IntrinsicTests.csproj index 9075b363ba0..32acf2561d3 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/IntrinsicTests.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/IntrinsicTests.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 diff --git a/src/Simulation/Simulators.Tests/TestProjects/IonQExe/IonQExe.csproj b/src/Simulation/Simulators.Tests/TestProjects/IonQExe/IonQExe.csproj index e0b14d1bfa7..d60fa52b246 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/IonQExe/IonQExe.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/IonQExe/IonQExe.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj b/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj index 556eda7eaad..06b9464ee83 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 false diff --git a/src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj b/src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj index 2479cfcbd73..4ce1b17ce64 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 diff --git a/src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj b/src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj index 2479cfcbd73..4ce1b17ce64 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 diff --git a/src/Simulation/Simulators.Tests/TestProjects/QCIExe/QCIExe.csproj b/src/Simulation/Simulators.Tests/TestProjects/QCIExe/QCIExe.csproj index 6badef1cce5..847c0c804de 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/QCIExe/QCIExe.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/QCIExe/QCIExe.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/src/Simulation/Simulators.Tests/TestProjects/QSharpExe/QSharpExe.csproj b/src/Simulation/Simulators.Tests/TestProjects/QSharpExe/QSharpExe.csproj index b6ff9d91069..500cc3a4b76 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/QSharpExe/QSharpExe.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/QSharpExe/QSharpExe.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/src/Simulation/Simulators.Tests/TestProjects/TargetedExe/TargetedExe.csproj b/src/Simulation/Simulators.Tests/TestProjects/TargetedExe/TargetedExe.csproj index 91bf1cd73be..f14e6cef64b 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/TargetedExe/TargetedExe.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/TargetedExe/TargetedExe.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/src/Simulation/Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj index bd33c8fe071..f16fd41c26c 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 diff --git a/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj b/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj index 59f0a737128..7d6c9256df1 100644 --- a/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj +++ b/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Simulators.Type1.Tests/Tests.Microsoft.Quantum.Simulators.Type1.csproj b/src/Simulation/Simulators.Type1.Tests/Tests.Microsoft.Quantum.Simulators.Type1.csproj index 9e9d51b6c22..80077b83787 100644 --- a/src/Simulation/Simulators.Type1.Tests/Tests.Microsoft.Quantum.Simulators.Type1.csproj +++ b/src/Simulation/Simulators.Type1.Tests/Tests.Microsoft.Quantum.Simulators.Type1.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Simulators.Type2.Tests/Tests.Microsoft.Quantum.Simulators.Type2.csproj b/src/Simulation/Simulators.Type2.Tests/Tests.Microsoft.Quantum.Simulators.Type2.csproj index 094f95bf64b..698f53400f5 100644 --- a/src/Simulation/Simulators.Type2.Tests/Tests.Microsoft.Quantum.Simulators.Type2.csproj +++ b/src/Simulation/Simulators.Type2.Tests/Tests.Microsoft.Quantum.Simulators.Type2.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Simulators.Type3.Tests/Tests.Microsoft.Quantum.Simulators.Type3.csproj b/src/Simulation/Simulators.Type3.Tests/Tests.Microsoft.Quantum.Simulators.Type3.csproj index 8bd1d9a7828..d66eb0ee909 100644 --- a/src/Simulation/Simulators.Type3.Tests/Tests.Microsoft.Quantum.Simulators.Type3.csproj +++ b/src/Simulation/Simulators.Type3.Tests/Tests.Microsoft.Quantum.Simulators.Type3.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj b/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj index df891ab5eb0..2b2cf28cb92 100644 --- a/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj +++ b/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Type1Core/Microsoft.Quantum.Type1.Core.csproj b/src/Simulation/Type1Core/Microsoft.Quantum.Type1.Core.csproj index f21cd835b30..0519a40b106 100644 --- a/src/Simulation/Type1Core/Microsoft.Quantum.Type1.Core.csproj +++ b/src/Simulation/Type1Core/Microsoft.Quantum.Type1.Core.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Type2Core/Microsoft.Quantum.Type2.Core.csproj b/src/Simulation/Type2Core/Microsoft.Quantum.Type2.Core.csproj index 9bec9e8d8f2..5a3f78e20a5 100644 --- a/src/Simulation/Type2Core/Microsoft.Quantum.Type2.Core.csproj +++ b/src/Simulation/Type2Core/Microsoft.Quantum.Type2.Core.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Type3Core/Microsoft.Quantum.Type3.Core.csproj b/src/Simulation/Type3Core/Microsoft.Quantum.Type3.Core.csproj index fb0d25c2e8e..7d9715f3020 100644 --- a/src/Simulation/Type3Core/Microsoft.Quantum.Type3.Core.csproj +++ b/src/Simulation/Type3Core/Microsoft.Quantum.Type3.Core.csproj @@ -1,4 +1,4 @@ - + From 04743187346976cfba09a3b18d47aa524d2be48f Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 10 Feb 2021 13:58:36 -0800 Subject: [PATCH 09/14] Add tests for ref counts in array-of-arrays --- .../Simulators.Tests/Circuits/Arrays.qs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs index af7bf7c78f7..241505bbb0e 100644 --- a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs +++ b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs @@ -56,4 +56,22 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits { let xs = [x + "bar", size = 3]; AssertEqual(["foobar", "foobar", "foobar"], xs); } + + @Test("QuantumSimulator") + function SizedArrayShouldIncrementArrayItemRefCount() : Unit { + mutable item = [1]; + let items = [item, size = 1]; + set item w/= 0 <- 2; + + AssertEqual([2], item); + AssertEqual([[1]], items); + } + + @Test("QuantumSimulator") + function ArrayOfArraysShouldHaveCorrectReferenceCounts() : Unit { + mutable items = [[1], size = 2]; + set items w/= 0 <- items[0] w/ 0 <- 2; + + AssertEqual([[2], [1]], items); + } } From 6fe98bf9c0c9fef2c6ab14fa895191a200cda8f9 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 10 Feb 2021 14:22:39 -0800 Subject: [PATCH 10/14] Capture value expression --- src/Simulation/CSharpGeneration/SimulationCode.fs | 2 +- src/Simulation/Simulators.Tests/Circuits/Arrays.qs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Simulation/CSharpGeneration/SimulationCode.fs b/src/Simulation/CSharpGeneration/SimulationCode.fs index 9fbd0b2cef7..f673c3ae4e2 100644 --- a/src/Simulation/CSharpGeneration/SimulationCode.fs +++ b/src/Simulation/CSharpGeneration/SimulationCode.fs @@ -593,7 +593,7 @@ module SimulationCode = | _ -> failwith "" and buildSizedArray value size = - ident "QArray" <.> (ident "Repeat", [ buildExpression value; buildExpression size ]) + ident "QArray" <.> (ident "Repeat", [ captureExpression value; buildExpression size ]) and buildNewArray b count = let arrayType = (ArrayType b |> QArrayType).Value diff --git a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs index 241505bbb0e..6af632a5451 100644 --- a/src/Simulation/Simulators.Tests/Circuits/Arrays.qs +++ b/src/Simulation/Simulators.Tests/Circuits/Arrays.qs @@ -60,15 +60,15 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits { @Test("QuantumSimulator") function SizedArrayShouldIncrementArrayItemRefCount() : Unit { mutable item = [1]; - let items = [item, size = 1]; + let items = [item, size = 2]; set item w/= 0 <- 2; AssertEqual([2], item); - AssertEqual([[1]], items); + AssertEqual([[1], [1]], items); } @Test("QuantumSimulator") - function ArrayOfArraysShouldHaveCorrectReferenceCounts() : Unit { + function ArrayOfArraysShouldCopyOnUpdate() : Unit { mutable items = [[1], size = 2]; set items w/= 0 <- items[0] w/ 0 <- 2; From e7bdfd946028cc31dc9ab1eeba5ca605815f133f Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 10 Feb 2021 15:46:27 -0800 Subject: [PATCH 11/14] Undo QscVerbosity --- .../Tests.Microsoft.Quantum.Simulators.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj b/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj index 7d6c9256df1..27f152a4366 100644 --- a/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj +++ b/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj @@ -2,10 +2,6 @@ - - detailed - - From 51f1aaf53ee8ea2c249ff9f14eb70580921cd7fd Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 10 Feb 2021 16:04:14 -0800 Subject: [PATCH 12/14] Clean up QArrayInner.Extend --- src/Simulation/Core/QArray.cs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Simulation/Core/QArray.cs b/src/Simulation/Core/QArray.cs index 25b3a1f54d5..322825c03f8 100644 --- a/src/Simulation/Core/QArray.cs +++ b/src/Simulation/Core/QArray.cs @@ -142,18 +142,10 @@ public void Extend(long newLength) => /// internal void Extend(T value, long count) { - var intCount = Convert.ToInt32(count); - var total = Convert.ToInt32(Length) + intCount; - if (storage is null) - { - storage = new List(total); - } - else if (storage.Capacity < total) - { - storage.Capacity = total; - } - - storage.AddRange(Enumerable.Repeat(value, intCount)); + var total = Convert.ToInt32(Length + count); + storage ??= new List(total); + storage.Capacity = Math.Max(storage.Capacity, total); + storage.AddRange(Enumerable.Repeat(value, Convert.ToInt32(count))); } } From 588d49983c7b8222ef87c5be2d2ecc0ca7ae8678 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Fri, 12 Feb 2021 13:10:21 -0800 Subject: [PATCH 13/14] Increment ref count N times --- .../CSharpGeneration/SimulationCode.fs | 3 +- src/Simulation/Core/QArray.cs | 29 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Simulation/CSharpGeneration/SimulationCode.fs b/src/Simulation/CSharpGeneration/SimulationCode.fs index f673c3ae4e2..c80ae30877e 100644 --- a/src/Simulation/CSharpGeneration/SimulationCode.fs +++ b/src/Simulation/CSharpGeneration/SimulationCode.fs @@ -593,7 +593,8 @@ module SimulationCode = | _ -> failwith "" and buildSizedArray value size = - ident "QArray" <.> (ident "Repeat", [ captureExpression value; buildExpression size ]) + let supplier = ``() =>`` [] (captureExpression value) :> ExpressionSyntax + ident "QArray" <.> (ident "Filled", [ supplier; buildExpression size ]) and buildNewArray b count = let arrayType = (ArrayType b |> QArrayType).Value diff --git a/src/Simulation/Core/QArray.cs b/src/Simulation/Core/QArray.cs index 322825c03f8..7f4eee399cc 100644 --- a/src/Simulation/Core/QArray.cs +++ b/src/Simulation/Core/QArray.cs @@ -133,19 +133,24 @@ public void UnsafeSetElement(long index, T value) public void RemoveReference() => refCount--; - public void Extend(long newLength) => - Extend(Default.OfType(), Convert.ToInt32(newLength - Length)); + public void Extend(long newLength) + { + var value = Default.OfType(); + Extend(() => value, Convert.ToInt32(newLength - Length)); + } /// - /// Extends the length of the array by adding occurrences of - /// . + /// Extends the length of the array by calling times to + /// supply a value for each new index. /// - internal void Extend(T value, long count) + internal void Extend(Func supplier, long count) { var total = Convert.ToInt32(Length + count); storage ??= new List(total); storage.Capacity = Math.Max(storage.Capacity, total); - storage.AddRange(Enumerable.Repeat(value, Convert.ToInt32(count))); + storage.AddRange(Enumerable + .Repeat(null, Convert.ToInt32(count)) + .Select(_ => supplier())); } } @@ -243,12 +248,13 @@ public QArray(params T[] collection) }; /// - /// Creates an array that contains repeated times. + /// Creates an array filled by calling times to supply a + /// value for each index. /// - internal static QArray Repeat(T value, long count) + internal static QArray Filled(Func supplier, long count) { var array = new QArray { Length = count }; - array.storage.Extend(value, count); + array.storage.Extend(supplier, count); return array; } @@ -534,9 +540,10 @@ public static implicit operator QArray(QArray arg) => public static class QArray { /// - /// Creates an array that contains repeated times. + /// Creates an array filled by calling times to supply a + /// value for each index. /// - public static QArray Repeat(T value, long count) => QArray.Repeat(value, count); + public static QArray Filled(Func supplier, long count) => QArray.Filled(supplier, count); } /// From 71102e6ea65c917fe27e6c4467edef191be2ca4b Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Thu, 25 Feb 2021 17:00:12 -0800 Subject: [PATCH 14/14] Add comment about exception type --- src/Simulation/Simulators.Tests/CoreTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Simulation/Simulators.Tests/CoreTests.cs b/src/Simulation/Simulators.Tests/CoreTests.cs index 80b6e501cc8..2a337c041f7 100644 --- a/src/Simulation/Simulators.Tests/CoreTests.cs +++ b/src/Simulation/Simulators.Tests/CoreTests.cs @@ -327,6 +327,8 @@ public void InternalCallables() => [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(() => OperationsTestHelper.RunWithMultipleSimulators(simulator => Circuits.CreateArrayWithNegativeSize.Run(simulator).Wait()));