Observable.hpp 3.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*******************************************************************************
 * Copyright (c) 2019 UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompanies this
 * distribution. The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
 *License is available at https://eclipse.org/org/documents/edl-v10.php
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
#ifndef XACC_IR_OBSERVABLE_HPP_
#define XACC_IR_OBSERVABLE_HPP_
15
#include "CompositeInstruction.hpp"
16
17
#include "Utils.hpp"

18
namespace xacc {
19
20
21
class AcceleratorBuffer;
class SparseTriplet
    : std::tuple<std::uint64_t, std::uint64_t, std::complex<double>> {
22
23
24
25
26
27
28
29
30
31
public:
  SparseTriplet(std::uint64_t r, std::uint64_t c, std::complex<double> coeff) {
    std::get<0>(*this) = r;
    std::get<1>(*this) = c;
    std::get<2>(*this) = coeff;
  }
  const std::uint64_t row() { return std::get<0>(*this); }
  const std::uint64_t col() { return std::get<1>(*this); }
  const std::complex<double> coeff() { return std::get<2>(*this); }
};
32

33
class Observable : public Identifiable {
34
public:
35
36
  virtual std::vector<std::shared_ptr<CompositeInstruction>>
  observe(std::shared_ptr<CompositeInstruction> CompositeInstruction) = 0;
Mccaskey, Alex's avatar
Mccaskey, Alex committed
37

38
39
40
41
42
43
44
45
46
47
48
49
50
51
  // Overload for observe with grouping enabled.
  virtual std::vector<std::shared_ptr<CompositeInstruction>>
  observe(std::shared_ptr<CompositeInstruction> function,
          const HeterogeneousMap &grouping_options) {
    if (grouping_options.size() != 0) {
      XACCLogger::instance()->error("Observable '" + name() +
                                    "' doesn't support Observable grouping.");
      return {};
    } else {
      // Gracefully use non-grouping method.
      return observe(function);
    }
  }

52
  virtual const std::string toString() = 0;
53
  virtual void fromString(const std::string str) = 0;
54
  virtual const int nBits() = 0;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  virtual void fromOptions(const HeterogeneousMap &&options) {
    fromOptions(options);
    return;
  }
  virtual void fromOptions(const HeterogeneousMap &options) = 0;

  virtual std::vector<std::shared_ptr<Observable>> getSubTerms() { return {}; }

  virtual std::vector<std::shared_ptr<Observable>> getNonIdentitySubTerms() {
    return {};
  }

  virtual std::shared_ptr<Observable> getIdentitySubTerm() { return nullptr; }

  virtual std::complex<double> coefficient() {
    return std::complex<double>(1.0, 0.0);
71
  }
72
73
74
75
76

  virtual std::vector<SparseTriplet>
  to_sparse_matrix() {
    return {};
  }
77

78
   virtual std::shared_ptr<Observable> commutator(std::shared_ptr<Observable>){
79
80
    return nullptr;
  }
81
82

  virtual void normalize() { return; }
83
84
85
86
87
88
89
90
91
92
93
  // Some pre-defined tasks (Observable sub-classes can have custom tasks)
  struct PostProcessingTask {
    static inline const std::string EXP_VAL_CALC = "exp-val";
    static inline const std::string VARIANCE_CALC = "variance";
  };
  // Post process the execution data (stored on the AcceleratorBuffer)
  virtual double
  postProcess(
      std::shared_ptr<AcceleratorBuffer> buffer,
      const std::string &postProcessTask = PostProcessingTask::EXP_VAL_CALC,
      const HeterogeneousMap &extra_data = {}) = 0;
94
95
};

96
97
98
99
100
template Observable *
HeterogeneousMap::getPointerLike<Observable>(const std::string key) const;
template bool
HeterogeneousMap::pointerLikeExists<Observable>(const std::string key) const;

101
102
} // namespace xacc
#endif