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

Fix for https://github.com/Qiskit/qiskit-aer/issues/2235 #2239

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions releasenotes/notes/fix-amplitude-e28a8d127ae1d5e7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
prelude: >
AerState::probability and AerState::amplitude returned the wrong states
for the MPS simulator.
fixes:
- |
Added a function that provides the correct states, ``reorder_qubits_rev``,
which is called now by ``MPS::get_amplitude_vector``.
For more details, refer to:
`#2235 <https://github.com/Qiskit/qiskit-aer/issues/2235>`
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ template <class vec_t>
void reorder_all_qubits(const vec_t &orig_probvector, const reg_t &qubits,
vec_t &new_probvector);
uint_t reorder_qubits(const reg_t &qubits, uint_t index);
uint_t reorder_qubits_rev(const reg_t &qubits, uint_t index);

//------------------------------------------------------------------------
// Function name: permute_all_qubits
Expand Down Expand Up @@ -208,6 +209,29 @@ uint_t reorder_qubits(const reg_t &qubits, uint_t index) {
return new_index;
}

uint_t reorder_qubits_rev(const reg_t &qubits, uint_t index) {
uint_t new_index = 0;

int_t current_pos = 0, current_val = 0, new_pos = 0, shift = 0;
uint_t num_qubits = qubits.size();
for (uint_t i = 0; i < num_qubits; i++) {
current_pos = qubits[i];
current_val = 1ULL << current_pos;
new_pos = i;
shift = new_pos - current_pos;
if (index & current_val) {
if (shift > 0) {
new_index += current_val << shift;
} else if (shift < 0) {
new_index += current_val >> -shift;
} else {
new_index += current_val;
}
}
}
return new_index;
}

template <class vec_t>
void permute_all_qubits(const vec_t &orig_statevector,
const reg_t &input_qubits, const reg_t &output_qubits,
Expand Down Expand Up @@ -1350,7 +1374,7 @@ Vector<complex_t> MPS::get_amplitude_vector(const reg_t &base_values) {
// Since the qubits may not be ordered, we determine the actual index
// by the internal order of the qubits, to obtain the actual_base_value
uint_t actual_base_value =
reorder_qubits(qubit_ordering_.order_, base_values[i]);
reorder_qubits_rev(qubit_ordering_.order_, base_values[i]);
base_value = AER::Utils::int2string(actual_base_value);
amplitude_vector[i] = get_single_amplitude(base_value);
}
Expand Down