PauliOperator.hpp 9.69 KB
Newer Older
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*******************************************************************************
 * 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 QUANTUM_UTILS_PAULIOPERATOR_HPP_
#define QUANTUM_UTILS_PAULIOPERATOR_HPP_
#include <ios>
#include <unordered_map>
#include <complex>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#include "Observable.hpp"
#include "IR.hpp"

#include "Cloneable.hpp"

// Putting this here due to clang error
// not able to find operator!= from operators.hpp
namespace xacc {
namespace quantum {
class PauliOperator;
}
} // namespace xacc

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

#include "operators.hpp"

namespace xacc {

namespace quantum {

// A Term can be a coefficient, a variable coefficient, and the terms themselves
using TermTuple =
    std::tuple<std::complex<double>, std::string, std::map<int, std::string>>;
using c = std::complex<double>;
using ActionResult = std::pair<std::string, c>;
enum ActionType { Bra, Ket };
class Triplet : std::tuple<std::uint64_t, std::uint64_t, std::complex<double>> {
public:
  Triplet(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); }
};

class Term : public TermTuple,
             public tao::operators::commutative_multipliable<Term>,
             public tao::operators::equality_comparable<Term> {

protected:
  // static std::map<std::string, std::pair<c, std::string>>
  void create_map() {
    // static std::map<std::string, std::pair<c, std::string>> m;
    pauliProducts.insert({"II", {c(1.0, 0.0), "I"}});
    pauliProducts.insert({"IX", {c(1.0, 0.0), "X"}});
    pauliProducts.insert({"XI", {c(1.0, 0.0), "X"}});
    pauliProducts.insert({"IY", {c(1.0, 0.0), "Y"}});
    pauliProducts.insert({"YI", {c(1.0, 0.0), "Y"}});
    pauliProducts.insert({"ZI", {c(1.0, 0.0), "Z"}});
    pauliProducts.insert({"IZ", {c(1.0, 0.0), "Z"}});
    pauliProducts.insert({"XX", {c(1.0, 0.0), "I"}});
    pauliProducts.insert({"YY", {c(1.0, 0.0), "I"}});
    pauliProducts.insert({"ZZ", {c(1.0, 0.0), "I"}});
    pauliProducts.insert({"XY", {c(0.0, 1.0), "Z"}});
    pauliProducts.insert({"XZ", {c(0.0, -1.0), "Y"}});
    pauliProducts.insert({"YX", {c(0.0, -1.0), "Z"}});
    pauliProducts.insert({"YZ", {c(0.0, 1.0), "X"}});
    pauliProducts.insert({"ZX", {c(0.0, 1.0), "Y"}});
    pauliProducts.insert({"ZY", {c(0.0, -1.0), "X"}});
    // return m;
  }

  std::map<std::string, std::pair<c, std::string>> pauliProducts;

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

  Term(const Term &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);
    create_map();
  }

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

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

  Term(std::complex<double> c, std::map<int, std::string> ops) {
    std::get<0>(*this) = c;
    std::get<1>(*this) = "";
    std::get<2>(*this) = ops;
    create_map();
  }

  Term(std::string var) {
    std::get<0>(*this) = std::complex<double>(1, 0);
    std::get<1>(*this) = var;
    std::get<2>(*this) = {};
    create_map();
  }

  Term(std::complex<double> c, std::string var) {
    std::get<0>(*this) = c;
    std::get<1>(*this) = var;
    std::get<2>(*this) = {};
    create_map();
  }

  Term(std::string var, std::map<int, std::string> ops) {
    std::get<0>(*this) = std::complex<double>(1, 0);
    std::get<1>(*this) = var;
    std::get<2>(*this) = ops;
    create_map();
  }

  Term(std::complex<double> c, std::string var,
       std::map<int, std::string> ops) {
    std::get<0>(*this) = c;
    std::get<1>(*this) = var;
    std::get<2>(*this) = ops;
    create_map();
  }

  Term(std::map<int, std::string> ops) {
    std::get<0>(*this) = std::complex<double>(1, 0);
    std::get<1>(*this) = "";
    std::get<2>(*this) = ops;
    create_map();
  }

