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 178
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 all 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,31 @@ | ||
# Microsoft Quantum QAOA Library | ||
|
||
This library provides a hybrid quantum-classical algorithm for solving optimization problems. | ||
It includes a Q# implementation of the Quantum Approximate Optimization Algorithm ([QAOA](https://arxiv.org/abs/1411.4028)) together with a classical optimizer in C#. | ||
The classical optimizer uses a quantum objective function to choose hopefully optimal parameters for running the QAOA. | ||
The quantum objective function is currently evaluated on a simulator backend provided by Q#. | ||
|
||
## Building and testing ## | ||
|
||
The quantum QAOA library consists of a cross-platform project built using [.NET Core](https://docs.microsoft.com/en-us/dotnet/core/): | ||
|
||
- **Qaoa.csproj**: Q# and C# sources used to encode combinatorial optimization problems and run QAOA and hybrid QAOA algorithms. | ||
|
||
The high-level diagram of the implementation (notation comes from the [QAOA paper](https://arxiv.org/abs/1411.4028)): | ||
|
||
[](https://postimg.cc/XpQTBTnw) | ||
|
||
Once .NET Core is installed, you may build and run its tests by executing the following from a command line: | ||
|
||
```bash | ||
dotnet test tests | ||
``` | ||
|
||
For more details about creating and running tests in Q#, | ||
see the [Testing and debugging](https://docs.microsoft.com/quantum/quantum-techniques-testinganddebugging) | ||
section of the [developer's guide](https://docs.microsoft.com/quantum). | ||
|
||
Dependencies: | ||
|
||
1) [Q# and Microsoft Quantum Libraries](https://docs.microsoft.com/en-us/quantum/language/) | ||
2) [C# Accord Math library](http://accord-framework.net/docs/html/N_Accord_Math.htm) |
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,155 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.Quantum.Qaoa.Jupyter | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Jupyter.Core; | ||
using Microsoft.Quantum.Qaoa.QaoaHybrid; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// This class makes it possible to run the hybrid QAOA with user-defined input parameters in languages other than C# that are supported by this mechanism. | ||
/// </summary> | ||
public class HybridQaoaRunMagic : MagicSymbol | ||
{ | ||
|
||
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 JSON input. Initial beta and gamma coefficients are provided by a user." | ||
}; | ||
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> | ||
/// A parameter related to the depth of a QAOA circuit. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "NHamiltonianApplications")] | ||
public int NHamiltonianApplications { get; set; } | ||
|
||
/// <summary> | ||
/// Description of a combinatorial problem to be solved. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "problem_instance")] | ||
public QaoaProblemInstance QaoaProblemInstance { get; set; } | ||
|
||
/// <summary> | ||
/// Initial QAOA parameters (beta and gamma angles). | ||
/// </summary> | ||
[JsonProperty(PropertyName = "initial_qaoa_parameters")] | ||
public QaoaParameters InitialQaoaParameters { get; set; } | ||
|
||
/// <summary> | ||
/// Flag whether optimization should be logged into a file. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "should_log")] | ||
public Boolean ShouldLog { get; set; } = false; | ||
} | ||
|
||
/// <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); | ||
|
||
var hybridQaoa = new HybridQaoa(args.NumberOfIterations, args.NHamiltonianApplications, args.QaoaProblemInstance, args.ShouldLog); | ||
|
||
return hybridQaoa.RunOptimization(args.InitialQaoaParameters).ToExecutionResult(); | ||
} | ||
} | ||
|
||
dlasecki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// <summary> | ||
/// This class makes it possible to run the hybrid QAOA with randomly initialized input parameters in languages other than C# that are supported by this mechanism. | ||
/// </summary> | ||
public class HybridQaoaWithRandomParametersRunMagic : MagicSymbol | ||
{ | ||
|
||
public HybridQaoaWithRandomParametersRunMagic() | ||
{ | ||
this.Name = $"%qaoa.hybridqaoa.random.params.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. Initial beta and gamma parameters are chosen randomly." | ||
}; | ||
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> | ||
/// A parameter related to the depth of a QAOA circuit. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "NHamiltonianApplications")] | ||
public int NHamiltonianApplications { get; set; } | ||
|
||
/// <summary> | ||
/// Description of a combinatorial problem to be solved. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "problem_instance")] | ||
public QaoaProblemInstance QaoaProblemInstance { 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> | ||
/// 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); | ||
|
||
var hybridQaoa = new HybridQaoa(args.NumberOfIterations, args.NHamiltonianApplications, args.QaoaProblemInstance, args.ShouldLog); | ||
|
||
return hybridQaoa.RunOptimization(args.NumberOfRandomStartingPoints).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,63 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.Quantum.Qaoa.Jupyter | ||
{ | ||
using System.Threading.Tasks; | ||
using Microsoft.Jupyter.Core; | ||
using Microsoft.Quantum.Qaoa.QaoaHybrid; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// This class makes it possible to initialize hybrid QAOA input parameters in languages other than C# that are supported by this mechanism. | ||
/// </summary> | ||
public class HybridQaoaParametersMagic : MagicSymbol | ||
{ | ||
public HybridQaoaParametersMagic() | ||
{ | ||
this.Name = $"%qaoa.hybridqaoa.create.parameters"; | ||
this.Documentation = new Documentation() | ||
{ | ||
Summary = "Prepares a QAOA parameters object 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> | ||
/// Betas QAOA coefficients. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "beta")] | ||
public double[] Beta { get; set; } | ||
|
||
/// <summary> | ||
/// Gammas QAOA coefficients. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "gamma")] | ||
public double[] Gamma { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Prepares a QaoaParameters 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); | ||
|
||
var qaoaParameters = new QaoaParameters(args.Beta, args.Gamma); | ||
|
||
return qaoaParameters.ToExecutionResult(); | ||
} | ||
} | ||
} |
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.