Quanlse/Calibration/TwoQubit.py
def czCaliDynamicalPhase(sche: Scheduler, q0: int, q1: int, runner: Callable = None, method: str = 'Nelder-Mead',
                         maxIter: int = 50, q0VZPhaseInit: float = None, q1VZPhaseInit: float = None,
                         options: Any = None):
    """
    Run the dynamical phase calibration procedure for the controlled-Z gates.

    :param sche: the Scheduler instance
    :param q0: the index of the first qubit to be calibrated
    :param q1: the index of the second qubit to be calibrated
    :param runner: a callable function which can pass the QJob/QJobList instances and obtain the result
    :param method: the optimization method, can be chose from 'dual_annealing' or 'Nelder-Mead'
    :param maxIter: the maximum iteration
    :param q0VZPhaseInit: the initial value of the amplitude of Z pulse on the first qubit
    :param q1VZPhaseInit: the initial value of the amplitude of Z pulse on the second qubit
    :param options: options pass to the runner function
    """

    def _dynamicalPhaseCaliLoss(x):
        """
        Obtain the calibration pulse jobs and calculate the loss function.
        """

        # Generate the pulse jobs for calibration
        caliJob = czCaliDynamicalPhaseJob(sche, q0, q1, x[0], x[1])

        # Simulate the calibration pulse
        if isinstance(sche, PulseModel):
            results = sche.simulate(job=caliJob, options=options)
        else:
            if runner is None or not callable(runner):
                raise ArgumentError("Please input a callable runner!")
            results = runner(job=caliJob, options=options)

        # Obtain the output population value
        pop = project(numpy.square(abs(results[0]["state"])).T[0], sche.subSysNum, sche.sysLevel, 2)

        # Obtain the ideal population
        uIdeal = sche.getMatrix()
        stateIdeal = uIdeal @ basis(uIdeal.shape[0], 0).T[0]
        popIdeal = numpy.square(abs(stateIdeal))

        # Calculate the loss function
        loss = numpy.sum(numpy.abs(pop - popIdeal)) / len(pop)
        return loss

    if method == 'dual_annealing':
        from scipy.optimize import dual_annealing
        opt = dual_annealing(_dynamicalPhaseCaliLoss, [(-2 * pi, 2 * pi), (-2 * pi, 2 * pi)], maxiter=maxIter)
    elif method == 'Nelder-Mead':
        from scipy.optimize import minimize
        if q0VZPhaseInit is None or q1VZPhaseInit is None:
            raise ArgumentError(f"You must specify 'q0VZPhaseInit' and 'q1VZPhaseInit' when using '{method}'!")
        opt = minimize(_dynamicalPhaseCaliLoss, array([q0VZPhaseInit, q1VZPhaseInit]), method=method)
    else:
        raise ArgumentError(f"Unsupported optimization method '{method}'!")

    return opt["x"][0], opt["x"][1], opt["fun"]

    Run the dynamical phase calibration procedure for the controlled-Z gates.

    :param sche: the Scheduler instance
    :param q0: the index of the first qubit to be calibrated
    :param q1: the index of the second qubit to be calibrated
    :param runner: a callable function which can pass the QJob/QJobList instances and obtain the result
    :param method: the optimization method, can be chose from 'dual_annealing' or 'Nelder-Mead'
    :param maxIter: the maximum iteration
    :param q0VZPhaseInit: the initial value of the amplitude of Z pulse on the first qubit
    :param q1VZPhaseInit: the initial value of the amplitude of Z pulse on the second qubit
    :param options: options pass to the runner function
    
Run the dynamical phase calibration procedure for the controlled-Z gates.
Run the dynamical phase calibration procedure for the controlled - Z gates .
def czCaliDynamicalPhase(sche: Scheduler, q0: int, q1: int, runner: Callable = None, method: str = 'Nelder-Mead',
                         maxIter: int = 50, q0VZPhaseInit: float = None, q1VZPhaseInit: float = None,
                         options: Any = None):
    def _dynamicalPhaseCaliLoss(x):
        caliJob = czCaliDynamicalPhaseJob(sche, q0, q1, x[0], x[1])
        if isinstance(sche, PulseModel):
            results = sche.simulate(job=caliJob, options=options)
        else:
            if runner is None or not callable(runner):
                raise ArgumentError("Please input a callable runner!")
            results = runner(job=caliJob, options=options)
        pop = project(numpy.square(abs(results[0]["state"])).T[0], sche.subSysNum, sche.sysLevel, 2)
        uIdeal = sche.getMatrix()
        stateIdeal = uIdeal @ basis(uIdeal.shape[0], 0).T[0]
        popIdeal = numpy.square(abs(stateIdeal))
        loss = numpy.sum(numpy.abs(pop - popIdeal)) / len(pop)
        return loss
    if method == 'dual_annealing':
        from scipy.optimize import dual_annealing
        opt = dual_annealing(_dynamicalPhaseCaliLoss, [(-2 * pi, 2 * pi), (-2 * pi, 2 * pi)], maxiter=maxIter)
    elif method == 'Nelder-Mead':
        from scipy.optimize import minimize
        if q0VZPhaseInit is None or q1VZPhaseInit is None:
            raise ArgumentError(f"You must specify 'q0VZPhaseInit' and 'q1VZPhaseInit' when using '{method}'!")
        opt = minimize(_dynamicalPhaseCaliLoss, array([q0VZPhaseInit, q1VZPhaseInit]), method=method)
    else:
        raise ArgumentError(f"Unsupported optimization method '{method}'!")
    return opt["x"][0], opt["x"][1], opt["fun"]
