Пример #1
0
    def _calc_angle(self, dX, dY):
        '''
        Calculate the angle between two deltas
        '''
        try:
            th = 0.0
            radius = self._calc_radius(dX, dY)

            if (dY < 0.0):
                if (dX == 0.0):
                    th = pi / 2.0
                else:
                    th = arccos(-dX / radius)
            elif (dY > 0.0):
                if (dX == 0.0):
                    th = 3.0 / 2.0 * pi
                else:
                    th = 2.0 * pi - arccos(-dX / radius)
            elif (dY == 0.0):
                if (dX > 0.0):
                    th = 0.0
                else:
                    th = pi
        except:
            traceError('failed to calculate angle')

        return th
Пример #2
0
def getAngle(coord0, coord1, coord2, is_angular=True):
    """
    getAngle calculates the angle from the coordinate of three atoms

    Parameters
    ----------
    coord0 : list or np.ndarray
        coordinate of atom 0
    coord1 : list or np.ndarray
        coordinate of atom 1
    coord2 : list or np.ndarray
        coordinate of atom 2
    is_angular : bool, optional
        flag to wether return a angular result, by default True

    Returns
    -------
    float
        angle
    """
    coord0 = convertToNdArray(coord0)
    coord1 = convertToNdArray(coord1)
    coord2 = convertToNdArray(coord2)

    v0 = coord0 - coord1
    v1 = coord2 - coord1

    cos_phi = np.dot(v0, v1) / (np.linalg.norm(v0) * np.linalg.norm(v1))

    if is_angular:
        return arccos(cos_phi)
    else:
        return arccos(cos_phi) / np.pi * 180
Пример #3
0
def getTorsion(coord0, coord1, coord2, coord3, is_angular=True):
    """
    getTorsion calculates the torsion from the coordinate of four atoms

    Method details: 
        - https://zh.wikipedia.org/wiki/%E4%BA%8C%E9%9D%A2%E8%A7%92, 
        - https://stackoverflow.com/questions/46978451/how-to-know-if-a-dihedral-angle-is-or

    Parameters
    ----------
    coord0 : list or np.ndarray
        coordinate of atom 0
    coord1 : list or np.ndarray
        coordinate of atom 1
    coord2 : list or np.ndarray
        coordinate of atom 2
    coord3 : list or np.ndarray
        coordinate of atom 3
    is_angular : bool, optional
        flag to wether return a angular result, by default True

    Returns
    -------
    float
        torsion angle
    """
    coord0 = convertToNdArray(coord0)
    coord1 = convertToNdArray(coord1)
    coord2 = convertToNdArray(coord2)
    coord3 = convertToNdArray(coord3)

    v0 = coord0 - coord1
    v1 = coord2 - coord1
    v2 = coord3 - coord2

    # Calculate the vertical vector of each plane
    # Note the order of cross product
    na = np.cross(v1, v0)
    nb = np.cross(v1, v2)

    # Note that we delete the absolute value
    cos_phi = np.dot(na, nb) / (np.linalg.norm(na) * np.linalg.norm(nb))

    # Sign of angle
    omega = np.dot(v0, np.cross(v1, v2))
    sign = omega / np.abs(omega)

    if is_angular:
        return sign * arccos(cos_phi)
    else:
        return sign * arccos(cos_phi) / np.pi * 180
def crystal_embed(e,eta):
    """
    Evaluates crystal embedding potential by solving the one-dimensional
    Schroedinger equation through one unit cell in both directions.
    """
    y0=[1.0,0.0,0.0,0.0]
    z1=np.linspace(-z_left,-z_left+alat,101)
    sol1=odeint(schroed,y0,z1,args=(e,eta))
    z2=np.linspace(-z_left+alat,-z_left,101)
    sol2=odeint(schroed,y0,z2,args=(e,eta))
    psi1=complex(sol1[50,0],sol1[50,1])
    psi1_prime=complex(sol1[50,2],sol1[50,3])
    psi2=complex(sol2[50,0],sol2[50,1])
    psi2_prime=complex(sol2[50,2],sol2[50,3])
    wronskian=psi1*psi2_prime-psi2*psi1_prime
    psi1=complex(sol1[100,0],sol1[100,1])
    psi2=complex(sol2[100,0],sol2[100,1])
    cos_ka=0.5*(psi1+psi2)
    ka=arccos(cos_ka)
    if ka.imag<0: ka=np.conj(ka)
    exp_ka=cos_ka+1.0j*sqrt(1.0-cos_ka**2)
    emb_cryst=0.5*wronskian/(exp_ka-psi2)
    if emb_cryst.imag>0.0:
        exp_ka=cos_ka-1.0j*sqrt(1.0-cos_ka**2)
        emb_cryst=0.5*wronskian/(exp_ka-psi2)
    return emb_cryst
Пример #5
0
    def fromLoop(cls, loop):
        """Returns a Model representing the loop"""
        #get necessary vectors
        offset_v = [loop.r_anchor[0].__dict__[c] - loop.l_anchor[0].__dict__[c] for c in 'xyz']
        sse0_v = Model.__get_sse_vector(loop.l_anchor, loop.atoms[0])
        sse1_v = Model.__get_sse_vector(loop.r_anchor, loop.atoms[-1]) 

        sFrame = TransformFrame.createFromVectors(loop.l_anchor[0], transform.Vec.from_array(offset_v), transform.Vec.from_array(sse0_v))
        
        #Theta and phi are the angles between the SSE and anchor-anchor vector
        theta = arccos(dot(sse0_v, negative(offset_v)) / (norm(sse0_v) * norm(offset_v)))
        phi = arccos(dot(sse1_v, offset_v) / (norm(sse1_v) * norm(offset_v)))
        
        #Length of the vectorn
        anchor_dist = norm(offset_v)
        
        return Model([loop], [Vec.from_array(sFrame.transformInto(atom)) for atom in loop.atoms], theta, phi, anchor_dist, [loop.l_type, loop.r_type], Model.__gen_seq([loop.seq]) , 1)
def initial_guess(f='../../data/m_hist.csv'):
    # Need more than theta_0 and theta_dot ?
    guessed_len, guessed_pivot, m_pos_hist = find_pivot(f)
    rel_pos_hist = m_pos_hist - guessed_pivot
    guessed_theta = arccos(rel_pos_hist[:, 0], rel_pos_hist[:, 1])
    guessed_theta_dot = guessed_theta[1] - guessed_theta[:-2]
    guessed_restitution = 1
    guessed_radius = 50
    # guessed_radius = 20 ball_tracking.radius TODO: implement this - but no one cares
    guessed_m = 50  # TODO : smarter guess than this
    return SimplePendulum(guessed_m, guessed_len, guessed_radius,
                          guessed_pivot, guessed_theta[0],
                          guessed_theta_dot[0], guessed_restitution)
Пример #7
0
def initial_guess(f='../../data/m_hist.csv'):
    # Take in only one period of data
    # f must contain predictions from object recognition model
    guessed_len, guessed_pivot, m_pos_hist = find_pivot(f)
    rel_pos_hist = m_pos_hist - guessed_pivot
    guessed_theta = arccos(rel_pos_hist[:, 0], rel_pos_hist[:, 1])
    guessed_theta_dot = guessed_theta[1] - guessed_theta[:-2]
    guessed_restitution = 1
    guessed_radius = 50
    # guessed_radius = 20 ball_tracking.radius TODO: implement this - but no one cares
    guessed_m = 50 # TODO : smarter guess than this
    return np.array(guessed_m, guessed_len, guessed_radius, guessed_pivot, guessed_theta[0], guessed_theta_dot[0],
                    guessed_restitution)
Пример #8
0
    def recalculateEquatorialPosition(self):
        Lat = self._observer.lat
        Alt = self._fixedBody.alt
        Az = self._fixedBody.az
#        Ra = self._fixedBody.ra
#        print("ALT-AZ", self._fixedBody.alt, self._fixedBody.az, "LST",self._observer.getLST())
        Dec = arcsin(sin(Alt) * sin(Lat) + cos(Alt) * cos(Lat) * cos(Az))
        HA = arccos((sin(Alt) - sin(Dec)*sin(Lat)) / (cos(Dec)*cos(Lat)))
#        HA2 = arcsin(-sin(Az)*cos(Alt)/cos(Dec))
        Ra = self._observer.getLST() -HA
#        print('Calculated','HA',getHours(HA),'RA-DEC',getCoordinates(Ra,Dec))
#        print('Original', 'HA', self.getCurrentHA(), 'RA-DEC',self._fixedBody.ra, self._fixedBody.dec)
#        getHours(self._observer.long)
        return getCoordinates(Ra,Dec)
Пример #9
0
def match_oneway(features_1, features_2):
    """One way descriptor matching image f1 to f2, adapted from Solem"""
    f1 = features_1
    f2 = features_2

    ratio = 0.6
    size = f1.shape[0]

    scores = np.zeros((size, 1), 'int')

    for i in range(size):
        product = 0.9999 * np.dot(f1[i, :], f2)
        cosines = npmath.arccos(product)
        index = np.argsort(cosines)

        if cosines[index[0]] < ratio * cosines[index[1]]:
            scores[i] = int(index[0])

    return scores
