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
Auto emulation rewrite step #514
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a845ccb
First proposal for auto emulation feature.
msoeken 9da6e3e
Merge branch 'main' into merge
msoeken 807e9d1
Deprecation.
msoeken c45a3cd
Update to newer version.
msoeken 3d78eae
Successful tests.
msoeken d92efa9
Compatibility checks.
msoeken 8654388
Do not change this file.
msoeken 5083938
Signature checking too restrictive.
msoeken 7adf9fa
Add integration test.
msoeken 8f55e5c
Merge branch 'main' into msoeken/auto-emulation
b28679a
Merge branch 'main' into msoeken/auto-emulation
41bb1a7
Merge branch 'main' into msoeken/auto-emulation
msoeken f363fa7
Update QDK version.
msoeken a2d25d7
Incorporate Microsoft/QuantumLibraries#426.
msoeken 4a92681
Rename emulation to substitution.
msoeken 8189afb
Rename projects and directories.
msoeken 681725b
Packaging.
msoeken 6c0f3d7
Fix line-feed problems.
msoeken f94c9b5
Fix LF.
msoeken 93fff5b
Merge branch 'main' into msoeken/auto-emulation
swernli 8a165ca
Adressing reviewer feedback.
msoeken 6096a22
Update Sdk version.
msoeken b1d6544
Fixes dependency error.
msoeken f85b2d2
Merge branch 'main' into msoeken/auto-emulation
msoeken 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
18 changes: 18 additions & 0 deletions
18
src/Simulation/AutoSubstitution.Integration.Tests/Integration.cs
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,18 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Quantum.Simulation.Simulators; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Quantum.AutoSubstitution.Testing | ||
| { | ||
| public class CodeGenerationTests | ||
| { | ||
| [Fact] | ||
| public void CanSimulateWithAlternativeSimulator() | ||
| { | ||
| var sim = new ToffoliSimulator(); | ||
| TestQuantumSwap.Run(sim).Wait(); | ||
| } | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/Simulation/AutoSubstitution.Integration.Tests/Integration.qs
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,32 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Quantum.AutoSubstitution.Testing { | ||
| open Microsoft.Quantum.Diagnostics; | ||
| open Microsoft.Quantum.Intrinsic; | ||
| open Microsoft.Quantum.Measurement; | ||
| open Microsoft.Quantum.Targeting; | ||
|
|
||
| @SubstitutableOnTarget("Microsoft.Quantum.Intrinsic.SWAP", "ToffoliSimulator") | ||
| operation QuantumSwap(a : Qubit, b : Qubit) : Unit { | ||
| within { | ||
| CNOT(a, b); | ||
| H(a); | ||
| H(b); | ||
| } apply { | ||
| CNOT(a, b); | ||
| } | ||
| } | ||
|
|
||
| operation TestQuantumSwap() : Unit { | ||
| use a = Qubit(); | ||
| use b = Qubit(); | ||
|
|
||
| X(a); | ||
|
|
||
| QuantumSwap(a, b); | ||
|
|
||
| EqualityFactR(MResetZ(a), Zero, "unexpected value for a after swap"); | ||
| EqualityFactR(MResetZ(b), One, "unexpected value for b after swap"); | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/Simulation/AutoSubstitution.Integration.Tests/Tests.AutoSubstitution.Integration.csproj
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 @@ | ||
| <Project Sdk="Microsoft.Quantum.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| <PlatformTarget>x64</PlatformTarget> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" /> | ||
| <PackageReference Include="xunit" Version="2.4.0" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
| </PackageReference> | ||
| <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\AutoSubstitution\AutoSubstitution.csproj" IsQscReference="true" /> | ||
| </ItemGroup> | ||
| </Project> |
90 changes: 90 additions & 0 deletions
90
src/Simulation/AutoSubstitution.Tests/CodeGenerationTests.cs
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,90 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
msoeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using System.Collections.Immutable; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Microsoft.Quantum.QsCompiler.AutoSubstitution; | ||
| using Microsoft.Quantum.QsCompiler.CompilationBuilder; | ||
| using Microsoft.Quantum.QsCompiler.ReservedKeywords; | ||
| using Microsoft.Quantum.QsCompiler.SyntaxTree; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Quantum.AutoSubstitution.Testing | ||
| { | ||
| public class CodeGenerationTests | ||
| { | ||
| [Fact] | ||
| public void CanGenerateAutoSubstitutionCode() | ||
| { | ||
| TestOneSuccessfulFile("Success"); | ||
| TestOneSuccessfulFile("SuccessA"); | ||
| TestOneSuccessfulFile("SuccessC"); | ||
| TestOneSuccessfulFile("SuccessCA"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanFailForVariousReasons() | ||
| { | ||
| TestOneFailingFile("FailAlternativeDoesNotExist"); | ||
| TestOneFailingFile("FailDifferentSignatures"); | ||
| TestOneFailingFile("FailDifferentSpecializationKinds"); | ||
| TestOneFailingFile("FailNoNamespace"); | ||
| } | ||
|
|
||
| private void TestOneSuccessfulFile(string fileName) | ||
| { | ||
| var step = new RewriteStep(); | ||
| var path = CreateNewTemporaryPath(); | ||
| step.AssemblyConstants[AssemblyConstants.OutputPath] = path; | ||
|
|
||
| var compilation = CreateCompilation(Path.Combine("TestFiles", "Core.qs"), "Substitution.qs", Path.Combine("TestFiles", $"{fileName}.qs")); | ||
|
|
||
| Assert.True(step.Transformation(compilation, out var transformed)); | ||
| var generatedFileName = Path.Combine(path, "__AutoSubstitution__.g.cs"); | ||
| Assert.True(File.Exists(generatedFileName)); | ||
|
|
||
| // uncomment this line, when creating new unit tests to | ||
| // create files with expected content | ||
| //File.Copy(generatedFileName, $"{fileName}.cs_", true); | ||
|
|
||
| Assert.Equal(File.ReadAllText(Path.Combine("TestFiles", $"{fileName}.cs_")).Replace("\r\n", "\n"), File.ReadAllText(generatedFileName).Replace("\r\n", "\n")); | ||
|
|
||
| Directory.Delete(path, true); | ||
| } | ||
|
|
||
| private void TestOneFailingFile(string fileName) | ||
| { | ||
| var step = new RewriteStep(); | ||
| var path = CreateNewTemporaryPath(); | ||
| step.AssemblyConstants[AssemblyConstants.OutputPath] = path; | ||
|
|
||
| var compilation = CreateCompilation(Path.Combine("TestFiles", "Core.qs"), "Substitution.qs", Path.Combine("TestFiles", $"{fileName}.qs")); | ||
|
|
||
| Assert.False(step.Transformation(compilation, out var transformed)); | ||
| Assert.Equal(2, step.GeneratedDiagnostics.Count()); | ||
| Assert.Equal(CodeAnalysis.DiagnosticSeverity.Error, step.GeneratedDiagnostics.Last().Severity); | ||
| } | ||
|
|
||
| private QsCompilation CreateCompilation(params string[] fileNames) | ||
| { | ||
| var mgr = new CompilationUnitManager(); | ||
| var files = CreateFileManager(fileNames); | ||
| mgr.AddOrUpdateSourceFilesAsync(files).Wait(); | ||
msoeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return mgr.Build().BuiltCompilation; | ||
| } | ||
|
|
||
| private ImmutableHashSet<FileContentManager> CreateFileManager(params string[] fileNames) => | ||
| CompilationUnitManager.InitializeFileManagers( | ||
| fileNames.Select(fileName => { | ||
| var fileId = new Uri(Path.GetFullPath(fileName)); | ||
| return (id: fileId, content: File.ReadAllText(fileName)); | ||
| }).ToDictionary(t => t.id, t => t.content) | ||
| ); | ||
|
|
||
| private readonly System.Random random = new System.Random(); | ||
| private string CreateNewTemporaryPath() => | ||
| Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), $"substitution-test-{random.Next(Int32.MaxValue)}")).FullName; | ||
| } | ||
| } | ||
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,5 @@ | ||
| // This file is needed to ensure that the Microsoft.Quantum.Core namespace | ||
| // exists for the tests. | ||
|
|
||
| namespace Microsoft.Quantum.Core { | ||
msoeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
6 changes: 6 additions & 0 deletions
6
src/Simulation/AutoSubstitution.Tests/TestFiles/FailAlternativeDoesNotExist.qs
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,6 @@ | ||
| namespace AutoSubstitutionTests { | ||
| open Microsoft.Quantum.Targeting; | ||
|
|
||
| @SubstitutableOnTarget("Namespace.NotExisting", "ToffoliSimulator") | ||
| operation Fail() : Unit {} | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/Simulation/AutoSubstitution.Tests/TestFiles/FailDifferentSignatures.qs
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,8 @@ | ||
| namespace AutoSubstitutionTests { | ||
| open Microsoft.Quantum.Targeting; | ||
|
|
||
| @SubstitutableOnTarget("AutoSubstitutionTests.FailClassical", "ToffoliSimulator") | ||
| operation Fail(a : Int) : Unit {} | ||
|
|
||
| operation FailClassical(a : Double) : Unit {} | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/Simulation/AutoSubstitution.Tests/TestFiles/FailDifferentSpecializationKinds.qs
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,8 @@ | ||
| namespace AutoSubstitutionTests { | ||
| open Microsoft.Quantum.Targeting; | ||
|
|
||
| @SubstitutableOnTarget("FailClassical", "ToffoliSimulator") | ||
| operation Fail() : Unit is Adj {} | ||
|
|
||
| operation FailClassical() : Unit {} | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/Simulation/AutoSubstitution.Tests/TestFiles/FailNoNamespace.qs
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,8 @@ | ||
| namespace AutoSubstitutionTests { | ||
| open Microsoft.Quantum.Targeting; | ||
|
|
||
| @SubstitutableOnTarget("FailClassical", "ToffoliSimulator") | ||
| operation Fail() : Unit {} | ||
|
|
||
| operation FailClassical() : Unit {} | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/Simulation/AutoSubstitution.Tests/TestFiles/Success.cs_
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,49 @@ | ||
| using System; | ||
| using Microsoft.Quantum.Simulation.Core; | ||
| using Microsoft.Quantum.Simulation.Simulators; | ||
|
|
||
| namespace AutoSubstitutionTests | ||
| { | ||
| public partial class Success | ||
| { | ||
| public class Native : Success | ||
| { | ||
| public Native(Microsoft.Quantum.Simulation.Core.IOperationFactory m): base(m) | ||
| { | ||
| sim0 = ((m) as ToffoliSimulator); | ||
| } | ||
|
|
||
| public override void __Init__() | ||
| { | ||
| base.__Init__(); | ||
| if ((sim0) != (null)) | ||
| { | ||
| alternative0 = (__Factory__.Get<AutoSubstitutionTests.SuccessClassical>(typeof(AutoSubstitutionTests.SuccessClassical))); | ||
| } | ||
| } | ||
|
|
||
| public override Func<QVoid, QVoid> __Body__ | ||
| { | ||
| get | ||
| { | ||
| return args => | ||
| { | ||
| if ((sim0) != (null)) | ||
| { | ||
| return alternative0.__Body__(args); | ||
| } | ||
| else | ||
| { | ||
| return base.__Body__(args); | ||
| } | ||
| } | ||
|
|
||
| ; | ||
| } | ||
| } | ||
|
|
||
| private AutoSubstitutionTests.SuccessClassical alternative0 = null; | ||
| private ToffoliSimulator sim0 = null; | ||
| } | ||
| } | ||
| } |
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,8 @@ | ||
| namespace AutoSubstitutionTests { | ||
| open Microsoft.Quantum.Targeting; | ||
|
|
||
| @SubstitutableOnTarget("AutoSubstitutionTests.SuccessClassical", "ToffoliSimulator") | ||
| operation Success() : Unit {} | ||
|
|
||
| operation SuccessClassical() : Unit {} | ||
| } |
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.