def test_evolution_operator(n_qubits, n_levels):
    #generate random parameters
    params = 0.01*np.random.rand(2, n_levels)
    gammas = params[0]
    betas = params[1]
    #generate random graph
    edges = []
    while len(edges) < 1:
        prob = 0.5
        graph = erdos_renyi_graph(n_qubits, prob)
        edges = list(graph.edges)
    #generate random n-qubits state
    list_gen_state = qucs.n_rand_qubits(n_qubits)
    gen_state = qu.tensor(list_gen_state)
    #Test if it works as expected
    obs = qaoa.evolution_operator(n_qubits, edges, gammas, betas)*gen_state
    exp = gen_state
    for i in range(len(gammas)):
        u_mix_hamilt_i = (-complex(0,betas[i])*qaoa.mix_hamilt(n_qubits)).expm()
        u_prob_hamilt_i = (-complex(0,gammas[i])*qaoa.prob_hamilt(n_qubits, edges)).expm()
        exp = u_mix_hamilt_i*u_prob_hamilt_i*exp
    assert (np.round(np.array(exp.full()), 8) == (np.round(np.array(obs.full()), 8))).all()
    #test if it evolves a state for known parameters
    exp = qu.qload('final_state_simple_graph_p=1')
    obs = qaoa.evolution_operator(3, [(0,1),(1,2)], [1.0], [0.4])*qaoa.initial_state(3)
def test_comp_basis_prob_dist(n_qubits):
    #generate a generic n-qubits state
    list_gen_state = qucs.n_rand_qubits(n_qubits)
    gen_state = qu.tensor(list_gen_state)
    #Test that, gor a generic qstate, sum probabilities is 1
    exp = 1.0
    obs = round(sum(qucs.comp_basis_prob_dist(gen_state)), 14)
    assert_equal(exp, obs)
def test_mix_hamilt(n_qubits):
    #generate a generic n-qubits state
    list_gen_state = qucs.n_rand_qubits(n_qubits)
    gen_state = qu.tensor(list_gen_state)
    #test is ìf the result is the one expected
    obs = qaoa.mix_hamilt(n_qubits)*gen_state
    list_exp = []
    for i in range(0,n_qubits):
        list_exp.append(qucs.n_sigmax(n_qubits,i)*gen_state)
    exp = sum(list_exp)
    assert_equal(obs,exp)
def test_n_ranf_qubits(n_qubits):
    #Initialazing the initial state
    list_qubits = qucs.n_rand_qubits(n_qubits)
    #Test if the shape of each qubit is (2,1)
    for i in range(n_qubits):
        exp = (2,1)
        obs = list_qubits[i].shape
        assert_equal(exp,obs)
    #Test if each qubit is a ket
    for i in range(n_qubits):
        exp = 'ket'
        obs = list_qubits[i].type
        assert_equal(exp,obs)
def test_n_sigmaz(n_qubits):
    #generate a generic n-qubits state
    list_gen_state = qucs.n_rand_qubits(n_qubits)
    gen_state = qu.tensor(list_gen_state)
    #Test if n_sigmaz applies sigmaz on qubit in qubit_pos
    qubit_pos = np.random.randint(0,n_qubits)
    list_exp = []
    for i in range(n_qubits):
        if i == qubit_pos:
            list_exp.append(qu.sigmaz()*list_gen_state[i])
        else:
            list_exp.append(list_gen_state[i])
    exp = qu.tensor(list_exp)
    obs = qucs.n_sigmaz(n_qubits,qubit_pos)*gen_state
    assert_equal(exp, obs)
def test_prob_hamilt(n_qubits):
    #generate a generic n-qubits state
    list_gen_state = qucs.n_rand_qubits(n_qubits)
    gen_state = qu.tensor(list_gen_state)
    #generate a random graph of n-vertices
    edges = []
    while len(edges) < 1:
        prob = 0.5
        graph = erdos_renyi_graph(n_qubits, prob)
        edges = list(graph.edges)
    #test is ìf the result is the one expected
    obs = qaoa.prob_hamilt(n_qubits,edges)*gen_state
    list_exp = []
    for j in range(0,len(edges)):
        list_exp.append(0.5*(qucs.n_qeye(n_qubits)
               -qucs.n_sigmaz(n_qubits,edges[j][0])*qucs.n_sigmaz(n_qubits,edges[j][1]))*gen_state)
    exp = sum(list_exp)
    assert_equal(obs,exp)