Esempio n. 1
0
    def test_hamiltonian_C(self):
        pass

        # Then compute MaxCut energies
        hc = hamiltonian.HamiltonianMaxCut(g)
        self.assertTrue(hc.hamiltonian[0, 0] == 0)
        self.assertTrue(hc.hamiltonian[-1, -1] == 0)
        self.assertTrue(hc.hamiltonian[2, 2] == 3)
Esempio n. 2
0
def generate_inital_state_cf(graph, diff=2, verbose=False):
    cost = hamiltonian.HamiltonianMaxCut(graph, cost_function=True)
    maxcut = np.max(cost.hamiltonian)
    target = maxcut - diff
    where_target = sparse.find(cost.hamiltonian.real == target)[0]
    state = np.zeros(2**graph.n)
    state[where_target] = 1
    if verbose:
        print('num nonzero', len(np.argwhere(state != 0)))
    return State(state[:, np.newaxis] / np.linalg.norm(state),
                 is_ket=True,
                 graph=graph)
Esempio n. 3
0
def rate_vs_eigenenergy(times, graph=line_graph(n=2), which='S'):
    """For REIT, compute the total leakage from the ground state to a given state. Plot the total leakage versus
    the final eigenenergy"""
    bad = np.arange(0, 2**graph.n, 1)
    if which == 'S':
        index = 0
    elif which == 'L':
        index = -1
    else:
        index = which
    bad = np.delete(bad, index)
    full_rates = np.zeros(len(bad))

    # Good is a list of good eigenvalues
    # Bad is a list of bad eigenvalues. If 'other', defaults to all remaining eigenvalues outside of 'good'
    def schedule(t, tf):
        phi = (tf - t) / tf * np.pi / 2
        x.energies = (np.sin(phi)**2, )

    x = hamiltonian.HamiltonianDriver(graph=graph,
                                      IS_subspace=False,
                                      energies=(1, ))
    zz = hamiltonian.HamiltonianMaxCut(graph,
                                       cost_function=False,
                                       energies=(1 / 2, ))
    #dissipation = lindblad_operators.SpontaneousEmission(graph=graph, rates=(1,))
    eq = SchrodingerEquation(hamiltonians=[x, zz])

    def compute_rate():
        # Construct the first order transition matrix
        energies, states = eq.eig(k='all')
        rates = np.zeros(len(bad))
        for j in range(graph.n):
            for i in range(len(bad)):
                rates[i] = rates[i] + (np.abs(
                    states[i].conj() @ qubit.left_multiply(
                        State(states[index].T), [j], qubit.Z))**2)[0, 0]
        # Select the relevant rates from 'good' to 'bad'
        print(rates)
        return rates

    for i in range(len(times)):
        print(times[i])
        schedule(times[i], 1)
        full_rates = full_rates + compute_rate()
        eigval, eigvec = eq.eig(k='all')
    return full_rates, eigval
Esempio n. 4
0
state = State(state, IS_subspace=False)
for j in range(1):
    i = 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
    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)
Esempio n. 5
0
from qsim.test.tools_test import sample_graph
from qsim.tools.tools import equal_superposition, outer_product
from qsim.graph_algorithms.graph import Graph, ring_graph
from qsim.codes.quantum_state import State
import networkx as nx
from qsim.evolution import quantum_channels, hamiltonian
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')