def update(
        cls,
        calibrations: Calibrations,
        exp_data: ExperimentData,
        parameter: str,
        schedule: Union[ScheduleBlock, str],
        result_index: Optional[int] = -1,
        group: str = "default",
        target_angle: float = np.pi,
        **options,
    ):
        """Update the value of a drag parameter measured by the FineDrag experiment.

        Args:
            calibrations: The calibrations to update.
            exp_data: The experiment data from which to update.
            parameter: The name of the parameter in the calibrations to update.
            schedule: The ScheduleBlock instance or the name of the instance to which the parameter
                is attached.
            result_index: The result index to use. By default search entry by name.
            group: The calibrations group to update. Defaults to "default."
            target_angle: The target rotation angle of the pulse.
            options: Trailing options.

        Raises:
            CalibrationError: If we cannot get the pulse's standard deviation from the schedule.
        """
        qubits = exp_data.metadata["physical_qubits"]

        if isinstance(schedule, str):
            schedule = calibrations.get_schedule(schedule, qubits)

        # Obtain sigma as it is needed for the fine DRAG update rule.
        sigma = None
        for block in schedule.blocks:
            if isinstance(block, Play) and hasattr(block.pulse, "sigma"):
                sigma = getattr(block.pulse, "sigma")

        if sigma is None:
            raise CalibrationError(f"Could not infer sigma from {schedule}.")

        d_theta = BaseUpdater.get_value(exp_data, "d_theta", result_index)

        # See the documentation in fine_drag.py for the derivation of this rule.
        d_beta = -np.sqrt(np.pi) * d_theta * sigma / target_angle**2

        old_beta = calibrations.get_parameter_value(parameter,
                                                    qubits,
                                                    schedule,
                                                    group=group)
        new_beta = old_beta + d_beta

        cls.add_parameter_value(calibrations, exp_data, new_beta, parameter,
                                schedule, group)
Ejemplo n.º 2
0
class TestAmplitudeUpdate(QiskitTestCase):
    """Test the update functions in the update library."""
    def setUp(self):
        """Setup amplitude values."""
        super().setUp()
        self.cals = Calibrations()
        self.qubit = 1

        axp = Parameter("amp")
        chan = Parameter("ch0")
        with pulse.build(name="xp") as xp:
            pulse.play(pulse.Gaussian(duration=160, amp=axp, sigma=40),
                       pulse.DriveChannel(chan))

        ax90p = Parameter("amp")
        with pulse.build(name="x90p") as x90p:
            pulse.play(pulse.Gaussian(duration=160, amp=ax90p, sigma=40),
                       pulse.DriveChannel(chan))

        self.x90p = x90p

        self.cals.add_schedule(xp, num_qubits=1)
        self.cals.add_schedule(x90p, num_qubits=1)
        self.cals.add_parameter_value(0.2, "amp", self.qubit, "xp")
        self.cals.add_parameter_value(0.1, "amp", self.qubit, "x90p")

    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)