Skip to content

Conversation

yaelbh
Copy link
Contributor

@yaelbh yaelbh commented May 29, 2018

Thanks to Guy Szweigman and Mohammad Heeb, who noticed that the C++ simulator does not always display correct density matrix and probabilities vector.

This happens in the presence of an optimization, which applies to no-noise circuits where all measurements are located at the end. The optimization replaces the shots by a single shot + sampling. However in the calculation of the density matrix and probabilities vector there is a normalization by the number of shots. This results in a density matrix whose trace is 1/(number of shots), and a probabilities vector whose sum is 1/(number of shots).

Description

I fixed the number of shots when the optimization is applied.

Independently of this pull request, it should be revised whether it is correct to normalize by total_shots. Looks like there is an infrastructure to allow, in the future, that total_shots will be the accumulation of number of shots in executions on different engines. What happens if one of the engines does not support density_matrix?

Motivation and Context

How Has This Been Tested?

I updated test_qasm_simulator_cpp.py
Note: test__simulator_online_size in test_quantumprogram fails in the system.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
    test__simulator_online_size in test_quantumprogram fails in the system.

@@ -172,10 +172,13 @@ void VectorEngine::execute(const Circuit &prog, BaseBackend<QubitVector> *be,
// Check to see if circuit is ideal and allows for measurement optimization
if (prog.opt_meas && prog.noise.ideal) {

// This optimization replaces the shots by a single shot + sampling
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a second comment line here: // We need to subtract the additional shots added by BaseEngine class

chriseclectic
chriseclectic previously approved these changes May 29, 2018
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.

@yaelbh thanks for catching and fixing this! It looks like you need to rebase against the master before this can be merged, but the fix looks good to me.

…vector in the presence of optimization, which applies to no-noise circuits where all measurements are located at the end.
@yaelbh
Copy link
Contributor Author

yaelbh commented May 30, 2018

I added a comment requested by Chris and rebased against the master.

@atilag atilag merged commit cd6db9e into Qiskit:master May 30, 2018
@yaelbh yaelbh deleted the density_matrix branch May 30, 2018 08:59
lia-approves pushed a commit to edasgupta/qiskit-terra that referenced this pull request Jul 30, 2019
…metimes erroneous (Qiskit#518)

* Bug fix in the C++ simulator: wrong density matrix and probabilities vector in the presence of optimization, which applies to no-noise circuits where all measurements are located at the end.
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.

3 participants