Commit 75ce26f5 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Work on QAOA PyXASM example



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent d9f7a038
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
# Run this from the command line like this
#
# python3 qaoa_circuit.py 

from qcor import *
import numpy as np
# Define a Bell kernel
@qjit
def qaoa_circ(q: qreg, cost_ham: PauliOperator, nbSteps: int, theta: List[float], beta: List[float]):
    # Start off in the uniform superposition
    for i in range(q.size()):
        H(q[i])
    
    terms = cost_ham.getNonIdentitySubTerms()
    for step in range(nbSteps):
        # TODO: this looks weird (terms is a vector)
        # we need to support Pythonic len() method
        for i in range(terms.size()):
            exp_i_theta(q, theta[step], terms[i])

        # Reference Hamiltonian: 
        for i in range(q.size()):
            ref_ham_term = X(i)
            exp_i_theta(q, beta[step], ref_ham_term)
   
# Allocate 4 qubits
q = qalloc(2)
n_steps = 3
# Use the standard parameterization scheme: one param per step
theta_angle = np.random.rand(n_steps)
beta_angle = np.random.rand(n_steps)
H = -5.0 - 0.5 * (Z(0) - Z(3) - Z(1) * Z(2)) - Z(2) + 2 * Z(0) * Z(2) + 2.5 * Z(2) * Z(3)
comp = qaoa_circ.extract_composite(q, H, n_steps, theta_angle, beta_angle)
print(comp.toString())
+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ namespace {
// Here we enumerate them as a Variant
using AllowedKernelArgTypes =
    xacc::Variant<bool, int, double, std::string, xacc::internal_compiler::qreg,
                  std::vector<double>>;
                  std::vector<double>, qcor::PauliOperator>;

// We will take as input a mapping of arg variable names to the argument itself.
using KernelArgDict = std::map<std::string, AllowedKernelArgTypes>;
+4 −2
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ import re
from collections import defaultdict 

List = typing.List

PauliOperator = xacc.quantum.PauliOperator

def X(idx):
    return xacc.quantum.PauliOperator({idx: 'X'}, 1.0)
@@ -126,7 +126,9 @@ class qjit(object):
        self.kwargs = kwargs
        self.function = function
        self.allowed_type_cpp_map = {'<class \'_pyqcor.qreg\'>': 'qreg',
                                     '<class \'float\'>': 'double', 'typing.List[float]': 'std::vector<double>'}
                                     '<class \'float\'>': 'double', 'typing.List[float]': 'std::vector<double>', 
                                     '<class \'int\'>': 'int', 
                                     '<class \'_pyxacc.quantum.PauliOperator\'>': 'qcor::PauliOperator'}
        self.__dict__.update(kwargs)

        # Create the qcor just in time engine