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
6 changes: 4 additions & 2 deletions src/QsCompiler/Core/SymbolTable/NamespaceManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,8 +1524,10 @@ type NamespaceManager(syncRoot: IReaderWriterLock,
| StringLiteral (s, _) -> hash (6, s)
| ValueTuple vs -> hash (7, (vs |> Seq.map NamespaceManager.ExpressionHash |> Seq.toList))
| ValueArray vs -> hash (8, (vs |> Seq.map NamespaceManager.ExpressionHash |> Seq.toList))
| NewArray (bt, idx) -> hash (9, NamespaceManager.TypeHash bt, NamespaceManager.ExpressionHash idx)
| Identifier (GlobalCallable c, _) -> hash (10, c.Namespace, c.Name)
| SizedArray (value, size) ->
hash (9, NamespaceManager.ExpressionHash value, NamespaceManager.ExpressionHash size)
| NewArray (bt, idx) -> hash (10, NamespaceManager.TypeHash bt, NamespaceManager.ExpressionHash idx)
| Identifier (GlobalCallable c, _) -> hash (11, c.Namespace, c.Name)
| kind -> JsonConvert.SerializeObject kind |> hash

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ type ExpressionKindTransformationBase internal (options: TransformationOptions,
let bt, idx = this.Types.OnType bt, this.Expressions.OnTypedExpression idx
NewArray |> Node.BuildOr InvalidExpr (bt, idx)

abstract OnSizedArray: TypedExpression * TypedExpression -> ExpressionKind
abstract OnSizedArray: value:TypedExpression * size:TypedExpression -> ExpressionKind

default this.OnSizedArray(value, size) =
let value = this.Expressions.OnTypedExpression value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,48 @@ and private ConstantPropagationStatementKinds(parent: ConstantPropagation, calla
| ValueTuple _
| ValueArray _
| RangeLiteral _
| SizedArray _
| NewArray _ -> true
| CallLikeExpression ({ Expression = Identifier (GlobalCallable qualName, _) }, _) when (callables.[qualName])
| CallLikeExpression ({ Expression = Identifier (GlobalCallable name, _) }, _) when callables.[name]
.Kind = TypeConstructor -> true
| a when TypedExpression.IsPartialApplication a -> true
| _ -> false
| _ when TypedExpression.IsPartialApplication ex.Expression -> true
| UnitValue
| IntLiteral _
| BigIntLiteral _
| DoubleLiteral _
| BoolLiteral _
| StringLiteral _
| ResultLiteral _
| PauliLiteral _
| NEG _
| NOT _
| BNOT _
| ADD _
| SUB _
| MUL _
| DIV _
| MOD _
| POW _
| EQ _
| NEQ _
| LT _
| LTE _
| GT _
| GTE _
| AND _
| OR _
| BOR _
| BAND _
| BXOR _
| LSHIFT _
| RSHIFT _
| CONDITIONAL _
| CopyAndUpdate _
| AdjointApplication _
| ControlledApplication _
| CallLikeExpression _
| MissingExpr _
| InvalidExpr _ -> false
&& Seq.forall id sub)

expr.Fold folder
Expand Down
19 changes: 14 additions & 5 deletions src/QsCompiler/Optimizations/Utils/Evaluation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,21 @@ and private ExpressionKindEvaluator(parent,
|> ValueArray
| _ -> ArrayItem(arr, idx)

override this.OnNewArray(bt, idx) =
let idx = this.simplify idx
override this.OnSizedArray(value, size) =
let value = this.simplify value
let size = this.simplify size

match idx.Expression with
| IntLiteral i -> constructNewArray bt.Resolution (safeCastInt64 i) |? NewArray(bt, idx)
| _ -> NewArray(bt, idx)
match size.Expression with
| IntLiteral i when isLiteral callables value -> constructArray (safeCastInt64 i) value
| _ -> SizedArray(value, size)

override this.OnNewArray(itemType, length) =
let length = this.simplify length

match length.Expression with
| IntLiteral i -> defaultValue itemType.Resolution |> Option.map (safeCastInt64 i |> constructArray)
| _ -> None
|> Option.defaultValue (NewArray(itemType, length))

override this.OnCopyAndUpdateExpression(lhs, accEx, rhs) =
let lhs, accEx, rhs = this.simplify (lhs, accEx, rhs)
Expand Down
59 changes: 47 additions & 12 deletions src/QsCompiler/Optimizations/Utils/HelperFunctions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,46 @@ let rec internal isLiteral (callables: IDictionary<QsQualifiedName, QsCallable>)
| Identifier (GlobalCallable _, _)
| ValueTuple _
| ValueArray _
| SizedArray _
| RangeLiteral _
| NewArray _ -> true
| Identifier _ when ex.ResolvedType.Resolution = Qubit -> true
| CallLikeExpression ({ Expression = Identifier (GlobalCallable qualName, _) }, _) when (callables.[qualName])
.Kind = TypeConstructor -> true
| a when TypedExpression.IsPartialApplication a -> true
| _ -> false
| CallLikeExpression ({ Expression = Identifier (GlobalCallable name, _) }, _) when callables.[name].Kind = TypeConstructor ->
true
| _ when TypedExpression.IsPartialApplication ex.Expression -> true
| Identifier _
| ArrayItem _
| NamedItem _
| NEG _
| NOT _
| BNOT _
| ADD _
| SUB _
| MUL _
| DIV _
| MOD _
| POW _
| EQ _
| NEQ _
| LT _
| LTE _
| GT _
| GTE _
| AND _
| OR _
| BOR _
| BAND _
| BXOR _
| LSHIFT _
| RSHIFT _
| CONDITIONAL _
| CopyAndUpdate _
| UnwrapApplication _
| AdjointApplication _
| ControlledApplication _
| CallLikeExpression _
| MissingExpr
| InvalidExpr -> false
&& Seq.forall id sub

expr.Fold folder
Expand Down Expand Up @@ -195,16 +228,17 @@ let internal wrapStmt (stmt: QsStatementKind): QsStatement =
QsStatement.New QsComments.Empty Null (stmt, LocalDeclarations.New symbolDecl)


/// Returns a new array of the given type and length.
/// <summary>
/// Returns a new array containing the given value repeated <paramref name="length"/> times.
/// Returns None if the type doesn't have a default value as an expression.
let rec internal constructNewArray (bt: TypeKind) (length: int): ExprKind option =
defaultValue bt
|> Option.map (fun x -> ImmutableArray.CreateRange(List.replicate length (wrapExpr bt x)) |> ValueArray)
/// </summary>
let internal constructArray length =
List.replicate length >> ImmutableArray.CreateRange >> ValueArray

/// Returns the default value for a given type (from Q# documentation).
/// Returns None for types whose default values are not representable as expressions.
and internal defaultValue (bt: TypeKind): ExprKind option =
match bt with
let rec internal defaultValue (typeKind: TypeKind) =
match typeKind with
| UnitType -> UnitValue |> Some
| Int -> IntLiteral 0L |> Some
| BigInt -> BigIntLiteral BigInteger.Zero |> Some
Expand All @@ -213,9 +247,10 @@ and internal defaultValue (bt: TypeKind): ExprKind option =
| String -> StringLiteral("", ImmutableArray.Empty) |> Some
| Pauli -> PauliLiteral PauliI |> Some
| Result -> ResultLiteral Zero |> Some
| Range -> RangeLiteral(wrapExpr Int (IntLiteral 1L), wrapExpr Int (IntLiteral 0L)) |> Some
| ArrayType t -> constructNewArray t.Resolution 0
| Range -> RangeLiteral(IntLiteral 1L |> wrapExpr Int, IntLiteral 0L |> wrapExpr Int) |> Some
| ArrayType item -> defaultValue item.Resolution |> Option.map (constructArray 0)
| _ -> None
|> Option.map (wrapExpr typeKind)


/// Returns true if the expression contains missing expressions.
Expand Down
11 changes: 11 additions & 0 deletions src/QsCompiler/Transformations/QsharpCodeOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,17 @@ public override QsExpressionKind OnValueArray(ImmutableArray<TypedExpression> vs
return QsExpressionKind.NewValueArray(vs);
}

/// <inheritdoc/>
public override QsExpressionKind OnSizedArray(TypedExpression value, TypedExpression size)
{
var valueOutput = this.Recur(int.MinValue, value);
var sizeOutput = this.Recur(int.MinValue, size);
this.Output = $"[{valueOutput}, size = {sizeOutput}]";

this.currentPrecedence = int.MaxValue;
return QsExpressionKind.NewSizedArray(value, size);
}

/// <inheritdoc/>
public override QsExpressionKind OnNewArray(ResolvedType bt, TypedExpression idx)
{
Expand Down