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 2 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
82 changes: 40 additions & 42 deletions python/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ NB_MODULE(scaluq_core, m) {
&StateVector::get_amplitude_at_index,
"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 @@ -226,7 +228,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 @@ -296,28 +298,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 @@ -552,28 +554,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 @@ -683,7 +685,7 @@ 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",
Expand Down Expand Up @@ -760,19 +762,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 @@ -828,14 +826,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 @@ UINT Circuit::calculate_depth() const {
std::vector<UINT> filled_step(_n_qubits, 0ULL);
for (const auto& gate : _gate_list) {
std::vector<UINT> control_qubits = gate.index() == 0
? std::get<0>(gate)->get_control_qubit_list()
: std::get<1>(gate).first->get_control_qubit_list();
? std::get<0>(gate)->control_qubit_list()
: std::get<1>(gate).first->control_qubit_list();
std::vector<UINT> target_qubits = gate.index() == 0
? std::get<0>(gate)->get_target_qubit_list()
: std::get<1>(gate).first->get_target_qubit_list();
? std::get<0>(gate)->target_qubit_list()
: std::get<1>(gate).first->target_qubit_list();
UINT max_step_amount_target_qubits = 0;
for (UINT 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
2 changes: 1 addition & 1 deletion scaluq/circuit/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Circuit {

[[nodiscard]] inline UINT n_qubits() const { return _n_qubits; }
[[nodiscard]] inline const std::vector<GateWithKey>& gate_list() const { return _gate_list; }
[[nodiscard]] inline UINT gate_count() { return _gate_list.size(); }
[[nodiscard]] inline UINT 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) {
Expand Down
14 changes: 6 additions & 8 deletions scaluq/gate/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,18 @@ class GateBase : public std::enable_shared_from_this<GateBase> {
}
virtual ~GateBase() = default;

[[nodiscard]] virtual std::vector<UINT> get_target_qubit_list() const {
[[nodiscard]] virtual std::vector<UINT> target_qubit_list() const {
return mask_to_vector(_target_mask);
}
[[nodiscard]] virtual std::vector<UINT> get_control_qubit_list() const {
[[nodiscard]] virtual std::vector<UINT> control_qubit_list() const {
return mask_to_vector(_control_mask);
}
[[nodiscard]] virtual std::vector<UINT> get_operand_qubit_list() const {
[[nodiscard]] virtual std::vector<UINT> operand_qubit_list() const {
return mask_to_vector(_target_mask | _control_mask);
}
[[nodiscard]] virtual UINT get_target_qubit_mask() const { return _target_mask; }
[[nodiscard]] virtual UINT get_control_qubit_mask() const { return _control_mask; }
[[nodiscard]] virtual UINT get_operand_qubit_mask() const {
return _target_mask | _control_mask;
}
[[nodiscard]] virtual UINT target_qubit_mask() const { return _target_mask; }
[[nodiscard]] virtual UINT control_qubit_mask() const { return _control_mask; }
[[nodiscard]] virtual UINT operand_qubit_mask() const { return _target_mask | _control_mask; }

[[nodiscard]] virtual Gate get_inverse() const = 0;
[[nodiscard]] virtual ComplexMatrix get_matrix() const = 0;
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 @@ -136,7 +136,7 @@ inline Gate TwoTargetMatrix(UINT target1,
}
// まだ
inline Gate Pauli(const PauliOperator& pauli, const std::vector<UINT>& 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
11 changes: 5 additions & 6 deletions scaluq/gate/gate_pauli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ class PauliGateImpl : public GateBase {

public:
PauliGateImpl(UINT control_mask, const PauliOperator& pauli)
: GateBase(vector_to_mask<false>(pauli.get_target_qubit_list()), control_mask),
_pauli(pauli) {}
: GateBase(vector_to_mask<false>(pauli.target_qubit_list()), control_mask), _pauli(pauli) {}

PauliOperator pauli() const { return _pauli; };
std::vector<UINT> get_pauli_id_list() const { return _pauli.get_pauli_id_list(); }
std::vector<UINT> 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(); }
Expand All @@ -33,12 +32,12 @@ class PauliRotationGateImpl : public GateBase {

public:
PauliRotationGateImpl(UINT control_mask, const PauliOperator& pauli, double angle)
: GateBase(vector_to_mask<false>(pauli.get_target_qubit_list()), control_mask),
: GateBase(vector_to_mask<false>(pauli.target_qubit_list()), control_mask),
_pauli(pauli),
_angle(angle) {}

PauliOperator pauli() const { return _pauli; }
std::vector<UINT> get_pauli_id_list() const { return _pauli.get_pauli_id_list(); }
std::vector<UINT> pauli_id_list() const { return _pauli.pauli_id_list(); }
double angle() const { return _angle; }

Gate get_inverse() const override {
Expand All @@ -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()) +
Expand Down
Loading
Loading