Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change method name #161

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 49 additions & 51 deletions python/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}$.")
Expand All @@ -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",
Expand Down Expand Up @@ -195,8 +197,8 @@ NB_MODULE(scaluq_core, m) {
nb::overload_cast<std::uint64_t, const StateVector &>(
&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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -304,28 +306,28 @@ NB_MODULE(scaluq_core, m) {
nb::class_<GATE_TYPE>(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", \
Expand Down Expand Up @@ -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", \
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions scaluq/circuit/circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ std::uint64_t Circuit::calculate_depth() const {
std::vector<std::uint64_t> filled_step(_n_qubits, 0ULL);
for (const auto& gate : _gate_list) {
std::vector<std::uint64_t> 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<std::uint64_t> 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]) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions scaluq/circuit/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ class Circuit {

[[nodiscard]] inline std::uint64_t n_qubits() const { return _n_qubits; }
[[nodiscard]] inline const std::vector<GateWithKey>& 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<std::string> key_set() const {
std::set<std::string> key_set;
for (auto&& gate : _gate_list) {
if (gate.index() == 1) key_set.insert(std::get<1>(gate).second);
}
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<std::string> get_key(std::uint64_t idx) {
[[nodiscard]] inline std::optional<std::string> 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");
Expand Down
12 changes: 6 additions & 6 deletions scaluq/gate/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,18 @@ class GateBase : public std::enable_shared_from_this<GateBase> {
}
virtual ~GateBase() = default;

[[nodiscard]] virtual std::vector<std::uint64_t> get_target_qubit_list() const {
[[nodiscard]] virtual std::vector<std::uint64_t> target_qubit_list() const {
return mask_to_vector(_target_mask);
}
[[nodiscard]] virtual std::vector<std::uint64_t> get_control_qubit_list() const {
[[nodiscard]] virtual std::vector<std::uint64_t> control_qubit_list() const {
return mask_to_vector(_control_mask);
}
[[nodiscard]] virtual std::vector<std::uint64_t> get_operand_qubit_list() const {
[[nodiscard]] virtual std::vector<std::uint64_t> 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;
}

Expand Down
2 changes: 1 addition & 1 deletion scaluq/gate/gate_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ inline Gate TwoTargetMatrix(std::uint64_t target1,
}
// まだ
inline Gate Pauli(const PauliOperator& pauli, const std::vector<std::uint64_t>& controls = {}) {
auto tar = pauli.get_target_qubit_list();
auto tar = pauli.target_qubit_list();
return internal::GateFactory::create_gate<internal::PauliGateImpl>(
internal::vector_to_mask(controls), pauli);
}
Expand Down
Loading
Loading