  static const std::string id(const std::map<int, std::string> &ops,
                              const std::string &var = "") {
    std::string s;
    s = var;
    for (auto &t : ops) {
      if (t.second != "I") {
        s += t.second + std::to_string(t.first);
      }
    }

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

    return s;
  }

  const std::string id() const {
    std::string s;
    s = std::get<1>(*this);
    for (auto &t : std::get<2>(*this)) {
      if (t.second != "I") {
        s += t.second + std::to_string(t.first);
      }
    }

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

    return s;
  }

  std::map<int, std::string> &ops() { return std::get<2>(*this); }

  bool isIdentity() {
    if (ops().empty() || (ops().size() == 1 && ops().begin()->second == "I")) {
      return true;
    } else {
      return false;
    }
  }

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

  std::string &var() { return std::get<1>(*this); }

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

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

  std::vector<Triplet> getSparseMatrixElements(const int nQubits);

  ActionResult action(const std::string &bitString, ActionType type);

  std::pair<std::vector<int>, std::vector<int>>
  toBinaryVector(const int nQubits);
};

class PauliOperator
    : public xacc::Observable, public xacc::Cloneable<Observable>,
      public tao::operators::commutative_ring<PauliOperator>,
      public tao::operators::equality_comparable<PauliOperator>,
      public tao::operators::commutative_multipliable<PauliOperator, double>,
      public tao::operators::commutative_multipliable<PauliOperator,
                                                      std::complex<double>> {
protected:
  std::unordered_map<std::string, Term> terms;

public:
  std::shared_ptr<Observable> clone() override {
      return std::make_shared<PauliOperator>();
  }

  std::unordered_map<std::string, Term>::iterator begin() {
    return terms.begin();
  }
  std::unordered_map<std::string, Term>::iterator end() { return terms.end(); }

  PauliOperator();
  PauliOperator(std::complex<double> c);
  PauliOperator(double c);
  PauliOperator(std::string fromString);
  PauliOperator(std::complex<double> c, std::string var);
  PauliOperator(const PauliOperator &i);
  PauliOperator(std::map<int, std::string> operators);
  PauliOperator(std::map<int, std::string> operators, std::string var);
  PauliOperator(std::map<int, std::string> operators,
                std::complex<double> coeff);
  PauliOperator(std::map<int, std::string> operators, double coeff);
  PauliOperator(std::map<int, std::string> operators,
                std::complex<double> coeff, std::string var);

  std::vector<std::shared_ptr<CompositeInstruction>>
  observe(std::shared_ptr<CompositeInstruction> function) override;

  const std::vector<std::pair<std::string, std::complex<double>>>
  computeActionOnKet(const std::string &bitString);
  const std::vector<std::pair<std::string, std::complex<double>>>
  computeActionOnBra(const std::string &bitString);

  const int nTerms();
  std::pair<std::vector<int>, std::vector<int>>
  toBinaryVectors(const int nQubits) {
    return getTerms().begin()->second.toBinaryVector(nQubits);
  }

  const std::string toString() override;
  void fromString(const std::string str) override;

  bool contains(PauliOperator &op);
  bool commutes(PauliOperator &op);

  void mapQubitSites(std::map<int, int> &siteMap) {
    PauliOperator op;
    for (auto &termKv : *this) {
      auto ops = termKv.second.ops();
      std::map<int, std::string> nops;
      for (auto &kv : ops) {
        nops.insert({siteMap[kv.first], kv.second});
      }
      op += PauliOperator(nops, termKv.second.coeff());
    }

    *this = op;
  }
  void clear();

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

  std::vector<Triplet> getSparseMatrixElements();
  std::vector<std::complex<double>> toDenseMatrix(const int nQubits);

  std::shared_ptr<IR> toXACCIR();
  void fromXACCIR(std::shared_ptr<IR> ir);
  PauliOperator
  eval(const std::map<std::string, std::complex<double>> varToValMap);
  bool isClose(PauliOperator &other);
  int nQubits();
  const int nBits() override {return nQubits();}

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

  const std::string name() const override {
      return "pauli";
  }
  const std::string description() const override {
      return "";
  }
};
} // namespace quantum
} // namespace xacc

#endif