Exemple #1
0
def numpartition_qaoa(n_step, nums, minimizer=None, sampler=None):
    """Do the Number partition QAOA.

    :param n_step: The number of step of QAOA
    :param nums: The edges list of the graph.
    :returns Vqe object
    """
    hamiltonian = pauli.Expr.zero()
    for i, x in enumerate(nums):
        hamiltonian += pauli.Z[i] * x
    hamiltonian = (hamiltonian**2).simplify()

    return vqe.Vqe(vqe.QaoaAnsatz(hamiltonian, n_step), minimizer, sampler)
Exemple #2
0
def factoring_qaoa(n_step, num, minimizer=None, sampler=None, verbose=True):
    """Do the Number partition QAOA.

    :param num: The number to be factoring.
    :param n_step: The number of step of QAOA
    :param edges: The edges list of the graph.
    :returns result of QAOA
    """
    def get_nbit(n):
        m = 1
        while 2**m < n:
            m += 1
        return m

    n1_bits = get_nbit(int(num**0.5)) - 1
    n2_bits = get_nbit(int(num**0.5))

    def mk_expr(offset, n):
        expr = pauli.Expr.from_number(1)
        for i in range(n):
            expr = expr + 2**(i + 1) * q(i + offset)
        return expr

    def bitseparator(bits):
        assert len(bits) == n1_bits + n2_bits
        p = 1
        m = 1
        for b in bits[:n1_bits]:
            if b:
                p += 2**m
            m += 1
        q = 1
        m = 1
        for b in bits[n1_bits:]:
            if b:
                q += 2**m
            m += 1
        return p, q

    hamiltonian = (num - mk_expr(0, n1_bits) * mk_expr(n1_bits, n2_bits))**2
    return vqe.Vqe(vqe.QaoaAnsatz(hamiltonian, n_step), minimizer,
                   sampler), bitseparator
def maxcut_qaoa(n_step, edges, minimizer=None, sampler=None, verbose=True):
    """Setup QAOA.

    :param n_step: The number of step of QAOA
    :param n_sample: The number of sampling time of each measurement in VQE.
                     If None, use calculated ideal value.
    :param edges: The edges list of the graph.
    :returns Vqe object
    """
    sampler = sampler or vqe.non_sampling_sampler
    minimizer = minimizer or vqe.get_scipy_minimizer(
        method="Powell",
        options={"ftol": 5.0e-2, "xtol": 5.0e-2, "maxiter": 1000, "disp": True}
    )
    hamiltonian = pauli.I() * 0

    for i, j in edges:
        hamiltonian += pauli.Z(i) * pauli.Z(j)

    return vqe.Vqe(vqe.QaoaAnsatz(hamiltonian, n_step), minimizer, sampler)
Exemple #4
0
 def qaoa(self, shots=1, step=2, verbose=False):
     from blueqat import vqe
     return vqe.Vqe(vqe.QaoaAnsatz(pauli(self.qubo), step)).run()
Exemple #5
0
import numpy as np
from blueqat import vqe, opt
from blueqat.pauli import *
from blueqat.pauli import qubo_bit as q

# %% maxcut
hamiltonian = 0.5 * Z[0] * Z[1] + 0.5 * Z[0] * Z[3] + 0.5 * Z[1] * Z[
    2] + 0.5 * Z[2] * Z[3] + 0.5 * Z[3] * Z[4] + 0.5 * Z[2] * Z[4] - 3
step = 2

result = vqe.Vqe(vqe.QaoaAnsatz(hamiltonian, step)).run()
print(result.most_common(12))

# %% and gate
hami1 = -1 * q(0) + q(1)
hamiltonian = q(
    2) + q(0) * q(1) + q(1) * q(2) + q(0) * q(2) - q(0) * q(1) * q(2) * 4
step = 4
result = vqe.Vqe(vqe.QaoaAnsatz(hami1 + hamiltonian, step)).run()
print(result.most_common(12))

# %% xor get_rating_error
hami1 = -1 * q(0) + q(1)
hamiltonian = q(0) + q(1) + q(
    2) + q(0) * q(1) * q(2) * 4 - (q(0) * q(1) + q(1) * q(2) + q(0) * q(2)) * 2
step = 4
result = vqe.Vqe(vqe.QaoaAnsatz(hami1 + hamiltonian, step)).run()
print(result.most_common(12))

# %% binary integer linear programming
E = opt.optm("(3*q0+2*q1+q2-3)^2+(5*q0+2*q1+3*q2-5)^2-2*(q0+2*q1+q2)", 3)
Exemple #6
0
def ground_energy_ucc(hamiltonian, steps=6):
    runner = vqe.Vqe(UCCAnsatz(hamiltonian, steps, Circuit().x[0]))
    result = runner.run()
    return runner.ansatz.get_energy(result.circuit, runner.sampler)