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
Show all changes
63 commits
Select commit Hold shift + click to select a range
1fc8949
WIP
ScottCarda-MS Feb 3, 2021
451f28b
Removed type parameters for IEntryPoint interface.
ScottCarda-MS Feb 11, 2021
71abbfe
Allowed Diver to handle multiple entry points.
ScottCarda-MS Feb 15, 2021
7823083
Documentation.
ScottCarda-MS Feb 15, 2021
6cb13cf
Got Entry Point Classes to generate.
ScottCarda-MS Feb 23, 2021
1591e09
generated main class/method for entry point driver
ScottCarda-MS Feb 23, 2021
685f6fd
WIP
ScottCarda-MS Feb 24, 2021
6e09bd2
temp
ScottCarda-MS Feb 24, 2021
0813a2f
Got the simulator option working again
ScottCarda-MS Feb 24, 2021
f26fc22
return
ScottCarda-MS Feb 24, 2021
bd5eab4
Moved Main into separate ns
ScottCarda-MS Feb 24, 2021
dbc18cd
fixed hard-coded simulator name
ScottCarda-MS Feb 24, 2021
1a9090e
Support validators from being elevated to super-command when sub-comm…
ScottCarda-MS Feb 25, 2021
20296b3
cleaned up stuff
ScottCarda-MS Feb 25, 2021
319bc73
further cleanup
ScottCarda-MS Feb 25, 2021
4cd5b01
Moved using statements to be file-wide for the entry point files.
ScottCarda-MS Feb 25, 2021
1f7339b
Merge branch 'main' into sccarda/MultipleEntryPoints
ScottCarda-MS Feb 25, 2021
b7fde3c
fixed
ScottCarda-MS Feb 25, 2021
9379191
Fixed vs solution issue
ScottCarda-MS Feb 25, 2021
f64ceda
Added test for multiple entry points. It is skipped until this is sup…
ScottCarda-MS Feb 25, 2021
2853b56
PR comments and cleanup
ScottCarda-MS Feb 25, 2021
e079d75
Removed precondition for C# generation on only having one entry point.
ScottCarda-MS Feb 25, 2021
d206778
.
ScottCarda-MS Feb 26, 2021
91224ee
Updates version number, will need to be reverted
ScottCarda-MS Feb 26, 2021
df3bfe1
make default ep commands hidden
ScottCarda-MS Feb 27, 2021
969b807
Added custom type for commands with their validators.
ScottCarda-MS Feb 27, 2021
2d772a2
Added more tests for multiple entry points
ScottCarda-MS Feb 27, 2021
250eb65
Revert "Updates version number, will need to be reverted"
ScottCarda-MS Feb 27, 2021
0e5a61e
Merge branch 'main' into sccarda/MultipleEntryPoints
ScottCarda-MS Feb 27, 2021
112aa3b
changed line endings in Simulation.sln
ScottCarda-MS Feb 27, 2021
0405951
Moved the simulator and execution target options to the driver settin…
ScottCarda-MS Mar 1, 2021
faf5cd9
Merge branch 'main' into sccarda/MultipleEntryPoints
ScottCarda-MS Mar 1, 2021
89cffad
updated tests
ScottCarda-MS Mar 1, 2021
b01b82e
Fixed help text
ScottCarda-MS Mar 1, 2021
c6d8b39
Reverted the properties of the AzureSettings to public for compatibil…
ScottCarda-MS Mar 2, 2021
60f756e
updated submit multi-entry point tests
ScottCarda-MS Mar 2, 2021
46f4d80
update version numbers
ScottCarda-MS Mar 2, 2021
09a68c9
Merge branch 'main' into sccarda/MultipleEntryPoints
ScottCarda-MS Mar 2, 2021
9d92371
removed unused parameter
ScottCarda-MS Mar 2, 2021
e857473
Enabled tests, consolidated type casting
ScottCarda-MS Mar 4, 2021
82a8e8c
Changed the endings of generated files to avoid name collisions. Main…
ScottCarda-MS Mar 4, 2021
49f1648
Simplified CommandWithValidators data struct
ScottCarda-MS Mar 4, 2021
26eeee3
generateEntryPointClassName to entryPointClassFullName
ScottCarda-MS Mar 4, 2021
7353e08
Replaced IEnumerable usage.
ScottCarda-MS Mar 4, 2021
6ddcf7d
Revert "update version numbers"
ScottCarda-MS Mar 4, 2021
dad7fdf
Skipping tests while versions number doesn't support it.
ScottCarda-MS Mar 4, 2021
bb3879a
Updated included generated C# files for projects.
ScottCarda-MS Mar 4, 2021
40e7ee2
Enabled tests/updated version numbers
ScottCarda-MS Mar 4, 2021
44a8778
Update version number
ScottCarda-MS Mar 9, 2021
162d386
Using new compilation loader write file function
ScottCarda-MS Mar 9, 2021
2729cfc
added todo
ScottCarda-MS Mar 9, 2021
cf03902
Merge branch 'main' into sccarda/MultipleEntryPoints
ScottCarda-MS Mar 9, 2021
ad93c24
Merged in from main
ScottCarda-MS Mar 16, 2021
7cba363
sync versions with compiler branch
ScottCarda-MS Mar 17, 2021
af938e1
merged in from main
ScottCarda-MS Mar 18, 2021
8acf2f4
Remove dependency on WriteFile from compiler
ScottCarda-MS Mar 19, 2021
eb3d829
Use ImmutableList for validators
ScottCarda-MS Mar 19, 2021
5d8842b
merged in from main
ScottCarda-MS Mar 20, 2021
31bf4fa
version numbers, again
ScottCarda-MS Mar 20, 2021
9950f0e
version numbers
ScottCarda-MS Mar 20, 2021
c86723f
Updated tests to include entry points in other name spaces.
ScottCarda-MS Mar 22, 2021
d270354
Changed input to GeneratedFile method for Main file
ScottCarda-MS Mar 22, 2021
c6eb149
Merge branch 'main' into sccarda/MultipleEntryPoints
bettinaheim Mar 23, 2021
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 Simulation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IonQExe", "src\Simulation\S
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QCIExe", "src\Simulation\Simulators.Tests\TestProjects\QCIExe\QCIExe.csproj", "{C015FF41-9A51-4AF0-AEFC-2547D596B10A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TargetedExe", "src\Simulation\Simulators.Tests\TestProjects\TargetedExe\TargetedExe.csproj", "{D292BF18-3956-4827-820E-254C3F81EF09}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TargetedExe", "src\Simulation\Simulators.Tests\TestProjects\TargetedExe\TargetedExe.csproj", "{D292BF18-3956-4827-820E-254C3F81EF09}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Quantum.QSharp.Foundation", "src\Simulation\QSharpFoundation\Microsoft.Quantum.QSharp.Foundation.csproj", "{DB45AD73-4D91-43F3-85CC-C63614A96FB0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Quantum.Type2.Core", "src\Simulation\Type2Core\Microsoft.Quantum.Type2.Core.csproj", "{AF6CD304-8E03-433D-AAA2-6E0094B53071}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests.Microsoft.Quantum.Simulators.Type2", "src\Simulation\Simulators.Type2.Tests\Tests.Microsoft.Quantum.Simulators.Type2.csproj", "{ED3D7040-4B3F-4217-A75E-9DF63DD84707}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntrinsicTests", "src\Simulation\Simulators.Tests\TestProjects\IntrinsicTests\IntrinsicTests.csproj", "{4EF958CA-B4A6-4E5F-924A-100B5615BEC3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntrinsicTests", "src\Simulation\Simulators.Tests\TestProjects\IntrinsicTests\IntrinsicTests.csproj", "{4EF958CA-B4A6-4E5F-924A-100B5615BEC3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests.Microsoft.Quantum.Simulators.Type1", "src\Simulation\Simulators.Type1.Tests\Tests.Microsoft.Quantum.Simulators.Type1.csproj", "{EB6E3DBD-C884-4241-9BC4-8281191D1F53}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210324341-alpha">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210324462-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.210324341-alpha">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210324462-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.210324341-alpha">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210324462-alpha">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/test/QIR-static/qsharp/qir-gen.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210324341-alpha">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210324462-alpha">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
2 changes: 1 addition & 1 deletion src/QirRuntime/test/QIR-tracer/qsharp/tracer-qir.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210324341-alpha">
<Project Sdk="Microsoft.Quantum.Sdk/0.15.210324462-alpha">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
206 changes: 144 additions & 62 deletions src/Simulation/CSharpGeneration/EntryPoint.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,6 @@ let entryPointClassName = "__QsEntryPoint__"
/// The namespace containing the non-generated parts of the entry point driver.
let private driverNamespace = "Microsoft.Quantum.EntryPointDriver"

/// The driver settings object.
let private driverSettings =
let newDriverSettings = driverNamespace + ".DriverSettings" |> ``type`` |> SyntaxFactory.ObjectCreationExpression
let namedArg (name : string) expr = SyntaxFactory.NameColon name |> (SyntaxFactory.Argument expr).WithNameColon
let immutableList elements = invoke (ident "System.Collections.Immutable.ImmutableList.Create") ``(`` elements ``)``
let simulatorOptionAliases =
[ literal <| "--" + fst CommandLineArguments.SimulatorOption
literal <| "-" + snd CommandLineArguments.SimulatorOption ]
|> immutableList
[ namedArg "simulatorOptionAliases" simulatorOptionAliases
namedArg "quantumSimulatorName" <| literal AssemblyConstants.QuantumSimulator
namedArg "toffoliSimulatorName" <| literal AssemblyConstants.ToffoliSimulator
namedArg "resourcesEstimatorName" <| literal AssemblyConstants.ResourcesEstimator ]
|> SyntaxFactory.SeparatedList
|> SyntaxFactory.ArgumentList
|> newDriverSettings.WithArgumentList
:> ExpressionSyntax

/// A sequence of all of the named parameters in the argument tuple and their respective C# and Q# types.
let rec private parameters context doc = function
| QsTupleItem variable ->
Expand Down Expand Up @@ -75,7 +57,7 @@ let private parameterOptionsProperty parameters =
``property-arrow_get`` optionsEnumerableTypeName "Options" [``public``]
get (``=>`` (``new array`` (Some optionTypeName) options))

/// A method that creates an instance of the default simulator if it is a custom simulator.
/// A lambda that creates an instance of the default simulator if it is a custom simulator.
let private customSimulatorFactory name =
let isCustomSimulator =
not <| List.contains name [
Expand All @@ -87,11 +69,7 @@ let private customSimulatorFactory name =
if isCustomSimulator
then ``new`` (``type`` name) ``(`` [] ``)``
else upcast SyntaxFactory.ThrowExpression (``new`` (``type`` "InvalidOperationException") ``(`` [] ``)``)

arrow_method "IOperationFactory" "CreateDefaultCustomSimulator" ``<<`` [] ``>>``
``(`` [] ``)``
[``public``]
(Some (``=>`` factory))
``() =>`` [] factory :> ExpressionSyntax

/// A method that creates the argument tuple for the entry point, given the command-line parsing result.
let private createArgument context entryPoint =
Expand Down Expand Up @@ -119,65 +97,169 @@ let private callableTypeNames context (callable : QsCallable) =
let returnTypeName = SimulationCode.roslynTypeName context callable.Signature.ReturnType
callableName, argTypeName, returnTypeName

/// The main method for the standalone executable.
let private mainMethod context entryPoint =
/// Generates the class name for an entry point class.
let private entryPointClassFullName (entryPoint : QsCallable) =
{ Namespace = entryPoint.FullName.Namespace; Name = entryPointClassName + entryPoint.FullName.Name }

/// Generates the Submit method for an entry point class.
let private submitMethod context entryPoint =
let callableName, _, _ = callableTypeNames context entryPoint
let parseResultParamName = "parseResult"
let settingsParamName = "settings"
let args =
[
ident callableName <|.|> ident "Info"
ident "this" <.> (ident "CreateArgument", [ident parseResultParamName])
ident settingsParamName :> ExpressionSyntax
]
arrow_method "System.Threading.Tasks.Task<int>" "Submit" ``<<`` [] ``>>``
``(``
[
param parseResultParamName ``of`` (``type`` "System.CommandLine.Parsing.ParseResult")
param settingsParamName ``of`` (``type`` (driverNamespace + ".AzureSettings"))
]
``)``
[``public``]
(Some (``=>`` (ident (driverNamespace + ".Azure") <.> (ident "Submit", args))))

/// Generates the Simulate method for an entry point class.
let private simulateMethod context entryPoint =
let callableName, argTypeName, returnTypeName = callableTypeNames context entryPoint
let driverType = generic (driverNamespace + ".Driver") ``<<`` [callableName; argTypeName; returnTypeName] ``>>``
let entryPointInstance = ``new`` (``type`` entryPointClassName) ``(`` [] ``)``
let driver = ``new`` driverType ``(`` [driverSettings; entryPointInstance] ``)``
let commandLineArgsName = "args"
arrow_method "System.Threading.Tasks.Task<int>" "Main" ``<<`` [] ``>>``
``(`` [param commandLineArgsName ``of`` (``type`` "string[]")] ``)``
[``private``; ``static``; async]
(Some (``=>`` (await (driver <.> (ident "Run", [ident commandLineArgsName])))))
let simulationType = generic (driverNamespace + ".Simulation") ``<<`` [callableName; argTypeName; returnTypeName] ``>>``
let parseResultParamName = "parseResult"
let settingsParamName = "settings"
let simulatorParamName = "simulator"
let args =
[
ident "this" :> ExpressionSyntax
ident "this" <.> (ident "CreateArgument", [ident parseResultParamName])
ident settingsParamName :> ExpressionSyntax
ident simulatorParamName :> ExpressionSyntax
]
arrow_method "System.Threading.Tasks.Task<int>" "Simulate" ``<<`` [] ``>>``
``(``
[
param parseResultParamName ``of`` (``type`` "System.CommandLine.Parsing.ParseResult")
param settingsParamName ``of`` (``type`` (driverNamespace + ".DriverSettings"))
param simulatorParamName ``of`` (``type`` "string")
]
``)``
[``public``]
(Some (``=>`` (simulationType <.> (ident "Simulate", args))))

/// The class that adapts the entry point for use with the command-line parsing library and the driver.
let private entryPointClass context entryPoint =
let callableName, argTypeName, returnTypeName = callableTypeNames context entryPoint
let private entryPointClass context (entryPoint : QsCallable) =
let property name typeName value = ``property-arrow_get`` typeName name [``public``] get (``=>`` value)
let nameProperty =
entryPoint.FullName.ToString()
|> literal
|> property "Name" "string"
let summaryProperty =
(PrintSummary entryPoint.Documentation false).Trim ()
|> literal
|> property "Summary" "string"
let parameters = parameters context entryPoint.Documentation entryPoint.ArgumentTuple
let defaultSimulator =
context.assemblyConstants.TryGetValue AssemblyConstants.DefaultSimulator
|> fun (_, value) -> if String.IsNullOrWhiteSpace value then AssemblyConstants.QuantumSimulator else value
let defaultSimulatorNameProperty = literal defaultSimulator |> property "DefaultSimulatorName" "string"
let defaultExecutionTargetProperty =
context.assemblyConstants.TryGetValue AssemblyConstants.ExecutionTarget
|> (fun (_, value) -> if value = null then "" else value)
|> literal
|> property "DefaultExecutionTarget" "string"
let infoProperty =
property "Info" (sprintf "EntryPointInfo<%s, %s>" argTypeName returnTypeName)
(ident callableName <|.|> ident "Info")
let members : MemberDeclarationSyntax list = [
nameProperty
summaryProperty
parameterOptionsProperty parameters
defaultSimulatorNameProperty
defaultExecutionTargetProperty
infoProperty
customSimulatorFactory defaultSimulator
createArgument context entryPoint
mainMethod context entryPoint
submitMethod context entryPoint
simulateMethod context entryPoint
]
let baseName = sprintf "%s.IEntryPoint<%s, %s>" driverNamespace argTypeName returnTypeName
``class`` entryPointClassName``<<`` [] ``>>``
let baseName = sprintf "%s.IEntryPoint" driverNamespace
``class`` ((entryPointClassFullName entryPoint).Name) ``<<`` [] ``>>``
``:`` (Some (simpleBase baseName)) ``,`` []
[``internal``]
``{``
members
``}``

/// Generates C# source code for a standalone executable that runs the Q# entry point.
let generate context (entryPoint : QsCallable) =
let ns =
``namespace`` entryPoint.FullName.Namespace
/// Generates a namespace for a set of entry points that share the namespace
let private entryPointNamespace context name entryPoints =
``namespace`` name
``{``
[]
[for ep in entryPoints -> entryPointClass context ep]
``}``

/// Returns the driver settings object.
let private driverSettings context =
let newDriverSettings = driverNamespace + ".DriverSettings" |> ``type`` |> SyntaxFactory.ObjectCreationExpression
let namedArg (name : string) expr = SyntaxFactory.NameColon name |> (SyntaxFactory.Argument expr).WithNameColon
let immutableList elements = invoke (ident "System.Collections.Immutable.ImmutableList.Create") ``(`` elements ``)``
let simulatorOptionAliases =
[ literal <| "--" + fst CommandLineArguments.SimulatorOption
literal <| "-" + snd CommandLineArguments.SimulatorOption ]
|> immutableList
let defaultSimulator =
context.assemblyConstants.TryGetValue AssemblyConstants.DefaultSimulator
|> fun (_, value) -> if String.IsNullOrWhiteSpace value then AssemblyConstants.QuantumSimulator else value
let defaultExecutionTarget =
context.assemblyConstants.TryGetValue AssemblyConstants.ExecutionTarget
|> (fun (_, value) -> if value = null then "" else value)
|> literal
[
namedArg "simulatorOptionAliases" simulatorOptionAliases
namedArg "quantumSimulatorName" <| literal AssemblyConstants.QuantumSimulator
namedArg "toffoliSimulatorName" <| literal AssemblyConstants.ToffoliSimulator
namedArg "resourcesEstimatorName" <| literal AssemblyConstants.ResourcesEstimator
namedArg "defaultSimulatorName" <| literal defaultSimulator
namedArg "defaultExecutionTarget" <| defaultExecutionTarget
namedArg "createDefaultCustomSimulator" <| customSimulatorFactory defaultSimulator
]
|> SyntaxFactory.SeparatedList
|> SyntaxFactory.ArgumentList
|> newDriverSettings.WithArgumentList
:> ExpressionSyntax

/// The main method for the standalone executable.
let private mainMethod context entryPoints =

let entryPointArrayMembers =
[
for ep in entryPoints do
let name = entryPointClassFullName ep
``new`` (``type`` (name.ToString())) ``(`` [] ``)``
]

let entryPointArray =
``new array`` (Some (driverNamespace + ".IEntryPoint")) entryPointArrayMembers

let driver = ``new`` (``type`` (driverNamespace + ".Driver")) ``(`` [driverSettings context; entryPointArray] ``)``
let commandLineArgsName = "args"
arrow_method "System.Threading.Tasks.Task<int>" "Main" ``<<`` [] ``>>``
``(`` [param commandLineArgsName ``of`` (``type`` "string[]")] ``)``
[``private``; ``static``; async]
(Some (``=>`` (await (driver <.> (ident "Run", [ident commandLineArgsName])))))

/// Generates a namespace for the main function
let private mainNamespace context entryPoints =
let mainClass =
``class`` entryPointClassName ``<<`` [] ``>>``
``:`` None ``,`` []
[``internal``]
``{``
(Seq.map using SimulationCode.autoNamespaces)
[entryPointClass context entryPoint]
[mainMethod context entryPoints]
``}``
``compilation unit`` [] [] [ns]

``namespace`` entryPointClassName
``{``
[]
[mainClass]
``}``

/// Generates the C# source code for the file containing the Main function.
let generateMainSource context entryPoints =
let mainNS = mainNamespace context entryPoints
``compilation unit`` [] (Seq.map using SimulationCode.autoNamespaces) [mainNS :> MemberDeclarationSyntax]
|> ``with leading comments`` SimulationCode.autogenComment
|> SimulationCode.formatSyntaxTree

/// Generates C# source code for a standalone executable that runs the Q# entry point.
let generateSource context (entryPoints : seq<QsCallable>) =
let entryPointNamespaces = entryPoints |> Seq.groupBy (fun ep -> ep.FullName.Namespace)
let namespaces = [for ns, eps in entryPointNamespaces -> entryPointNamespace context ns eps :> MemberDeclarationSyntax]
``compilation unit`` [] (Seq.map using SimulationCode.autoNamespaces) namespaces
|> ``with leading comments`` SimulationCode.autogenComment
|> SimulationCode.formatSyntaxTree
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\Common\AssemblyCommon.props" />
<Import Project="..\Common\DebugSymbols.props" />

Expand All @@ -22,7 +22,7 @@

<ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.7.0" />
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.15.210324341-alpha" />
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.15.210324462-alpha" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading