Commit 4410d6e2 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding a few more allowed kernel args, demoed with 3-qbit deuteron

parent eb643dd9
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
# Run this from the command line like this
#
# python3 exp_fermion.py -shots 100

from qcor import *

@qjit
def ansatz(q: qreg, x: List[float], exp_args: List[FermionOperator]):
    X(q[0])
    for i in range(len(exp_args)):
        exp_i_theta(q, x[i], exp_args[i])

exp_args = [adag(0) * a(1) - adag(1)*a(0), adag(0)*a(2) - adag(2)*a(0)]

# Custom arg_translator in a Pythonic way
def ansatz_translate(self, q: qreg, x: List[float]):
    ret_dict = {}    
    ret_dict["q"] = q
    ret_dict["x"] = x
    ret_dict["exp_args"] = exp_args
    return ret_dict

ansatz.translate = MethodType(ansatz_translate, qjit)

# Define the hamiltonian
H = createOperator(
    '5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1 + 9.625 - 9.625 Z2 - 3.91 X1 X2 - 3.91 Y1 Y2')

# Create the ObjectiveFunction, specify central finite diff gradient
obj = createObjectiveFunction(
    ansatz, H, 2)
    #, {'gradient-strategy': 'central', 'step': 1e-1})

# create the lbfgs optimizer
optimizer = createOptimizer('nlopt')#, {'algorithm': 'l-bfgs', 'ftol': 1e-3})

# Run VQE...
results = optimizer.optimize(obj)
print(results)
+3 −1
Original line number Diff line number Diff line
@@ -54,7 +54,9 @@ 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<int>, qcor::PauliOperator, qcor::PairList<int>>;
                  std::vector<double>, std::vector<int>, qcor::PauliOperator,
                  qcor::FermionOperator, qcor::PairList<int>,
                  std::vector<qcor::PauliOperator>, std::vector<qcor::FermionOperator>>;

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

List = typing.List
Tuple = typing.Tuple
MethodType = types.MethodType

PauliOperator = xacc.quantum.PauliOperator
FermionOperator = xacc.quantum.FermionOperator 
FLOAT_REF = typing.NewType('value', float)
INT_REF = typing.NewType('value', int)

@@ -164,7 +166,10 @@ class qjit(object):
                                     '<class \'float\'>': 'double', 'typing.List[float]': 'std::vector<double>',
                                     '<class \'int\'>': 'int', 'typing.List[int]': 'std::vector<int>',
                                     '<class \'_pyxacc.quantum.PauliOperator\'>': 'qcor::PauliOperator',
                                     'typing.List[typing.Tuple[int, int]]': 'PairList<int>'}
                                     '<class \'_pyxacc.quantum.FermionOperator\'>': 'qcor::FermionOperator',
                                     'typing.List[typing.Tuple[int, int]]': 'PairList<int>',
                                     'typing.List[_pyxacc.quantum.PauliOperator]': 'std::vector<qcor::PauliOperator>',
                                     'typing.List[_pyxacc.quantum.FermionOperator]': 'std::vector<qcor::FermionOperator>'}
        self.__dict__.update(kwargs)

        # Create the qcor just in time engine