XACC
ddcl.hpp
1 /*******************************************************************************
2  * Copyright (c) 2019 UT-Battelle, LLC.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and Eclipse Distribution License v1.0 which accompanies this
6  * distribution. The Eclipse Public License is available at
7  * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
8  *License is available at https://eclipse.org/org/documents/edl-v10.php
9  *
10  * Contributors:
11  * Alexander J. McCaskey - initial API and implementation
12  *******************************************************************************/
13 #ifndef XACC_ALGORITHM_DDCL_HPP_
14 #define XACC_ALGORITHM_DDCL_HPP_
15 
16 #include "Algorithm.hpp"
17 #include <vector>
18 
19 namespace xacc {
20 namespace algorithm {
21 using Circuit = std::shared_ptr<CompositeInstruction>;
22 using Counts = std::map<std::string, int>;
23 
24 class LossStrategy : public Identifiable {
25 public:
26  virtual std::pair<double, std::vector<double>>
27  compute(Counts &counts, const std::vector<double> &target, const HeterogeneousMap& options = {}) = 0;
28 
29  virtual bool isValidGradientStrategy(const std::string &gradientStrategy) = 0;
30 };
31 
33 public:
34  // Generate circuits to run that will be used to
35  // compute gradients. Need the un-evaluated, original ansatz
36  // and the current iterate's parameters
37  virtual std::vector<Circuit>
38  getCircuitExecutions(Circuit circuit, const std::vector<double> &x) = 0;
39  virtual void compute(std::vector<double> &grad, std::vector<std::shared_ptr<AcceleratorBuffer>> results,
40  const std::vector<double> &q_dist,
41  const std::vector<double> &target_dist) = 0;
42 };
43 
45 public:
46  std::vector<Circuit>
47  getCircuitExecutions(Circuit circuit, const std::vector<double> &x) override {
48  return {};
49  }
50  void compute(std::vector<double> &grad, std::vector<std::shared_ptr<AcceleratorBuffer>> results,
51  const std::vector<double> &q_dist,
52  const std::vector<double> &target_dist) override {
53  return;
54  }
55  const std::string name() const override { return "null-gs"; }
56  const std::string description() const override { return ""; }
57 };
58 
59 class DDCL : public Algorithm {
60 protected:
61  std::shared_ptr<Optimizer> optimizer;
62  std::shared_ptr<CompositeInstruction> kernel;
63  std::shared_ptr<Accelerator> accelerator;
64  std::vector<double> initial_params;
65  std::vector<double> target_dist;
66  std::string gradient;
67  std::string loss;
68  bool persistBuffer = false;
69 
70  HeterogeneousMap _parameters;
71 
72 public:
73  bool initialize(const HeterogeneousMap &parameters) override;
74  const std::vector<std::string> requiredParameters() const override;
75 
76  void execute(const std::shared_ptr<AcceleratorBuffer> buffer) const override;
77  std::vector<double> execute(const std::shared_ptr<AcceleratorBuffer> buffer,
78  const std::vector<double> &parameters) override;
79  const std::string name() const override { return "ddcl"; }
80  const std::string description() const override { return ""; }
81 
82  DEFINE_ALGORITHM_CLONE(DDCL)
83 
84 };
85 } // namespace algorithm
86 } // namespace xacc
87 #endif
Definition: ddcl.hpp:24
Definition: Algorithm.hpp:34
Definition: ddcl.hpp:59
Definition: Accelerator.hpp:25
const std::string name() const override
Definition: ddcl.hpp:55
Definition: heterogeneous.hpp:45
const std::string name() const override
Definition: ddcl.hpp:79
Definition: Identifiable.hpp:25
const std::string description() const override
Definition: ddcl.hpp:80
const std::string description() const override
Definition: ddcl.hpp:56
Definition: ddcl.hpp:32