From 34696892fdb3f4dfd9776cd8e2e719960d790b71 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Wed, 23 Jun 2021 15:32:19 -0700 Subject: [PATCH 1/8] add autoemulation sample --- .../autosubstitution/AutoSubstitution.csproj | 9 +++++ samples/autosubstitution/AutoSubstitution.qs | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 samples/autosubstitution/AutoSubstitution.csproj create mode 100644 samples/autosubstitution/AutoSubstitution.qs diff --git a/samples/autosubstitution/AutoSubstitution.csproj b/samples/autosubstitution/AutoSubstitution.csproj new file mode 100644 index 000000000000..4399c48de403 --- /dev/null +++ b/samples/autosubstitution/AutoSubstitution.csproj @@ -0,0 +1,9 @@ + + + Exe + netcoreapp3.1 + + + + + diff --git a/samples/autosubstitution/AutoSubstitution.qs b/samples/autosubstitution/AutoSubstitution.qs new file mode 100644 index 000000000000..06e8f6b633fb --- /dev/null +++ b/samples/autosubstitution/AutoSubstitution.qs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +namespace Project { + open Microsoft.Quantum.Convert; + open Microsoft.Quantum.Math; + open Microsoft.Quantum.Measurement; + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Targeting; + + operation ClassicalSWAP(a : Qubit, b : Qubit) : Unit is Adj + Ctl { + Message("Classical version"); + CNOT(a, b); + CNOT(b, a); + CNOT(a, b); + } + + @SubstitutableOnTarget("Project.ClassicalSWAP", "ToffoliSimulator") + operation ApplySingleDirectionSWAP(a : Qubit, b : Qubit) : Unit is Adj + Ctl { + Message("Quantum version"); + within { + CNOT(a, b); + H(a); + H(b); + } apply { + CNOT(a, b); + } + } + + @EntryPoint() + operation RunProgram() : Unit { + use a = Qubit(); + use b = Qubit(); + + let _ = ClassicalSWAP; + ApplySingleDirectionSWAP(a, b); + } +} From 658c14d6990b57e741d8a54ef3e4341d008175b3 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Fri, 9 Jul 2021 15:04:17 +0200 Subject: [PATCH 2/8] Move to runtime folder. --- samples/{ => runtime}/autosubstitution/AutoSubstitution.csproj | 0 samples/{ => runtime}/autosubstitution/AutoSubstitution.qs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename samples/{ => runtime}/autosubstitution/AutoSubstitution.csproj (100%) rename samples/{ => runtime}/autosubstitution/AutoSubstitution.qs (100%) diff --git a/samples/autosubstitution/AutoSubstitution.csproj b/samples/runtime/autosubstitution/AutoSubstitution.csproj similarity index 100% rename from samples/autosubstitution/AutoSubstitution.csproj rename to samples/runtime/autosubstitution/AutoSubstitution.csproj diff --git a/samples/autosubstitution/AutoSubstitution.qs b/samples/runtime/autosubstitution/AutoSubstitution.qs similarity index 100% rename from samples/autosubstitution/AutoSubstitution.qs rename to samples/runtime/autosubstitution/AutoSubstitution.qs From 3ac42e19977a3de1f19b11cfa6a8296968c56005 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Fri, 9 Jul 2021 15:14:33 +0200 Subject: [PATCH 3/8] Docs. --- .../autosubstitution/AutoSubstitution.csproj | 5 ++- .../autosubstitution/AutoSubstitution.qs | 12 +++++- samples/runtime/autosubstitution/README.md | 41 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 samples/runtime/autosubstitution/README.md diff --git a/samples/runtime/autosubstitution/AutoSubstitution.csproj b/samples/runtime/autosubstitution/AutoSubstitution.csproj index 4399c48de403..9d3c80042ca4 100644 --- a/samples/runtime/autosubstitution/AutoSubstitution.csproj +++ b/samples/runtime/autosubstitution/AutoSubstitution.csproj @@ -1,9 +1,10 @@ - + Exe netcoreapp3.1 - + + diff --git a/samples/runtime/autosubstitution/AutoSubstitution.qs b/samples/runtime/autosubstitution/AutoSubstitution.qs index 06e8f6b633fb..6936b6313c46 100644 --- a/samples/runtime/autosubstitution/AutoSubstitution.qs +++ b/samples/runtime/autosubstitution/AutoSubstitution.qs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. + namespace Project { open Microsoft.Quantum.Convert; open Microsoft.Quantum.Math; @@ -15,9 +16,19 @@ namespace Project { CNOT(a, b); } + // This attribute indicates that when running this Q# program with + // ToffoliSimulator, the operation `ClassicalSWAP` is executed instead. @SubstitutableOnTarget("Project.ClassicalSWAP", "ToffoliSimulator") operation ApplySingleDirectionSWAP(a : Qubit, b : Qubit) : Unit is Adj + Ctl { + // Note: In version 0.18.2106148911 we must explicitly reference the + // operation; otherwise, the compiler removes the operation from the + // compilation unit before the auto-substitution rewrite step is executed. + let _ = ClassicalSWAP; + Message("Quantum version"); + + // Implements a SWAP operation in which all CNOT operations have the + // same control and target qubits. within { CNOT(a, b); H(a); @@ -32,7 +43,6 @@ namespace Project { use a = Qubit(); use b = Qubit(); - let _ = ClassicalSWAP; ApplySingleDirectionSWAP(a, b); } } diff --git a/samples/runtime/autosubstitution/README.md b/samples/runtime/autosubstitution/README.md new file mode 100644 index 000000000000..794f5820b4f8 --- /dev/null +++ b/samples/runtime/autosubstitution/README.md @@ -0,0 +1,41 @@ +--- +page_type: sample +languages: +- qsharp +products: +- qdk +description: "This sample uses the `Microsoft.Quantum.AutoSubstitution` NuGet package to provide alternative operations based on the simulator that is used." +--- + +# Using the AutoSubstitution rewrite step + +The `Microsoft.Quantum.AutoSubstitution` NuGet package offers a rewrite step +that can link an operation `Op` to a set of pairs of an alternative operations +`AltOp` and a simulator `Sim`, such that `AltOp` is executed as a replacement +for `Op` when being invoked in `Sim`. + +[This post](https://devblogs.microsoft.com/qsharp/) in the [Q# +blog](https://devblogs.microsoft.com/qsharp/) describes this sample in more +detail. + +## Running the program + +First, run + +```shell +dotnet run +``` + +and you should see `Quantum version` as output, but when running + +```shell +dotnet run -- -s ToffoliSimulator +``` + +the program prints `Classical version` instead, since the alternative operation +is executed. + +## Manifest + +- [AutoSubstitution.qs](./AutoSubstitution.qs): The main Q# example code implementing quantum operations for this sample. +- [AutoSubstitution.csproj](./AutoSubstitution.csproj): Main Q# project for the sample. From 1ed2b992fa50d9af131d3b9310aa02f3cd99dbe5 Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Fri, 9 Jul 2021 15:15:40 +0200 Subject: [PATCH 4/8] Entry in parent README. --- samples/runtime/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/runtime/README.md b/samples/runtime/README.md index 73fadc3b8abf..36a30bbe6622 100644 --- a/samples/runtime/README.md +++ b/samples/runtime/README.md @@ -2,6 +2,8 @@ These samples show how to work with the Q# simulation runtime. +- **[Auto substitution](./autosubstitution)**: + This sample uses the `Microsoft.Quantum.AutoSubstitution` NuGet package to provide alternative operations based on the simulator that is used. - **[Oracle Emulation](./oracle-emulation)**: This sample shows how to provide accelerated simulation functionality for different Q# operations and functions. - **[Reversible simulator (simple)](./reversible-simulator-simple)**: From 66481ac59ddd81f28517278e0d5800a3ec0f0fec Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Fri, 9 Jul 2021 15:18:28 +0200 Subject: [PATCH 5/8] Add project to solution file. --- samples/runtime/runtime.sln | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/samples/runtime/runtime.sln b/samples/runtime/runtime.sln index 4f7ab875b4ea..14a49e94499e 100644 --- a/samples/runtime/runtime.sln +++ b/samples/runtime/runtime.sln @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "host", "qpic-simulator\host EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "simulator", "qpic-simulator\simulator\simulator.csproj", "{3F729B1F-4856-4AFE-91C4-69254034E464}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoSubstitution", "autosubstitution\AutoSubstitution.csproj", "{30838650-E4C2-4B91-880F-CFB502963A08}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -143,6 +145,18 @@ Global {3F729B1F-4856-4AFE-91C4-69254034E464}.Release|x64.Build.0 = Release|Any CPU {3F729B1F-4856-4AFE-91C4-69254034E464}.Release|x86.ActiveCfg = Release|Any CPU {3F729B1F-4856-4AFE-91C4-69254034E464}.Release|x86.Build.0 = Release|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Debug|Any CPU.Build.0 = Debug|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Debug|x64.ActiveCfg = Debug|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Debug|x64.Build.0 = Debug|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Debug|x86.ActiveCfg = Debug|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Debug|x86.Build.0 = Debug|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Release|Any CPU.ActiveCfg = Release|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Release|Any CPU.Build.0 = Release|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Release|x64.ActiveCfg = Release|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Release|x64.Build.0 = Release|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Release|x86.ActiveCfg = Release|Any CPU + {30838650-E4C2-4B91-880F-CFB502963A08}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 5198f050b665611920558416e14593f05e77f12e Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Mon, 12 Jul 2021 18:38:08 +0200 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Guen Prawiroatmodjo --- samples/runtime/autosubstitution/README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/samples/runtime/autosubstitution/README.md b/samples/runtime/autosubstitution/README.md index 794f5820b4f8..35894f6441f7 100644 --- a/samples/runtime/autosubstitution/README.md +++ b/samples/runtime/autosubstitution/README.md @@ -9,10 +9,7 @@ description: "This sample uses the `Microsoft.Quantum.AutoSubstitution` NuGet pa # Using the AutoSubstitution rewrite step -The `Microsoft.Quantum.AutoSubstitution` NuGet package offers a rewrite step -that can link an operation `Op` to a set of pairs of an alternative operations -`AltOp` and a simulator `Sim`, such that `AltOp` is executed as a replacement -for `Op` when being invoked in `Sim`. +The `Microsoft.Quantum.AutoSubstitution` NuGet package offers an attribute `@SubstitutableOnTarget` that lets you replace one operation with another when using a particular simulator. For instance, decorating the operation `Op` with `@SubstitutableOnTarget("AltOp", "Sim")` lets the compiler perform a rewrite step that can link an operation `Op` to a set of pairs of an alternative operations `AltOp` and a simulator `Sim`, such that `AltOp` is executed as a replacement for `Op` when being invoked in `Sim`. [This post](https://devblogs.microsoft.com/qsharp/) in the [Q# blog](https://devblogs.microsoft.com/qsharp/) describes this sample in more From 212f3b8ac6e3141f2af880077402e0e3b8486c4e Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 13 Jul 2021 10:49:41 +0200 Subject: [PATCH 7/8] Apply suggestions from code review Co-authored-by: Chris Granade --- samples/runtime/README.md | 2 +- samples/runtime/autosubstitution/AutoSubstitution.qs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/runtime/README.md b/samples/runtime/README.md index 36a30bbe6622..78e141def3b7 100644 --- a/samples/runtime/README.md +++ b/samples/runtime/README.md @@ -3,7 +3,7 @@ These samples show how to work with the Q# simulation runtime. - **[Auto substitution](./autosubstitution)**: - This sample uses the `Microsoft.Quantum.AutoSubstitution` NuGet package to provide alternative operations based on the simulator that is used. + This sample uses the [`Microsoft.Quantum.AutoSubstitution` NuGet package](http://nuget.org/packages/Microsoft.Quantum.AutoSubstitution) to provide alternative operations based on the simulator that is used. - **[Oracle Emulation](./oracle-emulation)**: This sample shows how to provide accelerated simulation functionality for different Q# operations and functions. - **[Reversible simulator (simple)](./reversible-simulator-simple)**: diff --git a/samples/runtime/autosubstitution/AutoSubstitution.qs b/samples/runtime/autosubstitution/AutoSubstitution.qs index 6936b6313c46..c0f043a9521e 100644 --- a/samples/runtime/autosubstitution/AutoSubstitution.qs +++ b/samples/runtime/autosubstitution/AutoSubstitution.qs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. namespace Project { From 2f83469461af6478d495c3e5a5baa2a91dc033ee Mon Sep 17 00:00:00 2001 From: Mathias Soeken Date: Tue, 13 Jul 2021 11:08:46 +0200 Subject: [PATCH 8/8] Addressing reviewer comments. --- samples/runtime/autosubstitution/AutoSubstitution.qs | 10 +++++----- samples/runtime/autosubstitution/README.md | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/samples/runtime/autosubstitution/AutoSubstitution.qs b/samples/runtime/autosubstitution/AutoSubstitution.qs index c0f043a9521e..11195a0e164e 100644 --- a/samples/runtime/autosubstitution/AutoSubstitution.qs +++ b/samples/runtime/autosubstitution/AutoSubstitution.qs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -namespace Project { +namespace Microsoft.Quantum.Sample { open Microsoft.Quantum.Convert; open Microsoft.Quantum.Math; open Microsoft.Quantum.Measurement; @@ -9,7 +9,7 @@ namespace Project { open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Targeting; - operation ClassicalSWAP(a : Qubit, b : Qubit) : Unit is Adj + Ctl { + operation ApplyClassicalSWAP(a : Qubit, b : Qubit) : Unit is Adj + Ctl { Message("Classical version"); CNOT(a, b); CNOT(b, a); @@ -17,13 +17,13 @@ namespace Project { } // This attribute indicates that when running this Q# program with - // ToffoliSimulator, the operation `ClassicalSWAP` is executed instead. - @SubstitutableOnTarget("Project.ClassicalSWAP", "ToffoliSimulator") + // ToffoliSimulator, the operation `ApplyClassicalSWAP` is executed instead. + @SubstitutableOnTarget("Microsoft.Quantum.Sample.ApplyClassicalSWAP", "ToffoliSimulator") operation ApplySingleDirectionSWAP(a : Qubit, b : Qubit) : Unit is Adj + Ctl { // Note: In version 0.18.2106148911 we must explicitly reference the // operation; otherwise, the compiler removes the operation from the // compilation unit before the auto-substitution rewrite step is executed. - let _ = ClassicalSWAP; + let _ = ApplyClassicalSWAP; Message("Quantum version"); diff --git a/samples/runtime/autosubstitution/README.md b/samples/runtime/autosubstitution/README.md index 35894f6441f7..fc9fa0dae9a2 100644 --- a/samples/runtime/autosubstitution/README.md +++ b/samples/runtime/autosubstitution/README.md @@ -11,9 +11,10 @@ description: "This sample uses the `Microsoft.Quantum.AutoSubstitution` NuGet pa The `Microsoft.Quantum.AutoSubstitution` NuGet package offers an attribute `@SubstitutableOnTarget` that lets you replace one operation with another when using a particular simulator. For instance, decorating the operation `Op` with `@SubstitutableOnTarget("AltOp", "Sim")` lets the compiler perform a rewrite step that can link an operation `Op` to a set of pairs of an alternative operations `AltOp` and a simulator `Sim`, such that `AltOp` is executed as a replacement for `Op` when being invoked in `Sim`. -[This post](https://devblogs.microsoft.com/qsharp/) in the [Q# -blog](https://devblogs.microsoft.com/qsharp/) describes this sample in more -detail. +[This auto substitution blog +post](https://devblogs.microsoft.com/qsharp/the-autosubstitution-rewrite-step/) +in the [Q# blog](https://devblogs.microsoft.com/qsharp/) describes this sample +in more detail. ## Running the program