Skip to content

Commit

Permalink
This should be a better fix for Qiskit#2292
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanro committed Jan 16, 2025
1 parent aa40bfa commit f352eae
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ void MPS::apply_multi_qubit_gate(const reg_t &qubits, const cmatrix_t &mat,
// change qubit order in the matrix - instead of doing swaps on the qubits
uint_t nqubits = qubits.size();
uint_t sidelen = 1 << nqubits;

/*
cmatrix_t new_mat(sidelen, sidelen);
for (uint_t col = 0; col < sidelen; ++col) {
for (uint_t row = 0; row < sidelen; ++row) {
Expand All @@ -821,6 +823,27 @@ void MPS::apply_multi_qubit_gate(const reg_t &qubits, const cmatrix_t &mat,
new_mat(new_vec[row], new_vec[col]) = is_diagonal ? 0. : mat(row, col);
}
}
*/

// alternative solution, maybe it's better:
cmatrix_t new_mat(is_diagonal ? 1 : sidelen, sidelen);

if (is_diagonal) {
for (uint_t col = 0; col < sidelen; ++col) {
new_mat(0, new_vec[col]) =
mat.GetRows() == 1 ? mat(0, col) : mat(col, col); // this is just in case something passes a matrix
// instead of a vector even if is_diagonal is true
}
} else {
for (uint_t col = 0; col < sidelen; ++col) {
for (uint_t row = 0; row < sidelen; ++row) {
if (row == col)
new_mat(new_vec[row], new_vec[row]) = mat(row, row);
else
new_mat(new_vec[row], new_vec[col]) = mat(row, col);
}
}
}

if (is_ordered(qubits))
apply_matrix_to_target_qubits(qubits, new_mat, is_diagonal);
Expand Down

0 comments on commit f352eae

Please sign in to comment.