Skip to content

Commit

Permalink
Modified single-qubit-diagonal-matrix-gate and RZ-gate functions to b…
Browse files Browse the repository at this point in the history
…e usable multi-cpu
  • Loading branch information
m-ymzk committed Jan 9, 2023
1 parent 620d4cd commit e764228
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/csim/update_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,10 @@ void single_qubit_diagonal_matrix_gate_parallel_unroll(UINT target_qubit_index,
const CTYPE diagonal_matrix[2], CTYPE* state, ITYPE dim);
void single_qubit_diagonal_matrix_gate_parallel_simd(UINT target_qubit_index,
const CTYPE diagonal_matrix[2], CTYPE* state, ITYPE dim);
#ifdef _USE_MPI
DllExport void single_qubit_diagonal_matrix_gate_mpi(UINT target_qubit_index,
const CTYPE diagonal_matrix[2], CTYPE* state, ITYPE dim, UINT inner_qc);
#endif

/**
* \~english
Expand Down
43 changes: 42 additions & 1 deletion src/csim/update_ops_matrix_diagonal_single.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <stdlib.h>
#include <string.h>

#include "MPIutil.hpp"
#include "constant.hpp"
#include "update_ops.hpp"
#include "utility.hpp"
#ifdef _OPENMP
#include <omp.h>
#endif

#ifdef _USE_SIMD
#ifdef _MSC_VER
#include <intrin.h>
Expand Down Expand Up @@ -121,3 +121,44 @@ void single_qubit_diagonal_matrix_gate_parallel_simd(UINT target_qubit_index,
}
}
#endif

#ifdef _USE_MPI
void single_qubit_diagonal_matrix_gate_partial(
const CTYPE diagonal_matrix[2], CTYPE* state, ITYPE dim, int isone);

void single_qubit_diagonal_matrix_gate_mpi(UINT target_qubit_index,
const CTYPE diagonal_matrix[2], CTYPE* state, ITYPE dim, UINT inner_qc) {
if (target_qubit_index < inner_qc) {
single_qubit_diagonal_matrix_gate(
target_qubit_index, diagonal_matrix, state, dim);
} else {
#ifdef _OPENMP
OMPutil omputil = get_omputil();
omputil->set_qulacs_num_threads(dim, 12);
#endif
const MPIutil m = get_mpiutil();
const int rank = m->get_rank();
const int pair_rank_bit = 1 << (target_qubit_index - inner_qc);

single_qubit_diagonal_matrix_gate_partial(
diagonal_matrix, state, dim, (rank & pair_rank_bit) != 0);
#ifdef _OPENMP
omputil->reset_qulacs_num_threads();
#endif
}
}

void single_qubit_diagonal_matrix_gate_partial(
const CTYPE diagonal_matrix[2], CTYPE* state, ITYPE dim, int isone) {
// loop variables
ITYPE state_index;

{
#pragma omp parallel for
for (state_index = 0; state_index < dim; state_index += 2) {
state[state_index] *= diagonal_matrix[isone];
state[state_index + 1] *= diagonal_matrix[isone];
}
}
}
#endif
5 changes: 2 additions & 3 deletions src/csim/update_ops_pauli_single.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ void RZ_gate_mpi(UINT target_qubit_index, double angle, CTYPE* state, ITYPE dim,
single_qubit_diagonal_matrix_gate(
target_qubit_index, diagonal_matrix, state, dim);
} else {
// not supported yet!
// single_qubit_diagonal_matrix_gate_mpi(
// target_qubit_index, diagonal_matrix, state, dim, inner_qc);
single_qubit_diagonal_matrix_gate_mpi(
target_qubit_index, diagonal_matrix, state, dim, inner_qc);
}
}
#endif
25 changes: 1 addition & 24 deletions test/cppsim/test_gate_multicpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,6 @@ TEST(GateTest_multicpu, ApplySingleQubitGate) {
get_expanded_eigen_matrix_with_identity(target, mat, n) *
test_state2;

// std::cout << "# rank,repeat,funcNo,target: " << m->get_rank() <<
// "," << repeat << "," << ++j << "," << target <<
// ": " << state.data_cpp()[0] << ", " <<
// state_ref.data_cpp()[offs] << ", " << test_state1[offs] << "(" <<
// offs << ")"
// ": " << test_state2[offs] << "(" << offs << ")"
// "::: " << test_state1[(offs+16)%dim] << "(" << (offs+16)%dim <<
//")" << std::endl; std::cout << "# mat: " << mat << ", " <<
// small_mat << std::endl;
for (ITYPE i = 0; i < inner_dim; ++i)
ASSERT_NEAR(
abs(state.data_cpp()[i] - test_state1[i + offs]), 0, eps);
Expand Down Expand Up @@ -129,7 +120,7 @@ TEST(GateTest_multicpu, ApplySingleQubitRotationGate) {
funclist;
funclist.push_back(std::make_pair(gate::RX, X));
funclist.push_back(std::make_pair(gate::RY, Y));
//funclist.push_back(std::make_pair(gate::RZ, Z));
funclist.push_back(std::make_pair(gate::RZ, Z));

MPIutil m = get_mpiutil();
const ITYPE inner_dim = dim >> state.outer_qc;
Expand Down Expand Up @@ -164,20 +155,6 @@ TEST(GateTest_multicpu, ApplySingleQubitRotationGate) {
get_expanded_eigen_matrix_with_identity(target, mat, n) *
test_state2;

// std::cout << "# Rot:rank,repeat,funcNo,target: " << m->get_rank()
// << "," << repeat << "," << ++funcid << "," << target <<
// ":( " << offs << ") " << state.data_cpp()[0] << ", " <<
// test_state1[0 + offs] << ", " << test_state2[0 + offs] <<
// std::endl <<
// ":( " << offs << ") " << state.data_cpp()[1] << ", " <<
// test_state1[1 + offs] << ", " << test_state2[1 + offs] <<
// std::endl <<
// ":( " << offs << ") " << state.data_cpp()[2] << ", " <<
// test_state1[2 + offs] << ", " << test_state2[2 + offs] <<
// std::endl <<
// ":( " << offs << ") " << state.data_cpp()[3] << ", " <<
// test_state1[3 + offs] << ", " << test_state2[3 + offs] <<
// std::endl;
for (ITYPE i = 0; i < inner_dim; ++i)
ASSERT_NEAR(
real(state.data_cpp()[i] - test_state1[i + offs]), 0, eps);
Expand Down

0 comments on commit e764228

Please sign in to comment.