forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
204 lines (164 loc) · 7.88 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// -*- coding: utf-8 -*-
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Linq;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;
using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;
using Microsoft.Quantum.Chemistry;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using Microsoft.Quantum.Chemistry.OrbitalIntegrals;
using Microsoft.Quantum.Chemistry.Fermion;
using Microsoft.Quantum.Chemistry.QSharpFormat;
using McMaster.Extensions.CommandLineUtils;
using System.IO;
using Microsoft.Quantum.Chemistry.Broombridge;
// This loads a Hamiltonian from file and performs quantum phase estimation on
// - Jordan–Wigner Trotter step
// - Jordan–Wigner optimized Trotter step
// - Jordan–Wigner Qubitization iterate
namespace Microsoft.Quantum.Chemistry.Samples
{
class Program
{
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"
);
[Option("--n-bits", Description = "Number of bits of precision to report from phase estimation.")]
public int NBits { get; } = 10;
[Option("--n-reps", Description = "Number of repetitions to use to find the minimum energy.")]
public int NRepetitions { get; } = 5;
[Option("--skip-jw", Description = "Skips simulating phase estimation with a Jordan–Wigner transformed Trotter–Suzuki approximation.")]
public bool SkipJordanWigner { get; } = false;
public bool RunJordanWigner => !SkipJordanWigner;
[Option(Description = "Step size to use in the Trotter–Suzuki approximation.")]
public double StepSize { get; } = 0.4;
[Option("--skip-opt-jw", Description = "Skips simulating phase estimation with an optimized Jordan–Wigner transformed Trotter–Suzuki approximation.")]
public bool SkipOptimizedJordanWigner { get; } = false;
public bool RunOptimizedJordanWigner => !SkipOptimizedJordanWigner;
[Option("--skip-qubitized-jw", Description = "Skips simulating phase estimation with a Jordan–Wigner transformed qubitization representation.")]
public bool SkipQubitizedJordanWigner { get; } = false;
public bool RunQubitizedJordanWigner => !SkipQubitizedJordanWigner;
static void ParseLiQuiD(string path, out int nElectrons, out FermionHamiltonian hamiltonian)
{
using var reader = File.OpenText(path);
nElectrons = 2;
hamiltonian = LiQuiDSerializer
.Deserialize(reader)
.Single()
.OrbitalIntegralHamiltonian
.ToFermionHamiltonian(IndexConvention.UpDown);
}
static void ParseBroombridge(string path, out int nElectrons, out FermionHamiltonian hamiltonian)
{
using var reader = File.OpenText(path);
var description = BroombridgeSerializer
.Deserialize(reader)
.Single();
nElectrons = description.NElectrons;
hamiltonian = description
.OrbitalIntegralHamiltonian
.ToFermionHamiltonian(IndexConvention.UpDown);
}
void OnExecute()
{
var logger = new LoggerFactory().CreateLogger<Program>();
// Directory containing integral data generated by Microsoft.
// Example Liquid data format files
// spell-checker:words tzvp
/*
"h2_sto3g_4.dat" // 4 SO
"B_sto6g.dat" // 10 SO
"Be_sto6g_10.dat" // 10 SO
"h2o_sto6g_14.dat" // 14 SO
"h2s_sto6g_22.dat" // 22 SO
"co2_sto3g_30.dat" // 30 SO
"co2_p321_54.dat" // 54 SO
"fe2s2_sto3g.dat" // 110 SO
"nitrogenase_tzvp_54.dat" // 108 SO
*/
// Read Hamiltonian terms from file.
// Stopwatch for logging time to process file.
Stopwatch stopWatch = new Stopwatch();
#region For loading data in the format consumed by Liquid.
stopWatch.Start();
logger.LogInformation($"Processing {Path}...");
int nElectrons;
FermionHamiltonian fermionHamiltonian;
if (Format == DataFormat.Broombridge)
{
ParseBroombridge(Path, out nElectrons, out fermionHamiltonian);
}
else
{
ParseLiQuiD(Path, out nElectrons, out fermionHamiltonian);
}
logger.LogInformation($"Load took {stopWatch.ElapsedMilliseconds}ms.");
stopWatch.Restart();
#endregion
// Logs spin-orbital data in Logger.Message.
logger.LogInformation(fermionHamiltonian.ToString());
// Process Hamiltonian to obtain Jordan–Wigner representation.
var jordanWignerEncoding = fermionHamiltonian.ToPauliHamiltonian(Paulis.QubitEncoding.JordanWigner);
// Create input wavefunction.
var wavefunction = fermionHamiltonian.CreateHartreeFockState(nElectrons);
// Alternately, use wavefunction contained in problem description,
// if available
// var wavefunction = problemDescription.Wavefunctions["label"].ToIndexing(IndexConvention.UpDown);
// Logs Jordan–Wigner representation data in Logger.Message.
logger.LogInformation(jordanWignerEncoding.ToString());
logger.LogInformation("End read file");
// We begin by making an instance of the simulator that we will use to run our Q# code.
using (var sim = new QuantumSimulator())
{
// Package hamiltonian and wavefunction data into a format
// consumed by Q#.
var qSharpData = QSharpFormat.Convert.ToQSharpFormat(
jordanWignerEncoding.ToQSharpFormat(),
wavefunction.ToQSharpFormat()
);
#region Calling into Q#
if (RunJordanWigner)
{
for (int i = 0; i < NRepetitions; i++)
{
var (phaseEst, energyEst) = TrotterEstimateEnergy.Run(sim, qSharpData, NBits, StepSize).Result;
logger.LogInformation($"Trotter simulation. phase: {phaseEst}; energy {energyEst}");
}
}
if (RunOptimizedJordanWigner)
{
for (int i = 0; i < NRepetitions; i++)
{
var (phaseEst, energyEst) = OptimizedTrotterEstimateEnergy.Run(sim, qSharpData, NBits - 1, StepSize).Result;
logger.LogInformation($"Optimized Trotter simulation. phase {phaseEst}; energy {energyEst}");
}
}
if (RunQubitizedJordanWigner)
{
for (int i = 0; i < NRepetitions; i++)
{
var (phaseEst, energyEst) = QubitizationEstimateEnergy.Run(sim, qSharpData, NBits - 2).Result;
logger.LogInformation($"Qubitization simulation. phase: {phaseEst}; energy {energyEst}");
}
}
#endregion
}
if (System.Diagnostics.Debugger.IsAttached)
{
System.Console.ReadLine();
}
}
}
}