Skip to content

Commit

Permalink
reflect commit 4d65f3d
Browse files Browse the repository at this point in the history
  • Loading branch information
KowerKoint authored and KowerKoint committed Nov 13, 2024
1 parent 71f6d52 commit 271c908
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/gate/gate_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ std::shared_ptr<const GateBase<Fp>> SparseMatrixGateImpl<Fp>::get_inverse() cons
Kokkos::View<SparseValue<Fp>*, Kokkos::HostSpace> vec_h("h_view", num_nnz);
Kokkos::deep_copy(vec_h, _matrix._values);
// conversion to Eigen matrix (COO format)
ComplexMatrix<Fp> eigen_matrix(_matrix._row, _matrix._col);
ComplexMatrix<Fp> eigen_matrix = ComplexMatrix<Fp>::Zero(_matrix._row, _matrix._col);
for (std::size_t i = 0; i < vec_h.extent(0); i++) {
eigen_matrix(vec_h(i).r, vec_h(i).c) = vec_h(i).val;
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ CALL_MACRO_FOR_FLOAT(FUNC_MACRO)

template <std::floating_point Fp>
ComplexMatrix<Fp> convert_coo_to_external_matrix(SparseMatrix<Fp> mat) {
ComplexMatrix<Fp> eigen_matrix(mat._row, mat._col);
ComplexMatrix<Fp> eigen_matrix = ComplexMatrix<Fp>::Zero(mat._row, mat._col);
auto vec_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), mat._values);
for (std::size_t i = 0; i < mat._values.extent(0); i++) {
eigen_matrix(vec_h(i).r, vec_h(i).c) = vec_h(i).val;
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_executable(scaluq_test EXCLUDE_FROM_ALL
circuit/circuit_test.cpp
circuit/param_circuit_test.cpp
gate/gate_test.cpp
# # gate/merge_test.cpp
# gate/merge_test.cpp
gate/param_gate_test.cpp
operator/test_pauli_operator.cpp
operator/test_operator.cpp
Expand Down
26 changes: 12 additions & 14 deletions tests/gate/gate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,10 @@ void test_gate(Gate<Fp> gate_control,
amplitudes = state.get_amplitudes();
amplitudes_controlled = state_controlled.get_amplitudes();
for (std::uint64_t i : std::views::iota(0ULL, state_controlled.dim())) {
ASSERT_NEAR(
Kokkos::abs(amplitudes_controlled[i] -
amplitudes[internal::insert_zero_at_mask_positions(i, control_mask) |
control_mask]),
0.,
eps);
check_near(
(StdComplex<Fp>)amplitudes_controlled[i],
(StdComplex<Fp>)amplitudes[internal::insert_zero_at_mask_positions(i, control_mask) |
control_mask]);
}
}

Expand Down Expand Up @@ -883,12 +881,12 @@ void test_matrix_control(std::uint64_t n_qubits) {
Gate<Fp> d2 = gate::DenseMatrix<Fp>(new_targets, mat, {});
Gate<Fp> s1 = gate::SparseMatrix<Fp>(targets, mat.sparseView(), controls);
Gate<Fp> s2 = gate::SparseMatrix<Fp>(new_targets, mat.sparseView(), {});
test_gate(d1, d2, n_qubits, control_mask);
test_gate(s1, s2, n_qubits, control_mask);
test_gate<Fp>(d1, d2, n_qubits, control_mask);
test_gate<Fp>(s1, s2, n_qubits, control_mask);
} else if constexpr (num_target == 1) {
Eigen::Matrix<StdComplex<Fp>, 2, 2, Eigen::RowMajor> U =
get_eigen_matrix_random_one_target_unitary<Fp>();
internal::ComplexMatrix<Fp> mat(U.rows(), U.cols());
ComplexMatrix<Fp> mat = ComplexMatrix<Fp>::Zero(U.rows(), U.cols());
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
mat(i, j) = U(i, j);
Expand All @@ -906,7 +904,7 @@ void test_matrix_control(std::uint64_t n_qubits) {
Eigen::Matrix<StdComplex<Fp>, 2, 2, Eigen::RowMajor> U2 =
get_eigen_matrix_random_one_target_unitary<Fp>();
auto U = internal::kronecker_product<Fp>(U2, U1);
internal::ComplexMatrix<Fp> mat(U.rows(), U.cols());
ComplexMatrix<Fp> mat = ComplexMatrix<Fp>::Zero(U.rows(), U.cols());
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
mat(i, j) = U(i, j);
Expand All @@ -916,8 +914,8 @@ void test_matrix_control(std::uint64_t n_qubits) {
Gate<Fp> d2 = gate::DenseMatrix<Fp>(new_targets, mat, {});
Gate<Fp> s1 = gate::SparseMatrix<Fp>(targets, mat.sparseView(), controls);
Gate<Fp> s2 = gate::SparseMatrix<Fp>(new_targets, mat.sparseView(), {});
test_gate(d1, d2, n_qubits, control_mask);
test_gate(s1, s2, n_qubits, control_mask);
test_gate<Fp>(d1, d2, n_qubits, control_mask);
test_gate<Fp>(s1, s2, n_qubits, control_mask);
} else {
Eigen::Matrix<StdComplex<Fp>, 2, 2, Eigen::RowMajor> U1 =
get_eigen_matrix_random_one_target_unitary<Fp>();
Expand All @@ -936,8 +934,8 @@ void test_matrix_control(std::uint64_t n_qubits) {
Gate<Fp> d2 = gate::DenseMatrix<Fp>(new_targets, mat, {});
Gate<Fp> s1 = gate::SparseMatrix<Fp>(targets, mat.sparseView(), controls);
Gate<Fp> s2 = gate::SparseMatrix<Fp>(new_targets, mat.sparseView(), {});
test_gate(d1, d2, n_qubits, control_mask);
test_gate(s1, s2, n_qubits, control_mask);
test_gate<Fp>(d1, d2, n_qubits, control_mask);
test_gate<Fp>(s1, s2, n_qubits, control_mask);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/util/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const inline float eps_f = 1e-6;
template <std::floating_point Fp>
inline void check_near(const StdComplex<Fp>& a, const StdComplex<Fp>& b) {
if constexpr (std::is_same_v<Fp, double>)
EXPECT_NEAR(std::abs(a - b), 0, eps);
ASSERT_LE(std::abs(a - b), eps);
else
EXPECT_NEAR(std::abs(a - b), 0, eps_f);
ASSERT_LE(std::abs(a - b), eps_f);
}

template <std::floating_point Fp>
Expand Down

0 comments on commit 271c908

Please sign in to comment.