Beispiel #1
0
def mis_qaoa(n, method='minimize', show=True, analytic_gradient=True):
    penalty = 1
    psi0 = tools.equal_superposition(n)
    psi0 = State(psi0)
    G = make_ring_graph_multiring(n)
    if show:
        nx.draw_networkx(G)
        plt.show()

    depths = [2 * i for i in range(1, 2 * n + 1)]
    mis = []
    # Find MIS optimum
    # Uncomment to visualize graph
    hc_qubit = hamiltonian.HamiltonianMIS(G, energies=[1, penalty])
    cost = hamiltonian.HamiltonianMIS(G, energies=[1, penalty])
    # Set the default variational operators
    hb_qubit = hamiltonian.HamiltonianDriver()
    # Create Hamiltonian list
    sim = qaoa.SimulateQAOA(G, cost_hamiltonian=cost, hamiltonian=[], noise_model=None)
    sim.hamiltonian = []
    for p in depths:
        sim.hamiltonian.append(hc_qubit)
        sim.hamiltonian.append(hb_qubit)
        sim.depth = p
        # You should get the same thing
        print(p)
        if method == 'minimize':
            results = sim.find_parameters_minimize(verbose=True, initial_state=psi0,
                                                   analytic_gradient=analytic_gradient)

            approximation_ratio = np.real(results['approximation_ratio'])
            mis.append(approximation_ratio)
        if method == 'brute':
            results = sim.find_parameters_brute(n=15, verbose=True, initial_state=psi0)
            approximation_ratio = np.real(results['approximation_ratio'])
            mis.append(approximation_ratio)
        if method == 'basinhopping':
            if p >= 10:
                results = sim.find_parameters_basinhopping(verbose=True, initial_state=psi0, n=250,
                                                           analytic_gradient=analytic_gradient)
                print(results)
                approximation_ratio = np.real(results['approximation_ratio'])
                mis.append(approximation_ratio)

    # plt.plot(list(range(n)), maxcut, c='y', label='maxcut')
    print(mis)
    plt.plot(depths, [(i + 1) / (i + 2) for i in depths])
    plt.scatter(depths, [i / (i + 1) for i in depths], label='maxcut')
    plt.scatter(depths, mis, label='mis with $n=$' + str(n))
    plt.plot(depths, mis)

    plt.legend()
    if show:
        plt.show()
    state[i] = 1
    i = 1 - np.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0])
    i = tools.nary_to_int(np.roll(i, j))
    state[i] = 1
print(np.argwhere(state != 0))
state = state / np.linalg.norm(state)
"""where_optimal = degeneracy(cost)
state[where_optimal] = 10
state = state/np.linalg.norm(state)"""
# state = generate_inital_state_cf(graph, diff=2, verbose=True)
# state = generate_initial_state_legal_gw(graph)
cost = hamiltonian.HamiltonianMaxCut(graph, cost_function=True)

