Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Hybrid QAOA #309

Open
wants to merge 65 commits into
base: feature/qaoa
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 43 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 Jul 21, 2020
c956426
QAOA files added.
dlasecki Jul 26, 2020
46aceca
Unit test for running the hybrid QAOA added.
dlasecki Jul 26, 2020
e39f121
Unit test for hybrid qaoa added. Code refactoring.
dlasecki Jul 26, 2020
f8f6369
QAOA magic added with unit tests.
dlasecki Aug 2, 2020
227623a
Code refactoring.
dlasecki Aug 3, 2020
9c9bb74
Code refactoring
dlasecki Aug 3, 2020
6a3b06d
Build files updated.
dlasecki Aug 4, 2020
7facf12
Project structure improved. Basic quantum unit tests added.
dlasecki Aug 4, 2020
300495c
Qaoa unit tests extended. Code refactoring.
dlasecki Aug 6, 2020
0f4789f
Logger class added. Code refactoring.
dlasecki Aug 7, 2020
5b3f95b
Code refactoring.
dlasecki Aug 8, 2020
b811a81
Code refactoring.
dlasecki Aug 8, 2020
d5b7409
Code refactoring.
dlasecki Aug 8, 2020
20ced98
gitignore update
dlasecki Aug 8, 2020
8c34525
Unnecessary files removed.
dlasecki Aug 8, 2020
b3fd8bb
Files added.
dlasecki Aug 8, 2020
69c7208
Merge pull request #2 from microsoft/master
dlasecki Aug 8, 2020
eb7c0e5
QAOA files added.
dlasecki Jul 26, 2020
ce4d776
Unit test for running the hybrid QAOA added.
dlasecki Jul 26, 2020
06a04ba
Unit test for hybrid qaoa added. Code refactoring.
dlasecki Jul 26, 2020
07765de
QAOA magic added with unit tests.
dlasecki Aug 2, 2020
2133c2c
Code refactoring.
dlasecki Aug 3, 2020
9e16671
Code refactoring
dlasecki Aug 3, 2020
a2603b6
Build files updated.
dlasecki Aug 4, 2020
06e1842
Project structure improved. Basic quantum unit tests added.
dlasecki Aug 4, 2020
25cf02c
Qaoa unit tests extended. Code refactoring.
dlasecki Aug 6, 2020
3ecf43a
Logger class added. Code refactoring.
dlasecki Aug 7, 2020
0f35f5d
Code refactoring.
dlasecki Aug 8, 2020
63acbd4
Code refactoring.
dlasecki Aug 8, 2020
172a9d9
Code refactoring.
dlasecki Aug 8, 2020
fe3a968
gitignore update
dlasecki Aug 8, 2020
45c75f7
Unnecessary files removed.
dlasecki Aug 8, 2020
4edf93a
Files added.
dlasecki Aug 8, 2020
867bfc7
Rebase
dlasecki Aug 8, 2020
915d62c
Documentation extended.
dlasecki Aug 8, 2020
2b6f429
QAOA.sln modfied.
dlasecki Aug 8, 2020
a33c6aa
Removed unnecessary files.
dlasecki Aug 8, 2020
7c189c5
Code refactoring.
dlasecki Aug 8, 2020
c5a7f81
Code refactoring.
dlasecki Aug 8, 2020
edfc71e
Logger closing fix.
dlasecki Aug 10, 2020
a664bd6
Code review edits.
dlasecki Aug 11, 2020
a21e53d
Code review edits.
dlasecki Aug 13, 2020
68869a4
Code review edits.
dlasecki Aug 14, 2020
9087d5e
Improved handling of a hybrid QAOA with user-defined input parameters…
dlasecki Aug 15, 2020
f328713
Disposable pattern for QaoaLogger implemented. Code refactoring.
dlasecki Aug 22, 2020
2cf93ca
Code refactoring.
dlasecki Aug 22, 2020
9db4c4e
Copyright headers fix.
dlasecki Aug 26, 2020
7951491
Documentation and naming improved.
dlasecki Aug 26, 2020
2314b6c
Code refactoring.
dlasecki Aug 26, 2020
2ebf375
Namespace naming improved.
dlasecki Aug 26, 2020
33f8e4b
Several classes made internal, code refactoring.
dlasecki Aug 26, 2020
cbbba9b
Code refactoring.
dlasecki Aug 26, 2020
5c8e20d
Starting point for a readme added.
dlasecki Aug 26, 2020
8478615
Bug fixes.
dlasecki Aug 26, 2020
67812c3
Improved assembly name added.
dlasecki Aug 26, 2020
8be5bea
Build files fixed.
dlasecki Aug 26, 2020
f47381f
Classical arguments made first in function signatures.
dlasecki Aug 27, 2020
264c021
Improved usages of a logger.
dlasecki Aug 27, 2020
dae62ca
Made a method async.
dlasecki Aug 28, 2020
3fc5d2a
Code refactoring.
dlasecki Aug 28, 2020
c9623b8
Improved documentation.
dlasecki Aug 30, 2020
74ed626
Improved naming of the p parameter.
dlasecki Aug 30, 2020
f50e31d
ModeFinder fixed.
dlasecki Aug 31, 2020
0dc22bb
Merge branch 'feature/qaoa' into qaoa-integration
Nov 23, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,3 @@ __pycache__/
*.btm.cs
*.odx.cs
*.xsd.cs

