def _test_mat_to_op(hamiltonian_operator, jmin=0.5, jmax=100, tol=1e-8):
    """
    Tests that eigs computed using a function that generates hamiltonian
    operators are correct. This test doesn't check for extra eigs but
    only that the ones that should be there is there.
    @author: kajoel
    """
    from core.lipkin_quasi_spin import hamiltonian, eigs
    from openfermion.transforms import get_sparse_operator

    no_error = True
    for j2 in range(round(2 * jmin), round(2 * jmax) + 1):
        j = j2 / 2
        print("j = " + str(j))
        V = float(np.random.randn(1))
        H = hamiltonian(j, V)
        E = eigs(j, V)
        for i in range(len(H)):
            H_op = hamiltonian_operator(H[i])
            H_op = get_sparse_operator(H_op).toarray()
            E_op = np.linalg.eigvals(H_op)
            # Check that E_op contains all eigs in E[i]
            for E_ in E[i]:
                if all(abs(E_op - E_) > tol):
                    no_error = False
                    print("Max diff: " + str(max(abs(E_op - E_))))

    if no_error:
        print("Success!")
    else:
        print("Fail!")
Ejemplo n.º 2
0
def hamiltonians_of_size(size: int, V=1., e=1.) -> tuple:
    """

    :param size:
    :param V:
    :param e:
    :return:
    """
    mats = (
        hamiltonian(size - 1, V, e)[0],
        hamiltonian(size - 1 / 2, V, e)[0],
        hamiltonian(size - 1 / 2, V, e)[1],
        hamiltonian(size, V, e)[1]
    )
    eigs = (
        smallest_eig(mats[0]),
        smallest_eig(mats[1]),
        smallest_eig(mats[2]),
        smallest_eig(mats[3])
    )
    return mats, eigs
Ejemplo n.º 3
0
def _test_depth(ansatz, n_min=1, n_max=12, m=2, max_ops=10000):
    nn = n_max - n_min + 1
    nbr_ops = np.zeros(nn)
    for i in range(nn):
        n = n_min + i
        h = hamiltonian(n, 1)[0]  # (n+1)x(n+1) maxtrix
        temp = np.empty(m)
        for j in range(m):
            temp[j] = len(ansatz(h)(np.random.randn(n)))
        if np.any(temp[0] != temp):
            print(f'\nDifferent lengths for {ansatz}')
            print(temp)
        nbr_ops[i] = np.average(temp)
        print(n)
        if nbr_ops[i] > max_ops:
            return nbr_ops[:i + 1]
    return nbr_ops
def _test_complexity(mat2op, jmin=0.5, jmax=25):
    from core import lipkin_quasi_spin

    n = 1 + round(2 * (jmax - jmin))
    nbr_terms = np.empty(2 * n)
    max_nbr_ops = np.zeros(2 * n)
    matrix_size = np.empty(2 * n)
    for i in range(n):
        j = jmin + 0.5 * i
        print(j)
        H = lipkin_quasi_spin.hamiltonian(j, np.random.randn(1)[0])
        for k in range(len(H)):
            matrix_size[2 * i + 1 - k] = H[k].shape[0]
            terms = mat2op(H[k])
            nbr_terms[2 * i + 1 - k] = len(terms)
            for term in terms:
                if len(term) > max_nbr_ops[2 * i + 1 - k]:
                    max_nbr_ops[2 * i + 1 - k] = len(term)

    return matrix_size, nbr_terms, max_nbr_ops
Ejemplo n.º 5
0
def input_3(V, j, i):
    # Example of setting up the hamiltonian.
    return [lipkin_quasi_spin.hamiltonian(V, j)[i]]
from core import lipkin_quasi_spin, init_params
from pyquil import get_qc

from core import vqe_eig
from core import ansatz
from core import callback as cb

import numpy as np
from core import matrix_to_op

samples = 5000
sweep_params = 30

qc = get_qc('3q-qvm')
j, V = 1, 1
h = lipkin_quasi_spin.hamiltonian(j, V)[0]

# plot = liveplot.Liveplot()
# vqe_analysis.sweep_parameters(h, qc, new_version=True, samples=None,
#                               num_para=sweep_params, start=-3, stop=3, callback=
#                               plot.plotline, plot=False)

energies = vqe_eig.smallest(matrix_to_op.multi_particle(h),
                            qc,
                            init_params.ones(h.shape[0]),
                            ansatz_=ansatz.multi_particle(h.shape[0]),
                            samples=samples,
                            fatol=1e-1 * 16 / np.sqrt(samples),
                            return_all_data=True,
                            callback=cb.merge_callbacks(
                                cb.scatter(), cb.stop_if_stuck_x_times(2)))