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
4 changes: 2 additions & 2 deletions src/QsCompiler/Compiler/CompilationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ public CompilationLoader(SourceLoader loadSources, ReferenceLoader loadReference

if (this.Config.IsExecutable && this.CompilationOutput?.EntryPoints.Length == 0)
{
this.Logger?.Log(WarningCode.MissingEntryPoint, Array.Empty<string>());

if (this.Config.RuntimeCapabilities == RuntimeCapabilities.Unknown) this.Logger?.Log(WarningCode.MissingEntryPoint, Array.Empty<string>());
else this.LogAndUpdate(ref this.CompilationStatus.Validation, ErrorCode.MissingEntryPoint, Array.Empty<string>());
}

// executing the specified rewrite steps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.Quantum.QsCompiler.DataTypes;
using Microsoft.Quantum.QsCompiler.SyntaxTree;
using Microsoft.Quantum.QsCompiler.Transformations.ClassicallyControlled;

Expand Down
15 changes: 13 additions & 2 deletions src/QsCompiler/Core/NamespaceTransformation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type NamespaceTransformationBase internal (options : TransformationOptions, _int
abstract member OnAttribute : QsDeclarationAttribute -> QsDeclarationAttribute
default this.OnAttribute att = att

abstract member OnItemName : NonNullable<string> -> NonNullable<string>
default this.OnItemName name = name

abstract member OnTypeItems : QsTuple<QsTypeItem> -> QsTuple<QsTypeItem>
default this.OnTypeItems tItem =
match tItem with
Expand All @@ -63,10 +66,17 @@ type NamespaceTransformationBase internal (options : TransformationOptions, _int
QsTupleItem << Anonymous |> Node.BuildOr original t
| QsTupleItem (Named item) as original ->
let loc = item.Position, item.Range
let name = this.OnItemName item.VariableName
let t = this.Statements.Expressions.Types.OnType item.Type
let info = this.Statements.Expressions.OnExpressionInformation item.InferredInformation
QsTupleItem << Named << LocalVariableDeclaration<_>.New info.IsMutable |> Node.BuildOr original (loc, item.VariableName, t, info.HasLocalQuantumDependency)
QsTupleItem << Named << LocalVariableDeclaration<_>.New info.IsMutable |> Node.BuildOr original (loc, name, t, info.HasLocalQuantumDependency)

abstract member OnArgumentName : QsLocalSymbol -> QsLocalSymbol
default this.OnArgumentName arg =
match arg with
| ValidName name -> ValidName |> Node.BuildOr arg (this.Statements.OnVariableName name)
| InvalidName -> arg

abstract member OnArgumentTuple : QsArgumentTuple -> QsArgumentTuple
default this.OnArgumentTuple arg =
match arg with
Expand All @@ -75,9 +85,10 @@ type NamespaceTransformationBase internal (options : TransformationOptions, _int
QsTuple |> Node.BuildOr original transformed
| QsTupleItem item as original ->
let loc = item.Position, item.Range
let name = this.OnArgumentName item.VariableName
let t = this.Statements.Expressions.Types.OnType item.Type
let info = this.Statements.Expressions.OnExpressionInformation item.InferredInformation
QsTupleItem << LocalVariableDeclaration<_>.New info.IsMutable |> Node.BuildOr original (loc, item.VariableName, t, info.HasLocalQuantumDependency)
QsTupleItem << LocalVariableDeclaration<_>.New info.IsMutable |> Node.BuildOr original (loc, name, t, info.HasLocalQuantumDependency)

abstract member OnSignature : ResolvedSignature -> ResolvedSignature
default this.OnSignature (s : ResolvedSignature) =
Expand Down
8 changes: 6 additions & 2 deletions src/QsCompiler/Core/StatementTransformation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ and StatementTransformationBase internal (options : TransformationOptions, _inte
abstract member OnLocation : QsNullable<QsLocation> -> QsNullable<QsLocation>
default this.OnLocation loc = loc

abstract member OnVariableName : NonNullable<string> -> NonNullable<string>
default this.OnVariableName name = name

/// If DisableRebuild is set to true, this method won't walk the local variables declared by the statement.
abstract member OnLocalDeclarations : LocalDeclarations -> LocalDeclarations
default this.OnLocalDeclarations decl =
let onLocalVariableDeclaration (local : LocalVariableDeclaration<NonNullable<string>>) =
let loc = local.Position, local.Range
let info = this.Expressions.OnExpressionInformation local.InferredInformation
let name = this.OnVariableName local.VariableName
let varType = this.Expressions.Types.OnType local.Type
LocalVariableDeclaration.New info.IsMutable (loc, local.VariableName, varType, info.HasLocalQuantumDependency)
let info = this.Expressions.OnExpressionInformation local.InferredInformation
LocalVariableDeclaration.New info.IsMutable (loc, name, varType, info.HasLocalQuantumDependency)
let variableDeclarations = decl.Variables |> Seq.map onLocalVariableDeclaration
LocalDeclarations.New << ImmutableArray.CreateRange |> Node.BuildOr decl variableDeclarations

Expand Down
18 changes: 10 additions & 8 deletions src/QsCompiler/DataStructures/Diagnostics.fs
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,15 @@ type ErrorCode =
| InnerTupleInEntryPointArgument = 6234
| ArrayOfArrayInEntryPointArgument = 6235
| MultipleEntryPoints = 6236
| InvalidEntryPointSpecialization = 6237
| DuplicateEntryPointArgumentName = 6238
| EntryPointInLibrary = 6239
| InvalidTestAttributePlacement = 6240
| InvalidExecutionTargetForTest = 6241
| ExpectingFullNameAsAttributeArgument = 6242
| AttributeInvalidOnSpecialization = 6243
| AttributeInvalidOnCallable = 6244
| MissingEntryPoint = 6237
| InvalidEntryPointSpecialization = 6238
| DuplicateEntryPointArgumentName = 6239
| EntryPointInLibrary = 6240
| InvalidTestAttributePlacement = 6241
| InvalidExecutionTargetForTest = 6242
| ExpectingFullNameAsAttributeArgument = 6243
| AttributeInvalidOnSpecialization = 6244
| AttributeInvalidOnCallable = 6245

| TypeMismatchInReturn = 6301
| TypeMismatchInValueUpdate = 6302
Expand Down Expand Up @@ -612,6 +613,7 @@ type DiagnosticItem =
| ErrorCode.InnerTupleInEntryPointArgument -> "Anonymous tuple items or arrays of tuples are not supported in entry point arguments. All items need to be named."
| ErrorCode.ArrayOfArrayInEntryPointArgument -> "Multi-dimensional arrays are not supported in entry point arguments."
| ErrorCode.MultipleEntryPoints -> "Invalid entry point. An entry point {0} already exists in {1}."
| ErrorCode.MissingEntryPoint -> "Missing entry point. Execution on a quantum processor requires that a Q# entry point is defined using the @EntryPoint() attribute. Any C# driver code should be defined in a separate project."
| ErrorCode.InvalidEntryPointSpecialization -> "Entry points cannot have any other specializations besides the default body."
| ErrorCode.DuplicateEntryPointArgumentName -> "Invalid name for entry point argument. A similar argument name is already in use."
| ErrorCode.EntryPointInLibrary -> "Invalid entry point. Only executable Q# projects can have entry points."
Expand Down
3 changes: 3 additions & 0 deletions src/QsCompiler/DataStructures/ReservedKeywords.fs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ module GeneratedAttributes =
module AssemblyConstants =
let OutputPath = "OutputPath"
let AssemblyName = "AssemblyName"
let QsharpOutputType = "ResolvedQsharpOutputType"
let QsharpExe = "QsharpExe"
let QsharpLibrary = "QsharpLibrary"
let ExecutionTarget = "ResolvedExecutionTarget"
let AlfredProcessor = "AlfredProcessor"
let BrunoProcessor = "BrunoProcessor"
Expand Down
4 changes: 2 additions & 2 deletions src/QsCompiler/Transformations/CodeTransformations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static QsScope GenerateAdjoint(this QsScope scope)
}

/// <summary>
/// Given the body of an operation, auto-generates the (content of the) controlled specialization
/// using the default name for control qubits.
/// Given the body of an operation, auto-generates the (content of the) controlled specialization using the default name
/// for control qubits. Adds the control qubits names to the list of defined variables for the scope and each subscope.
/// Throws an ArgumentNullException if the given scope is null.
/// </summary>
public static QsScope GenerateControlled(this QsScope scope)
Expand Down
35 changes: 31 additions & 4 deletions src/QsCompiler/Transformations/FunctorGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace Microsoft.Quantum.QsCompiler.Transformations.FunctorGeneration
/// <summary>
/// Scope transformation that replaces each operation call within a given scope
/// with a call to the operation after application of the functor given on initialization.
/// The default values used for auto-generation will be used for the additional functor arguments.
/// The default values used for auto-generation will be used for the additional functor arguments.
/// Additional functor arguments will be added to the list of defined variables for each scope.
/// </summary>
public class ApplyFunctorToOperationCalls
: SyntaxTreeTransformation<ApplyFunctorToOperationCalls.TransformationsState>
Expand All @@ -31,10 +32,10 @@ public TransformationsState(QsFunctor functor) =>
this.FunctorToApply = functor ?? throw new ArgumentNullException(nameof(functor));
}


public ApplyFunctorToOperationCalls(QsFunctor functor)
: base(new TransformationsState(functor))
{
{
if (functor.IsControlled) this.Statements = new AddVariableDeclarations<TransformationsState>(this, ControlQubitsDeclaration);
this.StatementKinds = new IgnoreOuterBlockInConjugations<TransformationsState>(this);
this.ExpressionKinds = new ExpressionKindTransformation(this);
this.Types = new TypeTransformation<TransformationsState>(this, TransformationOptions.Disabled);
Expand All @@ -43,8 +44,19 @@ public ApplyFunctorToOperationCalls(QsFunctor functor)

// static methods for convenience

private static readonly NonNullable<string> ControlQubitsName =
NonNullable<string>.New(InternalUse.ControlQubitsName);

private static readonly TypedExpression ControlQubits =
SyntaxGenerator.ImmutableQubitArrayWithName(NonNullable<string>.New(InternalUse.ControlQubitsName));
SyntaxGenerator.ImmutableQubitArrayWithName(ControlQubitsName);

private static readonly LocalVariableDeclaration<NonNullable<string>> ControlQubitsDeclaration =
new LocalVariableDeclaration<NonNullable<string>>(
ControlQubitsName,
ControlQubits.ResolvedType,
ControlQubits.InferredInformation,
QsNullable<Tuple<int, int>>.Null,
QsCompilerDiagnostic.DefaultRange);

public static readonly Func<QsScope, QsScope> ApplyAdjoint =
new ApplyFunctorToOperationCalls(QsFunctor.Adjoint).Statements.OnScope;
Expand Down Expand Up @@ -86,6 +98,21 @@ public override QsExpressionKind<TypedExpression, Identifier, ResolvedType> OnOp
}
}

/// <summary>
/// Adds the given variable declarations to the list of defined variables for each scope.
/// </summary>
public class AddVariableDeclarations<T>
: StatementTransformation<T>
{
private readonly IEnumerable<LocalVariableDeclaration<NonNullable<string>>> AddedVariableDeclarations;

public AddVariableDeclarations(SyntaxTreeTransformation<T> parent, params LocalVariableDeclaration<NonNullable<string>>[] addedVars)
: base(parent) =>
this.AddedVariableDeclarations = addedVars ?? throw new ArgumentNullException(nameof(addedVars));

public override LocalDeclarations OnLocalDeclarations(LocalDeclarations decl) =>
base.OnLocalDeclarations(new LocalDeclarations(decl.Variables.AddRange(this.AddedVariableDeclarations)));
}

/// <summary>
/// Ensures that the outer block of conjugations is ignored during transformation.
Expand Down
16 changes: 15 additions & 1 deletion src/QsCompiler/Transformations/SearchAndReplace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,12 @@ public class UniqueVariableNames
public class TransformationState
{
private int VariableNr = 0;
private Dictionary<NonNullable<string>, NonNullable<string>> UniqueNames =
private readonly Dictionary<NonNullable<string>, NonNullable<string>> UniqueNames =
new Dictionary<NonNullable<string>, NonNullable<string>>();

internal bool TryGetUniqueName(NonNullable<string> name, out NonNullable<string> unique) =>
this.UniqueNames.TryGetValue(name, out unique);

internal QsExpressionKind AdaptIdentifier(Identifier sym, QsNullable<ImmutableArray<ResolvedType>> tArgs) =>
sym is Identifier.LocalVariable varName && this.UniqueNames.TryGetValue(varName.Item, out var unique)
? QsExpressionKind.NewIdentifier(Identifier.NewLocalVariable(unique), tArgs)
Expand All @@ -453,6 +456,7 @@ internal NonNullable<string> GenerateUniqueName(NonNullable<string> varName)
public UniqueVariableNames()
: base(new TransformationState())
{
this.Statements = new StatementTransformation(this);
this.StatementKinds = new StatementKindTransformation(this);
this.ExpressionKinds = new ExpressionKindTransformation(this);
this.Types = new TypeTransformation<TransformationState>(this, TransformationOptions.Disabled);
Expand All @@ -472,6 +476,16 @@ public static NonNullable<string> StripUniqueName(NonNullable<string> uniqueName

// helper classes

private class StatementTransformation
: StatementTransformation<TransformationState>
{
public StatementTransformation(SyntaxTreeTransformation<TransformationState> parent)
: base(parent) { }

public override NonNullable<string> OnVariableName(NonNullable<string> name) =>
this.SharedState.TryGetUniqueName(name, out var unique) ? unique : name;
}

private class StatementKindTransformation
: StatementKindTransformation<TransformationState>
{
Expand Down
12 changes: 6 additions & 6 deletions src/QuantumSdk/DefaultItems/DefaultItems.targets
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@

<!-- Resolving the OutputType to either QsharpExe or QsharpLibrary. -->
<PropertyGroup>
<ResolvedQsharpOutputType Condition="'$(OutputType)' == 'Library'">QsharpLibrary</ResolvedQsharpOutputType>
<ResolvedQsharpOutputType Condition="'$(OutputType)' == 'Exe'">QsharpExe</ResolvedQsharpOutputType>
<ResolvedQsharpOutputType Condition="'$(OutputType)' == 'Library' Or '$(NoEntryPoint)' == 'true'">QsharpLibrary</ResolvedQsharpOutputType>
<ResolvedQsharpOutputType Condition="'$(OutputType)' == 'Exe' And '$(NoEntryPoint)' != 'true'">QsharpExe</ResolvedQsharpOutputType>
<ResolvedQsharpOutputType Condition="'$(ResolvedQsharpOutputType)' != 'QsharpLibrary' And '$(ResolvedQsharpOutputType)' != 'QsharpExe'"></ResolvedQsharpOutputType>
<ValidOutputTypes>Possible values are 'Exe', or 'Library'.</ValidOutputTypes>
</PropertyGroup>

<!-- Resolving the ExecutionTarget to either AlfredProcessor, BrunoProcessor, ClementineProcessor or Unspecified.-->
<PropertyGroup>
<ResolvedQsharpExecutionTarget Condition="'$(ResolvedQsharpOutputType)' == 'QsharpExe' And '$(ExecutionTarget)' == 'Alfred'">AlfredProcessor</ResolvedQsharpExecutionTarget>
<ResolvedQsharpExecutionTarget Condition="'$(ResolvedQsharpOutputType)' == 'QsharpExe' And '$(ExecutionTarget)' == 'Bruno'">BrunoProcessor</ResolvedQsharpExecutionTarget>
<ResolvedQsharpExecutionTarget Condition="'$(ResolvedQsharpOutputType)' == 'QsharpExe' And '$(ExecutionTarget)' == 'Clementine'">ClementineProcessor</ResolvedQsharpExecutionTarget>
<ResolvedQsharpExecutionTarget Condition="('$(ResolvedQsharpOutputType)' == 'QsharpExe' Or '$(NoEntryPoint)' == 'true') And '$(ExecutionTarget)' == 'Alfred'">AlfredProcessor</ResolvedQsharpExecutionTarget>
<ResolvedQsharpExecutionTarget Condition="('$(ResolvedQsharpOutputType)' == 'QsharpExe' Or '$(NoEntryPoint)' == 'true') And '$(ExecutionTarget)' == 'Bruno'">BrunoProcessor</ResolvedQsharpExecutionTarget>
<ResolvedQsharpExecutionTarget Condition="('$(ResolvedQsharpOutputType)' == 'QsharpExe' Or '$(NoEntryPoint)' == 'true') And '$(ExecutionTarget)' == 'Clementine'">ClementineProcessor</ResolvedQsharpExecutionTarget>
<ResolvedQsharpExecutionTarget Condition="'$(ExecutionTarget)' == 'Any'">Unspecified</ResolvedQsharpExecutionTarget>
<ResolvedQsharpExecutionTarget Condition="'$(ResolvedQsharpExecutionTarget)' != 'AlfredProcessor' And '$(ResolvedQsharpExecutionTarget)' != 'BrunoProcessor' And '$(ResolvedQsharpExecutionTarget)' != 'ClementineProcessor' And '$(ResolvedQsharpExecutionTarget)' != 'Unspecified'"></ResolvedQsharpExecutionTarget>
<ValidExecutionTargets Condition="'$(ResolvedQsharpOutputType)' == 'QsharpExe'">Possible values are 'Alfred', 'Bruno', 'Clementine', or 'Any'.</ValidExecutionTargets>
<ValidExecutionTargets Condition="'$(ResolvedQsharpOutputType)' == 'QsharpExe' Or '$(NoEntryPoint)' == 'true'">Possible values are 'Alfred', 'Bruno', 'Clementine', or 'Any'.</ValidExecutionTargets>
<ValidExecutionTargets Condition="'$(ResolvedQsharpOutputType)' == 'QsharpLibrary'">The execution target for a Q# library needs to be 'Any'.</ValidExecutionTargets>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/QuantumSdk/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<_QscCommandRuntimeFlag>--runtime $(ResolvedRuntimeCapabilities)</_QscCommandRuntimeFlag>
<_QscCommandTargetPackageFlag Condition="'$(ResolvedTargetPackage)' != ''">--target-package "$(ResolvedTargetPackage)"</_QscCommandTargetPackageFlag>
<_QscCommandTestNamesFlag Condition="$(ExposeReferencesViaTestNames)">--load-test-names</_QscCommandTestNamesFlag>
<_QscCommandPredefinedAssemblyProperties>ResolvedExecutionTarget:$(ResolvedQsharpExecutionTarget)</_QscCommandPredefinedAssemblyProperties>
<_QscCommandPredefinedAssemblyProperties>ResolvedExecutionTarget:$(ResolvedQsharpExecutionTarget) ResolvedQsharpOutputType:$(ResolvedQsharpOutputType)</_QscCommandPredefinedAssemblyProperties>
<_QscCommandPredefinedAssemblyProperties Condition="$(DefaultSimulator) != ''">$(_QscCommandPredefinedAssemblyProperties) DefaultSimulator:$(DefaultSimulator)</_QscCommandPredefinedAssemblyProperties>
<_QscCommandAssemblyPropertiesFlag>--assembly-properties $(_QscCommandPredefinedAssemblyProperties) $(QscCommandAssemblyProperties)</_QscCommandAssemblyPropertiesFlag>
<_QscPackageLoadFallbackFoldersFlag Condition="@(ResolvedPackageLoadFallbackFolders->Count()) &gt; 0">--package-load-fallback-folders "@(ResolvedPackageLoadFallbackFolders,'" "')"</_QscPackageLoadFallbackFoldersFlag>
Expand Down