Skip to content

Commit

Permalink
Parameterize chemistry cmdline samples using CommandLineUtils. (micro…
Browse files Browse the repository at this point in the history
…soft#226)

* Allow for command-line parameterization of two chem samples.

* Ported GetGateCount to use same library as other two chem samples.

* Slight fixes to get-gatecount.
  • Loading branch information
Chris Granade authored Sep 12, 2019
1 parent 714ca3e commit feed9bf
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 139 deletions.
1 change: 1 addition & 0 deletions Chemistry/AnalyzeHamiltonian/1-AnalyzeHamiltonian.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
Expand Down
55 changes: 37 additions & 18 deletions Chemistry/AnalyzeHamiltonian/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Quantum.Chemistry;
using System.Linq;
using Microsoft.Quantum.Chemistry.OrbitalIntegrals;
using McMaster.Extensions.CommandLineUtils;

// This loads a Hamiltonian from file and computes some of its features
// - L1-Norm of terms
Expand All @@ -15,7 +16,23 @@ namespace Microsoft.Quantum.Chemistry.Sample
class Program
{

static void Main(string[] args)
public static int Main(string[] args) =>
CommandLineApplication.Execute<Program>(args);

public enum DataFormat
{
LiQuiD, Broombridge
}

[Option(Description = "Format to use when loading data.")]
public DataFormat Format { get; } = DataFormat.Broombridge;

[Option(Description = "Path to data to be loaded.")]
public string Path { get; } = System.IO.Path.Combine(
"..", "IntegralData", "YAML", "lih_sto-3g_0.800_int.yaml"
);

void OnExecute()
{
var logger = Logging.LoggerFactory.CreateLogger<Program>();

Expand All @@ -33,28 +50,31 @@ static void Main(string[] args)
"nitrogenase_tzvp_54.dat" // 108 SO
*/

string LiquidRoot = @"..\IntegralData\Liquid\";
string LiquidFilename = "Be_sto6g_10.dat";

// For loading data in the format consumed by Liquid.
logger.LogInformation($"Processing {LiquidFilename}");
var generalHamiltonian0 = LiQuiD.Deserialize($@"{LiquidRoot}\{LiquidFilename}").Single()
.OrbitalIntegralHamiltonian
.ToFermionHamiltonian(IndexConvention.UpDown);
logger.LogInformation($"Processing {Path}...");
var generalHamiltonian =
(
Format == DataFormat.Broombridge

// For loading data in the YAML format.
string YAMLRoot = @"..\IntegralData\YAML\";
string YAMLFilename = "lih_sto-3g_0.800_int.yaml";
var generalHamiltonian1 = Broombridge.Deserializers.DeserializeBroombridge($@"{YAMLRoot}\{YAMLFilename}")
.ProblemDescriptions.Single()
.OrbitalIntegralHamiltonian
? Broombridge
.Deserializers
.DeserializeBroombridge(Path)
.ProblemDescriptions
.Single()
.OrbitalIntegralHamiltonian

: LiQuiD
.Deserialize(Path)
.Single()
.OrbitalIntegralHamiltonian
)
.ToFermionHamiltonian(IndexConvention.UpDown);
// Read Hamiltonian terms from file.

logger.LogInformation("End read file. Computing one-norms.");
foreach (var termType in generalHamiltonian0.Terms.Keys)

foreach (var termType in generalHamiltonian.Terms.Keys)
{
var line = $"One-norm for term type {termType}: {generalHamiltonian0.Norm(new[] { termType }, 1.0)}";
var line = $"One-norm for term type {termType}: {generalHamiltonian.Norm(new[] { termType }, 1.0)}";
logger.LogInformation(line);
}
logger.LogInformation("Computed one-norm.");
Expand All @@ -66,4 +86,3 @@ static void Main(string[] args)
}
}
}

2 changes: 1 addition & 1 deletion Chemistry/GetGateCount/3-GetGateCount.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<None Remove="TrotterGateCountEstimates.PrimitiveOperationsCounter.csv" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
<PackageReference Include="Microsoft.PowerShell.5.ReferenceAssemblies" Version="1.1.0" NoWarn="NU1701" />
<PackageReference Include="Microsoft.Quantum.Chemistry" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Standard" Version="0.8.1907.1701" />
<PackageReference Include="Microsoft.Quantum.Development.Kit" Version="0.8.1907.1701" />
<PackageReference Include="Mono.Options" Version="5.3.0.1" />
</ItemGroup>


Expand Down
114 changes: 41 additions & 73 deletions Chemistry/GetGateCount/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
// in a robust way that makes it easy to turn on and off different messages.
using Microsoft.Extensions.Logging;

// We use the Mono.Options and System.Management.Automation
// libraries to make it easy to use this sample from the command line.
using Mono.Options;
// We use the McMaster.Extensions.CommandLineUtils
// library to make it easy to use this sample from the command line.
using McMaster.Extensions.CommandLineUtils;

