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 parameterized_qobj(backend, shots=1000, measure=True, snapshot=False): """Return ParameterizedQobj for settings.""" single_shot = shots == 1 pcirc1, param1 = snapshot_expval_circuit_parameterized( single_shot=single_shot, measure=measure, snapshot=snapshot) circuits2to4 = snapshot_expval_circuits(pauli=True, skip_measure=(not measure), single_shot=single_shot) pcirc2, param2 = snapshot_expval_circuit_parameterized( single_shot=single_shot, measure=measure, snapshot=snapshot) circuits = [pcirc1] + circuits2to4 + [pcirc2] params = [param1, [], [], [], param2] qobj = assemble(circuits, backend=backend, shots=shots, parameterizations=params) return qobj