Пример #10
0
def qubit_vs_toffoli(lam,
                     dE,
                     eps,
                     n,
                     chi,
                     beta,
                     M,
                     algorithm='half',
                     verbose=False):
    """
    Args:
        lam (float) - the lambda-value for the Hamiltonian
        dE (float) - allowable error in phase estimation. usually 0.001
        eps (float) - allowable error for synthesis (dE/(10 * lam)) usually
        n (int) - number of spin orbitals.
        chi (int) - number of bits of precision for state prep
        beta (int) - number of bits of precision for rotations
        M (int) - THC rank or r_{Thc}
        algorithm (str) - 'half', where half of the phasing angles are loaded
                                  at a time
                          'full', where angles loaded from QROM to perform
                                  phasing operations are loaded at the same time
                          Note: In arXiv:2011.03494,
                              'half' corresponds to Fig 11, while
                              'full' corresponds to Fig 12.
        verbose (bool) - do additional printing of intermediates?

    """
    # only valid algorithms accepted
    assert algorithm in ['half', 'full']

    # The number of iterations for the phase estimation.
    iters = np.ceil(pi * lam / (dE * 2))
    # The number of bits used for each register.
    nM = np.ceil(np.log2(M + 1))
    #This is number of distinct items of data we need to output, see Eq. (28).
    d = M * (M + 1) / 2 + n / 2
    # The number of bits used for the contiguous register.
    nc = np.ceil(np.log2(d))
    # The output size is 2*Log[M] for the alt values, χ for the keep value,
    # and 2 for the two sign bits.
    m = 2 * nM + 2 + chi

    # The next block of code finds the optimal number of bits to use for the
    # rotation angle for the amplitude amplification taking into account the
    # probability of failure and the cost of the rotations.
    oh = np.zeros(20, dtype=float)
    for p in range(1, 20 + 1):
        cos_term = arccos(np.power(2, nM) / np.sqrt(d) / 2)
        # print(cos_term)
        v = np.round(np.power(2, p) / (2 * pi) * cos_term)
        asin_term = arcsin(
            np.cos(v * 2 * pi / np.power(2, p)) * np.sqrt(d) / np.power(2, nM))
        sin_term = np.sin(3 * asin_term)**2
        oh[p - 1] = (20_000 * (1 / sin_term - 1) + 4 * p).real
    # br is the number of bits used in the rotation.
    br = np.argmin(oh) + 1
    # Next are the costs for the state preparation.
    cp1 = 2 * (10 * nM + 2 * br - 9)
    # There is cost 10*Log[M] for preparing the equal superposition over the
    # input registers. This is the costing from above Eq. (29).
    # This is the cost of computing the contiguous register and inverting it.
    # This is with a sophisticated scheme adding together triplets of bits.
    # This is the cost of step 2 in the list on page 14.
    cp2 = 2 * (nM**2 + nM - 1)
    # This is the cost of the QROM for the state preparation and its inverse.
    cp3 = QR(d, m)[1] + QI(d)[1]
    # The cost for the inequality test.
    cp4 = 2 * chi
    # The cost 2*nM for the controlled swaps.
    cp5 = 4 * nM
    # Then there is a cost of nM+1 for swapping the μ and ν registers, where
    # the +3 is because we need to control on two registers, and
    # control swap of the spin registers.
    cp6 = 2 * nM + 3
    #  The total cost in Eq. (33).
    CPCP = cp1 + cp2 + cp3 + cp4 + cp5 + cp6

    # Next are the costs for the select operation.
    # This is the cost of swapping based on the spin register. These costs are
    # from the list on page 15, and this is steps 1 and 7.
    cs1 = 2 * n
    k1 = 2**QI(M + n / 2)[0]
    cs2a = M + n / 2 - 2 + np.ceil(M / k1) + np.ceil(n / 2 / k1) + k1

    # The QROM for the rotation angles the first time.  Here M+n/2-2 is the cost
    # for generating them, and the second part is the cost for inverting them
    # with advanced QROM. The QROM for the rotation angles the second time.
    # Here the cost M-2 is for generating the angles the second time, and QI[M]
    # is for inverting the QROM. Steps 2 and 6.
    cs2b = M - 2 + QI(M)[1]
    # The cost of the rotations steps 3 and 5.
    cs3 = 4 * n * (beta - 2)
    # Cost for extra part in making the Z doubly controlled step 4.
    cs4 = 1
    # The total select cost in Eq. (43).
    CS = cs1 + cs2a + cs2b + cs3 + cs4
    # The cost given slightly above Eq. (44) is 2*nM+5. That is a typo and it
    # should have the aleph (equivalent to χ here) like at the top of the
    # column. Here we have +3 in this line, +1 in the next line and +1 for cs4,
    #  to give the same total.
    costref = 2 * nM + chi + 3
    cost = CPCP + CS + costref + 1

    # Next are qubit costs.
    ac1 = 2 * np.ceil(np.log2(iters + 1)) - 1
    ac2 = n
    ac3 = 2 * nM
    ac47 = 5
    ac8 = beta
    ac9 = nc

    kt = 2**QR(d, m)[0]
    ac10 = m * kt + np.ceil(np.log2(d / kt))
    # This is for the equal superposition state to perform the inequality test
    # with the keep register.
    ac11 = chi
    # The qubit to control the swap of the μ and ν registers.
    ac12 = 1
    aca = ac1 + ac2 + ac3 + ac47 + ac8 + ac9 + ac11 + ac12
    # This is the data needed for the rotations.
    ac13 = beta * n / 2
    # These are the ancillas needed for adding into the phase gradient state.
    ac14 = beta - 2
    # These are the temporary ancillas in between erasing the first QROM
    # ancillas and inverting that QROM. The +m is for output of first QROM.
    acc = ac13 + ac14 + m

    if verbose:
        print("Total Toffoli cost ", cost * iters)
        print("Ancilla for first QROM ", aca + ac10)
        print("Actual ancilla ... ", np.max([aca + ac10, aca + acc]))
        print("Spacetime volume ", np.max([aca + ac10, aca + acc]) * cost)

    # First are the numbers of qubits that must be kept throughout the
    # computation. See page 18.
    if algorithm == 'half':
        # The qubits used as the control registers for the phase estimation,
        # that must be kept the whole way through. If we used independent
        # controls each time that would increase the Toffoli cost by
        # np.ceil(np.log2iters+1]]-3, while saving
        # np.ceil(np.log2iters+1]]-1 qubits
        ac1 = np.ceil(np.log2(iters + 1))
    elif algorithm == 'full':
        # The qubits used as the control registers for the phase estimation,
        # that must be kept the whole way through. If we used independent
        # controls each time that would increase the Toffoli cost by
        # np.ceil(np.log2iters+1]]-3, while saving
        # np.ceil(np.log2iters+1]]-1 qubits.
        ac1 = 2 * np.ceil(np.log2(iters + 1)) - 1
    # The system qubits that must always be included.
    ac2 = n
    # The μ and ν registers, that must be kept because they are control
    # registers that aren't fully erased and must be reflected on.
    ac3 = 2 * nM
    # These are the qubits for the spin in the control state as well as the
    # qubit that is rotated for the preparation of the equal superposition
    # state, AND the qubit that is used to control.
    # None of these are fully inversely prepared.
    ac4512 = 4
    # The qubits for the phase gradient state.
    ac8 = beta
    # This is for the equal superposition state to perform the inequality test
    # with the keep register. It must be kept around and reflected upon.
    ac11 = chi
    # The total number of permanent qubits.
    perm = ac1 + ac2 + ac3 + ac4512 + ac8 + ac11
    # In preparing the equal superposition state there are 6 temporary qubits
    # used in the rotation of the ancilla.  There are another three that are
    # needed for the temporary results of inequality tests. By far the largest
    # number, however, come from keeping the temporary ancillas from the
    # inequality tests.  That should be 3*nM+nN-4.  There are an other two
    # qubits in output at the end that will be kept until this step is undone.
    # Note: not used?
    #nN = np.ceil(np.log2(n / 2))

    # This is the maximum number of qubits used while preparing the equal
    # superposition state.
    qu1 = perm + 4 * nM - 1
    # To explain the number of temporary ancillas, we have nM+1 to perform the
    # inequality test on mu and nu with out-of-place addition.  We have another
    # nM-2 for the equality test.  Then we can do the inequality tests on
    # mu and nu with constants (temporarily) overwriting these variables, and
    # keeping nM-1 qubits on each.  Then there are another 2 temporary qubits
    # used for the reflection.  That gives 4*nM-1 total.
    # This is the number of Toffolis during this step.
    tof1 = 10 * nM + 2 * br - 9

    # This is increasing the running number of permanent ancillas by 2 for the
    #  ν=M+1 flag qubit and the success flag qubit.
    perm = perm + 2
    # The number of temporary qubits used in this computation is the the same
    # as the number of Toffolis plus one.
    qu2 = perm + nM**2 + nM
    # The Toffoli cost of computing the contiguous register.
    tof2 = nM**2 + nM - 1
    # The running number of qubits is increased by the number needed for the
    # contiguous register.
    perm = perm + nc

    if algorithm == 'half':
        # Here I'm setting the k-value for the QROM by hand instead of choosing
        # the optimal one for Toffolis.
        kt = 16
    elif algorithm == 'full':
        # Here I'm setting the k-value for the QROM by hand instead of choosing
        #  the optimal one for Toffolis.
        kt = 32
    # This is the number of qubits needed during the QROM.
    qu3 = perm + m * kt + np.ceil(np.log2(d / kt))
    # The number of Toffolis for the QROM.
    tof3 = np.ceil(d / kt) + m * (kt - 1)
    # The number of ancillas used increases by the actual output size of  QROM.
    perm = perm + m
    # The number of ancilla qubits used for the subtraction for the ineql test.
    qu4 = perm + chi
    # We can use one of the qubits from the registers that are subtracted as
    # the flag qubit so we don't need an extra flag qubit.
    # The number of Toffolis needed for the inequality test. The number of
    # permanent ancillas is unchanged.*)
    tof4 = chi
    # We don't need any extra ancillas for the controlled swaps.
    qu5 = perm
    # We are swapping pairs of registers of size nM
    tof5 = 2 * nM
    # One extra ancilla for the controlled swap of mu and nu because it is
    # controlled on two qubits.
    qu6 = perm
    # One more Toffoli for the double controls.
    tof6 = nM + 1
    # Swapping based on the spin register.
    qu7 = perm
    tof7 = n / 2

    if algorithm == 'half':
        # We use these temporary ancillas for the first QROM for the rot angles.
        qu8 = perm + nM + beta * n / 4
    elif algorithm == 'full':
        # We use these temporary ancillas for the first QROM for the rot angles.
        qu8 = perm + nM + beta * n / 2
    # The cost of outputting the rotation angles including those for the
    # one-electron part.
    tof8 = M + n / 2 - 2

    if algorithm == 'half':
        # We are now need the output rotation angles, though we don't need the
        # temporary qubits from the unary iteration.
        perm = perm + beta * n / 4
    elif algorithm == 'full':
        # We are now need the output rotation angles, though we don't need the
        # temporary qubits from the unary iteration.
        perm = perm + beta * n / 2
    # We need a few temp registers for adding into the phase grad register.
    qu9 = perm + (beta - 2)

    if algorithm == 'half':
        # The cost of the rotations.
        tof9 = n * (beta - 2) / 2
        # Make a list where we keep subtr the data qubits that can be erased.
        # Table[-j*beta,{j,0,n/4-1}]+perm+(beta-2)
        qu10 = np.array([-j * beta for j in range(int(n / 4))]) \
               + perm + beta - 2
        # The cost of the rotations.
        # Table[2*(beta-2),{j,0,n/4-1}]
        tof10 = np.array([2 * (beta - 2) for j in range(int(n / 4))])
        # We've erased the data
        perm = perm - beta * n / 4
    elif algorithm == 'full':
        # The cost of the rotations.
        tof9 = n * (beta - 2)
        # Make a list where we keep subtr the data qubits that can be erased.
        # Table[-j*beta,{j,0,n/2-1}]+perm+(beta-2)
        qu10 = np.array([-j * beta for j in range(int(n / 2))]) \
               + perm + beta - 2
        # The cost of the rotations.
        # Table[2*(beta-2),{j,0,n/2-1}]
        tof10 = np.array([2 * (beta - 2) for j in range(int(n / 2))])
        # We've erased the data
        perm = perm - beta * n / 2

    # Find the k for the phase fixup for the erasure of the rotations.
    k1 = 2**QI(M + n / 2)[0]

    # Temp qubits used. Data qubits were already erased, so don't change perm.
    qu11 = perm + k1 + np.ceil(np.log2(M / k1))
    tof11 = np.ceil(M / k1) + np.ceil(n / 2 / k1) + k1

    # Swapping based on the spin register.
    qu12 = perm
    tof12 = n / 2

    # Swapping the spin registers.
    qu12a = perm
    tof12a = 1

    # Swapping based on the spin register.
    qu13 = perm
    tof13 = n / 2

    if algorithm == 'half':
        # We use these temp ancillas for the second QROM for the rot angles.
        qu14 = perm + nM - 1 + beta * n / 4
        perm = perm + beta * n / 4
    elif algorithm == 'full':
        # We use these temp ancillas for the second QROM for the rot angles.
        qu14 = perm + nM - 1 + beta * n / 2
        perm = perm + beta * n / 2
    tof14 = M - 2

    # We need a few temporary registers for adding into the phase grad register
    qu15 = perm + (beta - 2)

    if algorithm == 'half':
        # The cost of the rotations.
        tof15 = n * (beta - 2) / 2
    elif algorithm == 'full':
        # The cost of the rotations.
        tof15 = n * (beta - 2)

    # Just one Toffoli to do the controlled Z1.
    qu16 = perm
    tof16 = 1

    if algorithm == 'half':
        # Make a list where we keep subtr the data qubits that can be erased.
        # Table[-j*beta,{j,0,n/4-1}]+perm+(beta-2)
        qu17 = np.array([-j * beta
                         for j in range(int(n / 4))]) + perm + beta - 2
        # The cost of the rotations.
        # Table[2*(beta-2),{j,0,n/4-1}]
        tof17 = np.array([2 * (beta - 2) for j in range(int(n / 4))])
        # We've erased the data.
        perm = perm - beta * n / 4
    elif algorithm == 'full':
        # Make a list where we keep subtr the data qubits that can be erased.
        # Table[-j*beta,{j,0,n/2-1}]+perm+(beta-2)
        qu17 = np.array([-j * beta for j in range(int(n / 2))]) \
               + perm + beta - 2
        # The cost of the rotations.
        # Table[2*(beta-2),{j,0,n/2-1}]
        tof17 = np.array([2 * (beta - 2) for j in range(int(n / 2))])
        # We've erased the data
        perm = perm - beta * n / 2

    # Find the k for the phase fixup for the erasure of the rotations.
    k1 = 2**QI(M)[0]

    # The temp qubits used. The data qubits were already erased,
    # so don't change perm.
    qu18 = perm + k1 + np.ceil(np.log2(M / k1))
    tof18 = np.ceil(M / k1) + k1

    # Swapping based on the spin register.
    qu19 = perm
    tof19 = n / 2

    # One extra ancilla for the controlled swap of mu and nu because it is
    # controlled on two qubits.
    qu20 = perm + 1
    # One extra Toffoli, because we are controlling on two qubits.
    tof20 = nM + 1

    # We don't need any extra ancillas for the controlled swaps.
    qu21 = perm
    # We are swapping pairs of registers of size nM
    tof21 = 2 * nM

    # The number of ancilla qubits used for the subtraction for the inequal test
    qu22 = perm + chi
    # We can use one of the qubits from the registers that are subtracted as
    # the flag qubit so we don't need an extra flag qubit.
    # The number of Toffolis needed for inverting the inequality test.
    # The number of permanent ancillas is unchanged.
    tof22 = chi
    # We can erase the data for the QROM for inverting the state preparation,
    # then do the phase fixup.
    perm = perm - m

    kt = 2**QI(d)[0]
    # This is the number of qubits needed during the QROM.
    qu23 = perm + kt + np.ceil(np.log2(d / kt))
    # The number of Toffolis for the QROM.
    tof23 = np.ceil(d / kt) + kt
    # The number of temporary qubits used in this computation is the same as
    # the number of Toffolis plus one. We are erasing the contiguous register
    # as we go so can subtract nc.
    qu24 = perm - nc + nM**2 + nM
    # The Toffoli cost of computing the contiguous register.
    tof24 = nM**2 + nM - 1
    # The contiguous register has now been deleted.
    perm = perm - nc
    # This is the maximum number of qubits used while preparing the equal
    # superposition state.
    qu25 = perm + 4 * nM - 1
    # This is the number of Toffolis during this step.
    tof25 = 10 * nM + 2 * br - 9
    # This is increasing the running number of permanent ancillas by 2 for
    # the ν=M+1 flag qubit and the success flag qubit.
    perm = perm - 2

    if algorithm == 'half':
        # We need some ancillas to perform a reflection on multiple qubits.
        # We are including one more Toffoli to make it controlled.
        qu26 = perm + costref + np.ceil(np.log2(iters + 1))
        tof26 = costref + np.ceil(np.log2(iters + 1))
    elif algorithm == 'full':
        # We need some ancillas to perform a reflection on multiple qubits.
        # We are including one more Toffoli to make it controlled.
        qu26 = perm + costref
        tof26 = costref

    qu27 = perm  # Iterate the control register.
    tof27 = 1

    # Labels
    sm = 'small element'
    pq = 'preparation QROM'
    rq = 'rotation QROM'
    ri = r'R$^{\dag}$'
    ro = 'R'
    iq = 'inverse QROM'

    color_dict = {
        sm: '#435CE8',
        pq: '#E83935',
        rq: '#F59236',
        ri: '#E3D246',
        ro: '#36B83E',
        iq: '#E83935'
    }

    if algorithm == 'half':
        tgates = np.hstack((np.array([
            tof1, tof2, tof3, tof4, tof5, tof6, tof7, tof8, tof9, tof8, tof9,
            tof9, tof8
        ]), tof10,
                            np.array([
                                tof11, tof12, tof12a, tof13, tof14, tof15,
                                tof14, tof15, tof16, tof15, tof14
                            ]), tof17,
                            np.array([
                                tof18, tof19, tof20, tof21, tof22, tof23,
                                tof24, tof25, tof26, tof27
                            ])))
        qubits = np.hstack((np.array(
            [qu1, qu2, qu3, qu4, qu5, qu6, qu7, qu8, qu9, qu8, qu9, qu9,
             qu8]), qu10,
                            np.array([
                                qu11, qu12, qu12a, qu13, qu14, qu15, qu14,
                                qu15, qu16, qu15, qu14
                            ]), qu17,
                            np.array([
                                qu18, qu19, qu20, qu21, qu22, qu23, qu24, qu25,
                                qu26, qu27
                            ])))
        labels = [sm, sm, pq, sm, sm, sm, sm, rq, ri, rq, ri, ro, rq] + \
                 [ro] * len(qu10) + \
                 [rq, sm, sm, sm, rq, ri, rq, ri, sm, ro, rq] + \
                 [ro] * len(qu17) + \
                 [rq, sm, sm, sm, sm, iq, sm, sm, sm, sm]

        colors = [color_dict[i] for i in labels]
    elif algorithm == 'full':
        tgates = np.hstack(
            (np.array([tof1, tof2, tof3, tof4, tof5, tof6, tof7, tof8,
                       tof9]), tof10,
             np.array([tof11, tof12, tof12a, tof13, tof14, tof15,
                       tof16]), tof17,
             np.array([
                 tof18, tof19, tof20, tof21, tof22, tof23, tof24, tof25, tof26,
                 tof27
             ])))
        qubits = np.hstack(
            (np.array([qu1, qu2, qu3, qu4, qu5, qu6, qu7, qu8, qu9]), qu10,
             np.array([qu11, qu12, qu12a, qu13, qu14, qu15, qu16]), qu17,
             np.array(
                 [qu18, qu19, qu20, qu21, qu22, qu23, qu24, qu25, qu26,
                  qu27])))
        labels = [sm, sm, pq, sm, sm, sm, sm, rq, ri] + \
                 [ro] * len(qu10) + \
                 [rq, sm, sm, sm, rq, ri, sm] + \
                 [ro] * len(qu17) + \
                 [rq, sm, sm, sm, sm, iq, sm, sm, sm, sm]

        colors = [color_dict[i] for i in labels]

    # check lists are at least consistent
    assert all(
        len(element) == len(tgates) for element in [qubits, labels, colors])

    return tgates, qubits, labels, colors
