FermionOperator.hpp 6.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*******************************************************************************
 * 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
 *******************************************************************************/
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
#ifndef XACC_FERMIONOPERATOR_HPP_
#define XACC_FERMIONOPERATOR_HPP_

#include "Observable.hpp"
#include "operators.hpp"
#include <memory>
#include <unordered_map>
#include "Cloneable.hpp"

namespace xacc {
namespace quantum {
class FermionOperator;
}
} // namespace xacc

bool operator==(const xacc::quantum::FermionOperator &lhs,
                const xacc::quantum::FermionOperator &rhs);

namespace xacc {
namespace quantum {
using Operator = std::pair<int, bool>;
using Operators = std::vector<Operator>;
// using SiteMap = std::map<Operators, std::complex<double>>;

using FermionTermTuple = std::tuple<std::complex<double>, Operators, std::string>;
class FermionTerm : public FermionTermTuple,
             public tao::operators::commutative_multipliable<FermionTerm>,
             public tao::operators::equality_comparable<FermionTerm> {

public:
  FermionTerm() {
    std::get<0>(*this) = std::complex<double>(1.0, 0.0);
    std::get<1>(*this) = {};
    std::get<2>(*this) = "";
  }

  FermionTerm(const FermionTerm &t) {
    std::get<0>(*this) = std::get<0>(t);
    std::get<1>(*this) = std::get<1>(t);
    std::get<2>(*this) = std::get<2>(t);
  }

  FermionTerm(std::complex<double> c) {
    std::get<0>(*this) = c;
    std::get<1>(*this) = {};
    std::get<2>(*this) = "";
  }

 FermionTerm(double c) {
    std::get<0>(*this) = std::complex<double>(c,0.0);
    std::get<1>(*this) = {};
    std::get<2>(*this) = "";

  }

  FermionTerm(std::complex<double> c, Operators ops) {
    std::get<0>(*this) = c;
    std::get<1>(*this) = ops;
    std::get<2>(*this) = "";
  }

 FermionTerm(std::complex<double> c, Operators ops, std::string var) {
    std::get<0>(*this) = c;
    std::get<1>(*this) = ops;
    std::get<2>(*this) = var;
  }
  FermionTerm(Operators ops) {
    std::get<0>(*this) = std::complex<double>(1.0, 0.0);
    std::get<1>(*this) = ops;
    std::get<2>(*this) = "";
  }

  static const std::string id(const Operators &ops) {
    std::vector<int> creations, annhilations;
    for (auto &t : ops) {
      if (t.second) {
        creations.push_back(t.first);
      } else {
        annhilations.push_back(t.first);
      }
    }

    // std::sort(creations.rbegin(), creations.rend());
    // std::sort(annhilations.rbegin(), annhilations.rend());

    std::stringstream s;
    for (auto &t : creations) {
      s << t << "^" << std::string(" ");
    }
    for (auto &t : annhilations) {
      s << t << std::string(" ");
    }

    if (s.str().empty()) {
      return "I";
    }

    return s.str();
  }

  const std::string id() {
    std::vector<int> creations, annhilations;
    for (auto &t : ops()) {
      if (t.second) {
        creations.push_back(t.first);
      } else {
        annhilations.push_back(t.first);
      }
    }

    // std::sort(creations.rbegin(), creations.rend());
    // std::sort(annhilations.rbegin(), annhilations.rend());

    std::stringstream s;
    for (auto &t : creations) {
      s << t << "^" << std::string(" ");
    }

    for (auto &t : annhilations) {
      s << t << std::string(" ");
    }

    if (s.str().empty()) {
      return "I";
    }

    return s.str();
  }

  Operators &ops() { return std::get<1>(*this); }

  std::complex<double> &coeff() { return std::get<0>(*this); }
  std::string var() {return std::get<2>(*this);}

  FermionTerm &operator*=(const FermionTerm &v) noexcept;

  bool operator==(const FermionTerm &v) noexcept {
    return (std::get<1>(*this) == std::get<1>(v));
  }
};

class FermionOperator
    : public Observable, public Cloneable<Observable>,
      public std::enable_shared_from_this<FermionOperator>,
      public tao::operators::commutative_ring<FermionOperator>,
      public tao::operators::equality_comparable<FermionOperator>,
      public tao::operators::commutative_multipliable<FermionOperator, double>,
      public tao::operators::commutative_multipliable<FermionOperator,
                                                      std::complex<double>> {

protected:
  std::unordered_map<std::string, FermionTerm> terms;

public:
  std::shared_ptr<Observable> clone() override {
      return std::make_shared<FermionOperator>();
  }
  FermionOperator();
  FermionOperator(std::complex<double> c);
  FermionOperator(double c);
  FermionOperator(std::string fromString);
  FermionOperator(const FermionOperator &i);
  FermionOperator(Operators operators);
  FermionOperator(Operators operators, std::complex<double> coeff);
  FermionOperator(Operators operators, double coeff);
  FermionOperator(Operators operators, double coeff, std::string var);

  std::vector<std::shared_ptr<CompositeInstruction>>
  observe(std::shared_ptr<CompositeInstruction> function) override;
  const std::string toString() override;
  void fromString(const std::string str) override;
  const int nBits() override;

  void clear();
  std::unordered_map<std::string, FermionTerm> getTerms() const { return terms; }

  FermionOperator &operator+=(const FermionOperator &v) noexcept;
  FermionOperator &operator-=(const FermionOperator &v) noexcept;
  FermionOperator &operator*=(const FermionOperator &v) noexcept;
  bool operator==(const FermionOperator &v) noexcept;
  FermionOperator &operator*=(const double v) noexcept;
  FermionOperator &operator*=(const std::complex<double> v) noexcept;

  const std::string name() const override {
      return "fermion";
  }

  const std::string description() const override {
      return "";
  }
203
204
205
void fromOptions(const HeterogeneousMap& options) override {
      return;
  }
206

207
208
209
  std::shared_ptr<Observable>
  commutator(std::shared_ptr<Observable> obs) override;

210
211
212
213
214
};

} // namespace quantum
} // namespace xacc
#endif