gate_general.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "gate.hpp"
4 #include "state.hpp"
5 #include "utility.hpp"
6 
11 protected:
13  std::vector<double> _distribution;
14  std::vector<double> _cumulative_distribution;
15  std::vector<QuantumGateBase*> _gate_list;
16 
17 public:
24  QuantumGate_Probabilistic(std::vector<double> distribution, std::vector<QuantumGateBase*> gate_list) {
25  _distribution = distribution;
26 
27  double sum = 0.;
28  _cumulative_distribution.push_back(0.);
29  for (auto val : distribution) {
30  sum += val;
31  _cumulative_distribution.push_back(sum);
32  }
33  for (auto gate : gate_list) {
34  _gate_list.push_back(gate->copy());
35  }
36  };
37 
39  for (unsigned int i = 0; i < _gate_list.size(); ++i) {
40  delete _gate_list[i];
41  }
42  }
43 
49  virtual void update_quantum_state(QuantumStateBase* state) override {
50 
51  double r = random.uniform();
52  auto ite = std::lower_bound(_cumulative_distribution.begin(), _cumulative_distribution.end(), r);
53  assert(ite != _cumulative_distribution.begin());
54  size_t gate_index = std::distance(_cumulative_distribution.begin(), ite) - 1;
55 
56  if (gate_index < _gate_list.size()) {
57  _gate_list[gate_index]->update_quantum_state(state);
58  }
59  };
65  virtual QuantumGateBase* copy() const override {
66  std::vector<QuantumGateBase*> new_gate_list;
67  for (auto item : _gate_list) {
68  new_gate_list.push_back(item->copy());
69  }
70  return new QuantumGate_Probabilistic(_distribution, new_gate_list);
71  };
72 
78  virtual void set_matrix(ComplexMatrix& matrix) const override {
79  std::cerr << "* Warning : Gate-matrix of probabilistic gate cannot be obtained. Identity matrix is returned." << std::endl;
80  matrix = Eigen::MatrixXcd::Ones(1, 1);
81  }
82 };
83 
88 protected:
90  std::vector<QuantumGateBase*> _gate_list;
91 
92 public:
93  QuantumGate_CPTP(std::vector<QuantumGateBase*> gate_list) {
94  for (auto gate : gate_list) {
95  _gate_list.push_back(gate->copy());
96  }
97  };
98  virtual ~QuantumGate_CPTP() {
99  for (unsigned int i = 0; i < _gate_list.size(); ++i) {
100  delete _gate_list[i];
101  }
102  }
103 
109  virtual void update_quantum_state(QuantumStateBase* state) override {
110  double r = random.uniform();
111 
112  double sum = 0.;
113  double org_norm = state->get_norm();
114 
115  auto buffer = state->copy();
116  double norm;
117  for (auto gate : _gate_list) {
118  gate->update_quantum_state(buffer);
119  norm = buffer->get_norm()/org_norm;
120  sum += norm;
121  if (r < sum) {
122  state->load(buffer);
123  state->normalize(norm);
124  break;
125  }
126  else {
127  buffer->load(state);
128  }
129  }
130  if (!(r < sum)) {
131  std::cerr << "* Warning : CPTP-map was not trace preserving. Identity-map is applied." << std::endl;
132  }
133  delete buffer;
134  };
135 
141  virtual QuantumGateBase* copy() const override {
142  std::vector<QuantumGateBase*> new_gate_list;
143  for (auto item : _gate_list) {
144  new_gate_list.push_back(item->copy());
145  }
146  return new QuantumGate_CPTP(new_gate_list);
147  };
153  virtual void set_matrix(ComplexMatrix& matrix) const override {
154  std::cerr << "* Warning : Gate-matrix of CPTP-map cannot be obtained. Identity matrix is returned." << std::endl;
155  matrix = Eigen::MatrixXcd::Ones(1, 1);
156  }
157 };
158 
163 protected:
165  std::vector<QuantumGateBase*> _gate_list;
167 
168 public:
169  QuantumGate_Instrument(std::vector<QuantumGateBase*> gate_list, UINT classical_register_address) {
170  _classical_register_address = classical_register_address;
171  for (auto gate : gate_list) {
172  _gate_list.push_back(gate->copy());
173  }
174  };
176  for (unsigned int i = 0; i < _gate_list.size(); ++i) {
177  delete _gate_list[i];
178  }
179  }
180 
186  virtual void update_quantum_state(QuantumStateBase* state) override {
187  double r = random.uniform();
188 
189  double sum = 0.;
190  double org_norm = state->get_norm();
191 
192  auto buffer = state->copy();
193  double norm;
194  UINT index = 0;
195  for (auto gate : _gate_list) {
196  gate->update_quantum_state(buffer);
197  norm = buffer->get_norm() / org_norm;
198  sum += norm;
199  if (r < sum) {
200  state->load(buffer);
201  state->normalize(norm);
202  break;
203  }
204  else {
205  buffer->load(state);
206  index++;
207  }
208  }
209  if (!(r < sum)) {
210  std::cerr << "* Warning : Instrument-map was not trace preserving. Identity-map is applied." << std::endl;
211  }
212  delete buffer;
213 
214  state->set_classical_value(_classical_register_address, index);
215  };
221  virtual QuantumGateBase* copy() const override {
222  std::vector<QuantumGateBase*> new_gate_list;
223  for (auto item : _gate_list) {
224  new_gate_list.push_back(item->copy());
225  }
226  return new QuantumGate_Instrument(new_gate_list, _classical_register_address);
227  };
233  virtual void set_matrix(ComplexMatrix& matrix) const override {
234  std::cerr << "* Warning : Gate-matrix of Instrument cannot be obtained. Identity matrix is returned." << std::endl;
235  matrix = Eigen::MatrixXcd::Ones(1, 1);
236  }
237 };
238 
239 
244 protected:
246  std::function<bool(const std::vector<UINT>&)> _func;
247 public:
248  QuantumGate_Adaptive(QuantumGateBase* gate, std::function<bool(const std::vector<UINT>&)> func) {
249  _gate = gate->copy();
250  _func = func;
251  };
253  delete _gate;
254  }
255 
261  virtual void update_quantum_state(QuantumStateBase* state) override {
262  bool result = _func(state->get_classical_register());
263  if (result) {
265  }
266  };
272  virtual QuantumGateBase* copy() const override {
273  return new QuantumGate_Adaptive(_gate->copy(), _func);
274  };
280  virtual void set_matrix(ComplexMatrix& matrix) const override {
281  std::cerr << "* Warning : Gate-matrix of Adaptive-gate cannot be obtained. Identity matrix is returned." << std::endl;
282  matrix = Eigen::MatrixXcd::Ones(1, 1);
283  }
284 };
285 
virtual void update_quantum_state(QuantumStateBase *state) override
Definition: gate_general.hpp:49
Eigen::Matrix< CPPCTYPE, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > ComplexMatrix
Definition: type.hpp:17
virtual ~QuantumGate_Probabilistic()
Definition: gate_general.hpp:38
virtual ~QuantumGate_CPTP()
Definition: gate_general.hpp:98
Definition and basic functions for QuantumGate.
Definition: gate_general.hpp:87
Definition: utility.hpp:49
virtual QuantumGateBase * copy() const override
Definition: gate_general.hpp:65
std::function< bool(const std::vector< UINT > &)> _func
Definition: gate_general.hpp:246
virtual QuantumGateBase * copy() const override
Definition: gate_general.hpp:272
Definition: gate_general.hpp:243
Definition: gate_factory.cpp:17
virtual void update_quantum_state(QuantumStateBase *state) override
Definition: gate_general.hpp:109
QuantumGate_CPTP(std::vector< QuantumGateBase *> gate_list)
Definition: gate_general.hpp:93
Random random
Definition: gate_general.hpp:12
virtual ~QuantumGate_Adaptive()
Definition: gate_general.hpp:252
Definition: gate_general.hpp:10
virtual void set_matrix(ComplexMatrix &matrix) const override
Definition: gate_general.hpp:233
std::vector< QuantumGateBase * > _gate_list
Definition: gate_general.hpp:90
virtual ~QuantumGate_Instrument()
Definition: gate_general.hpp:175
virtual void update_quantum_state(QuantumStateBase *state) override
Definition: gate_general.hpp:261
std::vector< double > _cumulative_distribution
Definition: gate_general.hpp:14
virtual void set_matrix(ComplexMatrix &matrix) const override
Definition: gate_general.hpp:78
Definition: state.hpp:23
UINT _classical_register_address
Definition: gate_general.hpp:166
Random random
Definition: gate_general.hpp:89
virtual void set_matrix(ComplexMatrix &matrix) const override
Definition: gate_general.hpp:153
std::vector< double > _distribution
Definition: gate_general.hpp:13
virtual QuantumGateBase * copy() const override
Definition: gate_general.hpp:141
QuantumGateBase * _gate
Definition: gate_general.hpp:245
QuantumGate_Instrument(std::vector< QuantumGateBase *> gate_list, UINT classical_register_address)
Definition: gate_general.hpp:169
virtual QuantumGateBase * copy() const =0
std::vector< QuantumGateBase * > _gate_list
Definition: gate_general.hpp:15
Definition: state.cpp:14
virtual void update_quantum_state(QuantumStateBase *state) override
Definition: gate_general.hpp:186
Definition: gate_general.hpp:162
virtual void update_quantum_state(QuantumStateBase *state)=0
virtual QuantumGateBase * copy() const override
Definition: gate_general.hpp:221
Random random
Definition: gate_general.hpp:164
double uniform()
Definition: utility.hpp:76
QuantumGate_Adaptive(QuantumGateBase *gate, std::function< bool(const std::vector< UINT > &)> func)
Definition: gate_general.hpp:248
virtual void set_matrix(ComplexMatrix &matrix) const override
Definition: gate_general.hpp:280
std::vector< QuantumGateBase * > _gate_list
Definition: gate_general.hpp:165
QuantumGate_Probabilistic(std::vector< double > distribution, std::vector< QuantumGateBase *> gate_list)
Definition: gate_general.hpp:24
Definition: gate.hpp:75