This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
QIR controller (part 1) #620
Merged
achocron
merged 17 commits into
feature/azure-quantum-simulator
from
alchocro/qir-controller-logging
Apr 9, 2021
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0cbc02c
Create initial prototype for controller (with dependencies unimplemen…
achocron 171825e
Merge remote-tracking branch 'origin/feature/azure-quantum-simulator'…
achocron ddf30c8
Remove generated qir
achocron dca7fbb
Remove placeholder tests
achocron 2c8ca9f
Allow test cases folder not to exist
achocron 7558e46
Add default error handling
achocron d192415
Remove test artifacts from solution
achocron 84725d3
Create logger and tests
achocron 52cca9f
Add logs
achocron 3513858
Fix warning level
achocron 74fb929
Fix unit tests for linux
achocron 66ddfb0
Fix header
achocron f2ff7b2
Add include directory
achocron 0b19366
Clean up and fix input
achocron ae18d34
Change how directories are passed
achocron ef1a2f8
Add comment
achocron ce030fa
Do not delete output file on error
achocron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.Qir | ||
| { | ||
| public static class Constant | ||
| { | ||
| // TODO: errors will be added as dependencies are implemented. | ||
| public static class ErrorCode | ||
| { | ||
| public const string InternalError = "InternalError"; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,97 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Quantum.Qir.Driver; | ||
| using Microsoft.Quantum.Qir.Executable; | ||
| using Microsoft.Quantum.Qir.Model; | ||
| using Microsoft.Quantum.Qir.Utility; | ||
| using QirExecutionWrapperSerialization = Microsoft.Quantum.QsCompiler.BondSchemas.QirExecutionWrapper.Protocols; | ||
|
|
||
| namespace Microsoft.Quantum.Qir | ||
| { | ||
| internal static class Controller | ||
| public static class Controller | ||
| { | ||
| internal static void Execute( | ||
| FileInfo input, | ||
| FileInfo output, | ||
| FileInfo error) | ||
| private const string SourceDirectoryPath = "src"; | ||
| private const string BinaryDirectoryPath = "bin"; | ||
| private const string ExecutableName = "simulation.exe"; | ||
|
|
||
| public static async Task ExecuteAsync( | ||
| FileInfo inputFile, | ||
| FileInfo outputFile, | ||
| DirectoryInfo libraryDirectory, | ||
| DirectoryInfo includeDirectory, | ||
| FileInfo errorFile, | ||
| IQirDriverGenerator driverGenerator, | ||
| IQirExecutableGenerator executableGenerator, | ||
| IQuantumExecutableRunner executableRunner, | ||
| ILogger logger) | ||
| { | ||
| var outputFileStream = output.Exists ? output.OpenWrite() : output.Create(); | ||
| outputFileStream.Write(new UTF8Encoding().GetBytes("output")); | ||
| outputFileStream.Flush(); | ||
| outputFileStream.Close(); | ||
| var errorFileStream = error.Exists ? error.OpenWrite() : error.Create(); | ||
| errorFileStream.Write(new UTF8Encoding().GetBytes("error")); | ||
| errorFileStream.Flush(); | ||
| errorFileStream.Close(); | ||
| try | ||
| { | ||
| // Step 1: Parse input. | ||
| logger.LogInfo("Parsing input."); | ||
| using var inputFileStream = inputFile.OpenRead(); | ||
| var input = QirExecutionWrapperSerialization.DeserializeFromFastBinary(inputFileStream); | ||
|
|
||
| // Step 32: Create driver. | ||
| logger.LogInfo("Creating driver file."); | ||
| var sourceDirectory = new DirectoryInfo(SourceDirectoryPath); | ||
| await driverGenerator.GenerateQirDriverCppAsync(sourceDirectory, input.EntryPoint, input.QirBytecode); | ||
|
|
||
| // Step 3: Create executable. | ||
| logger.LogInfo("Compiling and linking executable."); | ||
| var binaryDirectory = new DirectoryInfo(BinaryDirectoryPath); | ||
| var executableFile = new FileInfo(Path.Combine(BinaryDirectoryPath, ExecutableName)); | ||
| await executableGenerator.GenerateExecutableAsync(executableFile, sourceDirectory, libraryDirectory, includeDirectory); | ||
|
|
||
| // Step 4: Run executable. | ||
| logger.LogInfo("Running executable."); | ||
| using var outputFileStream = outputFile.OpenWrite(); | ||
| await executableRunner.RunExecutableAsync(executableFile, input.EntryPoint, outputFile); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| logger.LogError("An error has been encountered. Will write an error to the error file and delete any output that has been generated."); | ||
| logger.LogException(e); | ||
| await WriteExceptionToFileAsync(e, errorFile); | ||
| } | ||
| } | ||
|
|
||
| private static async Task WriteExceptionToFileAsync(Exception e, FileInfo errorFile) | ||
| { | ||
| // Create the error object. | ||
| Error error; | ||
| if (e is ControllerException controllerException) | ||
| { | ||
| error = new Error | ||
| { | ||
| Code = controllerException.Code, | ||
| Message = controllerException.Message, | ||
| }; | ||
| } | ||
| else | ||
| { | ||
| error = new Error | ||
| { | ||
| Code = Constant.ErrorCode.InternalError, | ||
| Message = ErrorMessages.InternalError, | ||
| }; | ||
| } | ||
|
|
||
| // Serialize the error to JSON. | ||
| var errorJson = JsonSerializer.Serialize(error, new JsonSerializerOptions | ||
| { | ||
| WriteIndented = true, | ||
| }); | ||
|
|
||
| // Write the error to the error file. | ||
| using var errorFileStream = errorFile.OpenWrite(); | ||
| using var streamWriter = new StreamWriter(errorFileStream); | ||
| await streamWriter.WriteAsync(errorJson); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Quantum.Qir | ||
| { | ||
| /// <summary> | ||
| /// Exception that represents an error that can be written to an error file. | ||
| /// </summary> | ||
| public class ControllerException : Exception | ||
| { | ||
| public ControllerException(string message, string code) | ||
| : base(message) | ||
| { | ||
| Code = code; | ||
| } | ||
|
|
||
| public string Code { get; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Quantum.QsCompiler.BondSchemas.EntryPoint; | ||
|
|
||
| namespace Microsoft.Quantum.Qir.Driver | ||
| { | ||
| public interface IQirDriverGenerator | ||
| { | ||
| /// <summary> | ||
| /// Generates the C++ driver source file and writes the bytecode to a file. | ||
| /// </summary> | ||
| /// <param name="sourceDirectory">Directory to which driver and bytecode will be written.</param> | ||
| /// <param name="entryPointOperation">Entry point information.</param> | ||
| /// <param name="bytecode">The QIR bytecode.</param> | ||
| /// <returns></returns> | ||
| Task GenerateQirDriverCppAsync(DirectoryInfo sourceDirectory, EntryPointOperation entryPointOperation, ArraySegment<byte> bytecode); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Quantum.Qir.Utility; | ||
| using Microsoft.Quantum.QsCompiler.BondSchemas.EntryPoint; | ||
|
|
||
| namespace Microsoft.Quantum.Qir.Driver | ||
| { | ||
| public class QirDriverGenerator : IQirDriverGenerator | ||
| { | ||
| private readonly ILogger logger; | ||
|
|
||
| public QirDriverGenerator(ILogger logger) | ||
| { | ||
| this.logger = logger; | ||
| } | ||
|
|
||
| public Task GenerateQirDriverCppAsync(DirectoryInfo sourceDirectory, EntryPointOperation entryPointOperation, ArraySegment<byte> bytecode) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.