def czCaliDynamicalPhase ( sche : Scheduler , q0 : int , q1 : int , runner : Callable = None , method : str = 'Nelder-Mead' , maxIter : int = 50 , q0VZPhaseInit : float = None , q1VZPhaseInit : float = None , options : Any = None ) : def _dynamicalPhaseCaliLoss ( x ) : """
        Obtain the calibration pulse jobs and calculate the loss function.
        """ caliJob = czCaliDynamicalPhaseJob ( sche , q0 , q1 , x [ 0 ] , x [ 1 ] ) if isinstance ( sche , PulseModel ) : results = sche . simulate ( job = caliJob , options = options ) else : if runner is None or not callable ( runner ) : raise ArgumentError ( "Please input a callable runner!" ) results = runner ( job = caliJob , options = options ) pop = project ( numpy . square ( abs ( results [ 0 ] [ "state" ] ) ) . T [ 0 ] , sche . subSysNum , sche . sysLevel , 2 ) uIdeal = sche . getMatrix ( ) stateIdeal = uIdeal @ basis ( uIdeal . shape [ 0 ] , 0 ) . T [ 0 ] popIdeal = numpy . square ( abs ( stateIdeal ) ) loss = numpy . sum ( numpy . abs ( pop - popIdeal ) ) / len ( pop ) return loss if method == 'dual_annealing' : from scipy . optimize import dual_annealing opt = dual_annealing ( _dynamicalPhaseCaliLoss , [ ( - 2 * pi , 2 * pi ) , ( - 2 * pi , 2 * pi ) ] , maxiter = maxIter ) elif method == 'Nelder-Mead' : from scipy . optimize import minimize if q0VZPhaseInit is None or q1VZPhaseInit is None : raise ArgumentError ( f"You must specify 'q0VZPhaseInit' and 'q1VZPhaseInit' when using '{method}'!" ) opt = minimize ( _dynamicalPhaseCaliLoss , array ( [ q0VZPhaseInit , q1VZPhaseInit ] ) , method = method ) else : raise ArgumentError ( f"Unsupported optimization method '{method}'!" ) return opt [ "x" ] [ 0 ] , opt [ "x" ] [ 1 ] , opt [ "fun" ]
Run the dynamical phase calibration procedure for the controlled-Z gates.
Run the dynamical phase calibration procedure for the controlled - Z gates .
"""
    Run the dynamical phase calibration procedure for the controlled-Z gates.

    :param sche: the Scheduler instance
    :param q0: the index of the first qubit to be calibrated
    :param q1: the index of the second qubit to be calibrated
    :param runner: a callable function which can pass the QJob/QJobList instances and obtain the result
    :param method: the optimization method, can be chose from 'dual_annealing' or 'Nelder-Mead'
    :param maxIter: the maximum iteration
    :param q0VZPhaseInit: the initial value of the amplitude of Z pulse on the first qubit
    :param q1VZPhaseInit: the initial value of the amplitude of Z pulse on the second qubit
    :param options: options pass to the runner function
    """ """
        Obtain the calibration pulse jobs and calculate the loss function.
        """ # Generate the pulse jobs for calibration # Simulate the calibration pulse # Obtain the output population value # Obtain the ideal population # Calculate the loss function
sche
Scheduler
the Scheduler instance
the Scheduler instance
q0
int
the index of the first qubit to be calibrated
the index of the first qubit to be calibrated
q1
int
the index of the second qubit to be calibrated
the index of the second qubit to be calibrated
runner
Callable
a callable function which can pass the QJob/QJobList instances and obtain the result
a callable function which can pass the QJob / QJobList instances and obtain the result
method
str
the optimization method, can be chose from 'dual_annealing' or 'Nelder-Mead'
the optimization method can be chose from ' dual_annealing ' or ' Nelder - Mead '
maxIter
int
the maximum iteration
the maximum iteration
q0VZPhaseInit
float
the initial value of the amplitude of Z pulse on the first qubit
the initial value of the amplitude of Z pulse on the first qubit
q1VZPhaseInit
float
the initial value of the amplitude of Z pulse on the second qubit
the initial value of the amplitude of Z pulse on the second qubit
options
Any
options pass to the runner function
options pass to the runner function