3 changes: 3 additions & 0 deletions Build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Build-One 'publish' '../MachineLearning.sln'
Write-Host "##[info]Build Jupyter magic library"
Build-One 'publish' '../Magic.sln'

Write-Host "##[info]Build QAOA library"
Build-One 'publish' '../QAOA.sln'

if (-not $all_ok) {
throw "At least one test failed execution. Check the logs."
}
2 changes: 2 additions & 0 deletions Build/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"Microsoft.Quantum.Chemistry",
"Microsoft.Quantum.Numerics",
"Microsoft.Quantum.MachineLearning"
"Microsoft.Quantum.QAOA"
dlasecki marked this conversation as resolved.
Show resolved Hide resolved
);
Assemblies = @(
".\Standard\src\bin\$Env:BUILD_CONFIGURATION\netstandard2.1\Microsoft.Quantum.Standard.dll",
Expand All @@ -17,5 +18,6 @@
".\Chemistry\src\DataModel\bin\$Env:BUILD_CONFIGURATION\netstandard2.1\Microsoft.Quantum.Chemistry.DataModel.dll",
".\Chemistry\src\Runtime\bin\$Env:BUILD_CONFIGURATION\netstandard2.1\Microsoft.Quantum.Chemistry.Runtime.dll",
".\Chemistry\src\Tools\bin\$Env:BUILD_CONFIGURATION\netcoreapp3.1\qdk-chem.dll"
".\QAOA\src\bin\$Env:BUILD_CONFIGURATION\netcoreapp3.1\QAOA.dll"
dlasecki marked this conversation as resolved.
Show resolved Hide resolved
) | ForEach-Object { Get-Item (Join-Path $PSScriptRoot ".." $_) };
} | Write-Output;
6 changes: 6 additions & 0 deletions Build/pack.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ if ($Env:ENABLE_PYTHON -eq "false") {
Pack-Wheel '../Python/qsharp'
}

Write-Host "##[info]Pack QAOA library"
Pack-One '../QAOA/src/QAOA.csproj'

Write-Host "##[info]Pack QAOA library"
Pack-One '../QAOA/src/QAOA.csproj'

if (-not $all_ok) {
throw "At least one test failed execution. Check the logs."
}
9 changes: 9 additions & 0 deletions Build/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ Test-One '../Chemistry/tests/JupyterTests/JupyterTests.csproj'
Write-Host "##[info]Testing Numerics/tests/NumericsTests.csproj"
Test-One '../Numerics/tests/NumericsTests.csproj'

Write-Host "##[info]Testing QAOA/tests/QaoaTests/QaoaTests.csproj"
Test-One '../QAOA/tests/QaoaTests/QaoaTests.csproj'

