Skip to content

Commit

Permalink
Corrected qubits ordering in MPS::sample_measure_using_probabilities. (
Browse files Browse the repository at this point in the history
  • Loading branch information
merav-aharoni authored Nov 3, 2020
1 parent d3fee1e commit d3b927a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
10 changes: 10 additions & 0 deletions releasenotes/notes/sample_measure-bug-4ce3a1a6553e4900.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---

fixes:
- |
When invoking MPS::sample_measure, we need to first sort the qubits to the
default ordering because this is the assumption in qasm_controller.This is
done by invoking the method move_all_qubits_to_sorted_ordering. It was
correct in sample_measure_using_apply_measure, but missing in
sample_measure_using_probabilities.
10 changes: 6 additions & 4 deletions src/simulators/matrix_product_state/matrix_product_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class State : public Base::State<matrixproductstate_t> {
std::vector<reg_t>
sample_measure_using_probabilities(const reg_t &qubits,
uint_t shots,
RngEngine &rng) const;
RngEngine &rng);

// Computes sample_measure by copying the MPS to a temporary structure, and
// applying a measurement on the temporary MPS. This is done for every shot,
Expand Down Expand Up @@ -765,9 +765,9 @@ std::vector<reg_t> State::sample_measure(const reg_t &qubits,
// The parameters used below are based on experimentation.
// The user can override this by setting the parameter "mps_sample_measure_algorithm"
uint_t num_qubits = qubits.size();

if (MPS::get_sample_measure_alg() == Sample_measure_alg::PROB || num_qubits < 10)
if (MPS::get_sample_measure_alg() == Sample_measure_alg::PROB){
return sample_measure_using_probabilities(qubits, shots, rng);
}
if (MPS::get_sample_measure_alg() == Sample_measure_alg::APPLY_MEASURE ||
num_qubits >26 )
return sample_measure_using_apply_measure(qubits, shots, rng);
Expand All @@ -778,6 +778,8 @@ std::vector<reg_t> State::sample_measure(const reg_t &qubits,
// Sample_measure_alg::HEURISTIC
uint_t max_bond_dim = qreg_.get_max_bond_dimensions();

if (num_qubits <10)
return sample_measure_using_probabilities(qubits, shots, rng);
if (max_bond_dim <= 2) {
if (shots_dbl < 12.0 * pow(1.85, (num_qubits_dbl-10.0)))
return sample_measure_using_apply_measure(qubits, shots, rng);
Expand Down Expand Up @@ -805,7 +807,7 @@ std::vector<reg_t> State::sample_measure(const reg_t &qubits,
std::vector<reg_t> State::
sample_measure_using_probabilities(const reg_t &qubits,
uint_t shots,
RngEngine &rng) const {
RngEngine &rng) {

// Generate flat register for storing
rvector_t rnds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,15 @@ double MPS::norm(const reg_t &qubits, const cmatrix_t &mat) const {

//-----------------------------------------------------------------------------
reg_t MPS::sample_measure_using_probabilities(const rvector_t &rnds,
const reg_t &qubits) const {
const reg_t &qubits) {
// since input is always sorted in qasm_controller, therefore, we must return the qubits
// to their original location (sorted)
move_all_qubits_to_sorted_ordering();
return sample_measure_using_probabilities_internal(rnds, qubits);
}

reg_t MPS::sample_measure_using_probabilities_internal(const rvector_t &rnds,
const reg_t &qubits) const {
const uint_t SHOTS = rnds.size();
reg_t samples;
samples.assign(SHOTS, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class MPS{
double norm(const reg_t &qubits, const cmatrix_t &mat) const;

reg_t sample_measure_using_probabilities(const rvector_t &rnds,
const reg_t &qubits) const;
const reg_t &qubits);

reg_t apply_measure(const reg_t &qubits,
RngEngine &rng);
Expand Down Expand Up @@ -342,6 +342,9 @@ class MPS{
uint_t apply_measure(uint_t qubit,
RngEngine &rng);

reg_t sample_measure_using_probabilities_internal(const rvector_t &rnds,
const reg_t &qubits) const;

void initialize_from_matrix(uint_t num_qubits, cmatrix_t mat);
//----------------------------------------------------------------
// Function name: centralize_qubits
Expand Down

0 comments on commit d3b927a

Please sign in to comment.