示例#1
0
"""
Finding the minimum energy for an Ising problem by QAOA.
"""
import numpy as np
from scipy.optimize import minimize

import pyquil.api as api
from grove.pyqaoa.qaoa import QAOA
from pyquil.paulis import PauliSum, PauliTerm

CXN = api.QVMConnection()


def energy_value(h, J, sol):
    """
    Obtain energy of an Ising solution for a given Ising problem (h,J).

    :param h: (dict) External magnetic term of the Ising problem.
    :param J: (dict) Interaction terms of the Ising problem (may be k-local).
    :param sol: (list) Ising solution.
    :return: Energy of the Ising string.
    :rtype: Integer or float.

    """
    ener_ising = 0
    for elm in J.keys():
        paired_indices =  [(a, b) for a, b in zip(elm, elm)]
        if len(paired_indices) != len(set(paired_indices)):
            raise TypeError("Interaction term must connect different variables. "
                            "The term {} contains a duplicate.".format(elm))
        else:
示例#2
0
# =============================================================================
# imports
# =============================================================================

from pyquil.quil import Program
from pyquil import api
import pyquil.gates as gates
import sys
import time

# =============================================================================
# program and simulator
# =============================================================================

qprog = Program()
qvm = api.QVMConnection()

# =============================================================================
# number of qubits, depth, classical memory
# =============================================================================

# grab the number of qubits
if len(sys.argv) > 1:
    n = int(sys.argv[1])
else:
    n = 12

# grab the depth
if len(sys.argv) > 1:
    depth = int(sys.argv[2])
else:
def make_qvm(qvm=None):
    if qvm:
        return qvm
    else:
        return api.QVMConnection()