Write-Host "##[info]Testing QAOA/tests/HybridQaoaTests/HybridQaoaTests.csproj"
Test-One '../QAOA/tests/HybridQaoaTests/HybridQaoaTests.csproj'

Write-Host "##[info]Testing QAOA/tests/JupyterTests/JupyterTests.csproj"
Test-One '../QAOA/tests/JupyterTests/JupyterTests.csproj'

if (-not $all_ok) {
throw "At least one test failed execution. Check the logs."
}
53 changes: 53 additions & 0 deletions QAOA.sln
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
71 changes: 71 additions & 0 deletions QAOA/src/Examples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Quantum.QAOA
{
using Microsoft.Quantum.QAOA.QaoaHybrid;
dlasecki marked this conversation as resolved.
Show resolved Hide resolved
using System;


class Examples
{
static void Main(string[] args)
{
//PARAMETERS
var numberOfIterations = 70;
var p = 3;
var numberOfRandomStartingPoints = 1;

//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.
Show resolved Hide resolved
double[] dtz = { 3.182203, -1.139045, 0.221082, 0.537753, -0.417222 };
double[] oneLocalHamiltonianCoefficients = { 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 };
var twoLocalHamiltonianCoefficients = new[]
{
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,
};
var quantumSanta = new ProblemInstance(oneLocalHamiltonianCoefficients, twoLocalHamiltonianCoefficients);

double[] segmentCosts = { 4.70, 9.09, 9.03, 5.70, 8.02, 1.71 };


//MaxCut (medium.com/mdr-inc/qaoa-maxcut-using-blueqat-aaf33038f46e)
oneLocalHamiltonianCoefficients = new Double[] { 0,0,0,0,0};
twoLocalHamiltonianCoefficients = 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};
var maxCut1 = new ProblemInstance(oneLocalHamiltonianCoefficients, twoLocalHamiltonianCoefficients);

//Rigetti MaxCut unit tests
oneLocalHamiltonianCoefficients = new Double[]{-0.5,0,-1,0.5};
twoLocalHamiltonianCoefficients = new Double[]{0,1,2,0,
0,0,0.5,0,
0,0,0,2.5,
0,0,0,0};
var maxCut2 = new ProblemInstance(oneLocalHamiltonianCoefficients, twoLocalHamiltonianCoefficients);

oneLocalHamiltonianCoefficients = new Double[] { 0.8, -0.5 };
twoLocalHamiltonianCoefficients = new Double[]{ 0, -1, 0, 0};
var maxCut3 = new ProblemInstance(oneLocalHamiltonianCoefficients, twoLocalHamiltonianCoefficients);

oneLocalHamiltonianCoefficients = new Double[] {0, 0 };
twoLocalHamiltonianCoefficients = new Double[]{ 0, 1, 0, 0};
var maxCut4 = new ProblemInstance(oneLocalHamiltonianCoefficients, twoLocalHamiltonianCoefficients);

//END EXAMPLES

var hybridQaoa = new HybridQaoa(numberOfIterations, p, maxCut4, numberOfRandomStartingPoints, true);

Solution result = hybridQaoa.RunOptimization();
}
}
}
dlasecki marked this conversation as resolved.
Show resolved Hide resolved
144 changes: 144 additions & 0 deletions QAOA/src/Jupyter/HybridQaoaMagic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// 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;

public class HybridQaoaRunMagic : MagicSymbol
dlasecki marked this conversation as resolved.
Show resolved Hide 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>
/// A parameter related to the depth of a QAOA circuit.
/// </summary>
[JsonProperty(PropertyName = "p")]
public int p { get; set; }
dlasecki marked this conversation as resolved.
Show resolved Hide resolved

/// <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.
Show resolved Hide 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>
/// Size of the combinatorial problem in bits.
/// </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);

var problemInstance = new ProblemInstance(args.OneLocalHamiltonianCoefficients, args.TwoLocalHamiltonianCoefficients);

return problemInstance.ToExecutionResult();
}

}
}
14 changes: 14 additions & 0 deletions QAOA/src/QAOA.csproj
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.4.80531" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
Loading