Skip to content

Commit

Permalink
Fix required_memory_mb (Qiskit#1881)
Browse files Browse the repository at this point in the history
* Fix required_memory_mb

* add release note

---------

Co-authored-by: Hiroshi Horii <hhorii@users.noreply.github.com>
  • Loading branch information
doichanj and hhorii committed Aug 9, 2023
1 parent 8885eae commit 0d553b0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
requried_memory_mb function for statevector returns wrong value when number
of qubits is very large because of overflow of 64 bits integer.
Now it returns SIZE_MAX value when number of qubits is too large so that
Qiskit Aer can know memory is not sufficient for
statvector/unitary/density matrix methods.
1 change: 0 additions & 1 deletion src/simulators/density_matrix/densitymatrix_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ template <class densmat_t>
size_t State<densmat_t>::required_memory_mb(
uint_t num_qubits, const std::vector<Operations::Op> &ops) const {
(void)ops; // avoid unused variable compiler warning
(void)ops; // avoid unused variable compiler warning
densmat_t tmp;
return tmp.required_memory_mb(2 * num_qubits);
}
Expand Down
2 changes: 2 additions & 0 deletions src/simulators/statevector/qubitvector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,8 @@ size_t QubitVector<data_t>::required_memory_mb(uint_t num_qubits) const {

size_t unit = std::log2(sizeof(std::complex<data_t>));
size_t shift_mb = std::max<int_t>(0, num_qubits + unit - 20);
if (shift_mb >= 63)
return SIZE_MAX;
size_t mem_mb = 1ULL << shift_mb;
return mem_mb;
}
Expand Down
3 changes: 2 additions & 1 deletion src/simulators/statevector/qubitvector_thrust.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,9 @@ size_t QubitVectorThrust<data_t>::required_memory_mb(uint_t num_qubits) const {

size_t unit = std::log2(sizeof(std::complex<data_t>));
size_t shift_mb = std::max<int_t>(0, num_qubits + unit - 20);
if (shift_mb >= 63)
return SIZE_MAX;
size_t mem_mb = 1ULL << shift_mb;

return mem_mb;
}

Expand Down
7 changes: 2 additions & 5 deletions src/simulators/unitary/unitary_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,9 @@ void State<unitary_matrix_t>::apply_op(const Operations::Op &op,
template <class unitary_matrix_t>
size_t State<unitary_matrix_t>::required_memory_mb(
uint_t num_qubits, const std::vector<Operations::Op> &ops) const {
// An n-qubit unitary as 2^2n complex doubles
// where each complex double is 16 bytes
(void)ops; // avoid unused variable compiler warning
size_t shift_mb = std::max<int_t>(0, num_qubits + 4 - 20);
size_t mem_mb = 1ULL << (2 * shift_mb);
return mem_mb;
unitary_matrix_t tmp;
return tmp.required_memory_mb(2 * num_qubits);
}

template <class unitary_matrix_t>
Expand Down

0 comments on commit 0d553b0

Please sign in to comment.