MCPcopy Create free account
hub / github.com/OriginQ/QPanda-2 / approx_mps_encode

Method approx_mps_encode

include/QAlg/Encode/Encode.h:248–333  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

246 void efficient_sparse(const QVec &q, const std::map<std::string, qcomplex_t>&data);
247 template<typename Derived>
248 void approx_mps_encode(const QVec &q, const std::vector<Derived>& data, const int &layers = 3, const int &step = 100)
249 {
250 vector<Derived>data_temp(data);
251 //_normalized(data_temp);
252 if (!_check_normalized(data_temp)) {
253 throw run_fail("Data is not normalized");
254 }
255 int size = data_temp.size();
256 int log_ceil_size = std::ceil(std::log2(size));
257 if (data_temp.size() > (1 << log_ceil_size))
258 {
259 throw run_fail("Qubits size error.");
260 }
261 while (data_temp.size() < (1 << log_ceil_size))
262 {
263 data_temp.push_back(0);
264 }
265 if (data_temp.size() <= 2)
266 {
267 throw run_fail("Two elements data is not suitable for MPS");
268 }
269 size_t N = data_temp.size();
270
271 N = ceil(log2(N));
272 int count = 0;
273 vector<Derived> data_(data_temp);
274 vector<QCircuit> circuit_block;
275
276
277 vector<vector<dyn_mat<Derived>>>unitary_block_v_d;
278 while (count < layers) {
279
280 auto MPS = _to_approx_MPS(data_, N, 2);
281
282
283 _orthogonalize_mps(MPS, 0, N - 1, 2);
284
285 std::pair<QCircuit, std::vector<dyn_mat<Derived>>>circuit = _embedded_circuit(MPS, q, 2);
286
287 unitary_block_v_d.push_back(circuit.second);
288
289 circuit_block.push_back(circuit.first);
290 QProg prog1 = QProg();
291 prog1 << circuit.first;
292
293 auto stat = get_partial_unitary(prog1);
294
295
296 dyn_mat<Derived> _U(1 << N, 1 << N);
297 _qstat2eigen(stat, N, _U);
298
299 dyn_col_vect<Derived> v2(data.size());
300 int cnt = 0;
301 for (Derived i : data_) {
302 v2(cnt++) = i;
303 }
304
305 auto _data = _U.transpose().conjugate()*(v2);

Callers 1

test_mps_encodingFunction · 0.45

Calls 11

run_failClass · 0.85
ceilFunction · 0.50
log2Function · 0.50
QProgClass · 0.50
sizeMethod · 0.45
push_backMethod · 0.45
conjugateMethod · 0.45
transposeMethod · 0.45
rowsMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by 1

test_mps_encodingFunction · 0.36