Beispiel #1
0
def build_rotation(hi, basis):
    localop = op.LocalOperator(hi, constant=1.0)
    U_X = 1.0 / (ma.sqrt(2)) * np.asarray([[1.0, 1.0], [1.0, -1.0]])
    U_Y = 1.0 / (ma.sqrt(2)) * np.asarray([[1.0, -1j], [1.0, 1j]])

    N = hi.size

    assert len(basis) == hi.size
    for j in range(hi.size):
        if basis[j] == "X":
            localop *= op.LocalOperator(hi, U_X, [j])
        if basis[j] == "Y":
            localop *= op.LocalOperator(hi, U_Y, [j])
    return localop
Beispiel #2
0
def load(path_to_samples, path_to_bases):
    tsamples = np.loadtxt(path_to_samples)
    fin_bases = open(path_to_bases, 'r')
    lines = fin_bases.readlines()
    N = (len(lines[0].strip('\n')))

    # Create the hilbert space
    # TODO remove Hypercube here and put customgraph
    g = gr.Hypercube(length=N, n_dim=1)
    hi = hs.Qubit(graph=g)

    bases = []

    for b in lines:
        basis = ""
        assert (len(b) == N + 1)
        for j in range(N):
            basis += b[j]
        bases.append(basis)
    index_list = sorted(range(len(bases)), key=lambda k: bases[k])
    bases.sort()

    training_samples = []
    training_bases = []
    for i in range(len(tsamples)):
        training_samples.append(tsamples[index_list[i]].tolist())

    U_X = (1. / (m.sqrt(2)) * np.asarray([[1., 1.], [1., -1.]])).tolist()
    U_Y = (1. / (m.sqrt(2)) * np.asarray([[1., -1j], [1., 1j]])).tolist()

    rotations = []

    tmp = ''
    b_index = -1
    for b in bases:
        if (b != tmp):
            tmp = b
            localop = op.LocalOperator(hi, 1.0)

            for j in range(N):
                if (tmp[j] == 'X'):
                    localop *= op.LocalOperator(hi, U_X, [j])
                if (tmp[j] == 'Y'):
                    localop *= op.LocalOperator(hi, U_Y, [j])

            rotations.append(localop)
            b_index += 1
        training_bases.append(b_index)

    return hi, tuple(rotations), training_samples, training_bases