Skip to content

Commit

Permalink
add move_eigen_to_host
Browse files Browse the repository at this point in the history
  • Loading branch information
Glacialte committed Oct 11, 2024
1 parent 25600f9 commit acbf4ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
17 changes: 17 additions & 0 deletions scaluq/util/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ inline ComplexMatrix convert_coo_to_external_matrix(SparseMatrix mat) {
return eigen_matrix;
}

inline ComplexMatrix move_eigen_to_host(ComplexMatrix eigen_mat) {
if (eigen_mat.data() == nullptr) {
return ComplexMatrix();
}
Kokkos::View<Complex**, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>> device_view(
reinterpret_cast<Complex*>(eigen_mat.data()), eigen_mat.rows(), eigen_mat.cols());
Kokkos::View<Complex**, Kokkos::HostSpace> host_view(
"host_view", eigen_mat.rows(), eigen_mat.cols());
Kokkos::deep_copy(host_view, device_view);
ComplexMatrix host_mat(eigen_mat.rows(), eigen_mat.cols());
std::copy(host_view.data(),
host_view.data() + host_view.extent(0) * host_view.extent(1),
host_mat.data());
return host_mat;
}

inline ComplexMatrix transform_dense_matrix_by_order(const ComplexMatrix& mat,
const std::vector<std::uint64_t>& targets) {
std::vector<std::uint64_t> sorted(targets);
Expand Down Expand Up @@ -224,6 +240,7 @@ inline SparseComplexMatrix transform_sparse_matrix_by_order(
// hence this function will be refactored.
const SparseComplexMatrix& mat,
const std::vector<std::uint64_t>& targets) {
ComplexMatrix mat_h = move_eigen_to_host(mat);
ComplexMatrix dense_mat = mat.toDense();
return transform_dense_matrix_by_order(dense_mat, targets).sparseView();
}
Expand Down
32 changes: 8 additions & 24 deletions tests/gate/gate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,28 +454,20 @@ void run_random_gate_apply_general_dense(std::uint64_t n_qubits) {
}
// general single
{
Eigen::Matrix<StdComplex, 2, 2, Eigen::RowMajor> Umerge;
for (std::uint64_t rep = 0; rep < max_repeat; rep++) {
StateVector state = StateVector::Haar_random_state(n_qubits);
auto state_cp = state.get_amplitudes();
for (std::uint64_t i = 0; i < dim; i++) {
test_state[i] = state_cp[i];
}
U1 = get_eigen_matrix_random_one_target_unitary();
internal::ComplexMatrix mat(U1.rows(), U1.cols());
std::shuffle(index_list.begin(), index_list.end(), engine);
targets[0] = index_list[0];
Umerge = U1;
test_state =
get_expanded_eigen_matrix_with_identity(targets[0], U1, n_qubits) * test_state;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
mat(i, j) = U1(i, j);
}
}
std::vector<std::uint64_t> target_list = {targets[0]};
std::vector<std::uint64_t> control_list = {};
Gate dense_gate = gate::DenseMatrix(target_list, mat, control_list);
Gate dense_gate = gate::DenseMatrix(target_list, U1, control_list);
dense_gate->update_quantum_state(state);
state_cp = state.get_amplitudes();
for (std::uint64_t i = 0; i < dim; i++) {
Expand All @@ -498,22 +490,20 @@ void run_random_gate_apply_general_dense(std::uint64_t n_qubits) {
std::shuffle(index_list.begin(), index_list.end(), engine);
targets[0] = index_list[0];
targets[1] = index_list[1];
if (targets[0] > targets[1]) {
// if (targets[0] > targets[1]) {
// std::swap(targets[0], targets[1]);
// }
// check when the order is not ascending
if (targets[0] < targets[1]) {
std::swap(targets[0], targets[1]);
}
Umerge = internal::kronecker_product(U2, U1);
internal::ComplexMatrix mat(Umerge.rows(), Umerge.cols());
test_state = get_expanded_eigen_matrix_with_identity(targets[1], U2, n_qubits) *
get_expanded_eigen_matrix_with_identity(targets[0], U1, n_qubits) *
test_state;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
mat(i, j) = Umerge(i, j);
}
}
std::vector<std::uint64_t> target_list = {targets[0], targets[1]};
std::vector<std::uint64_t> control_list = {};
Gate dense_gate = gate::DenseMatrix(target_list, mat, control_list);
Gate dense_gate = gate::DenseMatrix(target_list, Umerge, control_list);
dense_gate->update_quantum_state(state);
state_cp = state.get_amplitudes();
for (std::uint64_t i = 0; i < dim; i++) {
Expand All @@ -540,20 +530,14 @@ void run_random_gate_apply_general_dense(std::uint64_t n_qubits) {
targets[2] = index_list[2];
std::sort(targets.begin(), targets.end());
Umerge = internal::kronecker_product(U3, internal::kronecker_product(U2, U1));
internal::ComplexMatrix mat(Umerge.rows(), Umerge.cols());

test_state = get_expanded_eigen_matrix_with_identity(targets[2], U3, n_qubits) *
get_expanded_eigen_matrix_with_identity(targets[1], U2, n_qubits) *
get_expanded_eigen_matrix_with_identity(targets[0], U1, n_qubits) *
test_state;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
mat(i, j) = Umerge(i, j);
}
}
std::vector<std::uint64_t> target_list = {targets[0], targets[1], targets[2]};
std::vector<std::uint64_t> control_list = {};
Gate dense_gate = gate::DenseMatrix(target_list, mat, control_list);
Gate dense_gate = gate::DenseMatrix(target_list, Umerge, control_list);
dense_gate->update_quantum_state(state);
state_cp = state.get_amplitudes();
for (std::uint64_t i = 0; i < dim; i++) {
Expand Down

0 comments on commit acbf4ed

Please sign in to comment.