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

Add batch update #179

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Add batch update #179

wants to merge 13 commits into from

Conversation

Glacialte
Copy link
Contributor

バッチ版の更新とテストを追加しました。
また、CIでエラーが起きていたのでpyproject.tomlのCIの項からnumpyの指定を削除しました。

Copy link
Contributor

@KowerKoint KowerKoint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

途中までしか見れてません
すみません

Comment on lines 169 to 175
[[nodiscard]] std::vector<std::uint64_t> mask_to_vector(std::uint64_t mask) const {
std::vector<std::uint64_t> qubits;
for (std::uint64_t i = 0; i < 64; ++i) {
if ((mask >> i) & 1) qubits.push_back(i);
}
return qubits;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もっと高速なのがutil/utility.hppにあるはず

std::ranges::upper_bound(_cumulative_distribution, r[i])) -
1;
if (indices[i] >= _gate_list.size()) indices[i] = _gate_list.size() - 1;
auto state_vector = states.get_state_vector_at(i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

かけられるのがIゲートとかなのにコピーするのは流石に嫌なので、
https://kokkos.org/kokkos-core-wiki/API/core/view/view.html#_CPPv44ViewRK4ViewI2DTDp4PropEDp4ArgsでUnmanaged Viewを作って底からStateVectorを得るようにしたい
ViewからStateVectorを作るコンストラクタを作って、

StateVector state_vector  = StateVector(Kokkos::View(states._raw, std::make_pair(i, 0), std::make_pair(i, dim)));

みたいにすれば良さそう

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先に抽選をしておいて、一定以上の個数がかけられるゲートは一度Batchにコピーして適用するのがいいかも

Copy link
Contributor

@KowerKoint KowerKoint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全体的にはかなりいい感じです!
ありがとうございます!
Parametricのparamをvectorにするのだけは必ずやってほしいです(QCLで使うらしいので)

[](const GATE_TYPE& gate, StateVectorBatched& states) { \
gate->update_quantum_state(states); \
}, \
"Apply gate to `state_vector_batched`. `states` in args is directly updated.") \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state_vector_batchedの部分、引数の名前として入れてるつもりだったのでstatesにしたほうがいい

@@ -114,6 +115,7 @@ class ParamGateBase {
[[nodiscard]] virtual internal::ComplexMatrix get_matrix(double param) const = 0;

virtual void update_quantum_state(StateVector& state_vector, double param) const = 0;
virtual void update_quantum_state(StateVectorBatched& states, double param) const = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paramのほうはstd::vectorでparamsを受けるようにして、それぞれの場所に別々のパラメータを渡せるようにしたいです。

std::ranges::upper_bound(_cumulative_distribution, r[i])) -
1;
if (indices[i] >= _gate_list.size()) indices[i] = _gate_list.size() - 1;
auto state_vector = states.get_state_vector_at(i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先に抽選をしておいて、一定以上の個数がかけられるゲートは一度Batchにコピーして適用するのがいいかも

insert_zero_at_mask_positions(it, target_mask | control_mask) | control_mask;
std::uint64_t basis_1 = basis_0 | lower_target_mask;
std::uint64_t basis_2 = basis_0 | upper_target_mask;
std::uint64_t basis_3 = basis_1 | target_mask;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

間違ってはないけどbasis1にかぶせてるの結構変な感じはしますね…

@@ -177,7 +177,7 @@ inline ComplexMatrix convert_internal_matrix_to_external_matrix(const Matrix& ma
}

inline ComplexMatrix convert_coo_to_external_matrix(SparseMatrix mat) {
ComplexMatrix eigen_matrix(mat._row, mat._col);
ComplexMatrix eigen_matrix = ComplexMatrix::Zero(mat._row, mat._col);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -50,6 +50,11 @@ class OneTargetMatrixGateImpl : public GateBase {
one_target_dense_matrix_gate(_target_mask, _control_mask, _matrix, state_vector);
}

void update_quantum_state(StateVectorBatched& states) const override {
check_qubit_mask_within_bounds(states.get_state_vector_at(0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

範囲の確認のためにget_state_vector_atしてるの,ちょっと気になります
check_qubit_mask_within_boundsには単に長さを渡すように変えてもいいかも

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statesを渡すcheck_qubit_mask_within_boundsを作りました

Comment on lines 177 to 180
[[nodiscard]]

std::string
get_qubit_info_as_string(const std::string& indent) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これシンプルに気になります

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants