Example #1
0
def evaluate_4TTN(params, training_data, ancilla, total, rounding):
    answers = np.zeros(training_data.shape[0])
    L = training_data.shape[1] - 1
    for i in range(training_data.shape[0]):
        """FOR EACH DATA POINT"""
        """Lets Encode the data elements first"""

        psi = initial_encode(training_data[i, :L], ancilla)
        """Stage 1: Unitaries on all of the qubits, ancilla or not"""
        for j in range(total):
            psi = np.matmul(ry(params[j], j, total), psi)
            """Stage 2: CNOT plus unitary for N-1 times (cascading)"""
        j = 0
        psi = np.matmul(cnot(j, j + 1, total), psi)
        psi = np.matmul(ry(params[total], j + 1, total), psi)
        j = 3
        psi = np.matmul(cnot(j, j - 1, total), psi)
        psi = np.matmul(ry(params[total + 1], j - 1, total), psi)
        j = 1
        psi = np.matmul(cnot(j, j + 1, total), psi)
        psi = np.matmul(ry(params[total + 2], j + 1, total), psi)
        """Stage 3: Trace and Measure"""
        zero_prob = prob_zero(keep_n(psi, total, 2))
        """Stage 4: Calculate Cost"""
        answers[i] = eval_cost(zero_prob, training_data[i, L], rounding)
    total_cost = np.sum(answers) / training_data.shape[0]
    return total_cost
def PEPS_3(params, training_data, ancilla, total, rounding):
    """Requires 27 parameters. """

    answers = np.zeros(training_data.shape[0])
    L = training_data.shape[1] - 1
    for i in range(training_data.shape[0]):
        """Stage 1: The top left five block"""
        """Encoding 4-3-2-1 + ancilla"""
        psi = np.real(initial_encode(training_data[i, :L], 0))
        psi = np.reshape(psi, (2**L, 1))

        psi = np.matmul(two_Q_2(params[0], params[1], 0, 1, total), psi)
        psi = np.matmul(two_Q_2(params[2], params[3], 2, 1, total), psi)
        psi = np.matmul(two_Q_2(params[4], params[5], 1, 4, total), psi)
        psi = np.matmul(two_Q_2(params[6], params[7], 0, 3, total), psi)
        psi = np.matmul(two_Q_2(params[8], params[9], 6, 3, total), psi)
        psi = np.matmul(two_Q_2(params[10], params[11], 3, 4, total), psi)
        psi = np.matmul(two_Q_2(params[12], params[13], 6, 7, total), psi)
        psi = np.matmul(two_Q_2(params[14], params[15], 8, 7, total), psi)
        psi = np.matmul(two_Q_2(params[16], params[17], 7, 4, total), psi)
        psi = np.matmul(two_Q_2(params[18], params[19], 2, 5, total), psi)
        psi = np.matmul(two_Q_2(params[20], params[21], 8, 5, total), psi)
        psi = np.matmul(two_Q_2(params[22], params[23], 5, 2, total), psi)

        psi = np.matmul(ry(params[24], 4, total), psi)
        """Stage 3: Trace and Measure"""
        zero_prob = prob_zero(keep_n(psi, total, 4))
        """Stage 4: Calculate Cost"""
        answers[i] = eval_cost(zero_prob, training_data[i, L], rounding)

    total_cost = np.sum(answers) / training_data.shape[0]
    return total_cost