diff --git a/src/qasm-simulator-cpp/src/engines/vector_engine.hpp b/src/qasm-simulator-cpp/src/engines/vector_engine.hpp index 6575610a845d..b8eea1e50859 100644 --- a/src/qasm-simulator-cpp/src/engines/vector_engine.hpp +++ b/src/qasm-simulator-cpp/src/engines/vector_engine.hpp @@ -172,10 +172,14 @@ void VectorEngine::execute(const Circuit &prog, BaseBackend *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 + // 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 @@ -492,4 +496,4 @@ inline void from_json(const json_t &js, VectorEngine &eng) { //------------------------------------------------------------------------------ } // end namespace QISKIT -#endif \ No newline at end of file +#endif diff --git a/test/python/qobj/cpp_measure_opt.json b/test/python/qobj/cpp_measure_opt.json index d98dc31fe1e6..964d7cb7bfa6 100644 --- a/test/python/qobj/cpp_measure_opt.json +++ b/test/python/qobj/cpp_measure_opt.json @@ -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": [ { diff --git a/test/python/test_qasm_simulator_cpp.py b/test/python/test_qasm_simulator_cpp.py index 2f8989aa83dc..e12391bb59ac 100644 --- a/test/python/test_qasm_simulator_cpp.py +++ b/test/python/test_qasm_simulator_cpp.py @@ -17,6 +17,8 @@ # ============================================================================= +from test.python.common import QiskitTestCase + import json import unittest import numpy as np @@ -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): @@ -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')