// Finally, we include the gate counting logic itself from GetGateCount.cs.
using static Microsoft.Quantum.Chemistry.Samples.GetGateCount;
Expand All @@ -30,83 +30,58 @@ namespace Microsoft.Quantum.Chemistry.Samples
{
class Program
{
static void Main(string[] args)
{
public static int Main(string[] args) =>
CommandLineApplication.Execute<Program>(args);


string filename = @"..\IntegralData\Liquid\h2s_sto6g_22.dat";
var format = IntegralDataFormat.Liquid;

bool runTrotterStep = true;
bool runMinQubitQubitizationStep = true;
bool runMinTCountQubitizationStep = true;
bool showHelp = false;
string outputFolder = null;

string logPath = null;

// These are arguments that can be set from command line.
var options = new OptionSet {
{ "h|?|help", "Shows this help message.", h => showHelp = true },
{ "p|path=", "Path to the integral data file to use.", f => filename = f },
{ "f|format=",
"Format to use when loading integral data.",
(string f) => format = (IntegralDataFormat) Enum.Parse(typeof(IntegralDataFormat), f)
},
{ "t|run-trotter=",
"Controls whether the Trotter simulation step will be estimated.",
(bool t) => runTrotterStep = t
},
{ "q|run-qubitization=",
"Controls whether the qubitization simulation step that minimizes qubit count will be estimated.",
(bool q) => runMinQubitQubitizationStep = q
},
{ "o|run-optimized-qubitization=",
"Controls whether the qubitization simulation step that minimizes T count will be estimated.",
(bool o) => runMinTCountQubitizationStep = o
},
{ "l|log=",
"Controls where log messages will be written to.",
(string l) => logPath = l
},
{ "output=",
"Specifies the folder into which gate count estimates should be written as CSVs.",
(string o) => outputFolder = o
}
};
[Option("-p|--path", Description = "Path to the integral data file to use.")]
public string Path { get; } = System.IO.Path.Combine(
"..", "IntegralData", "Liquid", "h2s_sto6g_22.dat"
);

// This parses the command line arguments, and catches undefined arguments.
List<string> extra;
try
{
extra = options.Parse(args);
}
catch (OptionException)
{
ShowHelp(options);
System.Environment.Exit(1);
}
[Option("-f|--format", Description="Format to use when loading integral data.")]
public IntegralDataFormat Format { get; } = IntegralDataFormat.Liquid;

[Option("--skip-trotter-suzuki", Description="If set, skips estimating for the Trotter–Suzuki simulation step.")]
public bool SkipTrotterSuzuki { get; } = false;
public bool RunTrotterSuzuki => !SkipTrotterSuzuki;

[Option("--skip-qubitization", Description = "If set, skips estimating for the qubitized simulation step.")]
public bool SkipQubitization { get; } = false;
public bool RunQubitization => !SkipQubitization;

if (showHelp)
[Option("--skip-opt-qubitization", Description = "If set, skips estimating for the optimized qubitized simulation step.")]
public bool SkipOptimizedQubitization { get; } = false;
public bool RunOptimizedQubitization => !SkipOptimizedQubitization;

[Option("-l|--log", Description = "Controls where log messages will be written to.")]
public string LogPath { get; } = null;

[Option("-o|--output", Description = "Specifies the folder into which gate count estimates should be written as CSVs.")]
public string OutputPath { get; } = null;


void OnExecute()
{
if (LogPath != null)
{
ShowHelp(options);
System.Environment.Exit(1);
Logging.LogPath = LogPath;
}

Logging.LogPath = logPath;
var logger = Logging.LoggerFactory.CreateLogger<Program>();

// Here, we specify the Hamiltonian simulation configurations we wish to run.
var configurations = Configure(
runTrotterStep: runTrotterStep,
runMinQubitQubitizationStep: runMinQubitQubitizationStep,
runMinTCountQubitizationStep: runMinTCountQubitizationStep);
runTrotterStep: RunTrotterSuzuki,
runMinQubitQubitizationStep: RunQubitization,
runMinTCountQubitizationStep: RunOptimizedQubitization
);

using (logger.BeginScope($"Using {filename}."))
using (logger.BeginScope($"Using {Path}."))
{
logger.LogInformation($"Loading...");

// Read Hamiltonian terms from file and run gate counts.
var gateCountResults = RunGateCount(filename, format, configurations, outputFolder).Result;
var gateCountResults = RunGateCount(Path, Format, configurations, OutputPath).Result;

foreach(var result in gateCountResults)
{
Expand All @@ -120,13 +95,6 @@ static void Main(string[] args)
}
}

public static void ShowHelp(OptionSet options)
{
System.Console.WriteLine("get-gatecount");
System.Console.WriteLine("Usage:");
options.WriteOptionDescriptions(Console.Out);
return;
}
}

}
Expand Down
1 change: 1 addition & 0 deletions Chemistry/RunSimulation/2-RunSimulation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
Expand Down
Loading

0 comments on commit feed9bf

Please sign in to comment.