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

MPS: Performance improvement when measuring all qubits #1277

Merged
merged 16 commits into from
Jun 25, 2021

Conversation

merav-aharoni
Copy link
Contributor

Summary

Performance improvement for the case where all qubits are measured.

Details and comments

In the previous implementation, after every qubit is measured, its effect is propagated to all other qubits. After that, the next qubit is measured. In the new implementation, if we are measuring all qubits, then it is not necessary to propagate to effect to all the remaining qubits. We only propagate to the nearest neighbors, because then we measure these neighbors next.
It is possible to implement for cases where a subset of the qubits is measured, but this will require additional effort to decide when propagation is necessary and when not.

@merav-aharoni
Copy link
Contributor Author

Also should review the heuristics that chooses between algorithms for measurement - the parameters should probably be changed.

@merav-aharoni
Copy link
Contributor Author

merav-aharoni commented Jun 21, 2021

Here are the results of the performance of the previous apply_measure as compared to the current implementation. The circuit is RealAmplitudes with 10000 shots.
measure-alg-num-shots-10000

@merav-aharoni merav-aharoni changed the title [WIP] MPS: Performance improvement when measuring all qubits MPS: Performance improvement when measuring all qubits Jun 21, 2021
@merav-aharoni
Copy link
Contributor Author

@yaelbh , @chriseclectic - please review

Copy link
Contributor

@yaelbh yaelbh left a comment

Choose a reason for hiding this comment

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

Very very nice

left_qubit = min_qubit == 0 ? 0 : min_qubit - 1;
}
if (num_qubits_ == 1)
right_qubit = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as in apply_measure_internal. There is an unclear separation in the order of qubits according to apply_meausre, which is perhaps not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the case of measure_all, since we are measuring in order from 0 to n-1, this will not really be necessary - there will be no propagation to the left. But I think it is better to cover this, for the sake of completeness. Let me know if you think I should do this differently.

@@ -1698,11 +1715,11 @@ void MPS::measure_reset_update_internal(const reg_t &qubits,
mps_container_t MPS::copy_to_mps_container() {
move_all_qubits_to_sorted_ordering();
mps_container_t ret;
for (auto i=0; i<num_qubits(); i++) {
for (uint_t i=0; i<num_qubits(); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not auto? By the way, when you used uint before, perhaps it should be uint_t?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed all of these, because the compiler was giving warnings on them and I wanted to eliminate the warnings.

Copy link
Member

@chriseclectic chriseclectic left a comment

Choose a reason for hiding this comment

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

Looks good. I'm not sure if doc string for "mps_sample_measure_algorithm" needs updating for this change or if it is an improvement to the existing options.

# ---------------------------------------------------------------------
# Test MPS algorithms for measure
# ---------------------------------------------------------------------
def test_mps_measure_alg_qv(self):
Copy link
Member

Choose a reason for hiding this comment

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

It might be better to add these tests to the new AerSimulator tests which are a lot cleaner for specifying that they run on specific simulation methods or with custom simulation options.

Eg see https://github.com/Qiskit/qiskit-aer/blob/main/test/terra/backends/aer_simulator/test_algorithms.py#L68-L73 for example of running test on specific method with custom simulator options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure where would be most suitable for this test. Perhaps under https://github.com/Qiskit/qiskit-aer/blob/main/test/terra/backends/aer_simulator/test_options.py? on the other hand, it is testing measurement, so that's why I put it in test_measure.
It's your call where to put it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your last comment somehow disappeared. You mentioned putting the option apply_measure in AerSimulator and in QasmSimulator. This option is not new. It is already in both these files.

@chriseclectic chriseclectic merged commit 172f8b3 into Qiskit:main Jun 25, 2021
@omarshehab
Copy link

omarshehab commented Jul 9, 2021

@chriseclectic , am I correct to understand the following?

  1. Previously, there were three options for the value to be passed on for "mps_sample_measure_algorithm" option. They are "mps_probabilities", "mps_apply_measure", and “mps_heuristic”.
  2. This fix adds one more option which is "mps_measure_all".
  3. We are using the QSVM API for 20 qubits. To take advantage of this improvement we will have to pass backend_options = {"mps_sample_measure_algorithm": "mps_measure_all"}.

@merav-aharoni
Copy link
Contributor Author

Hi @omarshehab, we didn't add another option mps_measure_all - that was something we did temporarily just to compare performance. The new improvement works by default, so you don't have to set anything, except for selecting the mps simulation method. It works as part of mps_apply_measure. Note however, that this improvement applies only when you measure all the qubits and not a subset.

@omarshehab
Copy link

@merav-aharoni , I understand that the default is "mps_sample_measure_algorithm": "mps_heuristic". In that case, if I don't set "mps_sample_measure_algorithm": "mps_apply_measure", the new improvements will not be used, right?

@mishmash
Copy link

mishmash commented Jul 9, 2021

@omarshehab , it may still be used, but that just depends on whether or not the heuristic chooses the "mps_apply_measure" option. But, as mentioned by @merav-aharoni above, the heuristic is now out of date with these improvements. I would personally be in favor of removing the heuristic altogether, default to "mps_apply_measure", and provide a more detailed description of the inner workings of the two options in the docs; e.g., "mps_apply_measure" generates samples directly from the MPS each taking O(n \chi^2) time [or O(n^2 \chi^2) if measuring only a subset], while "mps_probabilities" first reconstructs the full statevector from the MPS in O(2^n n \chi^2) time and then generates samples each in O(1) time -- from this information, the user should be able to make an informed decision as to which option to use.

@merav-aharoni
Copy link
Contributor Author

Thanks, @mishmash for adding the clarification. The intent is indeed to implement this algorithm also when measuring a subset of the qubits, and to either remove or improve the heuristic accordingly. I am not sure I will get to this in the near future, so perhaps it is best to update the documentation, as you suggest.

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.

None yet

6 participants