示例#1
0
def batch_rz_opt(batch_params):

    batch_params = batch_params /2

    batch_real = None
    batch_imag = None

    for i in range(batch_params.size(1)):

        params_slice = batch_params[:,i:i+1]

        params_real = tcat([ones(1,2)*cos(param) for param in params_slice], dim=0)
        params_imag = tcat([tcat([-sin(param).view(1,1),sin(param).view(1,1)], dim=1) for param in params_slice], dim=0)

        batch_real_ = params_real if batch_real is None else batch_k_rz(params_real,batch_real) - batch_k_rz(params_imag,batch_imag)
        batch_imag_ = params_imag if batch_imag is None else batch_k_rz(params_real,batch_imag) + batch_k_rz(params_imag,batch_real)

        batch_real = batch_real_
        batch_imag = batch_imag_

    hm_params = batch_real.size(1)

    batch_real = tstack([tstack([batch_real[:,rowcol]]*hm_params,dim=1) for rowcol in range(hm_params)],dim=2)
    batch_imag = tstack([tstack([batch_imag[:,rowcol]]*hm_params,dim=1) for rowcol in range(hm_params)],dim=2)

    return ComplexTensor.__graph_copy__(None,batch_real,batch_imag)
示例#2
0
def ry_a0(params_slice, qbit_id):

    batch_size = params_slice.size(0)
    sector_size = 2**qbit_id
    group_size = sector_size*2

    params_slice = params_slice.view(batch_size,1,1)

    group = cos(params_slice) * tstack([eye(group_size,group_size)]*batch_size,dim=0)

    sector_leftdown = sin(params_slice) * tstack([eye(sector_size,sector_size)]*batch_size,dim=0)
    sector_rightup = -sector_leftdown

    group[:,sector_size:,:sector_size] = sector_leftdown
    group[:,:sector_size,sector_size:] = sector_rightup

    hm_repeats = config.statevec_size//group_size

    matrix = zeros(batch_size,config.statevec_size,config.statevec_size)

    for i in range(hm_repeats):
        f = i*group_size
        t = (i+1)*group_size

        matrix[:,f:t,f:t] = group

    return matrix
示例#3
0
def batch_rz(batch_params):

    batch_params = batch_params /2

    batch_real = None
    batch_imag = None

    for i in range(batch_params.size(1)):

        params_slice = batch_params[:,i:i+1]

        params_real = tcat([tcat([cos(param).view(1,1)]*2, dim=1) for param in params_slice], dim=0)
        params_imag = tcat([tcat([-sin(param).view(1,1),sin(param).view(1,1)], dim=1) for param in params_slice], dim=0)

        batch_real_ = params_real if batch_real is None else batch_k_rz(params_real,batch_real) - batch_k_rz(params_imag,batch_imag)
        batch_imag_ = params_imag if batch_imag is None else batch_k_rz(params_real,batch_imag) + batch_k_rz(params_imag,batch_real)

        batch_real = batch_real_
        batch_imag = batch_imag_

    batch_real = tstack([eye(batch_real.size(1),batch_real.size(1))]*batch_real.size(0),dim=0) \
                * batch_real.view(batch_real.size(0),batch_real.size(1),1)

    batch_imag = tstack([eye(batch_imag.size(1),batch_imag.size(1))]*batch_imag.size(0),dim=0) \
                * batch_imag.view(batch_real.size(0),batch_real.size(1),1)

    return ComplexTensor.__graph_copy__(None,batch_real,batch_imag)
示例#4
0
def rotation_2d(points, angles):
    rot_sin = torch.sin(angles)
    rot_cos = torch.cos(angles)
    rot_mat_T = torch.stack(
        [tstack([rot_cos, rot_sin]),
         tstack([-rot_sin, rot_cos])])
    return torch.einsum('aij,jka->aik', (points, rot_mat_T))
def rotation_2d(points, angles):
    """rotation 2d points based on origin point clockwise when angle positive.

    Args:
        points (float array, shape=[N, point_size, 2]): points to be rotated.
        angles (float array, shape=[N]): rotation angle.

    Returns:
        float array: same shape as points
    """
    rot_sin = torch.sin(angles)
    rot_cos = torch.cos(angles)
    rot_mat_T = torch.stack([tstack([rot_cos, -rot_sin]), tstack([rot_sin, rot_cos])])
    return torch.einsum("aij,jka->aik", (points, rot_mat_T))
示例#6
0
def batch_rx(batch_params):

    batch_params = batch_params /2

    batch_real = None
    batch_imag = None

    for i in range(batch_params.size(1)):
        params_slice = batch_params[:,i:i+1]

        params_real = tstack([tcat([tcat([cos(param).view(1,1),tensor(.0).view(1,1)],dim=1),tcat([tensor(.0).view(1,1),cos(param).view(1,1)],dim=1)],dim=0) for param in params_slice],dim=0)
        params_imag = tstack([tcat([tcat([tensor(.0).view(1,1),-sin(param).view(1,1)],dim=1),tcat([-sin(param).view(1,1),tensor(.0).view(1,1)],dim=1)],dim=0) for param in params_slice],dim=0)

        batch_real_ = params_real if batch_real is None else batch_k_rx(params_real,batch_real) - batch_k_rx(params_imag,batch_imag)
        batch_imag_ = params_imag if batch_imag is None else batch_k_rx(params_real,batch_imag) + batch_k_rx(params_imag,batch_real)

        batch_real = batch_real_
        batch_imag = batch_imag_

    return ComplexTensor.__graph_copy__(None, batch_real, batch_imag)