# state = State(state[:, np.newaxis], is_ket=True, graph=graph)
driver = hamiltonian.HamiltonianDriver()
qaoa = qaoa.SimulateQAOA(graph=graph, hamiltonian=[], cost_hamiltonian=cost)
# print('oo',  cost.optimum_overlap(state))
print('cf', cost.cost_function(state))
print('maxcut', np.max(cost.hamiltonian))
for p in range(2, 10):
    max_result = 0
    print(p)
    hamiltonian = [driver] + [cost, driver] * p
    qaoa.hamiltonian = hamiltonian
    """num = 20
    gammas = np.linspace(0, np.pi/2, num)
    betas = np.linspace(0, np.pi, num)
    results = np.zeros((num,num))
    for g in range(len(gammas)):
        print(g)
        for b in range(len(betas)):
Beispiel #3
0
    def test_logical_codes(self):
        # Construct a known graph
        G = nx.random_regular_graph(1, 2)
        for e in G.edges:
            G[e[0]][e[1]]['weight'] = 1
        nx.draw_networkx(G)
        # Uncomment to visualize graph
        # plt.draw_graph(G)
        G = Graph(G)
        print('No logical encoding:')
        hc_qubit = hamiltonian.HamiltonianMIS(G)
        hamiltonians = [hc_qubit, hamiltonian.HamiltonianDriver()]
        sim = qaoa.SimulateQAOA(G,
                                cost_hamiltonian=hc_qubit,
                                hamiltonian=hamiltonians,
                                noise_model=None,
                                noise=noises)
        # Set the default variational operators
        results = sim.find_parameters_brute(n=10)
        self.assertTrue(np.isclose(results['approximation_ratio'], 1))

        print('Two qubit code:')
        hc_two_qubit_code = hamiltonian.HamiltonianMIS(G, code=two_qubit_code)
        hamiltonians = [
            hc_two_qubit_code,
            hamiltonian.HamiltonianDriver(code=two_qubit_code)
        ]

        sim_code = qaoa.SimulateQAOA(G,
                                     code=two_qubit_code,
                                     cost_hamiltonian=hc_two_qubit_code,
                                     hamiltonian=hamiltonians)

        # Find optimal parameters via brute force search
        sim_code.find_parameters_brute(n=10)
        self.assertTrue(np.isclose(results['approximation_ratio'], 1))

        print('Two qubit code with penalty:')
        # Set the default variational operators with a penalty Hamiltonian
        hc_qubit = hamiltonian.HamiltonianMIS(G, code=two_qubit_code)
        hamiltonians = [
            hc_qubit,
            hamiltonian.HamiltonianBookatzPenalty(code=two_qubit_code),
            hamiltonian.HamiltonianDriver(code=two_qubit_code)
        ]
        sim_penalty = qaoa.SimulateQAOA(G,
                                        cost_hamiltonian=hc_qubit,
                                        hamiltonian=hamiltonians,
                                        code=two_qubit_code)
        # You should get the same thing
        results = sim_penalty.find_parameters_brute(n=10)
        self.assertTrue(np.isclose(results['approximation_ratio'], 1))

        print('Jordan-Farhi-Shor code:')
        hc_jordan_farhi_shor = hamiltonian.HamiltonianMIS(
            G, code=jordan_farhi_shor)
        hamiltonians = [
            hc_jordan_farhi_shor,
            hamiltonian.HamiltonianDriver(code=jordan_farhi_shor)
        ]

        sim_code = qaoa.SimulateQAOA(G,
                                     code=jordan_farhi_shor,
                                     cost_hamiltonian=hc_jordan_farhi_shor,
                                     hamiltonian=hamiltonians)

        sim_code.find_parameters_brute(n=10)
        self.assertTrue(np.isclose(results['approximation_ratio'], 1))
Beispiel #4
0
from qsim.graph_algorithms import qaoa
from qsim.codes import two_qubit_code, jordan_farhi_shor

# Generate sample graph
N = 6

g = sample_graph()
ring = ring_graph(N)

hc = hamiltonian.HamiltonianMaxCut(g)
hc_ring = hamiltonian.HamiltonianMaxCut(ring)
hb = hamiltonian.HamiltonianDriver()
hamiltonians = [hc, hb]
ring_hamiltonians = [hc_ring, hb]

sim = qaoa.SimulateQAOA(g, cost_hamiltonian=hc, hamiltonian=hamiltonians)
sim_ring = qaoa.SimulateQAOA(ring,
                             cost_hamiltonian=hc_ring,
                             hamiltonian=ring_hamiltonians)
sim_ket = qaoa.SimulateQAOA(g, cost_hamiltonian=hc, hamiltonian=hamiltonians)
sim_noisy = qaoa.SimulateQAOA(g,
                              cost_hamiltonian=hc,
                              hamiltonian=hamiltonians,
                              noise_model='channel')

# Initialize in |000000>
psi0 = State(equal_superposition(N))
rho0 = State(outer_product(psi0, psi0))

noises = [quantum_channels.DepolarizingChannel(rates=(.001, ))]
sim_noisy.noise = noises * 2