Commit eb643dd9 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

setup python and exp_i_theta to be able to use FermionOperators

parent 71cc9830
Loading
Loading
Loading
Loading
Loading
+24 −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, t0: float):
    exponent_op1 = adag(0) * a(1) - adag(1) * a(0)
    X(q[0])
    exp_i_theta(q, t0, exponent_op1)    

# Define the hamiltonian
H = -2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) - 6.125 * Z(1) + 5.907

# Create the ObjectiveFunction, specify central finite diff gradient
obj = createObjectiveFunction(ansatz, H, 1, {'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)
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
@@ -32,6 +32,11 @@ def Y(idx):
def Z(idx):
    return xacc.quantum.PauliOperator({idx: 'Z'}, 1.0)

def adag(idx):
    return xacc.quantum.FermionOperator([(idx,True)], 1.0)

def a(idx):
    return xacc.quantum.FermionOperator([(idx,False)], 1.0)

cpp_matrix_gen_code = '''#include <pybind11/embed.h>
#include <pybind11/stl.h>
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ target_include_directories(${LIBRARY_NAME} PUBLIC . qrt
                            execution 
                            utils)

target_link_libraries(${LIBRARY_NAME} PUBLIC xacc::xacc xacc::quantum_gate qrt xacc::pauli)
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc::xacc xacc::quantum_gate qrt xacc::pauli xacc::fermion)

if(APPLE)
  set_target_properties(${LIBRARY_NAME}
+2 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ using namespace cppmicroservices;

#include <memory>
#include <set>
#include <iomanip> 

#include "AlgorithmGradientStrategy.hpp"
#include "xacc.hpp"
@@ -67,7 +68,7 @@ public:
      std_dev = std::sqrt(sq_sum / all_energies.size() - val * val);
    }

    std::cout << "<H>(" << this->current_iterate_parameters << ") = " << val;
    std::cout << "<H>(" << this->current_iterate_parameters << ") = " << std::setprecision(12) << val;
    if (std::fabs(std_dev) > 1e-12) {
      std::cout << " +- " << std_dev << "\n";
    } else {
+3 −0
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@ PauliOperator operator-(PauliOperator &op, double coeff) {
  return -1.0 * coeff + op;
}

FermionOperator adag(int idx) {return FermionOperator(xacc::quantum::Operators{{idx, true}});};
FermionOperator a(int idx) {return FermionOperator(xacc::quantum::Operators{{idx, false}});};

PauliOperator X(int idx) { return PauliOperator({{idx, "X"}}); }

PauliOperator Y(int idx) { return PauliOperator({{idx, "Y"}}); }
Loading