Skip to content

Commit

Permalink
fix matrix ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Glacialte committed Oct 15, 2024
1 parent acbf4ed commit f038959
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 29 deletions.
23 changes: 3 additions & 20 deletions scaluq/util/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,6 @@ 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 All @@ -225,10 +209,10 @@ inline ComplexMatrix transform_dense_matrix_by_order(const ComplexMatrix& mat,
ComplexMatrix ret(matrix_size, matrix_size);

for (std::size_t i = 0; i < matrix_size; i++) {
std::size_t row_dst = transformed[i];
std::size_t row_src = transformed[i];
for (std::size_t j = 0; j < matrix_size; j++) {
std::size_t col_dst = transformed[j];
ret(row_dst, col_dst) = mat(i, j);
std::size_t col_src = transformed[j];
ret(i, j) = mat(row_src, col_src);
}
}
return ret;
Expand All @@ -240,7 +224,6 @@ 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
9 changes: 0 additions & 9 deletions tests/gate/gate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ void run_random_gate_apply_sparse(std::uint64_t n_qubits) {
u3 = get_eigen_matrix_random_one_target_unitary();
std::vector<std::uint64_t> target_list = {targets[0], targets[1], targets[2]};
std::vector<std::uint64_t> control_list = {};
std::sort(target_list.begin(), target_list.end());

test_state = get_expanded_eigen_matrix_with_identity(target_list[2], u3, n_qubits) *
get_expanded_eigen_matrix_with_identity(target_list[1], u2, n_qubits) *
Expand Down Expand Up @@ -490,13 +489,6 @@ 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]) {
// 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);
test_state = get_expanded_eigen_matrix_with_identity(targets[1], U2, n_qubits) *
get_expanded_eigen_matrix_with_identity(targets[0], U1, n_qubits) *
Expand Down Expand Up @@ -528,7 +520,6 @@ void run_random_gate_apply_general_dense(std::uint64_t n_qubits) {
targets[0] = index_list[0];
targets[1] = index_list[1];
targets[2] = index_list[2];
std::sort(targets.begin(), targets.end());
Umerge = internal::kronecker_product(U3, internal::kronecker_product(U2, U1));

test_state = get_expanded_eigen_matrix_with_identity(targets[2], U3, n_qubits) *
Expand Down

0 comments on commit f038959

Please sign in to comment.