Пример #1
0
def store_all_two_qubit_gates():
    m = 2
    qubit_gates = dict()
    qubit_matrices = dict()

    for i in range(symplectic.numberofsymplectic(m)):
        S = symplectic.symplectic(i, m)
        S = decompose.transform_symplectic(S)
        gates = decompose.decompose_state(chp_py.CHP_Simulation(m, S))
        qubit_gates[i] = gates
        qubit_matrices[i] = S

    save_data(qubit_gates, "two_qubit_gates")
    save_data(qubit_matrices, "two_qubit_matrices")
def test_collision_probability():
    """
    Test cases for the collision probability algorithm
    Returns True if passed all test cases, otherwise returns False
    """
    print("Testing: collision probability algorithm")
    # n = number of qubits, m = size of qubit gate
    for n in range(1, 10):
        # sim1 is fast and sim2 is slow, as sim2 uses exponential
        # storage and runtime
        sim1 = chp_py.CHP_Simulation(n)
        sim2 = slow_sim.Slow_Simulation(n)

        for m in range(1, min(n, 5)):
            for j in range(100):
                # Get a random symplectic of size 2m x 2m
                i = np.random.randint(symplectic.numberofsymplectic(m))
                S = symplectic.symplectic(i, m)
                S = decompose.transform_symplectic(S)

                # Get m random qubits
                qubits = np.arange(m)
                np.random.shuffle(qubits)
                qubits = qubits[:m]

                # Get the gates and matrix represention of S
                gates = decompose.decompose_state(chp_py.CHP_Simulation(m, S))
                gates = decompose.change_gates(gates, qubits)

                decompose.apply_gates(gates, sim1)
                decompose.apply_gates(gates, sim2)

                k_1 = int(-sim1.log_collision_probability)
                k_2 = np.round(-np.log2(sim2.collision_probability))
                result = (k_1 == k_2)

                if not result:
                    print("Failed: collision probability algorithm returned " +
                          "incorrect result with n = " + str(n) + " and m = " +
                          str(m))
                    return False

    print("Passed: collision probability algorithm passed all tests\n")
    return True
def test_chp_py():
    """
    Test cases for the functions in chp_py.py
    Returns True if passed all test cases, otherwise returns False
    """
    print("Testing: chp_py.py")
    # n = number of qubits, m = size of qubit gate
    for n in range(1, 50):
        # Create two simulations
        # sim1 uses decomposed gates and sim2 uses matrix multiplication
        # apply 100 random gates to each one
        sim1 = chp_py.CHP_Simulation(n)
        sim2 = chp_py.CHP_Simulation(n)

        for m in range(1, min(n, 5)):
            for j in range(100):
                # Get a random symplectic of size 2m x 2m
                i = np.random.randint(symplectic.numberofsymplectic(m))
                S = symplectic.symplectic(i, m)
                S = decompose.transform_symplectic(S)

                # Get m random qubits
                qubits = np.arange(m)
                np.random.shuffle(qubits)
                qubits = qubits[:m]

                # Get the gates and matrix represention of S
                gates = decompose.decompose_state(chp_py.CHP_Simulation(m, S))
                gates = decompose.change_gates(gates, qubits)
                M = decompose.symplectic_to_matrix(S, n, qubits)

                sim1.apply_gates(gates)
                sim2.state = (sim2.state @ M) % 2
                result = (sim1.state == sim2.state).all()

                if not result:
                    print("Failed: found two simulations with different " +
                          "states for n = " + str(n))
                    return False

    print("Passed: chp_py.py passed all tests\n")
    return True
    def apply_random_symplectic(self, qubits):
        """
        Generates a random symplectic gate and then applies it
        to the qubits in the list qubits
        """
        # Here m is the number of qubits that the gate will be applied to
        # while n is the total number of qubits in the simulation
        m = len(qubits)

        # Generate a random symplectic matrix that is
        # symplectic with L = direct_sum_{j=1}^n X
        i = np.random.randint(symplectic.numberofsymplectic(m))
        S = symplectic.symplectic(i, m)

        # Convert this symplectic matrix to one that is symplectic
        # with L = [[0, I], [I, 0]]
        S = decompose.transform_symplectic(S)

        # Lastly, apply this to our state
        self.apply_symplectic(S, qubits)
def test_decompose():
    """
    Test cases for the functions in decompose.py
    Returns True if passed all test cases, otherwise returns False
    """
    print("Testing: decompose.py")

    for n in range(1, 6):
        top = np.hstack((np.zeros((n, n)), np.identity(n)))
        bottom = np.hstack((np.identity(n), np.zeros((n, n))))
        L = np.vstack((top, bottom))

        for j in range(500):
            # Get a random symplectic
            i = np.random.randint(symplectic.numberofsymplectic(n))
            S = symplectic.symplectic(i, n)
            S = decompose.transform_symplectic(S)

            # Make sure the transformation worked
            result = (S.T @ L @ S) % 2
            result = ((result == L).all())
            if not result:
                print("Failed: could not transform symplectic " +
                      "matrix with (n, i)= " + str((n, i)))
                return False

            # Make sure we can decompose it
            # Applying the decomposed gates to the identity should give us S
            gates = decompose.decompose_state(chp_py.CHP_Simulation(n, S))
            new_sim = chp_py.CHP_Simulation(n)
            new_sim.apply_gates(gates)
            result = (new_sim.state == S).all()

            if not result:
                print("Failed: found a bad decomposition for " +
                      "symplectic matrix with (n, i)= " + str((n, i)))
                return False

    print("Passed: decompose.py passed all tests\n")
    return True