From 160c050c2794f356ed5ba3f74f4186ad17988dd7 Mon Sep 17 00:00:00 2001 From: gandalfr-KY Date: Tue, 7 Jan 2025 03:19:26 +0000 Subject: [PATCH 1/4] gpu test pass --- CMakeLists.txt | 10 +- exe/main.cpp | 283 +++++++++++++++--------------- script/configure_dev | 3 +- src/CMakeLists.txt | 36 ++-- src/gate/merge_gate.cpp | 3 - tests/CMakeLists.txt | 17 +- tests/state/state_vector_test.cpp | 22 +-- tests/util/util.hpp | 2 +- 8 files changed, 189 insertions(+), 187 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 57501c05..0353b873 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -135,11 +135,15 @@ if ((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") OR (${CMAKE_CXX_COMPILER_ID} STREQ endif() # Debug options - if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + if (SCALUQ_USE_CUDA) target_compile_options(scaluq PUBLIC $,-O0 -g,-O3>) else() - target_compile_options(scaluq PUBLIC $,-O0 -g -fsanitize=address$undefined,-O3>) - target_link_options(scaluq PUBLIC $<$:-fsanitize=address$undefined>) + if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + target_compile_options(scaluq PUBLIC $,-O0 -g,-O3>) + else() + target_compile_options(scaluq PUBLIC $,-O0 -g -fsanitize=address,-O3>) + target_link_options(scaluq PUBLIC $<$:-fsanitize=address>) + endif() endif() endif() diff --git a/exe/main.cpp b/exe/main.cpp index 5659677e..084f3d21 100644 --- a/exe/main.cpp +++ b/exe/main.cpp @@ -7,153 +7,154 @@ using namespace nlohmann; int main() { scaluq::initialize(); // must be called before using any scaluq methods - { - std::uint64_t n_qubits = 3; - scaluq::StateVector state(n_qubits); - state.load({0, 1, 2, 3, 4, 5, 6, 7}); - Json j = state; - std::cout << j << std::endl; - state = j; - std::cout << state << std::endl; + // { + // std::uint64_t n_qubits = 3; + // scaluq::StateVector state(n_qubits); + // state.load({0, 1, 2, 3, 4, 5, 6, 7}); + // Json j = state; + // std::cout << j << std::endl; + // state = j; + // std::cout << state << std::endl; - scaluq::Circuit circuit(n_qubits); - circuit.add_gate(scaluq::gate::X(0)); - circuit.add_gate(scaluq::gate::CNot(0, 1)); - circuit.add_gate(scaluq::gate::Y(1)); - circuit.add_gate(scaluq::gate::RX(1, std::numbers::pi / 2)); - circuit.update_quantum_state(state); + // scaluq::Circuit circuit(n_qubits); + // circuit.add_gate(scaluq::gate::X(0)); + // circuit.add_gate(scaluq::gate::CNot(0, 1)); + // circuit.add_gate(scaluq::gate::Y(1)); + // circuit.add_gate(scaluq::gate::RX(1, std::numbers::pi / 2)); + // circuit.update_quantum_state(state); - scaluq::Operator observable(n_qubits); - observable.add_random_operator(1, 0); - auto value = observable.get_expectation_value(state); - std::cout << value << std::endl; - } - { - std::uint64_t n_qubits = 2, batch_size = 2; - scaluq::StateVectorBatched states(batch_size, n_qubits); - states.set_Haar_random_state(batch_size, n_qubits, false); - Json j = states; - std::cout << j << std::endl; - states = j; - std::cout << states << std::endl; - } - { - double coef = 2.0; - std::string pauli_string = "X 0 Z 1 Y 2"; - PauliOperator pauli(pauli_string, coef); - Json j = pauli; - std::cout << j << std::endl; - pauli = j; - } - { - std::uint64_t n_qubits = 3; - Operator op(n_qubits); - op.add_operator({0b001, 0b010, Complex(2)}); - op.add_operator({"X 2 Y 1", 1}); - Json j = op; - std::cout << j << std::endl; - op = j; - } - { - std::cout << Json(gate::I()) << std::endl; - std::cout << Json(gate::X(2, {0, 3})) << std::endl; - std::cout << Json(gate::Y(2, {0, 3})) << std::endl; - std::cout << Json(gate::Z(2, {0, 3})) << std::endl; - std::cout << Json(gate::H(2, {0, 3})) << std::endl; - std::cout << Json(gate::S(2, {0, 3})) << std::endl; - std::cout << Json(gate::Sdag(2, {0, 3})) << std::endl; - std::cout << Json(gate::T(2, {0, 3})) << std::endl; - std::cout << Json(gate::Tdag(2, {0, 3})) << std::endl; - std::cout << Json(gate::SqrtX(2, {0, 3})) << std::endl; - std::cout << Json(gate::SqrtXdag(2, {0, 3})) << std::endl; - std::cout << Json(gate::SqrtY(2, {0, 3})) << std::endl; - std::cout << Json(gate::SqrtYdag(2, {0, 3})) << std::endl; - std::cout << Json(gate::RX(2, 0.5, {0, 3})) << std::endl; - std::cout << Json(gate::RY(2, 0.5, {0, 3})) << std::endl; - std::cout << Json(gate::RZ(2, 0.5, {0, 3})) << std::endl; - std::cout << Json(gate::U1(2, 0.5, {0, 3})) << std::endl; - std::cout << Json(gate::U2(2, 0.5, 0.3, {0, 3})) << std::endl; - std::cout << Json(gate::U3(2, 0.5, 0.3, 0.1, {0, 3})) << std::endl; - std::cout << Json(gate::Swap(1, 2, {0, 3})) << std::endl; + // scaluq::Operator observable(n_qubits); + // observable.add_random_operator(1, 0); + // auto value = observable.get_expectation_value(state); + // std::cout << value << std::endl; + // } + // { + // std::uint64_t n_qubits = 2, batch_size = 2; + // scaluq::StateVectorBatched states(batch_size, n_qubits); + // states.set_Haar_random_state(batch_size, n_qubits, false); + // Json j = states; + // std::cout << j << std::endl; + // states = j; + // std::cout << states << std::endl; + // } + // { + // double coef = 2.0; + // std::string pauli_string = "X 0 Z 1 Y 2"; + // PauliOperator pauli(pauli_string, coef); + // Json j = pauli; + // std::cout << j << std::endl; + // pauli = j; + // } + // { + // std::uint64_t n_qubits = 3; + // Operator op(n_qubits); + // op.add_operator({0b001, 0b010, Complex(2)}); + // op.add_operator({"X 2 Y 1", 1}); + // Json j = op; + // std::cout << j << std::endl; + // op = j; + // } + // { + // std::cout << Json(gate::I()) << std::endl; + // std::cout << Json(gate::X(2, {0, 3})) << std::endl; + // std::cout << Json(gate::Y(2, {0, 3})) << std::endl; + // std::cout << Json(gate::Z(2, {0, 3})) << std::endl; + // std::cout << Json(gate::H(2, {0, 3})) << std::endl; + // std::cout << Json(gate::S(2, {0, 3})) << std::endl; + // std::cout << Json(gate::Sdag(2, {0, 3})) << std::endl; + // std::cout << Json(gate::T(2, {0, 3})) << std::endl; + // std::cout << Json(gate::Tdag(2, {0, 3})) << std::endl; + // std::cout << Json(gate::SqrtX(2, {0, 3})) << std::endl; + // std::cout << Json(gate::SqrtXdag(2, {0, 3})) << std::endl; + // std::cout << Json(gate::SqrtY(2, {0, 3})) << std::endl; + // std::cout << Json(gate::SqrtYdag(2, {0, 3})) << std::endl; + // std::cout << Json(gate::RX(2, 0.5, {0, 3})) << std::endl; + // std::cout << Json(gate::RY(2, 0.5, {0, 3})) << std::endl; + // std::cout << Json(gate::RZ(2, 0.5, {0, 3})) << std::endl; + // std::cout << Json(gate::U1(2, 0.5, {0, 3})) << std::endl; + // std::cout << Json(gate::U2(2, 0.5, 0.3, {0, 3})) << std::endl; + // std::cout << Json(gate::U3(2, 0.5, 0.3, 0.1, {0, 3})) << std::endl; + // std::cout << Json(gate::Swap(1, 2, {0, 3})) << std::endl; - PauliOperator pauli("X 2 Y 1"); - std::cout << Json(gate::Pauli(pauli)) << std::endl; - std::cout << Json(gate::PauliRotation(pauli, 0.5)) << std::endl; + // PauliOperator pauli("X 2 Y 1"); + // std::cout << Json(gate::Pauli(pauli)) << std::endl; + // std::cout << Json(gate::PauliRotation(pauli, 0.5)) << std::endl; - auto probgate = - gate::Probablistic({.1, .9}, {gate::X(0), gate::I()}); - std::cout << Json(probgate) << std::endl; + // auto probgate = + // gate::Probablistic({.1, .9}, {gate::X(0), gate::I()}); + // std::cout << Json(probgate) << std::endl; - std::cout << Json(gate::ParamRX(2, 1.5, {0, 3})) << std::endl; - std::cout << Json(gate::ParamRY(2, 1.5, {0, 3})) << std::endl; - std::cout << Json(gate::ParamRZ(2, 1.5, {0, 3})) << std::endl; - std::cout << Json(gate::ParamPauliRotation(pauli, 0.5)) << std::endl; + // std::cout << Json(gate::ParamRX(2, 1.5, {0, 3})) << std::endl; + // std::cout << Json(gate::ParamRY(2, 1.5, {0, 3})) << std::endl; + // std::cout << Json(gate::ParamRZ(2, 1.5, {0, 3})) << std::endl; + // std::cout << Json(gate::ParamPauliRotation(pauli, 0.5)) << std::endl; - auto paramprobgate = gate::ParamProbablistic( - {.1, .9}, {gate::ParamRX(0), gate::I()}); - std::cout << Json(paramprobgate) << std::endl; - } - { - auto x = gate::X(1, {2}); - Json j = x; - std::cout << j << std::endl; - Gate gate = j; - std::cout << gate << std::endl; - } - { - auto x = gate::RX(1, 0.5, {2}); - Json j = x; - std::cout << j << std::endl; - Gate gate = j; - std::cout << gate << std::endl; - } - { - auto x = gate::Swap(1, 3, {2}); - Json j = x; - std::cout << j << std::endl; - Gate gate = j; - std::cout << gate << std::endl; - } - { - PauliOperator pauli("X 2 Y 1"); - auto x = gate::PauliRotation(pauli, 1.5, {0, 3}); - Json j = x; - std::cout << j << std::endl; - Gate gate = j; - std::cout << gate << std::endl; - } - { - auto probgate = - gate::Probablistic({.1, .9}, {gate::X(0, {2, 3}), gate::I()}); - Json j = probgate; - std::cout << j << std::endl; - Gate gate = j; - std::cout << gate << std::endl; - } - { - auto x = gate::ParamRX(1, {2}); - Json j = x; - std::cout << j << std::endl; - ParamGate gate = j; - std::cout << gate << std::endl; - } - { - auto paramprobgate = gate::ParamProbablistic( - {.1, .9}, {gate::ParamRX(0), gate::I()}); - Json j = paramprobgate; - std::cout << j << std::endl; - ParamGate gate = j; - std::cout << gate << std::endl; - } - { - Circuit circuit(10); - circuit.add_gate(gate::X(0, {3})); - circuit.add_param_gate(gate::ParamRX(0, 0.5, {3}), "RX"); - Json j = circuit; - std::cout << j << std::endl; - Circuit c = j; - std::cout << Json(c) << std::endl; - } + // auto paramprobgate = gate::ParamProbablistic( + // {.1, .9}, {gate::ParamRX(0), gate::I()}); + // std::cout << Json(paramprobgate) << std::endl; + // } + // { + // auto x = gate::X(1, {2}); + // Json j = x; + // std::cout << j << std::endl; + // Gate gate = j; + // std::cout << gate << std::endl; + // } + // { + // auto x = gate::RX(1, 0.5, {2}); + // Json j = x; + // std::cout << j << std::endl; + // Gate gate = j; + // std::cout << gate << std::endl; + // } + // { + // auto x = gate::Swap(1, 3, {2}); + // Json j = x; + // std::cout << j << std::endl; + // Gate gate = j; + // std::cout << gate << std::endl; + // } + // { + // PauliOperator pauli("X 2 Y 1"); + // auto x = gate::PauliRotation(pauli, 1.5, {0, 3}); + // Json j = x; + // std::cout << j << std::endl; + // Gate gate = j; + // std::cout << gate << std::endl; + // } + // { + // auto probgate = + // gate::Probablistic({.1, .9}, {gate::X(0, {2, 3}), + // gate::I()}); + // Json j = probgate; + // std::cout << j << std::endl; + // Gate gate = j; + // std::cout << gate << std::endl; + // } + // { + // auto x = gate::ParamRX(1, {2}); + // Json j = x; + // std::cout << j << std::endl; + // ParamGate gate = j; + // std::cout << gate << std::endl; + // } + // { + // auto paramprobgate = gate::ParamProbablistic( + // {.1, .9}, {gate::ParamRX(0), gate::I()}); + // Json j = paramprobgate; + // std::cout << j << std::endl; + // ParamGate gate = j; + // std::cout << gate << std::endl; + // } + // { + // Circuit circuit(10); + // circuit.add_gate(gate::X(0, {3})); + // circuit.add_param_gate(gate::ParamRX(0, 0.5, {3}), "RX"); + // Json j = circuit; + // std::cout << j << std::endl; + // Circuit c = j; + // std::cout << Json(c) << std::endl; + // } Kokkos::finalize(); } diff --git a/script/configure_dev b/script/configure_dev index 18f3481b..abded865 100755 --- a/script/configure_dev +++ b/script/configure_dev @@ -4,7 +4,8 @@ set -eux CMAKE_C_COMPILER=${CMAKE_C_COMPILER:-"gcc"} CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER:-"g++"} -CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-"Release"} +CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-"Debug"} + CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX:-/usr/local} SCALUQ_USE_OMP=${SCALUQ_USE_OMP:-"ON"} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 705578ba..ab0bc5c7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,26 +1,26 @@ cmake_minimum_required(VERSION 3.21) target_sources(scaluq PRIVATE - circuit/circuit.cpp - gate/merge_gate.cpp - gate/gate_matrix.cpp - gate/gate_pauli.cpp - gate/gate_probablistic.cpp - gate/gate_standard.cpp - gate/gate.cpp - gate/param_gate.cpp - gate/param_gate_pauli.cpp - gate/param_gate_probablistic.cpp - gate/param_gate_standard.cpp - gate/param_gate.cpp - gate/update_ops_dense_matrix.cpp - gate/update_ops_sparse_matrix.cpp - gate/update_ops_standard.cpp + # circuit/circuit.cpp + # gate/merge_gate.cpp + # gate/gate_matrix.cpp + # gate/gate_pauli.cpp + # gate/gate_probablistic.cpp + # gate/gate_standard.cpp + # gate/gate.cpp + # gate/param_gate.cpp + # gate/param_gate_pauli.cpp + # gate/param_gate_probablistic.cpp + # gate/param_gate_standard.cpp + # gate/param_gate.cpp + # gate/update_ops_dense_matrix.cpp + # gate/update_ops_sparse_matrix.cpp + # gate/update_ops_standard.cpp state/state_vector.cpp state/state_vector_batched.cpp - operator/apply_pauli.cpp - operator/pauli_operator.cpp - operator/operator.cpp + # operator/apply_pauli.cpp + # operator/pauli_operator.cpp + # operator/operator.cpp util/utility.cpp kokkos.cpp types.cpp diff --git a/src/gate/merge_gate.cpp b/src/gate/merge_gate.cpp index d273b6d2..b0bce934 100644 --- a/src/gate/merge_gate.cpp +++ b/src/gate/merge_gate.cpp @@ -20,10 +20,7 @@ std::pair, Fp> merge_gate_dense_matrix(const Gate& gate1, const Gat gate2->target_qubit_list(), gate2->control_qubit_mask() & ~common_control_mask, merged_operand_vector); - std::cerr << matrix1 << std::endl; - std::cerr << matrix2 << std::endl; auto matrix = matrix2 * matrix1; - std::cerr << matrix << std::endl; return {gate::DenseMatrix( merged_operand_vector, matrix, internal::mask_to_vector(common_control_mask)), 0.}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b91dd1a6..38e048dd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,15 +3,14 @@ cmake_minimum_required(VERSION 3.21) enable_testing() add_executable(scaluq_test EXCLUDE_FROM_ALL - circuit/circuit_test.cpp - circuit/param_circuit_test.cpp - gate/gate_test.cpp - gate/batched_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 + # circuit/circuit_test.cpp + # circuit/param_circuit_test.cpp + # gate/gate_test.cpp + # gate/batched_gate_test.cpp + # # # gate/merge_test.cpp + # gate/param_gate_test.cpp + # operator/test_pauli_operator.cpp + # operator/test_operator.cpp state/state_vector_test.cpp state/state_vector_batched_test.cpp ) diff --git a/tests/state/state_vector_test.cpp b/tests/state/state_vector_test.cpp index ef3c0dc2..6f35e125 100644 --- a/tests/state/state_vector_test.cpp +++ b/tests/state/state_vector_test.cpp @@ -11,14 +11,14 @@ using namespace scaluq; TEST(StateVectorTest, HaarRandomStateNorm) { { - const int n_tries = 20; + const int n_tries = 6; for (int n = 1; n <= n_tries; n++) { const auto state = StateVector::Haar_random_state(n); ASSERT_NEAR(state.get_squared_norm(), 1., eps); } } { - const int n_tries = 20; + const int n_tries = 6; for (int n = 1; n <= n_tries; n++) { const auto state = StateVector::Haar_random_state(n); ASSERT_NEAR(state.get_squared_norm(), 1., eps); @@ -27,7 +27,7 @@ TEST(StateVectorTest, HaarRandomStateNorm) { } TEST(StateVectorTest, OperationAtIndex) { - auto state = StateVector::Haar_random_state(10); + auto state = StateVector::Haar_random_state(6); for (std::uint64_t i = 0; i < state.dim(); ++i) { state.set_amplitude_at(i, 1); ASSERT_NEAR(state.get_amplitude_at(i).real(), 1., eps); @@ -73,7 +73,7 @@ TEST(StateVectorTest, ComputationalBasisState) { } TEST(StateVectorTest, HaarRandomStateSameSeed) { - const std::uint64_t n = 10, m = 5; + const std::uint64_t n = 6, m = 5; for (std::uint64_t i = 0; i < m; ++i) { StateVector state1(StateVector::Haar_random_state(n, i)), state2(StateVector::Haar_random_state(n, i)); @@ -82,7 +82,7 @@ TEST(StateVectorTest, HaarRandomStateSameSeed) { } TEST(StateVectorTest, HaarRandomStateWithoutSeed) { - const std::uint64_t n = 10, m = 5; + const std::uint64_t n = 6, m = 5; for (std::uint64_t i = 0; i < m; ++i) { StateVector state1(StateVector::Haar_random_state(n)), state2(StateVector::Haar_random_state(n)); @@ -91,7 +91,7 @@ TEST(StateVectorTest, HaarRandomStateWithoutSeed) { } TEST(StateVectorTest, AddState) { - const std::uint64_t n = 10; + const std::uint64_t n = 6; StateVector state1(StateVector::Haar_random_state(n)); StateVector state2(StateVector::Haar_random_state(n)); auto vec1 = state1.get_amplitudes(); @@ -108,7 +108,7 @@ TEST(StateVectorTest, AddState) { TEST(StateVectorTest, AddStateWithCoef) { const CComplex coef(2.5, 1.3); - const std::uint64_t n = 10; + const std::uint64_t n = 6; StateVector state1(StateVector::Haar_random_state(n)); StateVector state2(StateVector::Haar_random_state(n)); auto vec1 = state1.get_amplitudes(); @@ -125,7 +125,7 @@ TEST(StateVectorTest, AddStateWithCoef) { } TEST(StateVectorTest, MultiplyCoef) { - const std::uint64_t n = 10; + const std::uint64_t n = 6; const CComplex coef(0.5, 0.2); StateVector state(StateVector::Haar_random_state(n)); @@ -141,7 +141,7 @@ TEST(StateVectorTest, MultiplyCoef) { } TEST(StateVectorTest, GetZeroProbability) { - const std::uint64_t n = 10; + const std::uint64_t n = 6; StateVector state(n); state.set_computational_basis(1); for (std::uint64_t i = 2; i <= 10; ++i) { @@ -213,7 +213,7 @@ TEST(StateVectorTest, GetMarginalProbability) { } TEST(StateVectorTest, SamplingSuperpositionState) { - const std::uint64_t n = 10; + const std::uint64_t n = 6; const std::uint64_t nshot = 65536; const std::uint64_t test_count = 10; std::uint64_t pass_count = 0; @@ -248,7 +248,7 @@ TEST(StateVectorTest, SamplingSuperpositionState) { } TEST(StateVectorTest, SamplingComputationalBasis) { - const std::uint64_t n = 10; + const std::uint64_t n = 7; const std::uint64_t nshot = 1024; StateVector state(n); state.set_computational_basis(100); diff --git a/tests/util/util.hpp b/tests/util/util.hpp index 4c1c9b08..775abd67 100644 --- a/tests/util/util.hpp +++ b/tests/util/util.hpp @@ -19,7 +19,7 @@ constexpr Fp eps_() { else if constexpr (std::is_same_v) return 1e-4; else - static_assert(internal::lazy_false_v, "unknown GateImpl"); + static_assert(internal::lazy_false_v, "unknown GateImpl"); } template constexpr Fp eps = eps_(); From 31f27799445c3b38e50478376a7b7f4207c709ef Mon Sep 17 00:00:00 2001 From: gandalfr-KY Date: Tue, 7 Jan 2025 17:45:10 +0000 Subject: [PATCH 2/4] test ok --- src/CMakeLists.txt | 36 ++++++++++++++++++------------------ tests/CMakeLists.txt | 16 ++++++++-------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ab0bc5c7..705578ba 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,26 +1,26 @@ cmake_minimum_required(VERSION 3.21) target_sources(scaluq PRIVATE - # circuit/circuit.cpp - # gate/merge_gate.cpp - # gate/gate_matrix.cpp - # gate/gate_pauli.cpp - # gate/gate_probablistic.cpp - # gate/gate_standard.cpp - # gate/gate.cpp - # gate/param_gate.cpp - # gate/param_gate_pauli.cpp - # gate/param_gate_probablistic.cpp - # gate/param_gate_standard.cpp - # gate/param_gate.cpp - # gate/update_ops_dense_matrix.cpp - # gate/update_ops_sparse_matrix.cpp - # gate/update_ops_standard.cpp + circuit/circuit.cpp + gate/merge_gate.cpp + gate/gate_matrix.cpp + gate/gate_pauli.cpp + gate/gate_probablistic.cpp + gate/gate_standard.cpp + gate/gate.cpp + gate/param_gate.cpp + gate/param_gate_pauli.cpp + gate/param_gate_probablistic.cpp + gate/param_gate_standard.cpp + gate/param_gate.cpp + gate/update_ops_dense_matrix.cpp + gate/update_ops_sparse_matrix.cpp + gate/update_ops_standard.cpp state/state_vector.cpp state/state_vector_batched.cpp - # operator/apply_pauli.cpp - # operator/pauli_operator.cpp - # operator/operator.cpp + operator/apply_pauli.cpp + operator/pauli_operator.cpp + operator/operator.cpp util/utility.cpp kokkos.cpp types.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 38e048dd..a0eaf17a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,14 +3,14 @@ cmake_minimum_required(VERSION 3.21) enable_testing() add_executable(scaluq_test EXCLUDE_FROM_ALL - # circuit/circuit_test.cpp - # circuit/param_circuit_test.cpp - # gate/gate_test.cpp - # gate/batched_gate_test.cpp - # # # gate/merge_test.cpp - # gate/param_gate_test.cpp - # operator/test_pauli_operator.cpp - # operator/test_operator.cpp + circuit/circuit_test.cpp + circuit/param_circuit_test.cpp + gate/gate_test.cpp + gate/batched_gate_test.cpp + gate/merge_test.cpp + gate/param_gate_test.cpp + operator/test_pauli_operator.cpp + operator/test_operator.cpp state/state_vector_test.cpp state/state_vector_batched_test.cpp ) From ea9e6caa502977ba7ce5959a6e25ad72a674767a Mon Sep 17 00:00:00 2001 From: gandalfr-KY Date: Fri, 10 Jan 2025 03:27:00 +0000 Subject: [PATCH 3/4] add undefined --- CMakeLists.txt | 4 +- exe/main.cpp | 283 ++++++++++++++++++++++++------------------------- 2 files changed, 143 insertions(+), 144 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0353b873..2475b928 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,8 +141,8 @@ if ((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") OR (${CMAKE_CXX_COMPILER_ID} STREQ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") target_compile_options(scaluq PUBLIC $,-O0 -g,-O3>) else() - target_compile_options(scaluq PUBLIC $,-O0 -g -fsanitize=address,-O3>) - target_link_options(scaluq PUBLIC $<$:-fsanitize=address>) + target_compile_options(scaluq PUBLIC $,-O0 -g -fsanitize=address,undefined,-O3>) + target_link_options(scaluq PUBLIC $<$:-fsanitize=address,undefined>) endif() endif() endif() diff --git a/exe/main.cpp b/exe/main.cpp index 084f3d21..5659677e 100644 --- a/exe/main.cpp +++ b/exe/main.cpp @@ -7,154 +7,153 @@ using namespace nlohmann; int main() { scaluq::initialize(); // must be called before using any scaluq methods - // { - // std::uint64_t n_qubits = 3; - // scaluq::StateVector state(n_qubits); - // state.load({0, 1, 2, 3, 4, 5, 6, 7}); - // Json j = state; - // std::cout << j << std::endl; - // state = j; - // std::cout << state << std::endl; + { + std::uint64_t n_qubits = 3; + scaluq::StateVector state(n_qubits); + state.load({0, 1, 2, 3, 4, 5, 6, 7}); + Json j = state; + std::cout << j << std::endl; + state = j; + std::cout << state << std::endl; - // scaluq::Circuit circuit(n_qubits); - // circuit.add_gate(scaluq::gate::X(0)); - // circuit.add_gate(scaluq::gate::CNot(0, 1)); - // circuit.add_gate(scaluq::gate::Y(1)); - // circuit.add_gate(scaluq::gate::RX(1, std::numbers::pi / 2)); - // circuit.update_quantum_state(state); + scaluq::Circuit circuit(n_qubits); + circuit.add_gate(scaluq::gate::X(0)); + circuit.add_gate(scaluq::gate::CNot(0, 1)); + circuit.add_gate(scaluq::gate::Y(1)); + circuit.add_gate(scaluq::gate::RX(1, std::numbers::pi / 2)); + circuit.update_quantum_state(state); - // scaluq::Operator observable(n_qubits); - // observable.add_random_operator(1, 0); - // auto value = observable.get_expectation_value(state); - // std::cout << value << std::endl; - // } - // { - // std::uint64_t n_qubits = 2, batch_size = 2; - // scaluq::StateVectorBatched states(batch_size, n_qubits); - // states.set_Haar_random_state(batch_size, n_qubits, false); - // Json j = states; - // std::cout << j << std::endl; - // states = j; - // std::cout << states << std::endl; - // } - // { - // double coef = 2.0; - // std::string pauli_string = "X 0 Z 1 Y 2"; - // PauliOperator pauli(pauli_string, coef); - // Json j = pauli; - // std::cout << j << std::endl; - // pauli = j; - // } - // { - // std::uint64_t n_qubits = 3; - // Operator op(n_qubits); - // op.add_operator({0b001, 0b010, Complex(2)}); - // op.add_operator({"X 2 Y 1", 1}); - // Json j = op; - // std::cout << j << std::endl; - // op = j; - // } - // { - // std::cout << Json(gate::I()) << std::endl; - // std::cout << Json(gate::X(2, {0, 3})) << std::endl; - // std::cout << Json(gate::Y(2, {0, 3})) << std::endl; - // std::cout << Json(gate::Z(2, {0, 3})) << std::endl; - // std::cout << Json(gate::H(2, {0, 3})) << std::endl; - // std::cout << Json(gate::S(2, {0, 3})) << std::endl; - // std::cout << Json(gate::Sdag(2, {0, 3})) << std::endl; - // std::cout << Json(gate::T(2, {0, 3})) << std::endl; - // std::cout << Json(gate::Tdag(2, {0, 3})) << std::endl; - // std::cout << Json(gate::SqrtX(2, {0, 3})) << std::endl; - // std::cout << Json(gate::SqrtXdag(2, {0, 3})) << std::endl; - // std::cout << Json(gate::SqrtY(2, {0, 3})) << std::endl; - // std::cout << Json(gate::SqrtYdag(2, {0, 3})) << std::endl; - // std::cout << Json(gate::RX(2, 0.5, {0, 3})) << std::endl; - // std::cout << Json(gate::RY(2, 0.5, {0, 3})) << std::endl; - // std::cout << Json(gate::RZ(2, 0.5, {0, 3})) << std::endl; - // std::cout << Json(gate::U1(2, 0.5, {0, 3})) << std::endl; - // std::cout << Json(gate::U2(2, 0.5, 0.3, {0, 3})) << std::endl; - // std::cout << Json(gate::U3(2, 0.5, 0.3, 0.1, {0, 3})) << std::endl; - // std::cout << Json(gate::Swap(1, 2, {0, 3})) << std::endl; + scaluq::Operator observable(n_qubits); + observable.add_random_operator(1, 0); + auto value = observable.get_expectation_value(state); + std::cout << value << std::endl; + } + { + std::uint64_t n_qubits = 2, batch_size = 2; + scaluq::StateVectorBatched states(batch_size, n_qubits); + states.set_Haar_random_state(batch_size, n_qubits, false); + Json j = states; + std::cout << j << std::endl; + states = j; + std::cout << states << std::endl; + } + { + double coef = 2.0; + std::string pauli_string = "X 0 Z 1 Y 2"; + PauliOperator pauli(pauli_string, coef); + Json j = pauli; + std::cout << j << std::endl; + pauli = j; + } + { + std::uint64_t n_qubits = 3; + Operator op(n_qubits); + op.add_operator({0b001, 0b010, Complex(2)}); + op.add_operator({"X 2 Y 1", 1}); + Json j = op; + std::cout << j << std::endl; + op = j; + } + { + std::cout << Json(gate::I()) << std::endl; + std::cout << Json(gate::X(2, {0, 3})) << std::endl; + std::cout << Json(gate::Y(2, {0, 3})) << std::endl; + std::cout << Json(gate::Z(2, {0, 3})) << std::endl; + std::cout << Json(gate::H(2, {0, 3})) << std::endl; + std::cout << Json(gate::S(2, {0, 3})) << std::endl; + std::cout << Json(gate::Sdag(2, {0, 3})) << std::endl; + std::cout << Json(gate::T(2, {0, 3})) << std::endl; + std::cout << Json(gate::Tdag(2, {0, 3})) << std::endl; + std::cout << Json(gate::SqrtX(2, {0, 3})) << std::endl; + std::cout << Json(gate::SqrtXdag(2, {0, 3})) << std::endl; + std::cout << Json(gate::SqrtY(2, {0, 3})) << std::endl; + std::cout << Json(gate::SqrtYdag(2, {0, 3})) << std::endl; + std::cout << Json(gate::RX(2, 0.5, {0, 3})) << std::endl; + std::cout << Json(gate::RY(2, 0.5, {0, 3})) << std::endl; + std::cout << Json(gate::RZ(2, 0.5, {0, 3})) << std::endl; + std::cout << Json(gate::U1(2, 0.5, {0, 3})) << std::endl; + std::cout << Json(gate::U2(2, 0.5, 0.3, {0, 3})) << std::endl; + std::cout << Json(gate::U3(2, 0.5, 0.3, 0.1, {0, 3})) << std::endl; + std::cout << Json(gate::Swap(1, 2, {0, 3})) << std::endl; - // PauliOperator pauli("X 2 Y 1"); - // std::cout << Json(gate::Pauli(pauli)) << std::endl; - // std::cout << Json(gate::PauliRotation(pauli, 0.5)) << std::endl; + PauliOperator pauli("X 2 Y 1"); + std::cout << Json(gate::Pauli(pauli)) << std::endl; + std::cout << Json(gate::PauliRotation(pauli, 0.5)) << std::endl; - // auto probgate = - // gate::Probablistic({.1, .9}, {gate::X(0), gate::I()}); - // std::cout << Json(probgate) << std::endl; + auto probgate = + gate::Probablistic({.1, .9}, {gate::X(0), gate::I()}); + std::cout << Json(probgate) << std::endl; - // std::cout << Json(gate::ParamRX(2, 1.5, {0, 3})) << std::endl; - // std::cout << Json(gate::ParamRY(2, 1.5, {0, 3})) << std::endl; - // std::cout << Json(gate::ParamRZ(2, 1.5, {0, 3})) << std::endl; - // std::cout << Json(gate::ParamPauliRotation(pauli, 0.5)) << std::endl; + std::cout << Json(gate::ParamRX(2, 1.5, {0, 3})) << std::endl; + std::cout << Json(gate::ParamRY(2, 1.5, {0, 3})) << std::endl; + std::cout << Json(gate::ParamRZ(2, 1.5, {0, 3})) << std::endl; + std::cout << Json(gate::ParamPauliRotation(pauli, 0.5)) << std::endl; - // auto paramprobgate = gate::ParamProbablistic( - // {.1, .9}, {gate::ParamRX(0), gate::I()}); - // std::cout << Json(paramprobgate) << std::endl; - // } - // { - // auto x = gate::X(1, {2}); - // Json j = x; - // std::cout << j << std::endl; - // Gate gate = j; - // std::cout << gate << std::endl; - // } - // { - // auto x = gate::RX(1, 0.5, {2}); - // Json j = x; - // std::cout << j << std::endl; - // Gate gate = j; - // std::cout << gate << std::endl; - // } - // { - // auto x = gate::Swap(1, 3, {2}); - // Json j = x; - // std::cout << j << std::endl; - // Gate gate = j; - // std::cout << gate << std::endl; - // } - // { - // PauliOperator pauli("X 2 Y 1"); - // auto x = gate::PauliRotation(pauli, 1.5, {0, 3}); - // Json j = x; - // std::cout << j << std::endl; - // Gate gate = j; - // std::cout << gate << std::endl; - // } - // { - // auto probgate = - // gate::Probablistic({.1, .9}, {gate::X(0, {2, 3}), - // gate::I()}); - // Json j = probgate; - // std::cout << j << std::endl; - // Gate gate = j; - // std::cout << gate << std::endl; - // } - // { - // auto x = gate::ParamRX(1, {2}); - // Json j = x; - // std::cout << j << std::endl; - // ParamGate gate = j; - // std::cout << gate << std::endl; - // } - // { - // auto paramprobgate = gate::ParamProbablistic( - // {.1, .9}, {gate::ParamRX(0), gate::I()}); - // Json j = paramprobgate; - // std::cout << j << std::endl; - // ParamGate gate = j; - // std::cout << gate << std::endl; - // } - // { - // Circuit circuit(10); - // circuit.add_gate(gate::X(0, {3})); - // circuit.add_param_gate(gate::ParamRX(0, 0.5, {3}), "RX"); - // Json j = circuit; - // std::cout << j << std::endl; - // Circuit c = j; - // std::cout << Json(c) << std::endl; - // } + auto paramprobgate = gate::ParamProbablistic( + {.1, .9}, {gate::ParamRX(0), gate::I()}); + std::cout << Json(paramprobgate) << std::endl; + } + { + auto x = gate::X(1, {2}); + Json j = x; + std::cout << j << std::endl; + Gate gate = j; + std::cout << gate << std::endl; + } + { + auto x = gate::RX(1, 0.5, {2}); + Json j = x; + std::cout << j << std::endl; + Gate gate = j; + std::cout << gate << std::endl; + } + { + auto x = gate::Swap(1, 3, {2}); + Json j = x; + std::cout << j << std::endl; + Gate gate = j; + std::cout << gate << std::endl; + } + { + PauliOperator pauli("X 2 Y 1"); + auto x = gate::PauliRotation(pauli, 1.5, {0, 3}); + Json j = x; + std::cout << j << std::endl; + Gate gate = j; + std::cout << gate << std::endl; + } + { + auto probgate = + gate::Probablistic({.1, .9}, {gate::X(0, {2, 3}), gate::I()}); + Json j = probgate; + std::cout << j << std::endl; + Gate gate = j; + std::cout << gate << std::endl; + } + { + auto x = gate::ParamRX(1, {2}); + Json j = x; + std::cout << j << std::endl; + ParamGate gate = j; + std::cout << gate << std::endl; + } + { + auto paramprobgate = gate::ParamProbablistic( + {.1, .9}, {gate::ParamRX(0), gate::I()}); + Json j = paramprobgate; + std::cout << j << std::endl; + ParamGate gate = j; + std::cout << gate << std::endl; + } + { + Circuit circuit(10); + circuit.add_gate(gate::X(0, {3})); + circuit.add_param_gate(gate::ParamRX(0, 0.5, {3}), "RX"); + Json j = circuit; + std::cout << j << std::endl; + Circuit c = j; + std::cout << Json(c) << std::endl; + } Kokkos::finalize(); } From a96e4c4154daee4d6c03fc12edce064c97fa0908 Mon Sep 17 00:00:00 2001 From: gandalfr-KY Date: Fri, 10 Jan 2025 03:31:37 +0000 Subject: [PATCH 4/4] use escape --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2475b928..203bbe11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,8 +141,8 @@ if ((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") OR (${CMAKE_CXX_COMPILER_ID} STREQ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") target_compile_options(scaluq PUBLIC $,-O0 -g,-O3>) else() - target_compile_options(scaluq PUBLIC $,-O0 -g -fsanitize=address,undefined,-O3>) - target_link_options(scaluq PUBLIC $<$:-fsanitize=address,undefined>) + target_compile_options(scaluq PUBLIC $,-O0 -g -fsanitize=address$undefined,-O3>) + target_link_options(scaluq PUBLIC $<$:-fsanitize=address$undefined>) endif() endif() endif()