4 #include <xacc_internal_compiler.hpp>
6 #include "Accelerator.hpp"
7 #include "AcceleratorBuffer.hpp"
9 #include "kernel_evaluator.hpp"
10 #include "objective_function.hpp"
11 #include "qcor_observable.hpp"
12 #include "qcor_optimizer.hpp"
13 #include "qcor_utils.hpp"
15 #include "quantum_kernel.hpp"
17 using CompositeInstruction = xacc::CompositeInstruction;
18 using Accelerator = xacc::Accelerator;
19 using Identifiable = xacc::Identifiable;
25 std::shared_ptr<CompositeInstruction> circuit;
26 std::vector<std::string> symbol_names;
42 const HeterogeneousMap ¶ms = {}) = 0;
51 virtual double evaluate(std::shared_ptr<CompositeInstruction> state_prep) = 0;
55 virtual std::vector<double> evaluate(
56 std::vector<std::shared_ptr<CompositeInstruction>> state_prep_circuits) {
58 std::vector<double> result;
59 for (
auto &circuit : state_prep_circuits) {
60 result.emplace_back(evaluate(circuit));
65 virtual bool initialize(
Operator *observable,
66 const HeterogeneousMap ¶ms = {});
70 HeterogeneousMap hyperParams;
76 using TdObservable = std::function<
Operator(
double)>;
86 observable(other.observable),
87 hamiltonian(other.hamiltonian),
88 user_defined_ansatz(other.user_defined_ansatz),
89 owns_observable(other.owns_observable) {
90 if (other.owns_observable) {
92 other.owns_observable =
false;
99 observable(other.observable),
100 hamiltonian(other.hamiltonian),
101 user_defined_ansatz(other.user_defined_ansatz),
102 owns_observable(other.owns_observable) {
103 if (other.owns_observable) {
105 other.owns_observable =
false;
117 TdObservable hamiltonian;
120 std::shared_ptr<KernelFunctor> user_defined_ansatz;
126 bool owns_observable =
false;
130 if (owns_observable) {
131 printf(
"Deleting the raw ptr\n");
150 std::string ext_dir =
"Z";
152 std::vector<int> initial_spins;
157 std::function<double(
double)> time_func;
161 void fromDict(
const HeterogeneousMap ¶ms);
163 bool validateModel()
const {
164 const bool ext_dir_valid =
165 (ext_dir ==
"X" || ext_dir ==
"Y" || ext_dir ==
"Z");
166 const bool initial_spins_valid =
167 (initial_spins.empty() || (initial_spins.size() == num_spins));
168 return ext_dir_valid && initial_spins_valid;
177 Operator *obs,
const HeterogeneousMap ¶ms = {});
179 Operator &obs,
const HeterogeneousMap ¶ms = {}) {
180 return createModel(&obs, params);
187 static QuantumSimulationModel createModel(
188 Operator *obs, TdObservable td_ham,
const HeterogeneousMap ¶ms = {});
190 static QuantumSimulationModel createModel(
191 Operator &obs, TdObservable td_ham,
const HeterogeneousMap ¶ms = {}) {
192 return createModel(&obs, td_ham, params);
204 static QuantumSimulationModel createModel(
205 const std::string &format,
const std::string &data,
206 const HeterogeneousMap ¶ms = {});
208 enum class ModelType { Heisenberg };
209 static QuantumSimulationModel createModel(ModelType type,
210 const HeterogeneousMap ¶ms);
213 template <
typename... Args>
214 static inline QuantumSimulationModel createModel(
215 void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
217 Operator *obs,
size_t nbQubits,
size_t nbParams) {
218 auto kernel_functor =
219 createKernelFunctor(quantum_kernel_functor, nbQubits, nbParams);
221 QuantumSimulationModel model;
222 model.observable = obs;
223 model.user_defined_ansatz = kernel_functor;
227 template <
typename... Args>
228 static inline QuantumSimulationModel createModel(
229 void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
231 Operator &obs,
size_t nbQubits,
size_t nbParams) {
232 return createModel(quantum_kernel_functor, &obs, nbQubits, nbParams);
236 static inline QuantumSimulationModel createModel(
237 std::shared_ptr<CompositeInstruction> composite, Operator *obs) {
238 QuantumSimulationModel model;
239 model.observable = obs;
240 model.user_defined_ansatz = createKernelFunctor(composite);
244 static inline QuantumSimulationModel createModel(
245 std::shared_ptr<CompositeInstruction> composite, Operator &obs) {
246 return createModel(composite, &obs);
254 using QuantumSimulationResult = HeterogeneousMap;
258 virtual bool initialize(
const HeterogeneousMap ¶ms) = 0;
259 virtual QuantumSimulationResult execute(
263 std::shared_ptr<CostFunctionEvaluator> evaluator;
267 std::shared_ptr<QuantumSimulationWorkflow> getWorkflow(
268 const std::string &name,
const HeterogeneousMap &init_params);
271 std::shared_ptr<CostFunctionEvaluator> getObjEvaluator(
272 Operator *observable,
const std::string &name =
"default",
273 const HeterogeneousMap &init_params = {});
274 inline std::shared_ptr<CostFunctionEvaluator> getObjEvaluator(
275 Operator &obs,
const std::string &name =
"default",
276 const HeterogeneousMap &init_params = {}) {
277 return getObjEvaluator(&obs, name, init_params);
281 void executePassManager(
282 std::vector<std::shared_ptr<CompositeInstruction>> evalKernels);