diff --git a/python/binding.cpp b/python/binding.cpp index 486d87ae..812c0285 100644 --- a/python/binding.cpp +++ b/python/binding.cpp @@ -105,14 +105,14 @@ NB_MODULE(scaluq_core, m) { "seed"_a = std::nullopt, "Constructing state vector with Haar random state. If seed is not specified, the value " "from random device is used.") - .def("set_amplitude_at_index", - &StateVector::set_amplitude_at_index, + .def("set_amplitude_at", + &StateVector::set_amplitude_at, "Manually set amplitude at one index.") - .def("get_amplitude_at_index", - &StateVector::get_amplitude_at_index, + .def("get_amplitude_at", + &StateVector::get_amplitude_at, "Get amplitude at one index.\n\n.. note:: If you want to get all amplitudes, you " "should " - "use `StateVector::amplitudes()`.") + "use `StateVector::get_amplitudes()`.") .def("set_zero_state", &StateVector::set_zero_state, "Initialize with computational basis $\\ket{00\\dots0}$.") @@ -122,7 +122,9 @@ NB_MODULE(scaluq_core, m) { .def("set_computational_basis", &StateVector::set_computational_basis, "Initialize with computational basis \\ket{\\mathrm{basis}}.") - .def("amplitudes", &StateVector::amplitudes, "Get all amplitudes with as `list[complex]`.") + .def("amplitudes", + &StateVector::get_amplitudes, + "Get all amplitudes with as `list[complex]`.") .def("n_qubits", &StateVector::n_qubits, "Get num of qubits.") .def("dim", &StateVector::dim, "Get dimension of the vector ($=2^\\mathrm{n\\_qubits}$).") .def("get_squared_norm", @@ -195,8 +197,8 @@ NB_MODULE(scaluq_core, m) { nb::overload_cast( &StateVectorBatched::set_state_vector), "Set the state vector for a specific batch.") - .def("get_state_vector", - &StateVectorBatched::get_state_vector, + .def("get_state_vector_at", + &StateVectorBatched::get_state_vector_at, "Get the state vector for a specific batch.") .def("set_zero_state", &StateVectorBatched::set_zero_state, @@ -234,7 +236,7 @@ NB_MODULE(scaluq_core, m) { "Construct batched state vectors with Haar random states. If seed is not " "specified, the value from random device is used.") .def("amplitudes", - &StateVectorBatched::amplitudes, + &StateVectorBatched::get_amplitudes, "Get all amplitudes with as `list[list[complex]]`.") .def("get_squared_norm", &StateVectorBatched::get_squared_norm, @@ -304,28 +306,28 @@ NB_MODULE(scaluq_core, m) { nb::class_(m, #GATE_TYPE, DESCRIPTION) \ .def("gate_type", &GATE_TYPE::gate_type, "Get gate type as `GateType` enum.") \ .def( \ - "get_target_qubit_list", \ - [](const GATE_TYPE &gate) { return gate->get_target_qubit_list(); }, \ + "target_qubit_list", \ + [](const GATE_TYPE &gate) { return gate->target_qubit_list(); }, \ "Get target qubits as `list[int]`. **Control qubits is not included.**") \ .def( \ - "get_control_qubit_list", \ - [](const GATE_TYPE &gate) { return gate->get_control_qubit_list(); }, \ + "control_qubit_list", \ + [](const GATE_TYPE &gate) { return gate->control_qubit_list(); }, \ "Get control qubits as `list[int]`.") \ .def( \ - "get_operand_qubit_list", \ - [](const GATE_TYPE &gate) { return gate->get_operand_qubit_list(); }, \ + "operand_qubit_list", \ + [](const GATE_TYPE &gate) { return gate->operand_qubit_list(); }, \ "Get target and control qubits as `list[int]`.") \ .def( \ - "get_target_qubit_mask", \ - [](const GATE_TYPE &gate) { return gate->get_target_qubit_mask(); }, \ + "target_qubit_mask", \ + [](const GATE_TYPE &gate) { return gate->target_qubit_mask(); }, \ "Get target qubits as mask. **Control qubits is not included.**") \ .def( \ - "get_control_qubit_mask", \ - [](const GATE_TYPE &gate) { return gate->get_control_qubit_mask(); }, \ + "control_qubit_mask", \ + [](const GATE_TYPE &gate) { return gate->control_qubit_mask(); }, \ "Get control qubits as mask.") \ .def( \ - "get_operand_qubit_mask", \ - [](const GATE_TYPE &gate) { return gate->get_operand_qubit_mask(); }, \ + "operand_qubit_mask", \ + [](const GATE_TYPE &gate) { return gate->operand_qubit_mask(); }, \ "Get target and control qubits as mask.") \ .def( \ "get_inverse", \ @@ -546,10 +548,10 @@ NB_MODULE(scaluq_core, m) { DEF_GATE_FACTORY(Probablistic); nb::enum_(m, "ParamGateType", "Enum of ParamGate Type.") - .value("PRX", ParamGateType::PRX) - .value("PRY", ParamGateType::PRY) - .value("PRZ", ParamGateType::PRZ) - .value("PPauliRotation", ParamGateType::PPauliRotation); + .value("ParamRX", ParamGateType::ParamRX) + .value("ParamRY", ParamGateType::ParamRY) + .value("ParamRZ", ParamGateType::ParamRZ) + .value("ParamPauliRotation", ParamGateType::ParamPauliRotation); m.def( "merge_gate", &merge_gate, "Merge two gates. return value is (merged gate, global phase)."); @@ -560,28 +562,28 @@ NB_MODULE(scaluq_core, m) { &PGATE_TYPE::param_gate_type, \ "Get parametric gate type as `ParamGateType` enum.") \ .def( \ - "get_target_qubit_list", \ - [](const PGATE_TYPE &gate) { return gate->get_target_qubit_list(); }, \ + "target_qubit_list", \ + [](const PGATE_TYPE &gate) { return gate->target_qubit_list(); }, \ "Get target qubits as `list[int]`. **Control qubits is not included.**") \ .def( \ - "get_control_qubit_list", \ - [](const PGATE_TYPE &gate) { return gate->get_control_qubit_list(); }, \ + "control_qubit_list", \ + [](const PGATE_TYPE &gate) { return gate->control_qubit_list(); }, \ "Get control qubits as `list[int]`.") \ .def( \ - "get_operand_qubit_list", \ - [](const PGATE_TYPE &gate) { return gate->get_operand_qubit_list(); }, \ + "operand_qubit_list", \ + [](const PGATE_TYPE &gate) { return gate->operand_qubit_list(); }, \ "Get target and control qubits as `list[int]`.") \ .def( \ - "get_target_qubit_mask", \ - [](const PGATE_TYPE &gate) { return gate->get_target_qubit_mask(); }, \ + "target_qubit_mask", \ + [](const PGATE_TYPE &gate) { return gate->target_qubit_mask(); }, \ "Get target qubits as mask. **Control qubits is not included.**") \ .def( \ - "get_control_qubit_mask", \ - [](const PGATE_TYPE &gate) { return gate->get_control_qubit_mask(); }, \ + "control_qubit_mask", \ + [](const PGATE_TYPE &gate) { return gate->control_qubit_mask(); }, \ "Get control qubits as mask.") \ .def( \ - "get_operand_qubit_mask", \ - [](const PGATE_TYPE &gate) { return gate->get_operand_qubit_mask(); }, \ + "operand_qubit_mask", \ + [](const PGATE_TYPE &gate) { return gate->operand_qubit_mask(); }, \ "Get target and control qubits as mask.") \ .def( \ "get_inverse", \ @@ -610,67 +612,67 @@ NB_MODULE(scaluq_core, m) { ParamGate, "General class of parametric quantum gate.\n\n.. note:: Downcast to requred to use " "gate-specific functions.") - .def(nb::init(), "Upcast from `PRXGate`.") - .def(nb::init(), "Upcast from `PRYGate`.") - .def(nb::init(), "Upcast from `PRZGate`.") - .def(nb::init(), "Upcast from `PPauliRotationGate`."); + .def(nb::init(), "Upcast from `ParamRXGate`.") + .def(nb::init(), "Upcast from `ParamRYGate`.") + .def(nb::init(), "Upcast from `ParamRZGate`.") + .def(nb::init(), "Upcast from `ParamPauliRotationGate`."); - DEF_PGATE(PRXGate, + DEF_PGATE(ParamRXGate, "Specific class of parametric X rotation gate, represented as " - "$e^{-i\\frac{\\mathrm{angle}}{2}X}$. `angle` is given as `param * pcoef`."); - DEF_PGATE(PRYGate, + "$e^{-i\\frac{\\mathrm{angle}}{2}X}$. `angle` is given as `param * param_coef`."); + DEF_PGATE(ParamRYGate, "Specific class of parametric Y rotation gate, represented as " - "$e^{-i\\frac{\\mathrm{angle}}{2}Y}$. `angle` is given as `param * pcoef`."); - DEF_PGATE(PRZGate, + "$e^{-i\\frac{\\mathrm{angle}}{2}Y}$. `angle` is given as `param * param_coef`."); + DEF_PGATE(ParamRZGate, "Specific class of parametric Z rotation gate, represented as " - "$e^{-i\\frac{\\mathrm{angle}}{2}Z}$. `angle` is given as `param * pcoef`."); + "$e^{-i\\frac{\\mathrm{angle}}{2}Z}$. `angle` is given as `param * param_coef`."); - DEF_PGATE(PPauliRotationGate, + DEF_PGATE(ParamPauliRotationGate, "Specific class of parametric multi-qubit pauli-rotation gate, represented as " - "$e^{-i\\frac{\\mathrm{angle}}{2}P}$. `angle` is given as `param * pcoef`."); + "$e^{-i\\frac{\\mathrm{angle}}{2}P}$. `angle` is given as `param * param_coef`."); - DEF_PGATE(PProbablisticGate, + DEF_PGATE(ParamProbablisticGate, "Specific class of parametric probablistic gate. The gate to apply is picked from a " "cirtain " "distribution.") .def( "gate_list", - [](const PProbablisticGate &gate) { return gate->gate_list(); }, + [](const ParamProbablisticGate &gate) { return gate->gate_list(); }, nb::rv_policy::reference) .def( "distribution", - [](const PProbablisticGate &gate) { return gate->distribution(); }, + [](const ParamProbablisticGate &gate) { return gate->distribution(); }, nb::rv_policy::reference); - mgate.def("PRX", - &gate::PRX, - "Generate general ParamGate class instance of PRX.", + mgate.def("ParamRX", + &gate::ParamRX, + "Generate general ParamGate class instance of ParamRX.", "target"_a, "coef"_a = 1., "controls"_a = std::vector{}); - mgate.def("PRY", - &gate::PRY, - "Generate general ParamGate class instance of PRY.", + mgate.def("ParamRY", + &gate::ParamRY, + "Generate general ParamGate class instance of ParamRY.", "target"_a, "coef"_a = 1., "controls"_a = std::vector{}); - mgate.def("PRZ", - &gate::PRZ, - "Generate general ParamGate class instance of PRZ.", + mgate.def("ParamRZ", + &gate::ParamRZ, + "Generate general ParamGate class instance of ParamRZ.", "target"_a, "coef"_a = 1., "controls"_a = std::vector{}); - mgate.def("PPauliRotation", - &gate::PPauliRotation, - "Generate general ParamGate class instance of PPauliRotation.", + mgate.def("ParamPauliRotation", + &gate::ParamPauliRotation, + "Generate general ParamGate class instance of ParamPauliRotation.", "pauli"_a, "coef"_a = 1., "controls"_a = std::vector{}); - mgate.def("PProbablistic", - &gate::PProbablistic, - "Generate general ParamGate class instance of PProbablistic."); + mgate.def("ParamProbablistic", + &gate::ParamProbablistic, + "Generate general ParamGate class instance of ParamProbablistic."); mgate.def( - "PProbablistic", + "ParamProbablistic", [](const std::vector>> &prob_gate_list) { std::vector distribution; std::vector> gate_list; @@ -680,9 +682,9 @@ NB_MODULE(scaluq_core, m) { distribution.push_back(prob); gate_list.push_back(gate); } - return gate::PProbablistic(distribution, gate_list); + return gate::ParamProbablistic(distribution, gate_list); }, - "Generate general ParamGate class instance of PProbablistic."); + "Generate general ParamGate class instance of ParamProbablistic."); nb::class_(m, "Circuit", "Quantum circuit represented as gate array") .def(nb::init(), "Initialize empty circuit of specified qubits.") @@ -691,11 +693,11 @@ NB_MODULE(scaluq_core, m) { &Circuit::gate_list, "Get property of `gate_list`.", nb::rv_policy::reference) - .def("gate_count", &Circuit::gate_count, "Get property of `gate_count`.") + .def("n_gates", &Circuit::n_gates, "Get property of `n_gates`.") .def("key_set", &Circuit::key_set, "Get set of keys of parameters.") - .def("get", &Circuit::get, "Get reference of i-th gate.") - .def("get_key", - &Circuit::get_key, + .def("get_gate_at", &Circuit::get_gate_at, "Get reference of i-th gate.") + .def("get_param_key_at", + &Circuit::get_param_key_at, "Get parameter key of i-th gate. If it is not parametric, return None.") .def("calculate_depth", &Circuit::calculate_depth, "Get depth of circuit.") .def("add_gate", @@ -770,19 +772,15 @@ NB_MODULE(scaluq_core, m) { "target_qubit"_a, "pauli_id"_a, "Add a single pauli operation to the data.") - .def("get_coef", - &PauliOperator::Data::get_coef, - "Get the coefficient of the Pauli operator.") + .def("coef", &PauliOperator::Data::coef, "Get the coefficient of the Pauli operator.") .def("set_coef", &PauliOperator::Data::set_coef, "c"_a, "Set the coefficient of the Pauli operator.") - .def("get_target_qubit_list", - &PauliOperator::Data::get_target_qubit_list, + .def("target_qubit_list", + &PauliOperator::Data::target_qubit_list, "Get the list of target qubits.") - .def("get_pauli_id_list", - &PauliOperator::Data::get_pauli_id_list, - "Get the list of Pauli IDs.") + .def("pauli_id_list", &PauliOperator::Data::pauli_id_list, "Get the list of Pauli IDs.") .def("get_XZ_mask_representation", &PauliOperator::Data::get_XZ_mask_representation, "Get the X and Z mask representation as a tuple of vectors."); @@ -840,14 +838,14 @@ NB_MODULE(scaluq_core, m) { "from `i-th` bit of `bit_flip_mask` and `phase_flip_mask` as follows.\n\n.. " "csv-table::\n\n \"bit_flip\",\"phase_flip\",\"pauli\"\n \"0\",\"0\",\"I\"\n " "\"0\",\"1\",\"Z\"\n \"1\",\"0\",\"X\"\n \"1\",\"1\",\"Y\"") - .def("get_coef", &PauliOperator::get_coef, "Get property `coef`.") - .def("get_target_qubit_list", - &PauliOperator::get_target_qubit_list, + .def("coef", &PauliOperator::coef, "Get property `coef`.") + .def("target_qubit_list", + &PauliOperator::target_qubit_list, "Get qubits to be applied pauli.") - .def("get_pauli_id_list", - &PauliOperator::get_pauli_id_list, + .def("pauli_id_list", + &PauliOperator::pauli_id_list, "Get pauli id to be applied. The order is correspond to the result of " - "`get_target_qubit_list`") + "`target_qubit_list`") .def( "get_XZ_mask_representation", [](const PauliOperator &pauli) { diff --git a/scaluq/circuit/circuit.cpp b/scaluq/circuit/circuit.cpp index a349e877..4cf3badb 100644 --- a/scaluq/circuit/circuit.cpp +++ b/scaluq/circuit/circuit.cpp @@ -7,11 +7,11 @@ std::uint64_t Circuit::calculate_depth() const { std::vector filled_step(_n_qubits, 0ULL); for (const auto& gate : _gate_list) { std::vector control_qubits = - gate.index() == 0 ? std::get<0>(gate)->get_control_qubit_list() - : std::get<1>(gate).first->get_control_qubit_list(); + gate.index() == 0 ? std::get<0>(gate)->control_qubit_list() + : std::get<1>(gate).first->control_qubit_list(); std::vector target_qubits = - gate.index() == 0 ? std::get<0>(gate)->get_target_qubit_list() - : std::get<1>(gate).first->get_target_qubit_list(); + gate.index() == 0 ? std::get<0>(gate)->target_qubit_list() + : std::get<1>(gate).first->target_qubit_list(); std::uint64_t max_step_amount_target_qubits = 0; for (std::uint64_t control : control_qubits) { if (max_step_amount_target_qubits < filled_step[control]) { @@ -123,8 +123,8 @@ Circuit Circuit::get_inverse() const { } void Circuit::check_gate_is_valid(const Gate& gate) const { - auto targets = gate->get_target_qubit_list(); - auto controls = gate->get_control_qubit_list(); + auto targets = gate->target_qubit_list(); + auto controls = gate->control_qubit_list(); bool valid = true; if (!targets.empty()) valid &= *std::max_element(targets.begin(), targets.end()) < _n_qubits; if (!controls.empty()) valid &= *std::max_element(controls.begin(), controls.end()) < _n_qubits; @@ -134,8 +134,8 @@ void Circuit::check_gate_is_valid(const Gate& gate) const { } void Circuit::check_gate_is_valid(const ParamGate& param_gate) const { - auto targets = param_gate->get_target_qubit_list(); - auto controls = param_gate->get_control_qubit_list(); + auto targets = param_gate->target_qubit_list(); + auto controls = param_gate->control_qubit_list(); bool valid = true; if (!targets.empty()) valid &= *std::max_element(targets.begin(), targets.end()) < _n_qubits; if (!controls.empty()) valid &= *std::max_element(controls.begin(), controls.end()) < _n_qubits; diff --git a/scaluq/circuit/circuit.hpp b/scaluq/circuit/circuit.hpp index 656b87fe..21a21768 100644 --- a/scaluq/circuit/circuit.hpp +++ b/scaluq/circuit/circuit.hpp @@ -15,7 +15,7 @@ class Circuit { [[nodiscard]] inline std::uint64_t n_qubits() const { return _n_qubits; } [[nodiscard]] inline const std::vector& gate_list() const { return _gate_list; } - [[nodiscard]] inline std::uint64_t gate_count() { return _gate_list.size(); } + [[nodiscard]] inline std::uint64_t n_gates() { return _gate_list.size(); } [[nodiscard]] inline const std::set key_set() const { std::set key_set; for (auto&& gate : _gate_list) { @@ -23,13 +23,13 @@ class Circuit { } return key_set; } - [[nodiscard]] inline const GateWithKey& get(std::uint64_t idx) const { + [[nodiscard]] inline const GateWithKey& get_gate_at(std::uint64_t idx) const { if (idx >= _gate_list.size()) { - throw std::runtime_error("Circuit::get(std::uint64_t): index out of bounds"); + throw std::runtime_error("Circuit::get_gate_at(std::uint64_t): index out of bounds"); } return _gate_list[idx]; } - [[nodiscard]] inline std::optional get_key(std::uint64_t idx) { + [[nodiscard]] inline std::optional get_param_key_at(std::uint64_t idx) { if (idx >= _gate_list.size()) { throw std::runtime_error( "Circuit::get_parameter_key(std::uint64_t): index out of bounds"); diff --git a/scaluq/gate/gate.hpp b/scaluq/gate/gate.hpp index 884189e9..527402df 100644 --- a/scaluq/gate/gate.hpp +++ b/scaluq/gate/gate.hpp @@ -148,18 +148,18 @@ class GateBase : public std::enable_shared_from_this { } virtual ~GateBase() = default; - [[nodiscard]] virtual std::vector get_target_qubit_list() const { + [[nodiscard]] virtual std::vector target_qubit_list() const { return mask_to_vector(_target_mask); } - [[nodiscard]] virtual std::vector get_control_qubit_list() const { + [[nodiscard]] virtual std::vector control_qubit_list() const { return mask_to_vector(_control_mask); } - [[nodiscard]] virtual std::vector get_operand_qubit_list() const { + [[nodiscard]] virtual std::vector operand_qubit_list() const { return mask_to_vector(_target_mask | _control_mask); } - [[nodiscard]] virtual std::uint64_t get_target_qubit_mask() const { return _target_mask; } - [[nodiscard]] virtual std::uint64_t get_control_qubit_mask() const { return _control_mask; } - [[nodiscard]] virtual std::uint64_t get_operand_qubit_mask() const { + [[nodiscard]] virtual std::uint64_t target_qubit_mask() const { return _target_mask; } + [[nodiscard]] virtual std::uint64_t control_qubit_mask() const { return _control_mask; } + [[nodiscard]] virtual std::uint64_t operand_qubit_mask() const { return _target_mask | _control_mask; } diff --git a/scaluq/gate/gate_factory.hpp b/scaluq/gate/gate_factory.hpp index 1c78610f..6ed1580c 100644 --- a/scaluq/gate/gate_factory.hpp +++ b/scaluq/gate/gate_factory.hpp @@ -152,7 +152,7 @@ inline Gate TwoTargetMatrix(std::uint64_t target1, } // まだ inline Gate Pauli(const PauliOperator& pauli, const std::vector& controls = {}) { - auto tar = pauli.get_target_qubit_list(); + auto tar = pauli.target_qubit_list(); return internal::GateFactory::create_gate( internal::vector_to_mask(controls), pauli); } diff --git a/scaluq/gate/gate_pauli.hpp b/scaluq/gate/gate_pauli.hpp index ee864ae7..0671fbb1 100644 --- a/scaluq/gate/gate_pauli.hpp +++ b/scaluq/gate/gate_pauli.hpp @@ -13,11 +13,10 @@ class PauliGateImpl : public GateBase { public: PauliGateImpl(std::uint64_t control_mask, const PauliOperator& pauli) - : GateBase(vector_to_mask(pauli.get_target_qubit_list()), control_mask), - _pauli(pauli) {} + : GateBase(vector_to_mask(pauli.target_qubit_list()), control_mask), _pauli(pauli) {} PauliOperator pauli() const { return _pauli; }; - std::vector get_pauli_id_list() const { return _pauli.get_pauli_id_list(); } + std::vector pauli_id_list() const { return _pauli.pauli_id_list(); } Gate get_inverse() const override { return shared_from_this(); } ComplexMatrix get_matrix() const override { return this->_pauli.get_matrix(); } @@ -33,12 +32,12 @@ class PauliRotationGateImpl : public GateBase { public: PauliRotationGateImpl(std::uint64_t control_mask, const PauliOperator& pauli, double angle) - : GateBase(vector_to_mask(pauli.get_target_qubit_list()), control_mask), + : GateBase(vector_to_mask(pauli.target_qubit_list()), control_mask), _pauli(pauli), _angle(angle) {} PauliOperator pauli() const { return _pauli; } - std::vector get_pauli_id_list() const { return _pauli.get_pauli_id_list(); } + std::vector pauli_id_list() const { return _pauli.pauli_id_list(); } double angle() const { return _angle; } Gate get_inverse() const override { @@ -47,7 +46,7 @@ class PauliRotationGateImpl : public GateBase { ComplexMatrix get_matrix() const override { ComplexMatrix mat = this->_pauli.get_matrix_ignoring_coef(); - Complex true_angle = _angle * _pauli.get_coef(); + Complex true_angle = _angle * _pauli.coef(); StdComplex imag_unit(0, 1); mat = (StdComplex)Kokkos::cos(-true_angle / 2) * ComplexMatrix::Identity(mat.rows(), mat.cols()) + diff --git a/scaluq/gate/gate_probablistic.hpp b/scaluq/gate/gate_probablistic.hpp index 9be3946c..26a33478 100644 --- a/scaluq/gate/gate_probablistic.hpp +++ b/scaluq/gate/gate_probablistic.hpp @@ -31,34 +31,34 @@ class ProbablisticGateImpl : public GateBase { const std::vector& gate_list() const { return _gate_list; } const std::vector& distribution() const { return _distribution; } - std::vector get_target_qubit_list() const override { + std::vector target_qubit_list() const override { throw std::runtime_error( - "ProbablisticGateImpl::get_target_qubit_list(): This function must not be used in " + "ProbablisticGateImpl::target_qubit_list(): This function must not be used in " "ProbablisticGateImpl."); } - std::vector get_control_qubit_list() const override { + std::vector control_qubit_list() const override { throw std::runtime_error( - "ProbablisticGateImpl::get_control_qubit_list(): This function must not be used in " + "ProbablisticGateImpl::control_qubit_list(): This function must not be used in " "ProbablisticGateImpl."); } - std::vector get_operand_qubit_list() const override { + std::vector operand_qubit_list() const override { throw std::runtime_error( - "ProbablisticGateImpl::get_operand_qubit_list(): This function must not be used in " + "ProbablisticGateImpl::operand_qubit_list(): This function must not be used in " "ProbablisticGateImpl."); } - std::uint64_t get_target_qubit_mask() const override { + std::uint64_t target_qubit_mask() const override { throw std::runtime_error( - "ProbablisticGateImpl::get_target_qubit_mask(): This function must not be used in " + "ProbablisticGateImpl::target_qubit_mask(): This function must not be used in " "ProbablisticGateImpl."); } - std::uint64_t get_control_qubit_mask() const override { + std::uint64_t control_qubit_mask() const override { throw std::runtime_error( - "ProbablisticGateImpl::get_control_qubit_mask(): This function must not be used in " + "ProbablisticGateImpl::control_qubit_mask(): This function must not be used in " "ProbablisticGateImpl."); } - std::uint64_t get_operand_qubit_mask() const override { + std::uint64_t operand_qubit_mask() const override { throw std::runtime_error( - "ProbablisticGateImpl::get_operand_qubit_mask(): This function must not be used in " + "ProbablisticGateImpl::operand_qubit_mask(): This function must not be used in " "ProbablisticGateImpl."); } diff --git a/scaluq/gate/merge_gate.cpp b/scaluq/gate/merge_gate.cpp index 844984fc..2dc5bff1 100644 --- a/scaluq/gate/merge_gate.cpp +++ b/scaluq/gate/merge_gate.cpp @@ -32,8 +32,8 @@ std::pair merge_gate(const Gate& gate1, const Gate& gate2) { assert(!pauli_id1 || pauli_id1 != 0); assert(!pauli_id2 || pauli_id2 != 0); if (pauli_id1 && pauli_id2) { - std::uint64_t target1 = gate1->get_target_qubit_list()[0]; - std::uint64_t target2 = gate2->get_target_qubit_list()[0]; + std::uint64_t target1 = gate1->target_qubit_list()[0]; + std::uint64_t target2 = gate2->target_qubit_list()[0]; if (target1 == target2) { if (pauli_id1 == pauli_id2) return {gate::I(), 0.}; if (pauli_id1 == 1) { @@ -54,11 +54,11 @@ std::pair merge_gate(const Gate& gate1, const Gate& gate2) { (pauli_id2 || gate2.gate_type() == GateType::Pauli)) { auto pauli1 = gate_type1 == GateType::Pauli ? PauliGate(gate1)->pauli() - : PauliOperator(std::vector{gate1->get_target_qubit_list()[0]}, + : PauliOperator(std::vector{gate1->target_qubit_list()[0]}, std::vector{pauli_id1.value()}); auto pauli2 = gate_type2 == GateType::Pauli ? PauliGate(gate2)->pauli() - : PauliOperator(std::vector{gate2->get_target_qubit_list()[0]}, + : PauliOperator(std::vector{gate2->target_qubit_list()[0]}, std::vector{pauli_id2.value()}); return {gate::Pauli(pauli2 * pauli1), 0.}; } @@ -87,8 +87,8 @@ std::pair merge_gate(const Gate& gate1, const Gate& gate2) { auto oct_phase1 = get_oct_phase(gate_type1); auto oct_phase2 = get_oct_phase(gate_type2); if (oct_phase1 && oct_phase2) { - std::uint64_t target1 = gate1->get_target_qubit_list()[0]; - std::uint64_t target2 = gate2->get_target_qubit_list()[0]; + std::uint64_t target1 = gate1->target_qubit_list()[0]; + std::uint64_t target2 = gate2->target_qubit_list()[0]; if (target1 == target2) { auto g = oct_phase_gate(oct_phase1.value() + oct_phase2.value(), target1); if (g) return {g.value(), 0.}; @@ -96,8 +96,8 @@ std::pair merge_gate(const Gate& gate1, const Gate& gate2) { } if ((oct_phase1 || gate_type1 == GateType::RZ || gate_type1 == GateType::U1) && (oct_phase2 || gate_type2 == GateType::RZ || gate_type2 == GateType::U1)) { - std::uint64_t target1 = gate1->get_target_qubit_list()[0]; - std::uint64_t target2 = gate2->get_target_qubit_list()[0]; + std::uint64_t target1 = gate1->target_qubit_list()[0]; + std::uint64_t target2 = gate2->target_qubit_list()[0]; if (target1 == target2) { double phase1 = oct_phase1 ? oct_phase1.value() * PI() / 4 : gate_type1 == GateType::RZ ? RZGate(gate1)->angle() @@ -123,8 +123,8 @@ std::pair merge_gate(const Gate& gate1, const Gate& gate2) { auto rx_param1 = get_rx_angle(gate1, gate_type1); auto rx_param2 = get_rx_angle(gate2, gate_type2); if (rx_param1 && rx_param2) { - std::uint64_t target1 = gate1->get_target_qubit_list()[0]; - std::uint64_t target2 = gate2->get_target_qubit_list()[0]; + std::uint64_t target1 = gate1->target_qubit_list()[0]; + std::uint64_t target2 = gate2->target_qubit_list()[0]; double global_phase1 = gate_type1 == GateType::RX ? 0. : rx_param1.value() / 2; double global_phase2 = gate_type2 == GateType::RX ? 0. : rx_param2.value() / 2; if (target1 == target2) { @@ -145,8 +145,8 @@ std::pair merge_gate(const Gate& gate1, const Gate& gate2) { auto ry_param1 = get_ry_angle(gate1, gate_type1); auto ry_param2 = get_ry_angle(gate2, gate_type2); if (ry_param1 && ry_param2) { - std::uint64_t target1 = gate1->get_target_qubit_list()[0]; - std::uint64_t target2 = gate2->get_target_qubit_list()[0]; + std::uint64_t target1 = gate1->target_qubit_list()[0]; + std::uint64_t target2 = gate2->target_qubit_list()[0]; double global_phase1 = gate_type1 == GateType::RY ? 0. : ry_param1.value() / 2; double global_phase2 = gate_type2 == GateType::RY ? 0. : ry_param2.value() / 2; if (target1 == target2) { @@ -188,10 +188,10 @@ std::pair merge_gate(const Gate& gate1, const Gate& gate2) { } // General case - auto gate1_targets = gate1->get_target_qubit_list(); - std::ranges::copy(gate1->get_control_qubit_list(), std::back_inserter(gate1_targets)); - auto gate2_targets = gate2->get_target_qubit_list(); - std::ranges::copy(gate2->get_control_qubit_list(), std::back_inserter(gate2_targets)); + auto gate1_targets = gate1->target_qubit_list(); + std::ranges::copy(gate1->control_qubit_list(), std::back_inserter(gate1_targets)); + auto gate2_targets = gate2->target_qubit_list(); + std::ranges::copy(gate2->control_qubit_list(), std::back_inserter(gate2_targets)); std::vector merged_targets(gate1_targets.size() + gate2_targets.size()); std::ranges::copy(gate1_targets, merged_targets.begin()); std::ranges::copy(gate2_targets, merged_targets.begin() + gate1_targets.size()); diff --git a/scaluq/gate/param_gate.hpp b/scaluq/gate/param_gate.hpp index e0ff1cb8..edc159dd 100644 --- a/scaluq/gate/param_gate.hpp +++ b/scaluq/gate/param_gate.hpp @@ -12,26 +12,26 @@ class ParamGateBase; template concept ParamGateImpl = std::derived_from; -class PRXGateImpl; -class PRYGateImpl; -class PRZGateImpl; -class PPauliRotationGateImpl; +class ParamRXGateImpl; +class ParamRYGateImpl; +class ParamRZGateImpl; +class ParamPauliRotationGateImpl; template class ParamGatePtr; } // namespace internal using ParamGate = internal::ParamGatePtr; -enum class ParamGateType { Unknown, PRX, PRY, PRZ, PPauliRotation }; +enum class ParamGateType { Unknown, ParamRX, ParamRY, ParamRZ, ParamPauliRotation }; template constexpr ParamGateType get_param_gate_type() { if constexpr (std::is_same_v) return ParamGateType::Unknown; - if constexpr (std::is_same_v) return ParamGateType::PRX; - if constexpr (std::is_same_v) return ParamGateType::PRY; - if constexpr (std::is_same_v) return ParamGateType::PRZ; - if constexpr (std::is_same_v) - return ParamGateType::PPauliRotation; + if constexpr (std::is_same_v) return ParamGateType::ParamRX; + if constexpr (std::is_same_v) return ParamGateType::ParamRY; + if constexpr (std::is_same_v) return ParamGateType::ParamRZ; + if constexpr (std::is_same_v) + return ParamGateType::ParamPauliRotation; static_assert("unknown ParamGateImpl"); return ParamGateType::Unknown; } @@ -51,8 +51,8 @@ class ParamGateBase { } public: - ParamGateBase(std::uint64_t target_mask, std::uint64_t control_mask, double pcoef = 1.) - : _target_mask(target_mask), _control_mask(control_mask), _pcoef(pcoef) { + ParamGateBase(std::uint64_t target_mask, std::uint64_t control_mask, double param_coef = 1.) + : _target_mask(target_mask), _control_mask(control_mask), _pcoef(param_coef) { if (_target_mask & _control_mask) [[unlikely]] { throw std::runtime_error( "Error: ParamGate::ParamGate(std::uint64_t target_mask, std::uint64_t " @@ -62,20 +62,20 @@ class ParamGateBase { } virtual ~ParamGateBase() = default; - [[nodiscard]] double pcoef() { return _pcoef; } + [[nodiscard]] double param_coef() { return _pcoef; } - [[nodiscard]] virtual std::vector get_target_qubit_list() const { + [[nodiscard]] virtual std::vector target_qubit_list() const { return mask_to_vector(_target_mask); } - [[nodiscard]] virtual std::vector get_control_qubit_list() const { + [[nodiscard]] virtual std::vector control_qubit_list() const { return mask_to_vector(_control_mask); } - [[nodiscard]] virtual std::vector get_operand_qubit_list() const { + [[nodiscard]] virtual std::vector operand_qubit_list() const { return mask_to_vector(_target_mask | _control_mask); } - [[nodiscard]] virtual std::uint64_t get_target_qubit_mask() const { return _target_mask; } - [[nodiscard]] virtual std::uint64_t get_control_qubit_mask() const { return _control_mask; } - [[nodiscard]] virtual std::uint64_t get_operand_qubit_mask() const { + [[nodiscard]] virtual std::uint64_t target_qubit_mask() const { return _target_mask; } + [[nodiscard]] virtual std::uint64_t control_qubit_mask() const { return _control_mask; } + [[nodiscard]] virtual std::uint64_t operand_qubit_mask() const { return _target_mask | _control_mask; } diff --git a/scaluq/gate/param_gate_factory.hpp b/scaluq/gate/param_gate_factory.hpp index cfbd3e2f..c76e0a9d 100644 --- a/scaluq/gate/param_gate_factory.hpp +++ b/scaluq/gate/param_gate_factory.hpp @@ -15,35 +15,35 @@ class ParamGateFactory { }; } // namespace internal namespace gate { -inline ParamGate PRX(std::uint64_t target, - double pcoef = 1., - const std::vector& controls = {}) { - return internal::ParamGateFactory::create_gate( - internal::vector_to_mask({target}), internal::vector_to_mask(controls), pcoef); +inline ParamGate ParamRX(std::uint64_t target, + double param_coef = 1., + const std::vector& controls = {}) { + return internal::ParamGateFactory::create_gate( + internal::vector_to_mask({target}), internal::vector_to_mask(controls), param_coef); } -inline ParamGate PRY(std::uint64_t target, - double pcoef = 1., - const std::vector& controls = {}) { - return internal::ParamGateFactory::create_gate( - internal::vector_to_mask({target}), internal::vector_to_mask(controls), pcoef); +inline ParamGate ParamRY(std::uint64_t target, + double param_coef = 1., + const std::vector& controls = {}) { + return internal::ParamGateFactory::create_gate( + internal::vector_to_mask({target}), internal::vector_to_mask(controls), param_coef); } -inline ParamGate PRZ(std::uint64_t target, - double pcoef = 1., - const std::vector& controls = {}) { - return internal::ParamGateFactory::create_gate( - internal::vector_to_mask({target}), internal::vector_to_mask(controls), pcoef); +inline ParamGate ParamRZ(std::uint64_t target, + double param_coef = 1., + const std::vector& controls = {}) { + return internal::ParamGateFactory::create_gate( + internal::vector_to_mask({target}), internal::vector_to_mask(controls), param_coef); } // まだ -inline ParamGate PPauliRotation(const PauliOperator& pauli, - double pcoef = 1., - const std::vector& controls = {}) { - return internal::ParamGateFactory::create_gate( - internal::vector_to_mask(controls), pauli, pcoef); +inline ParamGate ParamPauliRotation(const PauliOperator& pauli, + double param_coef = 1., + const std::vector& controls = {}) { + return internal::ParamGateFactory::create_gate( + internal::vector_to_mask(controls), pauli, param_coef); } -inline ParamGate PProbablistic(const std::vector& distribution, - const std::vector>& gate_list) { - return internal::ParamGateFactory::create_gate(distribution, - gate_list); +inline ParamGate ParamProbablistic(const std::vector& distribution, + const std::vector>& gate_list) { + return internal::ParamGateFactory::create_gate( + distribution, gate_list); } } // namespace gate } // namespace scaluq diff --git a/scaluq/gate/param_gate_pauli.hpp b/scaluq/gate/param_gate_pauli.hpp index 0ba0a736..f9f1d959 100644 --- a/scaluq/gate/param_gate_pauli.hpp +++ b/scaluq/gate/param_gate_pauli.hpp @@ -8,25 +8,25 @@ namespace scaluq { namespace internal { -class PPauliRotationGateImpl : public ParamGateBase { +class ParamPauliRotationGateImpl : public ParamGateBase { const PauliOperator _pauli; public: - PPauliRotationGateImpl(std::uint64_t control_mask, - const PauliOperator& pauli, - double pcoef = 1.) - : ParamGateBase(vector_to_mask(pauli.get_target_qubit_list()), control_mask, pcoef), + ParamPauliRotationGateImpl(std::uint64_t control_mask, + const PauliOperator& pauli, + double param_coef = 1.) + : ParamGateBase(vector_to_mask(pauli.target_qubit_list()), control_mask, param_coef), _pauli(pauli) {} PauliOperator pauli() const { return _pauli; } - std::vector get_pauli_id_list() const { return _pauli.get_pauli_id_list(); } + std::vector pauli_id_list() const { return _pauli.pauli_id_list(); } ParamGate get_inverse() const override { - return std::make_shared(_control_mask, _pauli, -_pcoef); + return std::make_shared(_control_mask, _pauli, -_pcoef); } ComplexMatrix get_matrix(double param) const override { double angle = _pcoef * param; - Complex true_angle = angle * this->_pauli.get_coef(); + Complex true_angle = angle * this->_pauli.coef(); ComplexMatrix mat = this->_pauli.get_matrix_ignoring_coef(); StdComplex imag_unit(0, 1); mat = (StdComplex)Kokkos::cos(-true_angle / 2) * @@ -40,5 +40,5 @@ class PPauliRotationGateImpl : public ParamGateBase { }; } // namespace internal -using PPauliRotationGate = internal::ParamGatePtr; +using ParamPauliRotationGate = internal::ParamGatePtr; } // namespace scaluq diff --git a/scaluq/gate/param_gate_probablistic.hpp b/scaluq/gate/param_gate_probablistic.hpp index 063a1ea9..a6622299 100644 --- a/scaluq/gate/param_gate_probablistic.hpp +++ b/scaluq/gate/param_gate_probablistic.hpp @@ -8,15 +8,15 @@ namespace scaluq { namespace internal { -class PProbablisticGateImpl : public ParamGateBase { +class ParamProbablisticGateImpl : public ParamGateBase { using EitherGate = std::variant; std::vector _distribution; std::vector _cumlative_distribution; std::vector _gate_list; public: - PProbablisticGateImpl(const std::vector& distribution, - const std::vector>& gate_list) + ParamProbablisticGateImpl(const std::vector& distribution, + const std::vector>& gate_list) : ParamGateBase(0, 0), _distribution(distribution), _gate_list(gate_list) { std::uint64_t n = distribution.size(); if (n == 0) { @@ -35,35 +35,35 @@ class PProbablisticGateImpl : public ParamGateBase { const std::vector>& gate_list() const { return _gate_list; } const std::vector& distribution() const { return _distribution; } - std::vector get_target_qubit_list() const override { + std::vector target_qubit_list() const override { throw std::runtime_error( - "PProbablisticGateImpl::get_target_qubit_list(): This function must not be used in " - "PProbablisticGateImpl."); + "ParamProbablisticGateImpl::target_qubit_list(): This function must not be used in " + "ParamProbablisticGateImpl."); } - std::vector get_control_qubit_list() const override { + std::vector control_qubit_list() const override { throw std::runtime_error( - "PProbablisticGateImpl::get_control_qubit_list(): This function must not be used in " - "PProbablisticGateImpl."); + "ParamProbablisticGateImpl::control_qubit_list(): This function must not be used in " + "ParamProbablisticGateImpl."); } - std::vector get_operand_qubit_list() const override { + std::vector operand_qubit_list() const override { throw std::runtime_error( - "PProbablisticGateImpl::get_operand_qubit_list(): This function must not be used in " - "PProbablisticGateImpl."); + "ParamProbablisticGateImpl::operand_qubit_list(): This function must not be used in " + "ParamProbablisticGateImpl."); } - std::uint64_t get_target_qubit_mask() const override { + std::uint64_t target_qubit_mask() const override { throw std::runtime_error( - "PProbablisticGateImpl::get_target_qubit_mask(): This function must not be used in " - "PProbablisticGateImpl."); + "ParamProbablisticGateImpl::target_qubit_mask(): This function must not be used in " + "ParamProbablisticGateImpl."); } - std::uint64_t get_control_qubit_mask() const override { + std::uint64_t control_qubit_mask() const override { throw std::runtime_error( - "PProbablisticGateImpl::get_control_qubit_mask(): This function must not be used in " - "PProbablisticGateImpl."); + "ParamProbablisticGateImpl::control_qubit_mask(): This function must not be used in " + "ParamProbablisticGateImpl."); } - std::uint64_t get_operand_qubit_mask() const override { + std::uint64_t operand_qubit_mask() const override { throw std::runtime_error( - "PProbablisticGateImpl::get_operand_qubit_mask(): This function must not be used in " - "PProbablisticGateImpl."); + "ParamProbablisticGateImpl::operand_qubit_mask(): This function must not be used in " + "ParamProbablisticGateImpl."); } ParamGate get_inverse() const override { @@ -73,12 +73,12 @@ class PProbablisticGateImpl : public ParamGateBase { _gate_list, std::back_inserter(inv_gate_list), [](const EitherGate& gate) { return std::visit([](const auto& g) { return EitherGate{g->get_inverse()}; }, gate); }); - return std::make_shared(_distribution, inv_gate_list); + return std::make_shared(_distribution, inv_gate_list); } ComplexMatrix get_matrix(double) const override { throw std::runtime_error( - "PProbablisticGateImpl::get_matrix(): This function must not be used in " - "PProbablisticGateImpl."); + "ParamProbablisticGateImpl::get_matrix(): This function must not be used in " + "ParamProbablisticGateImpl."); } void update_quantum_state(StateVector& state_vector, double param) const override { @@ -98,5 +98,5 @@ class PProbablisticGateImpl : public ParamGateBase { }; } // namespace internal -using PProbablisticGate = internal::ParamGatePtr; +using ParamProbablisticGate = internal::ParamGatePtr; } // namespace scaluq diff --git a/scaluq/gate/param_gate_standard.hpp b/scaluq/gate/param_gate_standard.hpp index cf12afae..23da71b4 100644 --- a/scaluq/gate/param_gate_standard.hpp +++ b/scaluq/gate/param_gate_standard.hpp @@ -8,12 +8,12 @@ namespace scaluq { namespace internal { -class PRXGateImpl : public ParamGateBase { +class ParamRXGateImpl : public ParamGateBase { public: using ParamGateBase::ParamGateBase; ParamGate get_inverse() const override { - return std::make_shared(_target_mask, _control_mask, -_pcoef); + return std::make_shared(_target_mask, _control_mask, -_pcoef); } ComplexMatrix get_matrix(double param) const override { double angle = _pcoef * param; @@ -29,12 +29,12 @@ class PRXGateImpl : public ParamGateBase { } }; -class PRYGateImpl : public ParamGateBase { +class ParamRYGateImpl : public ParamGateBase { public: using ParamGateBase::ParamGateBase; ParamGate get_inverse() const override { - return std::make_shared(_target_mask, _control_mask, -_pcoef); + return std::make_shared(_target_mask, _control_mask, -_pcoef); } ComplexMatrix get_matrix(double param) const override { double angle = _pcoef * param; @@ -49,12 +49,12 @@ class PRYGateImpl : public ParamGateBase { } }; -class PRZGateImpl : public ParamGateBase { +class ParamRZGateImpl : public ParamGateBase { public: using ParamGateBase::ParamGateBase; ParamGate get_inverse() const override { - return std::make_shared(_target_mask, _control_mask, -_pcoef); + return std::make_shared(_target_mask, _control_mask, -_pcoef); } ComplexMatrix get_matrix(double param) const override { double angle = param * _pcoef; @@ -71,8 +71,8 @@ class PRZGateImpl : public ParamGateBase { } // namespace internal -using PRXGate = internal::ParamGatePtr; -using PRYGate = internal::ParamGatePtr; -using PRZGate = internal::ParamGatePtr; +using ParamRXGate = internal::ParamGatePtr; +using ParamRYGate = internal::ParamGatePtr; +using ParamRZGate = internal::ParamGatePtr; } // namespace scaluq diff --git a/scaluq/gate/update_ops_pauli.cpp b/scaluq/gate/update_ops_pauli.cpp index 58695a21..faa06422 100644 --- a/scaluq/gate/update_ops_pauli.cpp +++ b/scaluq/gate/update_ops_pauli.cpp @@ -24,7 +24,7 @@ void pauli_rotation_gate(std::uint64_t control_mask, std::uint64_t bit_flip_mask = internal::BitVector(bit_flip_mask_vector).data_raw()[0]; std::uint64_t phase_flip_mask = internal::BitVector(phase_flip_mask_vector).data_raw()[0]; std::uint64_t global_phase_90_rot_count = std::popcount(bit_flip_mask & phase_flip_mask); - Complex true_angle = angle * pauli.get_coef(); + Complex true_angle = angle * pauli.coef(); const Complex cosval = Kokkos::cos(-true_angle / 2); const Complex sinval = Kokkos::sin(-true_angle / 2); if (bit_flip_mask == 0) { diff --git a/scaluq/operator/operator.cpp b/scaluq/operator/operator.cpp index ee17d8d2..26184e7b 100644 --- a/scaluq/operator/operator.cpp +++ b/scaluq/operator/operator.cpp @@ -11,7 +11,7 @@ Operator::Operator(std::uint64_t n_qubits) : _n_qubits(n_qubits) {} std::string Operator::to_string() const { std::stringstream ss; for (auto itr = _terms.begin(); itr != _terms.end(); ++itr) { - ss << itr->get_coef() << " " << itr->get_pauli_string(); + ss << itr->coef() << " " << itr->get_pauli_string(); if (itr != prev(_terms.end())) { ss << " + "; } @@ -21,9 +21,9 @@ std::string Operator::to_string() const { void Operator::add_operator(const PauliOperator& mpt) { add_operator(PauliOperator{mpt}); } void Operator::add_operator(PauliOperator&& mpt) { - _is_hermitian &= mpt.get_coef().imag() == 0.; + _is_hermitian &= mpt.coef().imag() == 0.; if (![&] { - const auto& target_list = mpt.get_target_qubit_list(); + const auto& target_list = mpt.target_qubit_list(); if (target_list.empty()) return true; return *std::max_element(target_list.begin(), target_list.end()) < _n_qubits; }()) { @@ -49,7 +49,7 @@ void Operator::add_random_operator(std::uint64_t operator_count, std::uint64_t s void Operator::optimize() { std::map, Complex> pauli_and_coef; for (const auto& pauli : _terms) { - pauli_and_coef[pauli.get_XZ_mask_representation()] += pauli.get_coef(); + pauli_and_coef[pauli.get_XZ_mask_representation()] += pauli.coef(); } _terms.clear(); for (const auto& [mask, coef] : pauli_and_coef) { diff --git a/scaluq/operator/pauli_operator.cpp b/scaluq/operator/pauli_operator.cpp index 65815487..575d60ea 100644 --- a/scaluq/operator/pauli_operator.cpp +++ b/scaluq/operator/pauli_operator.cpp @@ -116,7 +116,7 @@ void PauliOperator::apply_to_state(StateVector& state_vector) const { } std::uint64_t bit_flip_mask = _ptr->_bit_flip_mask.data_raw()[0]; std::uint64_t phase_flip_mask = _ptr->_phase_flip_mask.data_raw()[0]; - Complex coef = get_coef(); + Complex coef = this->coef(); if (bit_flip_mask == 0) { Kokkos::parallel_for( state_vector.dim(), KOKKOS_LAMBDA(std::uint64_t state_idx) { diff --git a/scaluq/operator/pauli_operator.hpp b/scaluq/operator/pauli_operator.hpp index c5d04891..cbeed74f 100644 --- a/scaluq/operator/pauli_operator.hpp +++ b/scaluq/operator/pauli_operator.hpp @@ -30,12 +30,10 @@ class PauliOperator { const std::vector& phase_flip_mask, Complex coef); void add_single_pauli(std::uint64_t target_qubit, std::uint64_t pauli_id); - Complex get_coef() const { return _coef; } + Complex coef() const { return _coef; } void set_coef(Complex c) { _coef = c; } - const std::vector& get_target_qubit_list() const { - return _target_qubit_list; - } - const std::vector& get_pauli_id_list() const { return _pauli_id_list; } + const std::vector& target_qubit_list() const { return _target_qubit_list; } + const std::vector& pauli_id_list() const { return _pauli_id_list; } std::tuple, std::vector> get_XZ_mask_representation() const { return {_bit_flip_mask, _phase_flip_mask}; } @@ -62,12 +60,12 @@ class PauliOperator { Complex coef) : _ptr(std::make_shared(bit_flip_mask, phase_flip_mask, coef)) {} - [[nodiscard]] inline Complex get_coef() const { return _ptr->get_coef(); } - [[nodiscard]] inline const std::vector& get_target_qubit_list() const { - return _ptr->get_target_qubit_list(); + [[nodiscard]] inline Complex coef() const { return _ptr->coef(); } + [[nodiscard]] inline const std::vector& target_qubit_list() const { + return _ptr->target_qubit_list(); } - [[nodiscard]] inline const std::vector& get_pauli_id_list() const { - return _ptr->get_pauli_id_list(); + [[nodiscard]] inline const std::vector& pauli_id_list() const { + return _ptr->pauli_id_list(); } [[nodiscard]] inline std::tuple, std::vector> get_XZ_mask_representation() const { diff --git a/scaluq/state/state_vector.cpp b/scaluq/state/state_vector.cpp index d82e9c83..440c2419 100644 --- a/scaluq/state/state_vector.cpp +++ b/scaluq/state/state_vector.cpp @@ -12,13 +12,13 @@ StateVector::StateVector(std::uint64_t n_qubits) set_zero_state(); } -void StateVector::set_amplitude_at_index(std::uint64_t index, const Complex& c) { +void StateVector::set_amplitude_at(std::uint64_t index, const Complex& c) { Kokkos::View host_view("single_value"); host_view() = c; Kokkos::deep_copy(Kokkos::subview(_raw, index), host_view()); } -Complex StateVector::get_amplitude_at_index(std::uint64_t index) const { +Complex StateVector::get_amplitude_at(std::uint64_t index) const { Kokkos::View host_view("single_value"); Kokkos::deep_copy(host_view, Kokkos::subview(_raw, index)); return host_view(); @@ -26,7 +26,7 @@ Complex StateVector::get_amplitude_at_index(std::uint64_t index) const { void StateVector::set_zero_state() { Kokkos::deep_copy(_raw, 0); - set_amplitude_at_index(0, 1); + set_amplitude_at(0, 1); } void StateVector::set_zero_norm_state() { Kokkos::deep_copy(_raw, 0); } @@ -39,7 +39,7 @@ void StateVector::set_computational_basis(std::uint64_t basis) { "computational basis must be smaller than 2^qubit_count"); } Kokkos::deep_copy(_raw, 0); - set_amplitude_at_index(basis, 1); + set_amplitude_at(basis, 1); } StateVector StateVector::Haar_random_state(std::uint64_t n_qubits, std::uint64_t seed) { @@ -60,7 +60,7 @@ std::uint64_t StateVector::n_qubits() const { return this->_n_qubits; } std::uint64_t StateVector::dim() const { return this->_dim; } -std::vector StateVector::amplitudes() const { +std::vector StateVector::get_amplitudes() const { return internal::convert_device_view_to_host_vector(_raw); } @@ -219,7 +219,7 @@ std::vector StateVector::sampling(std::uint64_t sampling_count, std::string StateVector::to_string() const { std::stringstream os; - auto amp = this->amplitudes(); + auto amp = this->get_amplitudes(); os << " *** Quantum State ***\n"; os << " * Qubit Count : " << _n_qubits << '\n'; os << " * Dimension : " << _dim << '\n'; diff --git a/scaluq/state/state_vector.hpp b/scaluq/state/state_vector.hpp index 3ef21324..e294ee68 100644 --- a/scaluq/state/state_vector.hpp +++ b/scaluq/state/state_vector.hpp @@ -25,12 +25,12 @@ class StateVector { /** * @attention Very slow. You should use load() instead if you can. */ - void set_amplitude_at_index(std::uint64_t index, const Complex& c); + void set_amplitude_at(std::uint64_t index, const Complex& c); /** - * @attention Very slow. You should use amplitudes() instead if you can. + * @attention Very slow. You should use get_amplitudes() instead if you can. */ - [[nodiscard]] Complex get_amplitude_at_index(std::uint64_t index) const; + [[nodiscard]] Complex get_amplitude_at(std::uint64_t index) const; [[nodiscard]] static StateVector Haar_random_state(std::uint64_t n_qubits, std::uint64_t seed = std::random_device()()); @@ -46,7 +46,7 @@ class StateVector { [[nodiscard]] std::uint64_t dim() const; - [[nodiscard]] std::vector amplitudes() const; + [[nodiscard]] std::vector get_amplitudes() const; [[nodiscard]] double get_squared_norm() const; diff --git a/scaluq/state/state_vector_batched.cpp b/scaluq/state/state_vector_batched.cpp index 9d029f01..f56fc861 100644 --- a/scaluq/state/state_vector_batched.cpp +++ b/scaluq/state/state_vector_batched.cpp @@ -38,7 +38,7 @@ void StateVectorBatched::set_state_vector(std::uint64_t batch_id, const StateVec Kokkos::fence(); } -StateVector StateVectorBatched::get_state_vector(std::uint64_t batch_id) const { +StateVector StateVectorBatched::get_state_vector_at(std::uint64_t batch_id) const { StateVector ret(_n_qubits); Kokkos::parallel_for( _dim, KOKKOS_CLASS_LAMBDA(std::uint64_t i) { ret._raw(i) = _raw(batch_id, i); }); @@ -133,7 +133,7 @@ std::vector> StateVectorBatched::sampling(std::uint64 Kokkos::DefaultExecutionSpace::array_layout>(result); } -std::vector> StateVectorBatched::amplitudes() const { +std::vector> StateVectorBatched::get_amplitudes() const { return internal::convert_2d_device_view_to_host_vector(_raw); } diff --git a/scaluq/state/state_vector_batched.hpp b/scaluq/state/state_vector_batched.hpp index 6c4c5048..60127e3d 100644 --- a/scaluq/state/state_vector_batched.hpp +++ b/scaluq/state/state_vector_batched.hpp @@ -25,7 +25,7 @@ class StateVectorBatched { void set_state_vector(const StateVector& state); void set_state_vector(std::uint64_t batch_id, const StateVector& state); - [[nodiscard]] StateVector get_state_vector(std::uint64_t batch_id) const; + [[nodiscard]] StateVector get_state_vector_at(std::uint64_t batch_id) const; void set_zero_state(); void set_computational_basis(std::uint64_t basis); @@ -40,7 +40,7 @@ class StateVectorBatched { bool set_same_state, std::uint64_t seed = std::random_device()()); - [[nodiscard]] std::vector> amplitudes() const; + [[nodiscard]] std::vector> get_amplitudes() const; [[nodiscard]] std::vector get_squared_norm() const; diff --git a/tests/circuit/circuit_test.cpp b/tests/circuit/circuit_test.cpp index acbac053..932cb25c 100644 --- a/tests/circuit/circuit_test.cpp +++ b/tests/circuit/circuit_test.cpp @@ -22,7 +22,7 @@ TEST(CircuitTest, CircuitBasic) { StateVector state = StateVector::Haar_random_state(n); Eigen::VectorXcd state_eigen(dim); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (std::uint64_t i = 0; i < dim; ++i) state_eigen[i] = state_cp[i]; Circuit circuit(n); @@ -158,7 +158,7 @@ TEST(CircuitTest, CircuitBasic) { circuit.update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); for (std::uint64_t i = 0; i < dim; ++i) ASSERT_NEAR(std::abs(state_eigen[i] - (CComplex)state_cp[i]), 0, eps); } @@ -170,7 +170,7 @@ TEST(CircuitTest, CircuitRev) { Random random; StateVector state = StateVector::Haar_random_state(n); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); Eigen::VectorXcd state_eigen(dim); for (std::uint64_t i = 0; i < dim; ++i) state_eigen[i] = state_cp[i]; @@ -256,7 +256,7 @@ TEST(CircuitTest, CircuitRev) { auto revcircuit = circuit.get_inverse(); revcircuit.update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); for (std::uint64_t i = 0; i < dim; ++i) ASSERT_NEAR(abs(state_eigen[i] - (CComplex)state_cp[i]), 0, eps); } diff --git a/tests/circuit/param_circuit_test.cpp b/tests/circuit/param_circuit_test.cpp index 1fd7f553..b59e13c4 100644 --- a/tests/circuit/param_circuit_test.cpp +++ b/tests/circuit/param_circuit_test.cpp @@ -37,15 +37,15 @@ TEST(ParamCircuitTest, ApplyParamCircuit) { if (param_gate_kind == 0) { std::uint64_t target = random.int32() % n_qubits; circuit.add_gate(gate::RX(target, coef * params[pidx])); - pcircuit.add_param_gate(gate::PRX(target, coef), pkeys[pidx]); + pcircuit.add_param_gate(gate::ParamRX(target, coef), pkeys[pidx]); } else if (param_gate_kind == 1) { std::uint64_t target = random.int32() % n_qubits; circuit.add_gate(gate::RY(target, coef * params[pidx])); - pcircuit.add_param_gate(gate::PRY(target, coef), pkeys[pidx]); + pcircuit.add_param_gate(gate::ParamRY(target, coef), pkeys[pidx]); } else if (param_gate_kind == 2) { std::uint64_t target = random.int32() % n_qubits; circuit.add_gate(gate::RZ(target, coef * params[pidx])); - pcircuit.add_param_gate(gate::PRZ(target, coef), pkeys[pidx]); + pcircuit.add_param_gate(gate::ParamRZ(target, coef), pkeys[pidx]); } else { std::vector target_vec, pauli_id_vec; for (std::uint64_t target = 0; target < n_qubits; target++) { @@ -54,7 +54,7 @@ TEST(ParamCircuitTest, ApplyParamCircuit) { } PauliOperator pauli(target_vec, pauli_id_vec, 1.0); circuit.add_gate(gate::PauliRotation(pauli, coef * params[pidx])); - pcircuit.add_param_gate(gate::PPauliRotation(pauli, coef), pkeys[pidx]); + pcircuit.add_param_gate(gate::ParamPauliRotation(pauli, coef), pkeys[pidx]); } } else { std::uint64_t control = random.int32() % n_qubits; @@ -69,11 +69,11 @@ TEST(ParamCircuitTest, ApplyParamCircuit) { } StateVector state = StateVector::Haar_random_state(n_qubits); StateVector state_cp = state.copy(); - auto amplitudes_base = state.amplitudes(); + auto amplitudes_base = state.get_amplitudes(); circuit.update_quantum_state(state); pcircuit.update_quantum_state(state_cp, pmap); - auto amplitudes = state.amplitudes(); - auto amplitudes_cp = state_cp.amplitudes(); + auto amplitudes = state.get_amplitudes(); + auto amplitudes_cp = state_cp.get_amplitudes(); for (std::uint64_t idx : std::views::iota(std::uint64_t{0}, 1ULL << n_qubits)) { ASSERT_NEAR(Kokkos::abs(amplitudes[idx] - amplitudes_cp[idx]), 0, eps); } @@ -81,8 +81,8 @@ TEST(ParamCircuitTest, ApplyParamCircuit) { auto ipcircuit = pcircuit.get_inverse(); icircuit.update_quantum_state(state); ipcircuit.update_quantum_state(state_cp, pmap); - amplitudes = state.amplitudes(); - amplitudes_cp = state_cp.amplitudes(); + amplitudes = state.get_amplitudes(); + amplitudes_cp = state_cp.get_amplitudes(); for (std::uint64_t idx : std::views::iota(std::uint64_t{0}, 1ULL << n_qubits)) { ASSERT_NEAR(Kokkos::abs(amplitudes[idx] - amplitudes_base[idx]), 0, eps); ASSERT_NEAR(Kokkos::abs(amplitudes_cp[idx] - amplitudes_base[idx]), 0, eps); @@ -92,9 +92,9 @@ TEST(ParamCircuitTest, ApplyParamCircuit) { TEST(ParamCircuitTest, InsufficientParameterGiven) { Circuit circuit(1); - circuit.add_param_gate(gate::PRX(0), "0"); - circuit.add_param_gate(gate::PRX(0), "1"); - circuit.add_param_gate(gate::PRX(0), "0"); + circuit.add_param_gate(gate::ParamRX(0), "0"); + circuit.add_param_gate(gate::ParamRX(0), "1"); + circuit.add_param_gate(gate::ParamRX(0), "0"); StateVector state(1); ASSERT_NO_THROW(circuit.update_quantum_state(state, {{"0", 0}, {"1", 0}})); ASSERT_NO_THROW(circuit.update_quantum_state(state, {{"0", 0}, {"1", 0}, {"2", 0}})); diff --git a/tests/gate/gate_test.cpp b/tests/gate/gate_test.cpp index 289c4ee8..1a4459f9 100644 --- a/tests/gate/gate_test.cpp +++ b/tests/gate/gate_test.cpp @@ -23,14 +23,14 @@ void run_random_gate_apply(std::uint64_t n_qubits) { Eigen::VectorXcd test_state = Eigen::VectorXcd::Zero(dim); for (int repeat = 0; repeat < 10; repeat++) { auto state = StateVector::Haar_random_state(n_qubits); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (int i = 0; i < dim; i++) { test_state[i] = state_cp[i]; } const Gate gate = QuantumGateConstructor(); gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); test_state = test_state; @@ -48,7 +48,7 @@ void run_random_gate_apply(std::uint64_t n_qubits) { Eigen::VectorXcd test_state = Eigen::VectorXcd::Zero(dim); for (int repeat = 0; repeat < 10; repeat++) { auto state = StateVector::Haar_random_state(n_qubits); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (int i = 0; i < dim; i++) { test_state[i] = state_cp[i]; } @@ -56,7 +56,7 @@ void run_random_gate_apply(std::uint64_t n_qubits) { const double angle = M_PI * random.uniform(); const Gate gate = QuantumGateConstructor(angle, {}); gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); test_state = std::polar(1., angle) * test_state; @@ -76,7 +76,7 @@ void run_random_gate_apply(std::uint64_t n_qubits, Eigen::VectorXcd test_state = Eigen::VectorXcd::Zero(dim); for (int repeat = 0; repeat < 10; repeat++) { auto state = StateVector::Haar_random_state(n_qubits); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (int i = 0; i < dim; i++) { test_state[i] = state_cp[i]; } @@ -84,7 +84,7 @@ void run_random_gate_apply(std::uint64_t n_qubits, const std::uint64_t target = random.int64() % n_qubits; const Gate gate = QuantumGateConstructor(target, {}); gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); test_state = get_expanded_eigen_matrix_with_identity(target, matrix, n_qubits) * test_state; @@ -103,7 +103,7 @@ void run_random_gate_apply(std::uint64_t n_qubits, Eigen::VectorXcd test_state = Eigen::VectorXcd::Zero(dim); for (int repeat = 0; repeat < 10; repeat++) { auto state = StateVector::Haar_random_state(n_qubits); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (int i = 0; i < dim; i++) { test_state[i] = state_cp[i]; } @@ -113,7 +113,7 @@ void run_random_gate_apply(std::uint64_t n_qubits, const std::uint64_t target = random.int64() % n_qubits; const Gate gate = QuantumGateConstructor(target, angle, {}); gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); test_state = get_expanded_eigen_matrix_with_identity(target, matrix, n_qubits) * test_state; @@ -132,7 +132,7 @@ void run_random_gate_apply_IBMQ( Eigen::VectorXcd test_state = Eigen::VectorXcd::Zero(dim); for (int repeat = 0; repeat < 10; repeat++) { auto state = StateVector::Haar_random_state(n_qubits); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (int gate_type = 0; gate_type < 3; gate_type++) { for (int i = 0; i < dim; i++) { test_state[i] = state_cp[i]; @@ -158,7 +158,7 @@ void run_random_gate_apply_IBMQ( gate = gate::U3(target, theta, phi, lambda, {}); } gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); test_state = get_expanded_eigen_matrix_with_identity(target, matrix, n_qubits) * test_state; @@ -180,7 +180,7 @@ void run_random_gate_apply_two_target(std::uint64_t n_qubits) { auto state = StateVector::Haar_random_state(n_qubits); for (int g = 0; g < 2; g++) { Gate gate; - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (int i = 0; i < dim; i++) { test_state[i] = state_cp[i]; } @@ -196,7 +196,7 @@ void run_random_gate_apply_two_target(std::uint64_t n_qubits) { func_eig = get_eigen_matrix_full_qubit_CZ; } gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); Eigen::MatrixXcd test_mat = func_eig(control, target, n_qubits); test_state = test_mat * test_state; @@ -209,7 +209,7 @@ void run_random_gate_apply_two_target(std::uint64_t n_qubits) { for (int repeat = 0; repeat < 10; repeat++) { auto state = StateVector::Haar_random_state(n_qubits); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (int i = 0; i < dim; i++) { test_state[i] = state_cp[i]; } @@ -219,7 +219,7 @@ void run_random_gate_apply_two_target(std::uint64_t n_qubits) { if (target1 == target2) target1 = (target1 + 1) % n_qubits; auto gate = gate::Swap(target1, target2); gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); Eigen::MatrixXcd test_mat = get_eigen_matrix_full_qubit_Swap(target1, target2, n_qubits); test_state = test_mat * test_state; @@ -239,7 +239,7 @@ void run_random_gate_apply_pauli(std::uint64_t n_qubits) { // Test for PauliGate for (int repeat = 0; repeat < 10; repeat++) { StateVector state = StateVector::Haar_random_state(n_qubits); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); auto state_bef = state.copy(); for (std::uint64_t i = 0; i < dim; i++) { @@ -277,7 +277,7 @@ void run_random_gate_apply_pauli(std::uint64_t n_qubits) { Gate pauli_gate = gate::Pauli(pauli); pauli_gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); test_state = matrix * test_state; // check if the state is updated correctly @@ -285,10 +285,10 @@ void run_random_gate_apply_pauli(std::uint64_t n_qubits) { ASSERT_NEAR(std::abs((CComplex)state_cp[i] - test_state[i]), 0, eps); } - auto state_bef_cp = state_bef.amplitudes(); + auto state_bef_cp = state_bef.get_amplitudes(); Gate pauli_gate_inv = pauli_gate->get_inverse(); pauli_gate_inv->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); // check if the state is restored correctly for (std::uint64_t i = 0; i < dim; i++) { @@ -299,7 +299,7 @@ void run_random_gate_apply_pauli(std::uint64_t n_qubits) { // Test for PauliRotationGate for (int repeat = 0; repeat < 10; repeat++) { StateVector state = StateVector::Haar_random_state(n_qubits); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); auto state_bef = state.copy(); assert(test_state.size() == (int)state_cp.size()); for (std::uint64_t i = 0; i < dim; i++) { @@ -337,7 +337,7 @@ void run_random_gate_apply_pauli(std::uint64_t n_qubits) { PauliOperator pauli(target_vec, pauli_id_vec, 1.0); Gate pauli_gate = gate::PauliRotation(pauli, angle); pauli_gate->update_quantum_state(state); - state_cp = state.amplitudes(); + state_cp = state.get_amplitudes(); test_state = matrix * test_state; assert((int)state_cp.size() == test_state.size()); // check if the state is updated correctly @@ -346,8 +346,8 @@ void run_random_gate_apply_pauli(std::uint64_t n_qubits) { } Gate pauli_gate_inv = pauli_gate->get_inverse(); pauli_gate_inv->update_quantum_state(state); - state_cp = state.amplitudes(); - auto state_bef_cp = state_bef.amplitudes(); + state_cp = state.get_amplitudes(); + auto state_bef_cp = state_bef.get_amplitudes(); // check if the state is restored correctly for (std::uint64_t i = 0; i < dim; i++) { ASSERT_NEAR(std::abs((CComplex)(state_cp[i] - state_bef_cp[i])), 0, eps); diff --git a/tests/gate/param_gate_test.cpp b/tests/gate/param_gate_test.cpp index 526a50b7..383caf0e 100644 --- a/tests/gate/param_gate_test.cpp +++ b/tests/gate/param_gate_test.cpp @@ -26,13 +26,13 @@ void test_apply_parametric_single_pauli_rotation(std::uint64_t n_qubits, const std::uint64_t target = random.int32() % n_qubits; const double param = M_PI * random.uniform(); - const double pcoef = random.uniform() * 2 - 1; - const Gate gate = factory_fixed(target, pcoef * param, {}); - const ParamGate pgate = factory_parametric(target, pcoef, {}); + const double param_coef = random.uniform() * 2 - 1; + const Gate gate = factory_fixed(target, param_coef * param, {}); + const ParamGate pgate = factory_parametric(target, param_coef, {}); gate->update_quantum_state(state); pgate->update_quantum_state(state_cp, param); - auto state_amp = state.amplitudes(); - auto state_cp_amp = state_cp.amplitudes(); + auto state_amp = state.get_amplitudes(); + auto state_cp_amp = state_cp.get_amplitudes(); for (std::uint64_t i = 0; i < dim; i++) { ASSERT_NEAR(Kokkos::abs(state_cp_amp[i] - state_amp[i]), 0, eps); @@ -40,8 +40,8 @@ void test_apply_parametric_single_pauli_rotation(std::uint64_t n_qubits, ParamGate pgate_inv = pgate->get_inverse(); pgate_inv->update_quantum_state(state, param); - state_amp = state.amplitudes(); - auto state_bef_amp = state_bef.amplitudes(); + state_amp = state.get_amplitudes(); + auto state_bef_amp = state_bef.get_amplitudes(); for (std::uint64_t i = 0; i < dim; i++) { ASSERT_NEAR(Kokkos::abs(state_bef_amp[i] - state_amp[i]), 0, eps); } @@ -57,7 +57,7 @@ void test_apply_parametric_multi_pauli_rotation(std::uint64_t n_qubits) { auto state_cp = state.copy(); auto state_bef = state.copy(); const double param = M_PI * random.uniform(); - const double pcoef = random.uniform() * 2 - 1; + const double param_coef = random.uniform() * 2 - 1; std::vector target_vec, pauli_id_vec; for (std::uint64_t target = 0; target < n_qubits; target++) { target_vec.emplace_back(target); @@ -65,20 +65,20 @@ void test_apply_parametric_multi_pauli_rotation(std::uint64_t n_qubits) { } PauliOperator pauli(target_vec, pauli_id_vec, 1.0); - Gate gate = gate::PauliRotation(pauli, pcoef * param); - ParamGate pgate = gate::PPauliRotation(pauli, pcoef); + Gate gate = gate::PauliRotation(pauli, param_coef * param); + ParamGate pgate = gate::ParamPauliRotation(pauli, param_coef); gate->update_quantum_state(state); pgate->update_quantum_state(state_cp, param); - auto state_amp = state.amplitudes(); - auto state_cp_amp = state_cp.amplitudes(); + auto state_amp = state.get_amplitudes(); + auto state_cp_amp = state_cp.get_amplitudes(); // check if the state is updated correctly for (std::uint64_t i = 0; i < dim; i++) { ASSERT_NEAR(Kokkos::abs(state_cp_amp[i] - state_amp[i]), 0, eps); } ParamGate pgate_inv = pgate->get_inverse(); pgate_inv->update_quantum_state(state, param); - state_amp = state.amplitudes(); - auto state_bef_amp = state_bef.amplitudes(); + state_amp = state.get_amplitudes(); + auto state_bef_amp = state_bef.get_amplitudes(); // check if the state is restored correctly for (std::uint64_t i = 0; i < dim; i++) { ASSERT_NEAR(Kokkos::abs((state_bef_amp[i] - state_amp[i])), 0, eps); @@ -86,19 +86,19 @@ void test_apply_parametric_multi_pauli_rotation(std::uint64_t n_qubits) { } } -TEST(ParamGateTest, ApplyPRXGate) { - test_apply_parametric_single_pauli_rotation(5, &gate::RX, &gate::PRX); +TEST(ParamGateTest, ApplyParamRXGate) { + test_apply_parametric_single_pauli_rotation(5, &gate::RX, &gate::ParamRX); } -TEST(ParamGateTest, ApplyPRYGate) { - test_apply_parametric_single_pauli_rotation(5, &gate::RX, &gate::PRX); +TEST(ParamGateTest, ApplyParamRYGate) { + test_apply_parametric_single_pauli_rotation(5, &gate::RX, &gate::ParamRX); } -TEST(ParamGateTest, ApplyPRZGate) { - test_apply_parametric_single_pauli_rotation(5, &gate::RX, &gate::PRX); +TEST(ParamGateTest, ApplyParamRZGate) { + test_apply_parametric_single_pauli_rotation(5, &gate::RX, &gate::ParamRX); } -TEST(ParamGateTest, ApplyPPauliRotationGate) { test_apply_parametric_multi_pauli_rotation(5); } +TEST(ParamGateTest, ApplyParamPauliRotationGate) { test_apply_parametric_multi_pauli_rotation(5); } -TEST(ParamGateTest, ApplyPProbablisticGate) { - auto probgate = gate::PProbablistic({.1, .9}, {gate::PRX(0), gate::I()}); +TEST(ParamGateTest, ApplyParamProbablisticGate) { + auto probgate = gate::ParamProbablistic({.1, .9}, {gate::ParamRX(0), gate::I()}); std::uint64_t x_cnt = 0, i_cnt = 0; StateVector state(1); for ([[maybe_unused]] auto _ : std::views::iota(0, 100)) { diff --git a/tests/operator/test_operator.cpp b/tests/operator/test_operator.cpp index bf0b3a00..3dd17683 100644 --- a/tests/operator/test_operator.cpp +++ b/tests/operator/test_operator.cpp @@ -56,7 +56,7 @@ TEST(OperatorTest, CheckExpectationValue) { generate_random_observable_with_eigen(n, random); auto state = StateVector::Haar_random_state(n); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); Eigen::VectorXcd test_state = Eigen::VectorXcd::Zero(dim); for (std::uint64_t i = 0; i < dim; ++i) test_state[i] = state_cp[i]; @@ -78,11 +78,11 @@ TEST(OperatorTest, CheckTransitionAmplitude) { generate_random_observable_with_eigen(n, random); auto state_bra = StateVector::Haar_random_state(n); - auto state_bra_cp = state_bra.amplitudes(); + auto state_bra_cp = state_bra.get_amplitudes(); Eigen::VectorXcd test_state_bra = Eigen::VectorXcd::Zero(dim); for (std::uint64_t i = 0; i < dim; ++i) test_state_bra[i] = state_bra_cp[i]; auto state_ket = StateVector::Haar_random_state(n); - auto state_ket_cp = state_ket.amplitudes(); + auto state_ket_cp = state_ket.get_amplitudes(); Eigen::VectorXcd test_state_ket = Eigen::VectorXcd::Zero(dim); for (std::uint64_t i = 0; i < dim; ++i) test_state_ket[i] = state_ket_cp[i]; @@ -164,7 +164,7 @@ TEST(OperatorTest, ApplyToStateTest) { Complex(-14, 0), Complex(-12, 1), }; - ASSERT_EQ(state_vector.amplitudes(), expected); + ASSERT_EQ(state_vector.get_amplitudes(), expected); } TEST(OperatorTest, Optimize) { @@ -180,7 +180,7 @@ TEST(OperatorTest, Optimize) { {"X 0 Y 1", 10.}, {"Y 0 Z 1", 2.}, {"Z 1", 7.}}; std::vector> test; for (const auto& pauli : op.terms()) { - test.emplace_back(pauli.get_pauli_string(), pauli.get_coef()); + test.emplace_back(pauli.get_pauli_string(), pauli.coef()); } std::ranges::sort(expected, [](const auto& l, const auto& r) { return l.first < r.first; }); std::ranges::sort(test, [](const auto& l, const auto& r) { return l.first < r.first; }); diff --git a/tests/operator/test_pauli_operator.cpp b/tests/operator/test_pauli_operator.cpp index 3c1357a6..5d47d5e1 100644 --- a/tests/operator/test_pauli_operator.cpp +++ b/tests/operator/test_pauli_operator.cpp @@ -13,15 +13,15 @@ TEST(PauliOperatorTest, ContainsExtraWhitespace) { PauliOperator expected = PauliOperator("X 0", 1.0); PauliOperator pauli_whitespace = PauliOperator("X 0 ", 1.0); - EXPECT_EQ(1, pauli_whitespace.get_target_qubit_list().size()); - EXPECT_EQ(1, pauli_whitespace.get_pauli_id_list().size()); + EXPECT_EQ(1, pauli_whitespace.target_qubit_list().size()); + EXPECT_EQ(1, pauli_whitespace.pauli_id_list().size()); EXPECT_EQ(expected.get_pauli_string(), pauli_whitespace.get_pauli_string()); } TEST(PauliOperatorTest, EmptyStringConstructsIdentity) { const auto identity = PauliOperator("", 1.0); - ASSERT_EQ(0, identity.get_target_qubit_list().size()); - ASSERT_EQ(0, identity.get_pauli_id_list().size()); + ASSERT_EQ(0, identity.target_qubit_list().size()); + ASSERT_EQ(0, identity.pauli_id_list().size()); ASSERT_EQ("", identity.get_pauli_string()); } @@ -62,7 +62,7 @@ TEST(PauliOperatorTest, SpacedPauliString) { double coef = 2.0; std::string Pauli_string = "X 0 Y 1 "; PauliOperator pauli = PauliOperator(Pauli_string, coef); - size_t PauliSize = pauli.get_target_qubit_list().size(); + size_t PauliSize = pauli.target_qubit_list().size(); ASSERT_EQ(PauliSize, 2); } @@ -95,7 +95,7 @@ TEST_P(PauliOperatorMultiplyTest, MultiplyTest) { const auto p = GetParam(); PauliOperator res = p.op1 * p.op2; EXPECT_EQ(p.expected.get_pauli_string(), res.get_pauli_string()); - EXPECT_EQ(p.expected.get_coef(), res.get_coef()); + EXPECT_EQ(p.expected.coef(), res.coef()); } TEST_P(PauliOperatorMultiplyTest, MultiplyAssignmentTest) { @@ -103,7 +103,7 @@ TEST_P(PauliOperatorMultiplyTest, MultiplyAssignmentTest) { PauliOperator res = p.op1; res = res * p.op2; EXPECT_EQ(p.expected.get_pauli_string(), res.get_pauli_string()); - EXPECT_EQ(p.expected.get_coef(), res.get_coef()); + EXPECT_EQ(p.expected.coef(), res.coef()); } INSTANTIATE_TEST_CASE_P( @@ -190,5 +190,5 @@ TEST(PauliOperatorTest, ApplyToStateTest) { PauliOperator op(std::vector{1, 0, 0}, std::vector{0, 1, 0}, Complex(2)); op.apply_to_state(state_vector); std::vector expected = {2, 0, -6, -4, 10, 8, -14, -12}; - ASSERT_EQ(state_vector.amplitudes(), expected); + ASSERT_EQ(state_vector.get_amplitudes(), expected); } diff --git a/tests/state/state_vector_batched_test.cpp b/tests/state/state_vector_batched_test.cpp index d156a8f0..354a555b 100644 --- a/tests/state/state_vector_batched_test.cpp +++ b/tests/state/state_vector_batched_test.cpp @@ -33,7 +33,7 @@ TEST(StateVectorBatchedTest, LoadAndAmplitues) { StateVectorBatched states(batch_size, n_qubits); states.load(states_h); - auto amps = states.amplitudes(); + auto amps = states.get_amplitudes(); for (std::uint64_t b = 0; b < batch_size; ++b) { for (std::uint64_t i = 0; i < dim; ++i) { ASSERT_EQ(amps[b][i].real(), b * dim + i); @@ -49,30 +49,30 @@ TEST(StateVectorBatchedTest, OperateState) { auto states_cp = states.copy(); for (std::uint64_t b = 0; b < batch_size; ++b) { - ASSERT_TRUE(same_state(states.get_state_vector(b), states_cp.get_state_vector(b))); + ASSERT_TRUE(same_state(states.get_state_vector_at(b), states_cp.get_state_vector_at(b))); } states.add_state_vector(states_add); for (std::uint64_t b = 0; b < batch_size; ++b) { - auto v = states_cp.get_state_vector(b); - v.add_state_vector(states_add.get_state_vector(b)); - ASSERT_TRUE(same_state(v, states.get_state_vector(b))); + auto v = states_cp.get_state_vector_at(b); + v.add_state_vector(states_add.get_state_vector_at(b)); + ASSERT_TRUE(same_state(v, states.get_state_vector_at(b))); } states_cp = states.copy(); states.add_state_vector_with_coef(coef, states_add); for (std::uint64_t b = 0; b < batch_size; ++b) { - auto v = states_cp.get_state_vector(b); - v.add_state_vector_with_coef(coef, states_add.get_state_vector(b)); - ASSERT_TRUE(same_state(v, states.get_state_vector(b))); + auto v = states_cp.get_state_vector_at(b); + v.add_state_vector_with_coef(coef, states_add.get_state_vector_at(b)); + ASSERT_TRUE(same_state(v, states.get_state_vector_at(b))); } states_cp = states.copy(); states.multiply_coef(coef); for (std::uint64_t b = 0; b < batch_size; ++b) { - auto v = states_cp.get_state_vector(b); + auto v = states_cp.get_state_vector_at(b); v.multiply_coef(coef); - ASSERT_TRUE(same_state(v, states.get_state_vector(b))); + ASSERT_TRUE(same_state(v, states.get_state_vector_at(b))); } } @@ -83,7 +83,7 @@ TEST(StateVectorBatchedTest, ZeroProbs) { for (std::uint64_t i = 0; i < n_qubits; ++i) { auto zero_probs = states.get_zero_probability(i); for (std::uint64_t b = 0; b < batch_size; ++b) { - auto state = states.get_state_vector(b); + auto state = states.get_state_vector_at(b); ASSERT_NEAR(zero_probs[b], state.get_zero_probability(i), eps); } } @@ -101,7 +101,7 @@ TEST(StateVectorBatchedTest, MarginalProbs) { } auto mg_probs = states.get_marginal_probability(targets); for (std::uint64_t b = 0; b < batch_size; ++b) { - auto state = states.get_state_vector(b); + auto state = states.get_state_vector_at(b); ASSERT_NEAR(mg_probs[b], state.get_marginal_probability(targets), eps); } } @@ -113,7 +113,7 @@ TEST(StateVectorBatchedTest, Entropy) { auto entropies = states.get_entropy(); for (std::uint64_t b = 0; b < batch_size; ++b) { - auto state = states.get_state_vector(b); + auto state = states.get_state_vector_at(b); ASSERT_NEAR(entropies[b], state.get_entropy(), eps); } } diff --git a/tests/state/state_vector_test.cpp b/tests/state/state_vector_test.cpp index 30c5e895..e40356d1 100644 --- a/tests/state/state_vector_test.cpp +++ b/tests/state/state_vector_test.cpp @@ -25,9 +25,9 @@ TEST(StateVectorTest, HaarRandomStateNorm) { TEST(StateVectorTest, OperationAtIndex) { auto state = StateVector::Haar_random_state(10); for (std::uint64_t i = 0; i < state.dim(); ++i) { - state.set_amplitude_at_index(i, 1); - ASSERT_NEAR(state.get_amplitude_at_index(i).real(), 1, eps); - ASSERT_NEAR(state.get_amplitude_at_index(i).imag(), 0., eps); + state.set_amplitude_at(i, 1); + ASSERT_NEAR(state.get_amplitude_at(i).real(), 1, eps); + ASSERT_NEAR(state.get_amplitude_at(i).imag(), 0., eps); } } @@ -35,8 +35,8 @@ TEST(StateVectorTest, CopyState) { const int n = 5; const auto state = StateVector::Haar_random_state(n); StateVector state_cp = state.copy(); - auto vec1 = state.amplitudes(); - auto vec2 = state_cp.amplitudes(); + auto vec1 = state.get_amplitudes(); + auto vec2 = state_cp.get_amplitudes(); ASSERT_EQ(vec1, vec2); } @@ -45,7 +45,7 @@ TEST(StateVectorTest, ZeroNormState) { StateVector state(StateVector::Haar_random_state(n)); state.set_zero_norm_state(); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (std::uint64_t i = 0; i < state.dim(); ++i) { ASSERT_EQ((CComplex)state_cp[i], CComplex(0, 0)); @@ -57,7 +57,7 @@ TEST(StateVectorTest, ComputationalBasisState) { StateVector state(StateVector::Haar_random_state(n)); state.set_computational_basis(31); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); for (std::uint64_t i = 0; i < state.dim(); ++i) { if (i == 31) { @@ -90,10 +90,10 @@ TEST(StateVectorTest, AddState) { const std::uint64_t n = 10; StateVector state1(StateVector::Haar_random_state(n)); StateVector state2(StateVector::Haar_random_state(n)); - auto vec1 = state1.amplitudes(); - auto vec2 = state2.amplitudes(); + auto vec1 = state1.get_amplitudes(); + auto vec2 = state2.get_amplitudes(); state1.add_state_vector(state2); - auto new_vec = state1.amplitudes(); + auto new_vec = state1.get_amplitudes(); for (std::uint64_t i = 0; i < state1.dim(); ++i) { CComplex res = new_vec[i], val = (CComplex)vec1[i] + (CComplex)vec2[i]; @@ -107,11 +107,11 @@ TEST(StateVectorTest, AddStateWithCoef) { const std::uint64_t n = 10; StateVector state1(StateVector::Haar_random_state(n)); StateVector state2(StateVector::Haar_random_state(n)); - auto vec1 = state1.amplitudes(); - auto vec2 = state2.amplitudes(); + auto vec1 = state1.get_amplitudes(); + auto vec2 = state2.get_amplitudes(); state1.add_state_vector_with_coef(coef, state2); - auto new_vec = state1.amplitudes(); + auto new_vec = state1.get_amplitudes(); for (std::uint64_t i = 0; i < state1.dim(); ++i) { CComplex res = new_vec[i], val = (CComplex)vec1[i] + coef * (CComplex)vec2[i]; @@ -125,9 +125,9 @@ TEST(StateVectorTest, MultiplyCoef) { const CComplex coef(0.5, 0.2); StateVector state(StateVector::Haar_random_state(n)); - auto vec = state.amplitudes(); + auto vec = state.get_amplitudes(); state.multiply_coef(coef); - auto new_vec = state.amplitudes(); + auto new_vec = state.get_amplitudes(); for (std::uint64_t i = 0; i < state.dim(); ++i) { CComplex res = new_vec[i], val = coef * (CComplex)vec[i]; @@ -160,7 +160,7 @@ TEST(StateVectorTest, EntropyCalculation) { StateVector state(n); for (std::uint64_t rep = 0; rep < max_repeat; ++rep) { state = StateVector::Haar_random_state(n); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); ASSERT_NEAR(state.get_squared_norm(), 1, eps); Eigen::VectorXcd test_state(dim); for (std::uint64_t i = 0; i < dim; ++i) test_state[i] = (CComplex)state_cp[i]; @@ -181,7 +181,7 @@ TEST(StateVectorTest, GetMarginalProbability) { const std::uint64_t n = 2; const std::uint64_t dim = 1 << n; StateVector state(StateVector::Haar_random_state(n)); - auto state_cp = state.amplitudes(); + auto state_cp = state.get_amplitudes(); std::vector probs; for (std::uint64_t i = 0; i < dim; ++i) { probs.push_back(internal::squared_norm(state_cp[i])); diff --git a/tests/util/util.hpp b/tests/util/util.hpp index 9948023f..45fe050a 100644 --- a/tests/util/util.hpp +++ b/tests/util/util.hpp @@ -10,8 +10,8 @@ using namespace std::complex_literals; using namespace scaluq; inline bool same_state(const StateVector& s1, const StateVector& s2, const double eps = 1e-12) { - auto s1_cp = s1.amplitudes(); - auto s2_cp = s2.amplitudes(); + auto s1_cp = s1.get_amplitudes(); + auto s2_cp = s2.get_amplitudes(); assert(s1.n_qubits() == s2.n_qubits()); for (std::uint64_t i = 0; i < s1.dim(); ++i) { if (std::abs((std::complex)s1_cp[i] - (std::complex)s2_cp[i]) > eps) @@ -23,8 +23,8 @@ inline bool same_state(const StateVector& s1, const StateVector& s2, const doubl inline bool same_state_except_global_phase(const StateVector& s1, const StateVector& s2, const double eps = 1e-12) { - auto s1_cp = s1.amplitudes(); - auto s2_cp = s2.amplitudes(); + auto s1_cp = s1.get_amplitudes(); + auto s2_cp = s2.get_amplitudes(); assert(s1.n_qubits() == s2.n_qubits()); std::uint64_t significant = 0; for (std::uint64_t i = 0; i < s1.dim(); ++i) {