Commit 5e4a7705 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding openfermion integration example to python examples

parent 15991809
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
from qcor import *
from openfermion.ops import FermionOperator as FOp

# Define the quantum kernel by providing a 
# python function that is annotated with qjit for 
# quantum just in time compilation
@qjit
def ansatz(q: qreg, theta: List[float]):
    X(q[0])
    Ry(q[1], theta[0])
    CX(q[1], q[0])

# Define the hamiltonian
H = FOp('', 0.0002899) + FOp('0^ 0', -.43658) + \
    FOp('1 0^', 4.2866) + FOp('1^ 0', -4.2866) + FOp('1^ 1', 12.25) 

# Create the ObjectiveFunction, default is VQE
n_params = 1
obj = createObjectiveFunction(ansatz, H, n_params)

# evaluate at a concrete set of params
vqe_energy = obj([.59])

# Run full optimization
optimizer = createOptimizer('nlopt')
results = optimizer.optimize(obj)
print(results)
 No newline at end of file
+20 −0
Original line number Diff line number Diff line

from qcor import qjit, qreg
import numpy as np
def decompose(q : qreg):
    return 

@qjit
def ccnot(q :qreg):
    for i in range(q.size()):
        X(q[i])

    with decompose as ccnot_mat:
        ccnot_mat = np.eye(8,8)
        ccnot_mat[6, 6] = 0.0
        ccnot_mat[7, 7] = 0.0
        ccnot_mat[6, 7] = 1.0
        ccnot_mat[7, 6] = 1.0

    for i in range(q.size()):
        Measure(q[i])
 No newline at end of file
+11 −0
Original line number Diff line number Diff line
@@ -468,6 +468,17 @@ PYBIND11_MODULE(_pyqcor, m) {
        return obj;
      },
      "");
  m.def(
      "createObjectiveFunction",
      [](py::object kernel, py::object &py_obs, const int n_params) {
        auto obs = convertToPauliOperator(py_obs);
        auto q = ::qalloc(obs->nBits());
        std::shared_ptr<qcor::ObjectiveFunction> obj =
            std::make_shared<qcor::PyObjectiveFunction>(kernel, obs, n_params,
                                                        "vqe");
        return obj;
      },
      "");
  m.def(
      "createObjectiveFunction",
      [](py::object kernel, qcor::PauliOperator &obs, const int n_params,