XACC
commuting_set_generator.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 VQE_TRANSFORMATION_COMMUTINGSETGENERATOR_HPP_
14 #define VQE_TRANSFORMATION_COMMUTINGSETGENERATOR_HPP_
15 
16 #include "PauliOperator.hpp"
17 #include <Eigen/Core>
18 #include <numeric>
19 
20 using namespace xacc::quantum;
21 
22 namespace xacc {
23 
24 namespace vqe {
26 
27 private:
28 
29  std::pair<Eigen::VectorXi, Eigen::VectorXi> bv(Term& op, int nQubits) {
30  Eigen::VectorXi vx = Eigen::VectorXi::Zero(nQubits);
31  Eigen::VectorXi vz = Eigen::VectorXi::Zero(nQubits);
32 
33  for (auto term : op.ops()) {
34  if (term.second == "X") {
35  vx(term.first) += 1;
36  } else if (term.second == "Z") {
37  vz(term.first) += 1;
38  } else if (term.second == "Y") {
39  vx(term.first) += 1;
40  vz(term.first) += 1;
41  }
42  }
43 
44  return std::make_pair(vx, vz);
45  };
46 
47  int bv_commutator(Term& term1, Term& term2, int nQubits) {
48  auto pair1 = bv(term1, nQubits);
49  auto pair2 = bv(term2, nQubits);
50  auto scalar = pair1.first.dot(pair2.second) + pair1.second.dot(pair2.first);
51  return scalar % 2;
52  };
53 
54 public:
55 
56  std::vector<std::vector<Term>> getCommutingSet(
57  PauliOperator& composite, int n_qubits) {
58 
59  std::vector<std::vector<Term>> commuting_ops;
60  std::vector<Term> allTerms;
61  for (auto& kv : composite.getTerms()) {
62  allTerms.push_back(kv.second);
63  }
64 
65  for (int i = 0; i < allTerms.size(); i++) {
66 
67  auto t_i = allTerms[i];
68 
69  if (i == 0) {
70  commuting_ops.push_back({t_i});
71  } else {
72  auto comm_ticker = 0;
73  for (int j = 0; j < commuting_ops.size(); j++) {
74  auto j_op_list = commuting_ops[j];
75  int sum = 0;
76  int innerCounter = 0;
77  for (auto j_op : j_op_list) {
78  auto t_jopPtr = allTerms[innerCounter];
79  sum += bv_commutator(t_i, t_jopPtr,
80  n_qubits);
81  innerCounter++;
82  }
83 
84  if (sum == 0) {
85  commuting_ops[j].push_back(t_i);
86  comm_ticker += 1;
87  break;
88  }
89  }
90 
91  if (comm_ticker == 0) {
92  commuting_ops.push_back({t_i});
93  }
94  }
95  }
96 
97  return commuting_ops;
98  }
99 
100 };
101 
102 }
103 }
104 
105 #endif
Definition: commuting_set_generator.hpp:25
Definition: Accelerator.hpp:25
Definition: PauliOperator.hpp:213
Definition: PauliOperator.hpp:51
Definition: DefaultParameterSetter.cpp:17