def test_calibrations(self): """Test that the calibrations are preserved and that the circuit transpiles.""" experiments = [] for qubit in range(3): rabi = Rabi(qubit) rabi.set_experiment_options(amplitudes=[0.5]) experiments.append(rabi) par_exp = ParallelExperiment(experiments) par_circ = par_exp.circuits()[0] # If the calibrations are not there we will not be able to transpile try: transpile(par_circ, basis_gates=["rz", "sx", "x", "cx"]) except QiskitError as error: self.fail("Failed to transpile with error: " + str(error)) # Assert that the calibration keys are in the calibrations of the composite circuit. for qubit in range(3): rabi_circuit = experiments[qubit].circuits()[0] cal_key = next(iter(rabi_circuit.calibrations["Rabi"].keys())) self.assertEqual(cal_key[0], (qubit, )) self.assertTrue(cal_key in par_circ.calibrations["Rabi"])
def test_default_schedule(self): """Test the default schedule.""" rabi = Rabi(2) rabi.set_experiment_options(amplitudes=[0.5]) circs = rabi.circuits(RabiBackend()) with pulse.build() as expected: pulse.play(pulse.Gaussian(160, 0.5, 40), pulse.DriveChannel(2)) self.assertEqual(circs[0].calibrations["Rabi"][((2, ), (0.5, ))], expected) self.assertEqual(len(circs), 1)
def test_wrong_processor(self): """Test that we can override the data processing by giving a faulty data processor.""" backend = MockIQBackend(RabiHelper()) rabi = Rabi(self.qubit, self.sched) fail_key = "fail_key" rabi.analysis.set_options(data_processor=DataProcessor(fail_key, [])) # pylint: disable=no-member rabi.set_run_options(shots=2) data = rabi.run(backend) result = data.analysis_results() self.assertEqual(data.status(), ExperimentStatus.ERROR) self.assertEqual(len(result), 0)
def test_wrong_processor(self): """Test that we can override the data processing by giving a faulty data processor.""" backend = RabiBackend() rabi = Rabi(self.qubit, self.sched) fail_key = "fail_key" rabi.analysis.set_options(data_processor=DataProcessor(fail_key, [])) rabi.set_run_options(shots=2) data = rabi.run(backend) result = data.analysis_results() self.assertEqual(len(result), 0)
def test_calibrations(self): """Test that the calibrations are preserved and that the circuit transpiles.""" experiments = [] for qubit in range(3): with pulse.build() as sched: pulse.play(pulse.Gaussian(160, Parameter("amp"), 40), pulse.DriveChannel(qubit)) experiments.append(Rabi(qubit, sched, amplitudes=[0.5])) par_exp = ParallelExperiment(experiments) par_circ = par_exp.circuits()[0] # If the calibrations are not there we will not be able to transpile try: transpile(par_circ, basis_gates=["rz", "sx", "x", "cx"]) except QiskitError as error: self.fail("Failed to transpile with error: " + str(error)) # Assert that the calibration keys are in the calibrations of the composite circuit. for qubit in range(3): rabi_circuit = experiments[qubit].circuits()[0] cal_key = next(iter(rabi_circuit.calibrations["Rabi"].keys())) self.assertEqual(cal_key[0], (qubit, )) self.assertTrue(cal_key in par_circ.calibrations["Rabi"])
def test_user_schedule(self): """Test the user given schedule.""" amp = Parameter("my_double_amp") with pulse.build() as my_schedule: pulse.play(pulse.Drag(160, amp, 40, 10), pulse.DriveChannel(2)) pulse.play(pulse.Drag(160, amp, 40, 10), pulse.DriveChannel(2)) rabi = Rabi(2) rabi.set_experiment_options(schedule=my_schedule, amplitudes=[0.5]) circs = rabi.circuits(RabiBackend()) assigned_sched = my_schedule.assign_parameters({amp: 0.5}, inplace=False) self.assertEqual(circs[0].calibrations["Rabi"][((2, ), (0.5, ))], assigned_sched)
def test_amplitude(self): """Test amplitude update from Rabi.""" rabi = Rabi(self.qubit) rabi.set_experiment_options(amplitudes=np.linspace(-0.95, 0.95, 21)) exp_data = rabi.run(RabiBackend()) exp_data.block_for_results() with self.assertRaises(CalibrationError): self.cals.get_schedule("xp", qubits=0) to_update = [(np.pi, "amp", "xp"), (np.pi / 2, "amp", self.x90p)] self.assertEqual(len(self.cals.parameters_table()), 2) Amplitude.update(self.cals, exp_data, angles_schedules=to_update) with self.assertRaises(CalibrationError): self.cals.get_schedule("xp", qubits=0) self.assertEqual(len(self.cals.parameters_table()["data"]), 4) # Now check the corresponding schedules result = exp_data.analysis_results(1) rate = 2 * np.pi * result.value.value amp = np.round(np.pi / rate, decimals=8) with pulse.build(name="xp") as expected: pulse.play(pulse.Gaussian(160, amp, 40), pulse.DriveChannel(self.qubit)) self.assertEqual(self.cals.get_schedule("xp", qubits=self.qubit), expected) amp = np.round(0.5 * np.pi / rate, decimals=8) with pulse.build(name="xp") as expected: pulse.play(pulse.Gaussian(160, amp, 40), pulse.DriveChannel(self.qubit)) self.assertEqual(self.cals.get_schedule("x90p", qubits=self.qubit), expected)
def test_rabi_end_to_end(self): """Test the Rabi experiment end to end.""" test_tol = 0.01 rabi_experiment_helper = RabiHelper() backend = MockIQBackend(rabi_experiment_helper) rabi = Rabi(self.qubit, self.sched) rabi.set_experiment_options(amplitudes=np.linspace(-0.95, 0.95, 21)) expdata = rabi.run(backend) self.assertExperimentDone(expdata) result = expdata.analysis_results(0) self.assertEqual(result.quality, "good") # The comparison is made against the object that exists in the backend for accurate testing self.assertAlmostEqual(result.value[1], backend.experiment_helper.rabi_rate(), delta=test_tol) # updating 'amplitude_to_angle' parameter in the experiment helper rabi_experiment_helper.amplitude_to_angle = np.pi / 2 expdata = rabi.run(backend) self.assertExperimentDone(expdata) result = expdata.analysis_results(0) self.assertEqual(result.quality, "good") self.assertAlmostEqual(result.value[1], backend.experiment_helper.rabi_rate(), delta=test_tol) # updating 'amplitude_to_angle' parameter in the experiment helper and experiment options rabi_experiment_helper.amplitude_to_angle = 2.5 * np.pi rabi.set_experiment_options(amplitudes=np.linspace(-0.95, 0.95, 101)) expdata = rabi.run(backend) self.assertExperimentDone(expdata) result = expdata.analysis_results(0) self.assertEqual(result.quality, "good") self.assertAlmostEqual(result.value[1], backend.experiment_helper.rabi_rate(), delta=test_tol)
def test_experiment_config(self): """Test converting to and from config works""" exp = Rabi(0, self.sched) loaded_exp = Rabi.from_config(exp.config()) self.assertNotEqual(exp, loaded_exp) self.assertTrue(self.json_equiv(exp, loaded_exp))
def test_rabi_end_to_end(self): """Test the Rabi experiment end to end.""" test_tol = 0.01 backend = RabiBackend() rabi = Rabi(self.qubit, self.sched) rabi.set_experiment_options(amplitudes=np.linspace(-0.95, 0.95, 21)) expdata = rabi.run(backend) result = expdata.analysis_results(0) self.assertEqual(result.quality, "good") self.assertTrue( abs(result.value.value[1] - backend.rabi_rate) < test_tol) backend = RabiBackend(amplitude_to_angle=np.pi / 2) rabi = Rabi(self.qubit, self.sched) rabi.set_experiment_options(amplitudes=np.linspace(-0.95, 0.95, 21)) expdata = rabi.run(backend) result = expdata.analysis_results(0) self.assertEqual(result.quality, "good") self.assertTrue( abs(result.value.value[1] - backend.rabi_rate) < test_tol) backend = RabiBackend(amplitude_to_angle=2.5 * np.pi) rabi = Rabi(self.qubit, self.sched) rabi.set_experiment_options(amplitudes=np.linspace(-0.95, 0.95, 101)) expdata = rabi.run(backend) result = expdata.analysis_results(0) self.assertEqual(result.quality, "good") self.assertTrue( abs(result.value.value[1] - backend.rabi_rate) < test_tol)
def test_roundtrip_serializable(self): """Test round trip JSON serialization""" exp = Rabi(0, self.sched) self.assertRoundTripSerializable(exp, self.json_equiv)