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/CSharpGeneration/SimulationCode.fs b/src/Simulation/CSharpGeneration/SimulationCode.fs
index 6b4029c0030..c80ae30877e 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,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])
diff --git a/src/Simulation/Core/QArray.cs b/src/Simulation/Core/QArray.cs
index a6e7ac4aac3..7f4eee399cc 100644
--- a/src/Simulation/Core/QArray.cs
+++ b/src/Simulation/Core/QArray.cs
@@ -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(newLengthInt);
- }
- else if (storage.Capacity < newLengthInt)
- {
- storage.Capacity = newLengthInt;
- }
- storage.AddRange(Enumerable.Repeat(Default.OfType(), newLengthInt - storage.Count));
+ var value = Default.OfType();
+ Extend(() => value, Convert.ToInt32(newLength - Length));
+ }
+
+ ///
+ /// Extends the length of the array by calling times to
+ /// supply a value for each new index.
+ ///
+ 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