示例#7
0
def batch_u3(batch_params):

    batch_real = None
    batch_imag = None

    for i in range(batch_params.size(1) // 3):
        params_slice = batch_params[:,i*3:(i+1)*3]

        params_real = tstack([tcat([tcat([cos(p0/2).view(1,1), -(cos(p2)*sin(p0/2)).view(1,1)], dim=1),
                                    tcat([(cos(p1)*sin(p0/2)).view(1,1), (cos(p1+p2)*cos(p0/2)).view(1,1)], dim=1)], dim=0) for p0,p1,p2 in
                              params_slice], dim=0)
        params_imag = tstack([tcat([tcat([tensor(.0).view(1,1), -(sin(p2)*sin(p0/2)).view(1,1)], dim=1),
                                    tcat([(sin(p1)*sin(p0/2)).view(1,1), (sin(p1+p2)*cos(p0/2)).view(1,1)], dim=1)], dim=0) for p0,p1,p2 in
                              params_slice], dim=0)

        batch_real_ = params_real if batch_real is None else batch_k_rx(params_real, batch_real) - batch_k_rx(params_imag, batch_imag)
        batch_imag_ = params_imag if batch_imag is None else batch_k_rx(params_real, batch_imag) + batch_k_rx(params_imag, batch_real)

        batch_real = batch_real_
        batch_imag = batch_imag_

    return ComplexTensor.__graph_copy__(None, batch_real, batch_imag)
示例#8
0
def batch_ry_opt(batch_params):

    batch_params = batch_params /2

    batch_real = None

    for i in range(batch_params.size(1)):

        params_slice = batch_params[:,i:i+1]

        params_real = tstack([tcat([tcat([cos(param).view(1,1), -sin(param).view(1,1)],dim=1),
                                    tcat([sin(param).view(1,1), cos(param).view(1,1)],dim=1)], dim=0) for param in
                              params_slice], dim=0)

        batch_real = params_real if batch_real is None else batch_k_rx(params_real, batch_real)

    return batch_real
示例#9
0
def batch_ry(batch_params):

    batch_params = batch_params /2

    batch_real = None

    for i in range(batch_params.size(1)):

        params_slice = batch_params[:,i:i+1]

        params_real = tstack([tcat([tcat([cos(param).view(1,1), -sin(param).view(1,1)],dim=1),
                                    tcat([sin(param).view(1,1), cos(param).view(1,1)],dim=1)], dim=0) for param in
                              params_slice], dim=0)

        batch_real = params_real if batch_real is None else batch_k_rx(params_real, batch_real)

    return ComplexTensor.__graph_copy__(None, batch_real, zeros(batch_real.size()))
示例#10
0
def batch_a0(batch_params):

    batch_params = batch_params /2

    cx_guide, circuit = circuit_a0()

    batch_real = tstack([eye(config.statevec_size,config.statevec_size)]*batch_params.size(0),dim=0)

    param_ctr = 0

    for _ in range(config.circuit_layers):

        for element in circuit:

            if element[0] == 'RY':

                batch_real = ry_a0(batch_params[:,param_ctr],element[1]) @ batch_real
                param_ctr += 1

            elif element[0] == 'CX':

                batch_real = cx_guide[element[1]][element[2]] @ batch_real

    return batch_real
示例#11
0
def rotation_3d_in_axis(points, angles, axis=0):
    # points: [N, point_size, 3]
    # angles: [N]
    rot_sin = torch.sin(angles)
    rot_cos = torch.cos(angles)
    ones = torch.ones_like(rot_cos)
    zeros = torch.zeros_like(rot_cos)
    if axis == 1:
        rot_mat_T = tstack([
            tstack([rot_cos, zeros, -rot_sin]),
            tstack([zeros, ones, zeros]),
            tstack([rot_sin, zeros, rot_cos])
        ])
    elif axis == 2 or axis == -1:
        rot_mat_T = tstack([
            tstack([rot_cos, -rot_sin, zeros]),
            tstack([rot_sin, rot_cos, zeros]),
            tstack([zeros, zeros, ones])
        ])
    elif axis == 0:
        rot_mat_T = tstack([
            tstack([zeros, rot_cos, -rot_sin]),
            tstack([zeros, rot_sin, rot_cos]),
            tstack([ones, zeros, zeros])
        ])
    else:
        raise ValueError("axis should in range")

    return torch.einsum('aij,jka->aik', (points, rot_mat_T))