Skip to content

Commit

Permalink
Fixed bug in parallelization in MPS simulator. (Qiskit#292)
Browse files Browse the repository at this point in the history
* Fixed bug in parallelization. It does not work correctly with vector push_back. Need to use assignment instead.
  • Loading branch information
merav-aharoni authored and atilag committed Jul 24, 2019
1 parent 85efb8e commit 32bb773
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Removed

Fixed
-----
- Bug in handling parallelization in matrix_product_state.cpp (PR \#292)


[0.2.3](https://github.com/Qiskit/qiskit-aer/compare/0.2.2...0.2.3) - 2019-07-11
Expand Down
18 changes: 13 additions & 5 deletions src/simulators/tensor_network/matrix_product_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ cmatrix_t mul_matrix_by_lambda(const cmatrix_t &mat,
if (lambda == rvector_t {1.0}) return mat;
cmatrix_t res_mat(mat);
uint_t num_rows = mat.GetRows(), num_cols = mat.GetColumns();
for(uint_t row = 0; row < num_rows; row++) {
for(uint_t col = 0; col < num_cols; col++) {

#ifdef _WIN32
#pragma omp parallel for
#else
#pragma omp parallel for collapse(2)
#endif
for(int_t row = 0; row < num_rows; row++) {
for(int_t col = 0; col < num_cols; col++) {
res_mat(row, col) = mat(row, col) * lambda[col];
}
}
Expand Down Expand Up @@ -459,9 +465,10 @@ void MPS::full_state_vector(cvector_t& statevector) const
{
MPS_Tensor mps_vec = state_vec(0, num_qubits_-1);
uint_t length = 1ULL << num_qubits_; // length = pow(2, num_qubits_)
statevector.resize(length);
#pragma omp parallel for
for (int_t i = 0; i < static_cast<int_t>(length); i++) {
statevector.push_back(mps_vec.get_data(reverse_bits(i, num_qubits_))(0,0));
statevector[i] = mps_vec.get_data(reverse_bits(i, num_qubits_))(0,0);
}
#ifdef DEBUG
cout << *this;
Expand All @@ -473,9 +480,10 @@ void MPS::probabilities_vector(rvector_t& probvector) const
MPS_Tensor mps_vec = state_vec(0, num_qubits_-1);
uint_t length = 1ULL << num_qubits_; // length = pow(2, num_qubits_)
complex_t data = 0;
probvector.resize(length);
#pragma omp parallel for
for (int_t i = 0; i < static_cast<int_t>(length); i++) {
data = mps_vec.get_data(reverse_bits(i, num_qubits_))(0,0);
probvector.push_back(std::norm(data));
probvector[i] = std::norm(mps_vec.get_data(reverse_bits(i, num_qubits_))(0,0));
}
}

Expand Down

0 comments on commit 32bb773

Please sign in to comment.