def filter_and_discriminate(self): """ Test the process of discriminating and then applying the discriminator using a filter. """ i0, q0, i1, q1 = 0., -1., 0., 1. discriminator = LinearIQDiscriminator(self.cal_results, self.qubits, ['00', '11']) iq_filter = DiscriminationFilter(discriminator) new_result = iq_filter.apply(self.cal_results) for nr in new_result.results: self.assertEqual(nr.meas_level, 2) for state in new_result.results[0].data.counts.to_dict(): self.assertEqual(state, '0x0') for state in new_result.results[1].data.counts.to_dict(): self.assertEqual(state, '0x3') self.assertEqual(len(new_result.get_memory(0)), self.shots) self.qubits = [0] discriminator = LinearIQDiscriminator(self.cal_results, self.qubits, ['0', '1']) self.assertEqual(discriminator.discriminate([[i0, q0]])[0], '0') self.assertEqual(discriminator.discriminate([[i1, q1]])[0], '1')
def test_get_base(self): """ Test the get_base method to see if it can properly identify the number of basis states per quantum element. E.g. second excited level of transmon. """ expected_states = {'a': '02', 'b': '01', 'c': '00', 'd': '11'} base = DiscriminationFilter.get_base(expected_states) self.assertEqual(base, 3)
def test_apply(self): """ Set-up a discriminator based on simulated data, train it and then discriminate the calibration data. """ meas_cal, _ = circuits.tensored_meas_cal([[0], [1]]) backend = Aer.get_backend('qasm_simulator') job = qiskit.execute(meas_cal, backend=backend, shots=self.shots, meas_level=1) cal_results = job.result() i0, q0, i1, q1 = 0., -1., 0., 1. ground = utils.create_shots(i0, q0, 0.1, 0.1, self.shots, self.qubits) excited = utils.create_shots(i1, q1, 0.1, 0.1, self.shots, self.qubits) cal_results.results[0].meas_level = 1 cal_results.results[1].meas_level = 1 cal_results.results[0].data = ExperimentResultData(memory=ground) cal_results.results[1].data = ExperimentResultData(memory=excited) discriminator = LinearIQDiscriminator(cal_results, self.qubits, ['00', '11']) d_filter = DiscriminationFilter(discriminator) self.assertEqual(cal_results.results[0].meas_level, 1) new_results = d_filter.apply(cal_results) self.assertEqual(new_results.results[0].meas_level, 2) counts_00 = new_results.results[0].data.counts.to_dict()['0x0'] counts_11 = new_results.results[1].data.counts.to_dict()['0x3'] self.assertEqual(counts_00, self.shots) self.assertEqual(counts_11, self.shots)
def test_apply(self): """ Set-up a discriminator based on simulated data, train it and then discriminate the calibration data. """ result_pkl = os.path.join(os.path.dirname(__file__), 'test_result.pkl') with open(result_pkl, 'rb') as handle: result = Result.from_dict(pickle.load(handle)) discriminator = LinearIQDiscriminator(result, [0, 1]) d_filter = DiscriminationFilter(discriminator) self.assertEqual(result.results[0].meas_level, 1) new_results = d_filter.apply(result) self.assertEqual(new_results.results[0].meas_level, 2) for idx in range(3): counts_00 = new_results.results[idx].data.counts.to_dict()['0x0'] counts_11 = new_results.results[idx].data.counts.to_dict()['0x3'] self.assertEqual(counts_00 + counts_11, 512)
def test_count(self): """ Test to see if the filter properly converts the result of discriminator.discriminate to a dictionary of counts. """ fitter = LinearIQDiscriminator([], [], []) d_filter = DiscriminationFilter(fitter, 2) raw_counts = d_filter.count(['01', '00', '01', '00', '00', '10']) self.assertEqual(raw_counts['0x0'], 3) self.assertEqual(raw_counts['0x1'], 2) self.assertEqual(raw_counts['0x2'], 1) self.assertRaises(KeyError, getitem, raw_counts, '0x3') d_filter = DiscriminationFilter(fitter, 3) raw_counts = d_filter.count(['02', '02', '20', '21', '21', '02']) self.assertEqual(raw_counts['0x2'], 3) self.assertEqual(raw_counts['0x6'], 1) self.assertEqual(raw_counts['0x7'], 2)
def test_apply(self): """ Set-up a discriminator based on simulated data, train it and then discriminate the calibration data. """ cal_00_data = [[[1., 1.]], [[1.1, 0.9]], [[0.9, 1.1]], [[1., 1.1]], [[0.9, 1.2]]] cal_11_data = [[[-1., -1.]], [[-1.1, -0.9]], [[-0.9, -1.1]], [[-0.9, -1.2]], [[-1., -1.1]]] x90p_data = [[[-1.1, -1.]], [[1.1, 0.9]], [[-0.8, -1.0]], [[0.9, 1.1]], [[1., 1.]]] result = Result.from_dict({ 'job_id': '', 'backend_version': '1.3.0', 'backend_name': 'test', 'qobj_id': '', 'success': True, 'results': [{ "header": { "name": "cal_0" }, "shots": 5, "status": "DONE", 'meas_level': 1, "success": True, "meas_return": "single", "data": { "memory": cal_00_data } }, { "header": { "name": "cal_1" }, "shots": 5, "status": "DONE", 'meas_level': 1, "success": True, "meas_return": "single", "data": { "memory": cal_11_data } }, { "header": { "name": "x90p" }, "shots": 5, "status": "DONE", 'meas_level': 1, "success": True, "meas_return": "single", "data": { "memory": x90p_data } }] }) discriminator = LinearIQDiscriminator(result, [0]) d_filter = DiscriminationFilter(discriminator) self.assertEqual(result.results[0].meas_level, 1) new_results = d_filter.apply(result) self.assertEqual(new_results.results[0].meas_level, 2) for name in ['cal_0', 'cal_1', 'x90p']: counts = new_results.get_counts(name) counts_0 = counts.get('0', 0) counts_1 = counts.get('1', 0) self.assertEqual(counts_0 + counts_1, 5) if name == 'x90p': self.assertEqual(counts_0, 3) self.assertEqual(counts_1, 2)