Пример #11
0
def compute_cost(n: int,
                 lam: float,
                 dE: float,
                 L: int,
                 Lxi: int,
                 chi: int,
                 beta: int,
                 stps: int,
                 verbose: bool = False) -> Tuple[int, int, int]:
    """ Determine fault-tolerant costs using DF decomposition in quantum chem

    Args:
        n (int) - the number of spin-orbitals
        lam (float) - the lambda-value for the Hamiltonian
        dE (float) - allowable error in phase estimation
        L (int) - the rank of the first decomposition
        Lxi (int) - the total number of eigenvectors
        chi (int) - equivalent to aleph_1 and aleph_2 in the document, the
            number of bits for the representation of the coefficients
        beta (int) - equivalent to beth in the document, the number of bits
            for the rotations
        stps (int) - an approximate number of steps to choose the precision of
            single qubit rotations in preparation of the equal superpositn state
        verbose (bool) - do additional printing of intermediates?

    Returns:
        step_cost (int) - Toffolis per step
        total_cost (int) - Total number of Toffolis
        ancilla_cost (int) - Total ancilla cost
    """

    # The number of bits used for the second register.
    nxi = np.ceil(np.log2(n // 2))

    # The number of bits for the contiguous register.
    nLxi = np.ceil(np.log2(Lxi + n // 2))

    # The number of bits used for the first register.
    nL = np.ceil(np.log2(L + 1))

    # The power of 2 that is a factor of L + 1
    eta = power_two(L + 1)

    oh = [0] * 20
    for p in range(20):
        # JJG note: arccos arg may be > 1
        v = np.round(np.power(2,p+1) / (2 * np.pi) * arccos(np.power(2,nL) /\
            np.sqrt((L + 1)/2**eta)/2))
        oh[p] = np.real(stps * (1 / (np.sin(3 * arcsin(np.cos(v * 2 * np.pi / \
            np.power(2,p+1)) * \
            np.sqrt((L + 1)/2**eta) / np.power(2,nL)))**2) - 1) + 4 * (p + 1))

    # Bits of precision for rotation
    br = int(np.argmin(oh) + 1)

    # The following costs are from the list starting on page 50.

    # The cost for preparing an equal superposition for preparing the first
    # register in step 1 (a). We double this cost to account for the inverse.
    cost1a = 2 * (3 * nL + 2 * br - 3 * eta - 9)

    # The output size for the QROM for the first state preparation in Eq. (C27)
    bp1 = nL + chi

    # The cost of the QROM for the first state preparation in step 1 (b) and
    # its inverse.
    cost1b = QR(L + 1, bp1)[1] + QI(L + 1)[1]

    # The cost for the inequality test, controlled swap and their inverse in
    # steps 1 (c) and (d)
    cost1cd = 2 * (chi + nL)

    # The total cost for preparing the first register in step 1.
    cost1 = cost1a + cost1b + cost1cd

    # The output size for the QROM for the data to prepare the equal
    # superposition on the second register, as given in Eq. (C29).
    bo = nxi + nLxi + br + 1

    # This is step 2. This is the cost of outputting the data to prepare the
    # equal superposition on the second register. We will assume it is not
    # uncomputed, because we want to keep the offset for applying the QROM for
    # outputting the rotations.
    cost2 = QR(L + 1, bo)[1] + QI(L + 1)[1]

    # The number of bits for rotating the ancilla for the second preparation.
    # We are just entering this manually because it is a typical value.
    br = 7

    # The cost of preparing an equal superposition over the second register in
    # a controlled way. We pay this cost 4 times.
    cost3a = 4 * (7 * nxi + 2 * br - 6)

    # The cost of the offset to apply the QROM for state preparation on the
    # second register.
    cost3b = 4 * (nLxi - 1)

    bp2 = nxi + chi + 2

    # The cost of the QROMs and inverse QROMs for the state preparation, where
    # in the first one we need + n/2 to account for the one-electron terms.
    cost3c = QR(Lxi + n // 2, bp2)[1] + QI(Lxi + n // 2)[1] + QR(
        Lxi, bp2)[1] + QI(Lxi)[1]

    # The inequality test and state preparations.
    cost3d = 4 * (nxi + chi)

    # The total costs for state preparations on register 2.
    cost3 = cost3a + cost3b + cost3c + cost3d

    # The cost of adding offsets in steps 4 (a) and (h).
    cost4ah = 4 * (nLxi - 1)

    # The costs of the QROMs and their inverses in steps 4 (b) and (g).
    cost4bg = QR(Lxi + n // 2, n * beta // 2)[1] + QI(Lxi + n // 2)[1] + QR(
        Lxi, n * beta // 2)[1] + QI(Lxi)[1]

    # The cost of the controlled swaps based on the spin qubit in steps 4c and f
    cost4cf = 2 * n

    # The controlled rotations in steps 4 (d) and (f).
    cost4df = 4 * n * (beta - 2)

    # The controlled Z operations in the middle for step 4 (e).
    cost4e = 3

    # This is the cost of the controlled rotations for step 4.
    cost4 = cost4ah + cost4bg + cost4cf + cost4df + cost4e

    # This is the cost of the reflection on the second register from step 6.
    cost6 = nxi + chi + 2

    # The cost of the final reflection req'd to construct the step of the
    # quantum walk from step 9.
    cost9 = nL + nxi + chi + 1

    # The extra two qubits for unary iteration and making the rflxn controlled.
    cost10 = 2

    # The Toffoli cost for a single step
    cost = cost1 + cost2 + cost3 + cost4 + cost6 + cost9 + cost10

    # The number of steps needed
    iters = np.ceil(np.pi * lam / (2 * dE))

    # Now the number of qubits from the list on page 54.

    k1 = np.power(2, QR(Lxi + n // 2, n * beta // 2)[0])

    # The control register for phase estimation and iteration on it.
    ac1 = np.ceil(np.log2(iters + 1)) * 2 - 1

    # The system qubits
    ac2 = n

    # The first register prepared, a rotated qubit and a flag qubit.
    ac3 = nL + 2

    # The output of the QROM, the equal superposition state and a flag qubit.
    ac4 = nL + chi * 2 + 1

    # The data used for preparing the equal superposition state on the 2nd reg
    ac5 = bo

    # The second register, a rotated qubit and a flag qubit.
    ac6 = nxi + 2

    # The second preparation QROM output.
    ac8 = bp2

    # The equal superposition state and the result of the inequality test.
    ac9 = chi + 1

    # The angles for rotations.
    ac10 = k1 * n * beta // 2

    # The phase gradient state.
    ac11 = beta

    # A control qubit for the spin.
    ac12 = 1

    # A T state.
    ac13 = 1

    if verbose:
        print("[*] Top of routine")
        print("  [+] nxi = ", nxi)
        print("  [+] nLxi = ", nLxi)
        print("  [+] nL = ", nL)
        print("  [+] eta = ", eta)
        print("  [+] cost3 = ", cost3)
        print("  [+] cost4 = ", cost4)
        print("  [+] cost = ", cost)
        print("  [+] iters = ", iters)

    ancilla_cost = ac1 + ac2 + ac3 + ac4 + ac5 + ac6 + ac8 + ac9 + ac10 + ac11\
                 + ac12 + ac13

    # Sanity checks before returning as int
    assert cost.is_integer()
    assert iters.is_integer()
    assert ancilla_cost.is_integer()

    step_cost = int(cost)
    total_cost = int(cost * iters)
    ancilla_cost = int(ancilla_cost)

    return step_cost, total_cost, ancilla_cost
Пример #12
0
def compute_cost(n: int,
                 lam: float,
                 dE: float,
                 L: int,
                 chi: int,
                 stps: int,
                 verbose: bool = False) -> Tuple[int, int, int]:
    """ Determine fault-tolerant costs using SF decomposition in quantum chem

    Args:
        n (int) - the number of spin-orbitals
        lam (float) - the lambda-value for the Hamiltonian
        dE (float) - allowable error in phase estimation
        L (int) - the rank of the first decomposition
        chi (int) - equivalent to aleph_1 and aleph_2 in the document, the
            number of bits for the representation of the coefficients
        stps (int) - an approximate number of steps to choose the precision of
            single qubit rotations in preparation of the equal superposn state
        verbose (bool) - do additional printing of intermediates?

    Returns:
        step_cost (int) - Toffolis per step
        total_cost (int) - Total number of Toffolis
        total_qubit_count (int) - Total qubit count
    """

    # Number of trailing zeros
    eta = power_two(L + 1)

    # Number of qubits for the first register
    nL = np.ceil(np.log2(L + 1))

    # Number of qubits for p and q registers
    nN = np.ceil(np.log2(n // 2))

    oh = [0] * 20
    for p in range(20):
        # JJG note: arccos arg may be > 1
        v = np.round(np.power(2,p+1) / (2 * np.pi) * arccos(np.power(2,nL) /\
            np.sqrt((L + 1)/2**eta)/2))
        oh[p] = np.real(stps * (1 / (np.sin(3 * arcsin(np.cos(v * 2 * np.pi / \
            np.power(2,p+1)) * \
            np.sqrt((L + 1)/2**eta) / np.power(2,nL)))**2) - 1) + 4 * (p + 1))

    # Bits of precision for rotation
    br = int(np.argmin(oh) + 1)

    # The following costs are given in the list of steps on pages 43 and 44.

    # Cost of preparing the equal superposition state on the 1st register in 1a
    cost1a = 2 * (3 * nL - 3 * eta + 2 * br - 9)

    # We have added two qubits
    bL = nL + chi + 2

    # QROM costs for first register preparation in step 1(b)
    cost1b = QR(L + 1, bL)[-1] + QI(L + 1)[-1]

    # The inequality test of cost chi in step 1(c) and the controlled swap with
    # cost nL + 1 in step 1(d) (and their inversions).
    cost1cd = 2 * (chi + nL + 1)

    oh = [0] * 20
    nprime = int(n**2 // 8 + n // 4)
    for p in range(20):
        v = np.round(
            np.power(2, p + 1) / (2 * np.pi) *
            arccos(np.power(2, 2 * nN) / np.sqrt(nprime) / 2))
        oh[p] = np.real(20000 * (1 / (np.sin(3 * arcsin(np.cos(v * 2 * np.pi / \
            np.power(2, p+1)) * \
            np.sqrt(nprime) / np.power(2,2*nN)))**2) - 1) + 4 * (p + 1))

    # Bits of precision for rotation for preparing the next equal
    # superposition states
    br = int(np.argmin(oh) + 1)

    # Cost of preparing equal superposition of p and q registers in step 2(a).
    cost2a = 4 * (6 * nN + 2 * br - 7)

    # Cost of computing contiguous register in step 2 (b).
    cost2b = 4 * (nN**2 + nN - 1)

    # Number of coefficients for first state preparation on p & q.
    n1 = (L + 1) * nprime

    # Number of coefficients for second state preparation on p & q.
    n2 = L * nprime

    # The output size for the QROM for the second state preparation.
    bp = int(2 * nN + chi + 2)

    # Cost of QROMs for state preparation on p and q in step 2 (c).
    cost2c = QR2(L + 1, nprime, bp)[-1] + QI(n1)[-1] + QR2(L, nprime,
                                                           bp)[-1] + QI(n2)[-1]

    # The cost of the inequality test and controlled swap for the quantum alias
    # sampling in steps 2 (d) and (e).
    cost2de = 4 * (chi + 2 * nN)

    # Swapping the p & q registers for symmetry in step 3.  This needs to be
    # done and inverted twice, hence the factor of 4.
    cost3 = 4 * nN

    # The SELECT operation in step 4, which needs to be done twice.
    cost4 = 4 * n - 8

    # The reflection used on the 2nd register in the middle, with cost in step 6
    cost6 = 2 * nN + chi + 3

    # Step 7 involves performing steps 2 to 5 again, which are accounted for
    # above, but there is one extra Toffoli for checking that l <> 0.
    cost7 = 1

    # The total reflection for the quantum walk in step 9.
    cost9 = nL + 2 * nN + 2 * chi + 2

    # The two Toffolis for the control for phase estimation and iteration,
    # given in step 10.
    cost10 = 2

    # The number of steps.
    iters = np.ceil(np.pi * lam / (dE * 2))

    # The total Toffoli costs for a step.
    cost = cost1a + cost1b + cost1cd + cost2a + cost2b + cost2c + cost2de + \
           cost3 + cost4 + cost6 + cost7 + cost9 + cost10

    # Control for phase estimation and its iteration
    ac1 = 2 * np.ceil(np.log2(iters)) - 1

    # System qubits
    ac2 = n

    # First ell register that rotated and the flag for success
    ac3 = nL + 2

    # State preparation on the first register
    ac4 = nL + 2 * chi + 3

    # For p and q registers, the rotated qubit and the success flag
    ac5 = 2 * nN + 2

    # The size of the contiguous register
    ac6 = np.ceil(np.log2(nprime))

    kp = QR2(L + 1, nprime, bp)[:2]

    # The equal superposition state for the second state preparation
    ac7 = chi

    # The phase gradient state
    ac8 = br

    # The QROM on the p & q registers
    ac9 = kp[0] * kp[1] * bp + np.ceil(np.log2(
        (L + 1) / kp[0])) + np.ceil(np.log2(nprime / kp[1]))

    if verbose:
        print("[*] Top of routine")
        print("  [+] eta = ", eta)
        print("  [+] nL = ", nL)
        print("  [+] nN = ", nN)

    total_qubit_count = ac1 + ac2 + ac3 + ac4 + ac5 + ac6 + ac7 + ac8 + ac9

    # Sanity checks before returning as int
    assert cost.is_integer()
    assert iters.is_integer()
    assert total_qubit_count.is_integer()

    step_cost = int(cost)
    total_cost = int(cost * iters)
    total_qubit_count = int(total_qubit_count)

    return step_cost, total_cost, total_qubit_count
Пример #13
0
#a为一二维列表,取每行中奇数位数据,降维成一维列表后存放x中
x = a[:, ::2].flatten()
#a为一二维列表,取每行中偶数位数,降维成一维列表后据存放y中
y = a[:, 1::2].flatten()
#将起始位的经纬度存放在变量d中
d1 = np.array([[70, 40]])
#将经纬度组成对,存放于二维列表xy中
xy = np.c_[x, y]
#将起始位的经纬度添加到xy列表的收尾处
xy = np.r_[d1, xy, d1]
#求xy列表中的行数
N = xy.shape[0]
# 将xy列表中的经纬度转换为弧度,存于列表t中
t = np.radians(xy)
#计算表中各个点在地球上的距离,存于列表中
d = np.array([[6370 * arccos(np.cos(t[i, 0] - t[j, 0]) * np.cos(t[i, 1]) * np.cos(t[j, 1]) + \
                                np.sin(t[i, 1]) * np.sin(t[j, 1])) for i in range(N)] for j in range(N)]).real
#path记录遍历各点顺序的列表
path = np.arange(N)
#L记录遍历各点后距离的最小值
L = np.inf
#使用蒙特卡洛方法为模拟退火方法取一个初始值
for j in range(10000):
    path0 = np.arange(1, N - 1)
    np.random.shuffle(path0)
    path0 = np.r_[0, path0, N - 1]
    L0 = d[0, path0[1]]

    for i in range(1, N - 1):
        L0 += d[path0[i], path0[i + 1]]
    if L0 < L:
        path = path0
Пример #14
0
def compute_cost(n: int,
                 lam: float,
                 dE: float,
                 chi: int,
                 beta: int,
                 M: int,
                 stps: int,
                 verbose: bool = False) -> Tuple[int, int, int]:
    """ Determine fault-tolerant costs using THC decomposition in quantum chem

    Args:
        n (int) - the number of spin-orbitals
        lam (float) - the lambda-value for the Hamiltonian
        dE (float) - allowable error in phase estimation
        chi (int) - equivalent to aleph in the document, the number of bits for
            the representation of the coefficients
        beta (int) - equivalent to beth in the document, the number of bits for
            the rotations
        M (int) - the dimension for the THC decomposition
        stps (int) - an approximate number of steps to choose the precision of
            single qubit rotations in preparation of the equal superpositn state

    Returns:
        step_cost (int) - Toffolis per step
        total_cost (int) - Total number of Toffolis
        ancilla_cost (int) - Total ancilla cost
    """
    # The number of bits used for each register.
    nM = np.ceil(np.log2(M + 1))

    # This is the number of distinct items of data we need to output.
    d = M * (M + 1) // 2 + n // 2

    # The number of bits used for the contiguous register.
    nc = np.ceil(np.log2(d))

    # The output size is 2*log[M] for the alt values, chi for the keep value,
    # and 2 for the two sign bits, as in Eq. (29).
    m = 2 * nM + 2 + chi
    oh = [0] * 20
    for p in range(20):
        # arccos arg may be > 1
        v = np.round(
            np.power(2, p + 1) / (2 * np.pi) *
            arccos(np.power(2, nM) / np.sqrt(d) / 2))
        oh[p] = stps * (1 / (np.sin(3 * arcsin(np.cos(v * 2 * np.pi / \
            np.power(2,p+1)) * \
            np.sqrt(d) / np.power(2,nM)))**2) - 1) + 4 * (p + 1)

    # Set it to be the number of bits that minimises the cost, usually 7.
    # Python is 0-index, so need to add the one back in vs mathematica nb
    br = np.argmin(oh) + 1

    # This is the costing for preparing the equal superposition over the
    # input registers from below Eq. (27).
    cp1 = 2 * (10 * nM + 2 * br - 9)

    # This is the cost of computing the contiguous register and inverting it.
    # This is with a sophisticated scheme adding together triplets of bits.
    # This is the cost of step 2 in the list on pages 15 and 16, with a factor
    # of 2 to account for its inverse.
    cp2 = 2 * (nM**2 + nM - 1)

    # This is the cost of the QROM for the state preparation in step 3 and its
    # inverse. Note: arg_min is first value, min is second value
    cp3 = QR(d, m)[1] + QI(d)[1]

    # The cost for the inequality test in step 4 and its inverse.
    cp4 = 2 * chi

    # The cost 2*nM for the controlled swap in step 5 and its inverse.
    cp5 = 4 * nM

    # Then there is a cost of nM + 1 for swapping the mu and nu registers in
    # step 6, where the + 1 is because we need to control on two registers.
    # There is the same cost for the inverse.
    cp6 = 2 * nM + 2

    # This is the total cost in Eq. (32).
    CPCP = cp1 + cp2 + cp3 + cp4 + cp5 + cp6

    # This is the cost of swapping based on the spin register. It is steps 1
    # and 7 from the list of steps on pages 16 and 17.
    cs1 = 2 * n

    # The QROM for the rotation angles the first time in step 2. and the second
    # part is the cost for inverting them with advanced QROM (step 6).
    cs2a = M + n / 2 - 2

    # The QROM for the rotation angles the second time. Here the cost M - 2 is
    # for generating the angles the second time (again step 2), and QI[M] is
    # for inverting the QROM (step 6).
    cs2b = M - 2

    # The cost of the rotations in steps 3 and 5, which must be done 4 times.
    cs3 = 4 * n * (beta - 2)

    # Cost for making the Z doubly controlled in step 4, and a controlled swap
    #  of the spin qubits. Note: is this really just "2"? Does this need a var?
    cs4 = 2

    # want the argument, so that is first element
    k1 = np.power(2, QI(M + n // 2)[0])

    # The cost for inverting the rotation angles the first time in step 6.
    cs6a = np.ceil(M / k1) + np.ceil(n / 2 / k1) + k1

    # The cost for inverting the rotation angles the second time in step 6.
    # value is in second output argument of QI
    cs6b = QI(M)[1]

    # The total select cost in Eq. (42).
    CS = cs1 + cs2a + cs2b + cs3 + cs4 + cs6a + cs6b

    # The cost of the reflections used in Eq. (43).
    costref = 2 * nM + chi + 4

    # The total cost in Eq. (43).
    cost = CPCP + CS + costref

    # The cost of the control register and temporary registers given for part 1.
    ac1 = lambda iters: 2 * np.ceil(np.log2(iters + 1)) - 1

    # Qubits for the system register in part 2.
    ac2 = n

    # The cost for the \[Mu] and \[Nu] registers in part 3.
    ac3 = 2 * nM

    # This is for the equal superposition state to perform the inequal
    # test with the keep register.
    ac4 = chi

    # Some minor qubit counts in parts 5 to 9.
    ac59 = 7

    # The phase gradient state in part 10.
    ac10 = beta

    # The contiguous register in part 11
    ac11 = nc

    # we want arg_min so first argument
    kt = np.power(2, QR(d, m))[0]

    # The qubits used for the QROM in part 12, of which only m are persistent.
    ac12 = m * kt + np.ceil(np.log2(d / kt))

    # This is the data needed for the rotations in part 13.
    ac13 = beta * n / 2

    # These are the ancillas needed for adding into the phase gradient state
    # in part 14.
    ac14 = beta - 2

    # These are the temporary ancillas in between erasing the first QROM
    # ancillas and inverting that QROM. The + m is for output of the first QROM.
    acc = ac13 + ac14 + m

    # Total number of iterations
    iters = np.ceil(np.pi * lam / (dE * 2))
    aca = ac1(iters) + ac2 + ac3 + ac4 + ac59 + ac10 + ac11

    if verbose:
        print("[*] Top of routine")
        print("  [+] nM = ", nM)
        print("  [+] d = ", d)
        print("  [+] nc = ", nc)
        print("  [+] m = ", m)
        print("  [+] oh = ", oh)
        print("  [+] br = ", br)
        print("  [+] cp1 = ", cp1)
        print("  [+] cp2 = ", cp2)
        print("    [*] QR[d,m] = ", QR(d, m))
        print("    [*] QI[d] = ", QI(d))
        print("  [+] cp3 = ", cp3)
        print("  [+] cp4 = ", cp4)
        print("  [+] cp5 = ", cp5)
        print("  [+] cp6 = ", cp6)
        print("  [+] CPCP = ", CPCP)
        print("  [+] cs1 = ", cs1)
        print("  [+] cs2a = ", cs2a)
        print("  [+] cs2b = ", cs2b)
        print("  [+] cs3 = ", cs3)
        print("  [+] k1 = ", k1)
        print("  [+] cs6a = ", cs6a)
        print("  [+] cs6b = ", cs6b)
        print("  [+] CS = ", CS)
        print("  [+] costref = ", costref)
        print("  [+] cost = ", cost)
        print("  [+] ac1 = ", ac1)
        print("  [+] ac2 = ", ac2)
        print("  [+] ac3 = ", ac3)
        print("  [+] ac4 = ", ac4)
        print("  [+] ac10 = ", ac10)
        print("  [+] ac11 = ", ac11)
        print("  [+] kt = ", kt)
        print("  [+] ac12 = ", ac12)
        print("  [+] ac13 = ", ac13)
        print("  [+] ac14 = ", ac14)
        print("  [+] acc = ", acc)
        print("  [+] iters = ", iters)
        print("  [+] aca = ", aca)

    # Sanity checks before returning as int
    # assert cost.is_integer()
    # assert iters.is_integer()
    # assert aca.is_integer()
    # assert ac12.is_integer()
    # assert acc.is_integer()

    step_cost = int(cost)
    total_cost = int(cost * iters)
    ancilla_cost = int(np.max([aca + ac12, aca + acc]))

    # step-cost, Toffoli count, logical qubits
    return step_cost, total_cost, ancilla_cost
Пример #15
0
from numpy import loadtxt, radians, sin, cos, inf, exp
from numpy import array, r_, c_, arange, savetxt
from numpy.lib.scimath import arccos
from numpy.random import shuffle, randint, rand
from matplotlib.pyplot import plot, show, rc
a = loadtxt("Pdata17_1.txt")
x = a[:, ::2].flatten()
y = a[:, 1::2].flatten()
d1 = array([[70, 40]])
xy = c_[x, y]
xy = r_[d1, xy, d1]
N = xy.shape[0]
t = radians(xy)  #转化为弧度
d = array([[
    6370 * arccos(
        cos(t[i, 0] - t[j, 0]) * cos(t[i, 1]) * cos(t[j, 1]) +
        sin(t[i, 1]) * sin(t[j, 1])) for i in range(N)
] for j in range(N)]).real
savetxt('Pdata17_2.txt', c_[xy, d])  #把数据保存到文本文件,供下面使用
path = arange(N)
L = inf
for j in range(1000):
    path0 = arange(1, N - 1)
    shuffle(path0)
    path0 = r_[0, path0, N - 1]
    L0 = d[0, path0[1]]  #初始化
    for i in range(1, N - 1):
        L0 += d[path0[i], path0[i + 1]]
    if L0 < L:
        path = path0
        L = L0
Пример #16
0
def convlim(Ss, Vs, Vc, Ztf, Bf, Zc, Icmax, Vcmax, Vcmin, convi, epslim, printopt):
    """
    Check for converter operation within ac voltage and current limits.

    Converter operation checked with the converter's PQ capability diagram
    that are calculated based on the converter limits and grid operation
    state.

    @author:Jef Beerten (KU Leuven)
    @author:Roni Irnawan (Aalborg University)    
    """

    ## voltage limits order check
    if Vcmax<Vcmin:
        stderr.write('Vcmin is larger than Vcmax for converter %d\n'%convi)
    elif Vcmax==Vcmin:
        stderr.write('Vcmin is equal to Vcmax for converter %d\n'%convi)

    Ssold = Ss.copy()

    ## define voltage magnitudes, angles, powers
    Vsm = abs(Vs)
    Vsa = angle(Vs)
    Vcm = abs(Vc)
    Vca = angle(Vc)
    Ps = real(Ss)
    Qs = imag(Ss)

    ##--- initialization ---
    ## load existing parameters
    Zf = 1/(j*(Bf + eps))
    Ytf = 1/(Ztf + eps) ## avoid division by zero. If Ztf=0, Ytf=inf is not used
    Yf  = j*(Bf + eps)
    Yc  = 1/(Zc + eps)

    ## pi-equivalent parameters of the converter station (voltage limits)
    # Implementation based on:
    # J. Beerten, S. Cole and R. Belmans: "Generalized Steady-State VSC MTDC
    # Model for Sequential AC/DC Power Flow Algorithms", IEEE Trans. Pow.
    # Syst., vol. 27, no. 2, 2012, pp. 821 - 829.
    #           ======
    #  --------|  Z2  |---------
    #    ||     ======     ||
    #   ====              ====
    #  |    |            |    |
    #  | Z1 |            | Z3 |
    #  |    |            |    |
    #   ====              ====
    #    ||                ||
    #
    # Expressions for Z3 and Y3 are not included, since not used in program

    if Ztf!=0 and Bf!=0:
        Z1 = (Ztf*Zc+Zc*Zf+Zf*Ztf)/Zc
        Z2 = (Ztf*Zc+Zc*Zf+Zf*Ztf)/Zf
    elif Ztf==0 and Bf!=0:
        Z1 = Zf
        Z2 = Zc
    elif Ztf!= 0 and Bf==0:
        Z1 = inf
        Z2 = Ztf+Zc
    else: ## Ztf == 0 && Bf ==0
        Z1 = inf
        Z2 = Zc

    Y1 = 1/Z1
    Y2 = 1/Z2
    G2 = real(Y2)
    B2 = imag(Y2)
    Y12 = Y1 + Y2
    G12 = real(Y12)
    B12 = imag(Y12)

    ##--- voltage and current limit parameters ----
    ## maximum current limit circle parameters
    MPL1 = -Vsm**2*(1/(conj(Zf) + conj(Ztf)))*(Bf!=0)\
            + (0+j*0)*(Bf==0)  ## center of the current limit
    rL1 = Vsm*Icmax*(\
                (Ztf!=0)*(abs(conj(Ytf)/(conj(Yf) + conj(Ytf))))\
                + (Ztf==0)*1) ## radius of current limit circle

    ## maximum and minimal active power on current limit
    PmaxL1 = real(MPL1) + rL1
    PminL1 = real(MPL1) - rL1
    QPmaxL1 = imag(MPL1)
    QPminL1 = imag(MPL1)

    ## minimum and maximum voltage limit circle parameters
    MPL2 = -Vsm**2*conj(Y1+Y2) ## center of the voltage limits
    rL2 =  Vsm*r_[Vcmin,Vcmax]*abs(Y2) ## radius of voltage limits

    ##--- voltage limit compliance of min/max active power point ---
    ## find intersection points of Vcmin/Vcmax and current limit circles
    dL12 = sqrt((real(MPL2)-real(MPL1))**2+(imag(MPL2)-imag(MPL1))**2)
    alpha = arctan((imag(MPL2)-imag(MPL1))/(real(MPL2)-real(MPL1)))
    beta = arccos(((dL12**2+rL1**2)*ones((1,2))-rL2**2)/(2*dL12*rL1))
    delta = arccos(((dL12**2-rL1**2)*ones((1,2))+rL2**2)/(2*dL12*rL2))
    gamma = c_[alpha-beta,pi-alpha-beta]
    eta = c_[alpha+delta,alpha-delta]

    ## possible intersection points (current limit) => intersection vector
    x1 = zeros((2,gamma.shape[1]),dtype=complex)
    y1 = zeros((2,gamma.shape[1]),dtype=complex)
    x1[0,:] = real(MPL1) + rL1*cos(gamma)
    x1[1,:] = real(MPL1) - rL1*cos(gamma)
    y1[0,:] = imag(MPL1) + rL1*sin(gamma)
    y1[1,:] = imag(MPL1) - rL1*sin(gamma)

    ## possible intersection points (voltage limits) => intersection vector
    x2 = zeros((2,gamma.shape[1]),dtype=complex)
    y2 = zeros((2,gamma.shape[1]),dtype=complex)
    x2[0,:] = real(MPL2) + r_[rL2,rL2]*cos(eta);
    x2[1,:] = real(MPL2) - r_[rL2,rL2]*cos(eta);
    y2[0,:] = imag(MPL2) + r_[rL2,rL2]*sin(eta);
    y2[1,:] = imag(MPL2) - r_[rL2,rL2]*sin(eta);

    ## vectorize intersection point matrices
    x1v = x1.reshape((x1.size,1),order='F')
    x2v = x2.reshape((x2.size,1),order='F')
    y1v = y1.reshape((y1.size,1),order='F')
    y2v = y2.reshape((y2.size,1),order='F')

    ## decrease accuracy to detect the intersection points
    eps2 = 1e-8;
    x1r = round(x1v/eps2)*eps2;
    x2r = round(x2v/eps2)*eps2;
    y1r = round(y1v/eps2)*eps2;
    y2r = round(y2v/eps2)*eps2;

    ## corresponding elements in intersections vectors
    x12r = intersect1d(x1r,x2r)
    x1i = array([idx for y in x12r for idx,x in enumerate(x1r) if x==y])
    x2i = array([idx for y in x12r for idx,x in enumerate(x2r) if x==y])
    y12r = intersect1d(y1r,y2r)
    y1i = array([idx for y in y12r for idx,x in enumerate(y1r) if x==y])
    y2i = array([idx for y in y12r for idx,x in enumerate(y2r) if x==y])

    x12r1 = c_[x1i,x12r]
    x12r1 = x12r1[x12r1[:,0].argsort()]
    x12r2 = c_[x2i,x12r]
    x12r2 = x12r2[x12r2[:,0].argsort()]
    y12r1 = c_[y1i,y12r]
    y12r1 = y12r1[y12r1[:,0].argsort()]
    y12r2 = c_[y2i,y12r]
    y12r2 = y12r2[y12r2[:,0].argsort()]

    ## define intersection points (full accuracy)
    VcminPQ1 = x1v[int(real(x12r1[0,0]))] + j*y1v[int(real(y12r1[0,0]))]
    VcminPQ2 = x1v[int(real(x12r1[2,0]))] + j*y1v[int(real(y12r1[2,0]))]
    VcmaxPQ1 = x1v[int(real(x12r1[1,0]))] + j*y1v[int(real(y12r1[1,0]))]
    VcmaxPQ2 = x1v[int(real(x12r1[3,0]))] + j*y1v[int(real(y12r1[3,0]))]
    
    ## Remove imaginary intersection points (no intersections, due to low/high voltage limits)
    if isreal(x12r2[0,1])==0 or isreal(y12r2[0,1])==0 or isreal(x12r2[2,1])==0 \
        or isreal(y12r2[2,1])==0 :
        VcminPQ1 = array([])     ## Imaginary intersection points are found in pairs
        VcminPQ2 = array([])
        if printopt == 1 :
            stdout.write('\n  Lower voltage limit at converter %d : No intersections with current limit were found.\n'%convi)

    if isreal(x12r2[1,1])==0 or isreal(y12r2[1,1])==0 or isreal(x12r2[3,1])==0 \
        or isreal(y12r2[3,1])==0 :
        VcmaxPQ1 = array([])
        VcmaxPQ2 = array([])
        if printopt == 1 :
            stdout.write('\n  Upper voltage limit at converter %d : No intersections with current limit were found.\n'%convi )

    ## Define maximum and minimum power points
    # Initialisation
    Pmin = PminL1
    QPmin = QPminL1
    Pmax = PmaxL1
    QPmax = QPmaxL1

    # Redefine max and min power points if min/max voltage limits are high/low
    if not VcminPQ1.size == 0 or not VcminPQ2.size == 0 :
        if printopt == 1 and (imag(VcminPQ1) > QPminL1 or imag(VcminPQ2) > QPmaxL1) :
            stdout.write('\n  High lower voltage limit detected at converter %d. \n'% convi)
        if imag(VcminPQ1) > QPminL1 :
            Pmin = real(VcminPQ1)
            QPmin = imag(VcminPQ1)
        if imag(VcminPQ2) > QPmaxL1:
            Pmax = real(VcminPQ2)
            QPmax = imag(VcminPQ2)
    if not VcmaxPQ1.size == 0 or not VcmaxPQ2.size == 0 :
        if printopt == 1 and (imag(VcmaxPQ1) < QPminL1 or imag(VcmaxPQ2) < QPmaxL1):
            stdout.write('\n  Low upper voltage limit detected at converter %d. \n '% convi)
        if imag(VcmaxPQ1) < QPminL1:
            Pmin = real(VcmaxPQ1)
            QPmin = imag(VcmaxPQ1)
        if imag(VcmaxPQ2) < QPmaxL1:
            Pmax = real(VcmaxPQ2)
            QPmax = imag(VcmaxPQ2)

    ##--- Limit check ---
    if Pmin < Ps and Ps < Pmax:
        ## maximum current limit (L1)
        if imag(MPL1)<Qs :
            Qs1 = imag(MPL1) + sqrt(rL1**2-(Ps-real(MPL1))**2)
        else: ## if Qs<imag(MPL1)
            Qs1 = imag(MPL1) - sqrt(rL1**2-(Ps-real(MPL1))**2)

        ## minimum and maximum voltage limits (L2)
        Qs2 = imag(MPL2)+sqrt(rL2**2-(Ps-real(MPL2))**2)
        a = 1+(B2/G2)**2*ones((1,2))
        b = -2*B2/G2*(Ps+Vsm**2*G12)/(Vsm*r_[Vcmin,Vcmax]*G2)
        c = ((Ps+Vsm**2*G12)/(Vsm*r_[Vcmin,Vcmax]*G2))**2-1

        ## only positive solution retained (neg. solution refers to lower part)
        sinDd = (-b + sqrt(b**2-4*a*c))/(2*a)
        Dd = arcsin(sinDd)
        cosDd = cos(Dd)
        Qs2 = Vsm**2*B12+Vsm*r_[Vcmin,Vcmax]*(G2*sinDd-B2*cosDd);

        ## adopt working point to limits
        if Qs > imag(MPL1) :
            if Qs > r_[Qs1,Qs2[:,1]].min() :
                convlimviol = 1
                Qs = r_[Qs1,Qs2[:,1]].min()
            elif Qs < Qs2[:,0] :
                convlimviol = 1
                Qs = Qs2[:,0]
            else: ## Qs < min([Qs1 Qs2[1]])
                convlimviol = 0
        else: ## Qs < imag(MPL1)
            if Qs < r_[Qs1,Qs2[:,0]].max():
                    convlimviol = 1
                    Qs = r_[Qs1,Qs2[:,0]].max()
            elif Qs > Qs2[:,1] :
                    convlimviol = 1
                    Qs = Qs2[:,1]
            else: ## Qs < min([Qs1 Qs2[1]])
                    convlimviol = 0

    ## active power outside of current limit active power range
    elif Ps <= Pmin: ## grid injected active power lower than minimum value
        ## set active power to minimum
        convlimviol = 2
        Ps = Pmin
        Qs = QPmin


    else:  ## if PmaxL1 <= Ps %% grid injected active power higher than maximum value
        ## set active power to maximum
        convlimviol = 2
        Ps = Pmax
        Qs = QPmax

    ## define output argument Ss
    Ss = Ps + j*Qs

    ## remove violation when difference is small
    if abs(Ssold-Ss)<epslim:
        convlimviol = 0

    ## define plot arguments
    if convlimviol == 1:
        SsIcmax = Ps+j*Qs1
        SsVcmin = Ps+j*Qs2.min()
        SsVcmax = Ps+j*Qs2.max()
        plotarg = r_[convlimviol, Ztf, Zf, Zc, Y1, Y2, Yf, Ytf, Vs, Icmax,\
                Vcmax, Vcmin, Ssold, Ss, SsIcmax, SsVcmax, SsVcmin]
    else:
        plotarg = r_[convlimviol, Ztf, Zf, Zc, Y1, Y2, Yf, Ytf, Vs, Icmax,\
                Vcmax, Vcmin, Ssold, Ss, 0, 0, 0];

    return convlimviol, Ss, plotarg
Пример #17
0
def cost_sparse(n: int, lam: float, d: int, dE: float, chi: int,
                stps: int) -> Tuple[int, int, int]:
    """ Determine fault-tolerant costs using sparse decomposition in quantum
        chemistry

    Args:
        n (int) - the number of spin-orbitals
        lam (float) - the lambda-value for the Hamiltonian
        dE (float) - allowable error in phase estimation
        L (int) - the rank of the first decomposition
        Lxi (int) - the total number of eigenvectors
        chi (int) - equivalent to aleph_1 and aleph_2 in the document, the
            number of bits for the representation of the coefficients
        beta (int) - equivalent to beth in the document, the number of bits
            for the rotations
        stps (int) - an approximate number of steps to choose the precision
            of single qubit rotations in preparation of the equal superposition
            state

    Returns:
        step_cost (int) - Toffolis per step
        total_cost (int) - Total number of Toffolis
        ancilla_cost (int) - Total ancilla cost
    """

    # I think there is a bug in the mathematica notebook. It does not check if
    # 2 is a factor first, which it should, cf. the similar function in
    # costingdf.nb Below is correct using the power_two() function, to give
    # power of 2 that is a factor of d.
    eta = power_two(d)

    nN = np.ceil(np.log2(n // 2))

    m = chi + 8 * nN + 4  # Eq (A13)

    oh = [0] * 20

    nM = (np.ceil(np.log2(d)) - eta) / 2

    for p in range(2, 22):
        # JJG note: arccos arg may be > 1
        v = np.round(np.power(2,p+1) / (2 * np.pi) * arccos(np.power(2,nM) /\
            np.sqrt(d/2**eta)/2))
        oh[p-2] = np.real(stps * (1 / (np.sin(3 * arcsin(np.cos(v * 2 * np.pi /\
            np.power(2,p+1)) * \
            np.sqrt(d/2**eta) / np.power(2,nM)))**2) - 1) + 4 * (p + 1))

    # Bits of precision for rotation
    br = int(np.argmin(oh) + 1) + 2

    # Hand selecting the k expansion factor
    k1 = 32

    # Equation (A17)
    cost = np.ceil(d/k1) + m * (k1 -1) + QI(d)[1] + 4 * n + 8 * nN + 2 * chi + \
        7 * np.ceil(np.log2(d)) - 6 * eta + 4 * br - 19

    # Number of iterations needed for the phase estimation.
    iters = np.ceil(np.pi * lam / (dE * 2))

    # The following are the number of qubits from the listing on page 39.

    # Control registers for phase estimation and iteration on them.
    ac1 = 2 * np.ceil(np.log2(iters)) - 1

    # System qubits
    ac2 = n

    # The register used for the QROM
    ac3 = np.ceil(np.log2(d))

    # The qubit used for flagging the success of the equal superposition state
    # preparation and the ancilla qubit for rotation
    ac45 = 2

    # The phase gradient state
    ac6 = br

    # The equal superposition state for coherent alias sampling
    ac7 = chi

    # The ancillas used for QROM
    ac8 = np.ceil(np.log2(d / k1)) + m * k1

    ancilla_cost = ac1 + ac2 + ac3 + ac45 + ac6 + ac7 + ac8

    # Sanity checks before returning as int
    assert cost.is_integer()
    assert iters.is_integer()
    assert ancilla_cost.is_integer()

    step_cost = int(cost)
    total_cost = int(cost * iters)
    ancilla_cost = int(ancilla_cost)

    return step_cost, total_cost, ancilla_cost
Пример #18
0
def angl(v1, v2):
    a = v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]
    b = sqrt(pow(v1[0], 2) + pow(v1[1], 2) + pow(v1[2], 2))
    c = sqrt(pow(v2[0], 2) + pow(v2[1], 2) + pow(v2[2], 2))
    cos = a / (b * c)
    return arccos(cos)