Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/qasm-simulator-cpp/src/engines/vector_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ 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
Copy Markdown
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

// We need to subtract the additional shots added by BaseEngine class
total_shots -= (nshots-1);

// Find position of first measurement operation
uint_t pos = 0;
while (prog.operations[pos].id != gate_t::Measure &&
pos < prog.operations.size()) {
while (pos < prog.operations.size() &&
prog.operations[pos].id != gate_t::Measure) {
pos++;
}
// Execute operations before measurements
Expand Down Expand Up @@ -492,4 +496,4 @@ inline void from_json(const json_t &js, VectorEngine &eng) {
//------------------------------------------------------------------------------
} // end namespace QISKIT

#endif
#endif
3 changes: 2 additions & 1 deletion test/python/qobj/cpp_measure_opt.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"config": {
"shots": 2000,
"seed": 1,
"backend_name": "local_qasm_simulator_cpp"
"backend_name": "local_qasm_simulator_cpp",
"data" : ["density_matrix", "probabilities"]
},
"circuits": [
{
Expand Down
9 changes: 7 additions & 2 deletions test/python/test_qasm_simulator_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

# =============================================================================

from test.python.common import QiskitTestCase

import json
import unittest
import numpy as np
Expand All @@ -31,7 +33,6 @@
from qiskit.backends.local.qasm_simulator_cpp import (QasmSimulatorCpp,
cx_error_matrix,
x90_error_matrix)
from .common import QiskitTestCase


class TestLocalQasmSimulatorCpp(QiskitTestCase):
Expand Down Expand Up @@ -205,13 +206,17 @@ def test_qobj_measure_opt(self):
snapshots = result.get_snapshots(name)
self.assertEqual(set(snapshots), {'0'},
msg=name + ' snapshot keys')
self.assertEqual(len(snapshots['0']), 1,
self.assertEqual(len(snapshots['0']), 3,
msg=name + ' snapshot length')
state = snapshots['0']['statevector'][0]
expected_state = expected_data[name]['statevector']
fidelity = np.abs(expected_state.dot(state.conj())) ** 2
self.assertAlmostEqual(fidelity, 1.0, places=10,
msg=name + ' snapshot fidelity')
rho = snapshots['0']['density_matrix']
self.assertAlmostEqual(np.trace(rho), 1)
prob = snapshots['0']['probabilities']
self.assertAlmostEqual(np.sum(prob), 1)

def test_qobj_measure_opt_flag(self):
filename = self._get_resource_path('qobj/cpp_measure_opt_flag.json')
Expand Down