def test_to_real(self): """Test scaling and conversion to real part.""" processor = DataProcessor("memory", [ToReal(scale=1e-3)]) exp_data = ExperimentData(FakeExperiment()) exp_data.add_data(self.result_lvl1) new_data = processor(exp_data.data[0]) expected_old = { "memory": [ [[1103260.0, -11378508.0], [2959012.0, -16488753.0]], [[442170.0, -19283206.0], [-5279410.0, -15339630.0]], [[3016514.0, -14548009.0], [-3404756.0, -16743348.0]], ], "metadata": { "experiment_type": "fake_test_experiment", "x_values": 0.0 }, } expected_new = np.array([[1103.26, 2959.012], [442.17, -5279.41], [3016.514, -3404.7560]]) self.assertEqual(exp_data.data[0], expected_old) self.assertTrue(np.allclose(new_data, expected_new)) # Test that we can call with history. new_data, history = processor.call_with_history(exp_data.data[0]) self.assertEqual(exp_data.data[0], expected_old) self.assertTrue(np.allclose(new_data, expected_new)) self.assertEqual(history[0][0], "ToReal") self.assertTrue(np.allclose(history[0][1], expected_new))
def test_to_real(self): """Test scaling and conversion to real part.""" processor = DataProcessor("memory", [ToReal(scale=1e-3)]) exp_data = ExperimentData(FakeExperiment()) exp_data.add_data(self.result_lvl1) # Test to real on a single datum new_data = processor(exp_data.data(0)) expected_old = { "memory": [ [[1103260.0, -11378508.0], [2959012.0, -16488753.0]], [[442170.0, -19283206.0], [-5279410.0, -15339630.0]], [[3016514.0, -14548009.0], [-3404756.0, -16743348.0]], ], "metadata": {"experiment_type": "fake_test_experiment"}, "job_id": "job-123", "meas_level": 1, "shots": 3, } expected_new = np.array([[1103.26, 2959.012], [442.17, -5279.41], [3016.514, -3404.7560]]) self.assertEqual(exp_data.data(0), expected_old) np.testing.assert_array_almost_equal( unp.nominal_values(new_data), expected_new, ) self.assertTrue(np.isnan(unp.std_devs(new_data)).all()) # Test that we can call with history. new_data, history = processor.call_with_history(exp_data.data(0)) self.assertEqual(exp_data.data(0), expected_old) np.testing.assert_array_almost_equal( unp.nominal_values(new_data), expected_new, ) self.assertEqual(history[0][0], "ToReal") np.testing.assert_array_almost_equal( unp.nominal_values(history[0][1]), expected_new, ) # Test to real on more than one datum new_data = processor(exp_data.data()) expected_new = np.array( [ [[1103.26, 2959.012], [442.17, -5279.41], [3016.514, -3404.7560]], [[5131.962, 4438.87], [3415.985, 2942.458], [5199.964, 4030.843]], ] ) np.testing.assert_array_almost_equal( unp.nominal_values(new_data), expected_new, )
def test_to_imag(self): """Test that we can average the data.""" processor = DataProcessor("memory") processor.append(ToImag(scale=1e-3)) exp_data = ExperimentData(FakeExperiment()) exp_data.add_data(self.result_lvl1) new_data, error = processor(exp_data.data(0)) expected_old = { "memory": [ [[1103260.0, -11378508.0], [2959012.0, -16488753.0]], [[442170.0, -19283206.0], [-5279410.0, -15339630.0]], [[3016514.0, -14548009.0], [-3404756.0, -16743348.0]], ], "metadata": { "experiment_type": "fake_test_experiment" }, "job_id": "job-123", "meas_level": 1, "shots": 3, } expected_new = np.array([ [-11378.508, -16488.753], [-19283.206000000002, -15339.630000000001], [-14548.009, -16743.348], ]) self.assertEqual(exp_data.data(0), expected_old) self.assertTrue(np.allclose(new_data, expected_new)) self.assertIsNone(error) # Test that we can call with history. new_data, error, history = processor.call_with_history( exp_data.data(0)) self.assertEqual(exp_data.data(0), expected_old) self.assertTrue(np.allclose(new_data, expected_new)) self.assertEqual(history[0][0], "ToImag") self.assertTrue(np.allclose(history[0][1], expected_new)) # Test to imaginary on more than one datum new_data, error = processor(exp_data.data()) expected_new = np.array([ [[-11378.508, -16488.753], [-19283.206, -15339.630], [-14548.009, -16743.348]], [[-16630.257, -13752.518], [-16031.913, -15840.465], [-14955.998, -14538.923]], ]) self.assertTrue(np.allclose(new_data, expected_new))
def test_empty_processor(self): """Check that a DataProcessor without steps does nothing.""" data_processor = DataProcessor("counts") datum = data_processor(self.exp_data_lvl2.data(0)) self.assertEqual(datum, {"00": 4, "10": 6}) datum, history = data_processor.call_with_history(self.exp_data_lvl2.data(0)) self.assertEqual(datum, {"00": 4, "10": 6}) self.assertEqual(history, [])
def test_to_imag(self): """Test that we can average the data.""" processor = DataProcessor("memory") processor.append(ToImag(scale=1e-3)) exp_data = ExperimentData(FakeExperiment()) exp_data.add_data(self.result_lvl1) new_data = processor(exp_data.data[0]) expected_old = { "memory": [ [[1103260.0, -11378508.0], [2959012.0, -16488753.0]], [[442170.0, -19283206.0], [-5279410.0, -15339630.0]], [[3016514.0, -14548009.0], [-3404756.0, -16743348.0]], ], "metadata": { "experiment_type": "fake_test_experiment", "x_values": 0.0 }, } expected_new = np.array([ [-11378.508, -16488.753], [-19283.206000000002, -15339.630000000001], [-14548.009, -16743.348], ]) self.assertEqual(exp_data.data[0], expected_old) self.assertTrue(np.allclose(new_data, expected_new)) # Test that we can call with history. new_data, history = processor.call_with_history(exp_data.data[0]) self.assertEqual(exp_data.data[0], expected_old) self.assertTrue(np.allclose(new_data, expected_new)) self.assertEqual(history[0][0], "ToImag") self.assertTrue(np.allclose(history[0][1], expected_new))