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 181
Hybrid QAOA #309
Open
dlasecki
wants to merge
65
commits into
microsoft:feature/qaoa
Choose a base branch
from
dlasecki:qaoa-integration
base: feature/qaoa
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hybrid QAOA #309
Changes from 41 commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
da1b799
Merge pull request #1 from microsoft/master
dlasecki c956426
QAOA files added.
dlasecki 46aceca
Unit test for running the hybrid QAOA added.
dlasecki e39f121
Unit test for hybrid qaoa added. Code refactoring.
dlasecki f8f6369
QAOA magic added with unit tests.
dlasecki 227623a
Code refactoring.
dlasecki 9c9bb74
Code refactoring
dlasecki 6a3b06d
Build files updated.
dlasecki 7facf12
Project structure improved. Basic quantum unit tests added.
dlasecki 300495c
Qaoa unit tests extended. Code refactoring.
dlasecki 0f4789f
Logger class added. Code refactoring.
dlasecki 5b3f95b
Code refactoring.
dlasecki b811a81
Code refactoring.
dlasecki d5b7409
Code refactoring.
dlasecki 20ced98
gitignore update
dlasecki 8c34525
Unnecessary files removed.
dlasecki b3fd8bb
Files added.
dlasecki 69c7208
Merge pull request #2 from microsoft/master
dlasecki eb7c0e5
QAOA files added.
dlasecki ce4d776
Unit test for running the hybrid QAOA added.
dlasecki 06a04ba
Unit test for hybrid qaoa added. Code refactoring.
dlasecki 07765de
QAOA magic added with unit tests.
dlasecki 2133c2c
Code refactoring.
dlasecki 9e16671
Code refactoring
dlasecki a2603b6
Build files updated.
dlasecki 06e1842
Project structure improved. Basic quantum unit tests added.
dlasecki 25cf02c
Qaoa unit tests extended. Code refactoring.
dlasecki 3ecf43a
Logger class added. Code refactoring.
dlasecki 0f35f5d
Code refactoring.
dlasecki 63acbd4
Code refactoring.
dlasecki 172a9d9
Code refactoring.
dlasecki fe3a968
gitignore update
dlasecki 45c75f7
Unnecessary files removed.
dlasecki 4edf93a
Files added.
dlasecki 867bfc7
Rebase
dlasecki 915d62c
Documentation extended.
dlasecki 2b6f429
QAOA.sln modfied.
dlasecki a33c6aa
Removed unnecessary files.
dlasecki 7c189c5
Code refactoring.
dlasecki c5a7f81
Code refactoring.
dlasecki edfc71e
Logger closing fix.
dlasecki a664bd6
Code review edits.
dlasecki a21e53d
Code review edits.
dlasecki 68869a4
Code review edits.
dlasecki 9087d5e
Improved handling of a hybrid QAOA with user-defined input parameters…
dlasecki f328713
Disposable pattern for QaoaLogger implemented. Code refactoring.
dlasecki 2cf93ca
Code refactoring.
dlasecki 9db4c4e
Copyright headers fix.
dlasecki 7951491
Documentation and naming improved.
dlasecki 2314b6c
Code refactoring.
dlasecki 2ebf375
Namespace naming improved.
dlasecki 33f8e4b
Several classes made internal, code refactoring.
dlasecki cbbba9b
Code refactoring.
dlasecki 5c8e20d
Starting point for a readme added.
dlasecki 8478615
Bug fixes.
dlasecki 67812c3
Improved assembly name added.
dlasecki 8be5bea
Build files fixed.
dlasecki f47381f
Classical arguments made first in function signatures.
dlasecki 264c021
Improved usages of a logger.
dlasecki dae62ca
Made a method async.
dlasecki 3fc5d2a
Code refactoring.
dlasecki c9623b8
Improved documentation.
dlasecki 74ed626
Improved naming of the p parameter.
dlasecki f50e31d
ModeFinder fixed.
dlasecki 0dc22bb
Merge branch 'feature/qaoa' into qaoa-integration
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -329,4 +329,3 @@ __pycache__/ | |
| *.btm.cs | ||
| *.odx.cs | ||
| *.xsd.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
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
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,53 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.29920.165 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QAOA", "QAOA\src\QAOA.csproj", "{159D83BF-56A2-41B7-951B-35292F5A9F23}" | ||
| EndProject | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HybridQaoaTests", "QAOA\tests\HybridQaoaTests\HybridQaoaTests.csproj", "{1623F038-210A-4BA2-8D4E-9AF611610829}" | ||
| EndProject | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JupyterTests", "QAOA\tests\JupyterTests\JupyterTests.csproj", "{3C8B4AF6-5495-4F23-B7B2-634E5A667837}" | ||
| EndProject | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QaoaTests", "QAOA\tests\QaoaTests\QaoaTests.csproj", "{208C0012-D62F-4F29-8738-394A62BBE842}" | ||
| EndProject | ||
| Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AD73B607-0EF5-4C11-8832-22D584939780}" | ||
| EndProject | ||
| Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{80082123-4392-4F0E-8F87-26183C620693}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {159D83BF-56A2-41B7-951B-35292F5A9F23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {159D83BF-56A2-41B7-951B-35292F5A9F23}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {159D83BF-56A2-41B7-951B-35292F5A9F23}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {159D83BF-56A2-41B7-951B-35292F5A9F23}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {1623F038-210A-4BA2-8D4E-9AF611610829}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {1623F038-210A-4BA2-8D4E-9AF611610829}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {1623F038-210A-4BA2-8D4E-9AF611610829}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {1623F038-210A-4BA2-8D4E-9AF611610829}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {3C8B4AF6-5495-4F23-B7B2-634E5A667837}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {3C8B4AF6-5495-4F23-B7B2-634E5A667837}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {3C8B4AF6-5495-4F23-B7B2-634E5A667837}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {3C8B4AF6-5495-4F23-B7B2-634E5A667837}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {208C0012-D62F-4F29-8738-394A62BBE842}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {208C0012-D62F-4F29-8738-394A62BBE842}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {208C0012-D62F-4F29-8738-394A62BBE842}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {208C0012-D62F-4F29-8738-394A62BBE842}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(NestedProjects) = preSolution | ||
| {159D83BF-56A2-41B7-951B-35292F5A9F23} = {AD73B607-0EF5-4C11-8832-22D584939780} | ||
| {1623F038-210A-4BA2-8D4E-9AF611610829} = {80082123-4392-4F0E-8F87-26183C620693} | ||
| {3C8B4AF6-5495-4F23-B7B2-634E5A667837} = {80082123-4392-4F0E-8F87-26183C620693} | ||
| {208C0012-D62F-4F29-8738-394A62BBE842} = {80082123-4392-4F0E-8F87-26183C620693} | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {09481F99-E1AC-44CE-A892-7331820661DF} | ||
| EndGlobalSection | ||
| EndGlobal |
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,72 @@ | ||
| | ||
|
dlasecki marked this conversation as resolved.
Outdated
dlasecki marked this conversation as resolved.
Outdated
|
||
|
|
||
| namespace Quantum.QAOA | ||
| { | ||
| using Microsoft.Quantum.QAOA.QaoaHybrid; | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
| using System; | ||
|
|
||
|
|
||
| class Examples | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| //PARAMETERS | ||
| int numberOfIterations = 50; | ||
| int p = 3; | ||
| int numberOfRandomStartingPoints = 1; | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
|
|
||
| //EXAMPLES | ||
|
|
||
| //Quantum Santa (http://quantumalgorithmzoo.org/traveling_santa/) | ||
| double[] dtx = { 0.619193, 0.742566, 0.060035, -1.568955, 0.045490 }; | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
| double[] dtz = { 3.182203, -1.139045, 0.221082, 0.537753, -0.417222 }; | ||
| double[] segmentCosts = { 4.70, 9.09, 9.03, 5.70, 8.02, 1.71 }; | ||
| double[] dh = { 4 * 20 - 0.5 * 4.7, 4 * 20 - 0.5 * 9.09, 4 * 20 - 0.5 * 9.03, 4 * 20 - 0.5 * 5.70, 4 * 20 - 0.5 * 8.02, 4 * 20 - 0.5 * 1.71 }; | ||
| double[] dJ = { 40.0,40.0,20.0,40.0,40.0,40.0, | ||
| 40.0,40.0,40.0,20.0,40.0,40.0, | ||
| 40.0,40.0,40.0,40.0,40.0,40.0, | ||
| 40.0,40.0,40.0,40.0,40.0,40.0, | ||
| 40.0,40.0,40.0,40.0,40.0,20.0, | ||
| 40.0,40.0,40.0,40.0,40.0,40.0}; | ||
| ProblemInstance quantumSanta = new ProblemInstance(dh, dJ); | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| //MaxCut (medium.com/mdr-inc/qaoa-maxcut-using-blueqat-aaf33038f46e) | ||
| dh = new Double[] { 0,0,0,0,0}; | ||
| dJ = new Double[]{ 0,1,0,1,0, | ||
| 0,0,1,0,0, | ||
| 0,0,0,1,1, | ||
| 0,0,0,0,1, | ||
| 0,0,0,0,0}; | ||
| ProblemInstance maxCut1 = new ProblemInstance(dh, dJ); | ||
|
|
||
|
|
||
| //Rigetti MaxCut unit tests | ||
| dh = new Double[]{-0.5,0,-1,0.5}; | ||
| dJ = new Double[]{0,1,2,0, | ||
| 0,0,0.5,0, | ||
| 0,0,0,2.5, | ||
| 0,0,0,0}; | ||
| ProblemInstance maxCut2 = new ProblemInstance(dh, dJ); | ||
|
|
||
|
|
||
| dh = new Double[] { 0.8, -0.5 }; | ||
| dJ = new Double[]{ 0, -1, | ||
| 0, 0}; | ||
| ProblemInstance maxCut3 = new ProblemInstance(dh, dJ); | ||
|
|
||
| dh = new Double[] {0, 0 }; | ||
| dJ = new Double[]{ 0, 1, | ||
| 0, 0}; | ||
| ProblemInstance maxCut4 = new ProblemInstance(dh, dJ); | ||
|
|
||
| //END EXAMPLES | ||
|
|
||
| HybridQaoa cop = new HybridQaoa(numberOfIterations, p, maxCut4, numberOfRandomStartingPoints, true); | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
|
|
||
| OptimalSolution res = cop.RunOptimization(); | ||
| Console.WriteLine(res.optimalVector); | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
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,141 @@ | ||
| namespace QAOA.Jupyter | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
| { | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Jupyter.Core; | ||
| using Microsoft.Quantum.QAOA.QaoaHybrid; | ||
| using Newtonsoft.Json; | ||
|
|
||
| public class HybridQaoaRunMagic : MagicSymbol | ||
|
dlasecki marked this conversation as resolved.
|
||
| { | ||
|
|
||
| public HybridQaoaRunMagic() | ||
| { | ||
| this.Name = $"%qaoa.hybridqaoa.run"; | ||
| this.Documentation = new Documentation() { Summary = "Runs a hybrid QAOA algorithm with a classical optimizer that chooses input angles. QAOA parameters are provided as a json" }; | ||
| this.Kind = SymbolKind.Magic; | ||
| this.Execute = this.Run; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// List of arguments | ||
| /// </summary> | ||
| public class Arguments | ||
| { | ||
| /// <summary> | ||
| /// Number of iterations in the fidelity sampling. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "number_of_iterations")] | ||
| public int NumberOfIterations { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Depth of a QAOA circuit. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "p")] | ||
| public int p { get; set; } | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// <summary> | ||
| /// Description of a combinatorial problem to be solved. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "problem_instance")] | ||
| public ProblemInstance ProblemInstance { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Number of random starting points in the angles parameters spaces. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "number_of_random_starting_points")] | ||
| public int NumberOfRandomStartingPoints { get; set; } = 1; | ||
|
|
||
| /// <summary> | ||
| /// Flag whether optimization should be logged into a file. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "should_log")] | ||
| public Boolean ShouldLog { get; set; } = false; | ||
|
|
||
| /// <summary> | ||
| /// Initial beta angles. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "initial_beta")] | ||
| public Double[] InitialBeta { get; set; } = null; | ||
|
|
||
| /// <summary> | ||
| /// Initial gamma angles. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "initial_gamma")] | ||
| public Double[] InitialGamma { get; set; } = null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Runs a hybrid QAOA. | ||
| /// </summary> | ||
| public async Task<ExecutionResult> Run(string input, IChannel channel) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(input)) | ||
| { | ||
| channel.Stderr("Please provide correct arguments"); | ||
| return ExecuteStatus.Error.ToExecutionResult(); | ||
| } | ||
|
|
||
| var args = JsonConvert.DeserializeObject<Arguments>(input); | ||
|
|
||
| HybridQaoa hybridQaoa = new HybridQaoa(args.NumberOfIterations, args.p, args.ProblemInstance, args.NumberOfRandomStartingPoints, args.ShouldLog, args.InitialBeta, args.InitialGamma); | ||
|
|
||
| return hybridQaoa.RunOptimization().ToExecutionResult(); | ||
| } | ||
| } | ||
|
|
||
|
dlasecki marked this conversation as resolved.
|
||
| public class HybridQaoaProblemInstanceMagic : MagicSymbol | ||
| { | ||
| public HybridQaoaProblemInstanceMagic() | ||
| { | ||
| this.Name = $"%qaoa.hybridqaoa.create.problem.instance"; | ||
| this.Documentation = new Documentation() { Summary = "Prepares a problem instance objects that serves as one of arguments to %qaoa.hybridqaoa.run." }; | ||
| this.Kind = SymbolKind.Magic; | ||
| this.Execute = this.Run; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// List of arguments | ||
| /// </summary> | ||
| public class Arguments | ||
| { | ||
| /// <summary> | ||
| /// Coefficents for one-local Hamiltonian terms. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "one_local_hamiltonian_coefficients")] | ||
| public Double[] OneLocalHamiltonianCoefficients { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Coefficents for one-local Hamiltonian terms. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "two_local_hamiltonian_coefficients")] | ||
| public Double[] TwoLocalHamiltonianCoefficients { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Depth of a QAOA circuit. | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
| /// </summary> | ||
| [JsonProperty(PropertyName = "problem_size_in_bits")] | ||
| public int ProblemSizeInBits { get; set; } | ||
|
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Prepares a ProblemInstance object for a hybrid QAOA. | ||
| /// </summary> | ||
| public async Task<ExecutionResult> Run(string input, IChannel channel) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(input)) | ||
| { | ||
| channel.Stderr("Please provide correct arguments"); | ||
| return ExecuteStatus.Error.ToExecutionResult(); | ||
| } | ||
|
|
||
| var args = JsonConvert.DeserializeObject<Arguments>(input); | ||
|
|
||
| ProblemInstance problemInstance = new ProblemInstance(args.OneLocalHamiltonianCoefficients, args.TwoLocalHamiltonianCoefficients); | ||
|
|
||
| return problemInstance.ToExecutionResult(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
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 @@ | ||
| <Project Sdk="Microsoft.Quantum.Sdk/0.12.20072031"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Accord.Math" Version="3.8.0" /> | ||
| <PackageReference Include="Microsoft.Jupyter.Core" Version="1.3.52077" /> | ||
|
dlasecki marked this conversation as resolved.
Outdated
|
||
| <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
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.