Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug in parallelization in MPS simulator. #292

Merged
merged 8 commits into from
Jul 24, 2019
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