def test_snapshot_expval_matrix_post_measure(self): """Test snapshot expectation value (matrix) after final measurement""" shots = 1000 labels = snapshot_expval_labels() counts_targets = snapshot_expval_counts(shots) value_targets = snapshot_expval_post_meas_values() circuits = snapshot_expval_circuits(pauli=False, post_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) job = self.SIMULATOR.run(qobj, backend_options=self.BACKEND_OPTS) result = job.result() success = getattr(result, 'success', False) method = self.BACKEND_OPTS.get('method', 'automatic') if method not in QasmSnapshotExpValMatrixTests.SUPPORTED_QASM_METHODS: self.assertFalse(success) else: self.assertTrue(success) self.compare_counts(result, circuits, counts_targets, delta=0.1 * shots) # Check snapshots for j, circuit in enumerate(circuits): data = result.data(circuit) all_snapshots = self.expval_snapshots(data, labels) for label in labels: snaps = all_snapshots.get(label, {}) self.assertTrue(len(snaps), 1) for memory, value in snaps.items(): target = value_targets[j].get(label, {}).get(memory, {}) self.assertAlmostEqual(value, target, delta=1e-7)
def test_parameterized_qobj_qasm_snapshot_expval(self): """Test parameterized qobj with Expectation Value snapshot and qasm simulator.""" shots = 1000 labels = snapshot_expval_labels() * 3 counts_targets = snapshot_expval_counts(shots) * 3 value_targets = snapshot_expval_pre_meas_values() * 3 backend = QasmSimulator() qobj = self.parameterized_qobj(backend=backend, shots=1000, measure=True, snapshot=True) self.assertIn('parameterizations', qobj.to_dict()['config']) job = backend.run(qobj, self.BACKEND_OPTS) result = job.result() success = getattr(result, 'success', False) num_circs = len(result.to_dict()['results']) self.assertTrue(success) self.compare_counts(result, range(num_circs), counts_targets, delta=0.1 * shots) # Check snapshots for j in range(num_circs): data = result.data(j) all_snapshots = self.expval_snapshots(data, labels) for label in labels: snaps = all_snapshots.get(label, {}) self.assertTrue(len(snaps), 1) for memory, value in snaps.items(): target = value_targets[j].get(label, {}).get(memory, {}) self.assertAlmostEqual(value, target, delta=1e-7)