Loading python/examples/qsim_vqe.py 0 → 100644 +31 −0 Original line number Diff line number Diff line from qcor import * # Define the deuteron hamiltonian H = -2.1433 * X(0) * X(1) - 2.1433 * \ Y(0) * Y(1) + .21829 * Z(0) - 6.125 * Z(1) + 5.907 # 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 : float): X(q[0]) Ry(q[1], theta) CX(q[1], q[0]) # Create the problem model, provide the state # prep circuit, Hamiltonian and note how many qubits # and variational parameters num_params = 1 problemModel = qsim.ModelBuilder.createModel(ansatz, H, num_params) # Create the NLOpt derivative free optimizer optimizer = createOptimizer('nlopt') # Create the VQE workflow workflow = qsim.getWorkflow('vqe', {'optimizer': optimizer}) # Execute and print the result result = workflow.execute(problemModel) energy = result['energy'] print(energy) No newline at end of file python/py-qcor.cpp +33 −20 Original line number Diff line number Diff line Loading @@ -119,7 +119,6 @@ class PyObjectiveFunction : public qcor::ObjectiveFunction { PyObjectiveFunction(py::object q, qcor::PauliOperator &qq, const int n_dim, const std::string &helper_name) : py_kernel(q) { // Set the OptFunction dimensions _dim = n_dim; Loading Loading @@ -359,6 +358,20 @@ PYBIND11_MODULE(_pyqcor, m) { return qcor::qsim::ModelBuilder::createModel(obs, ham_func); }, "Return the Model for a time-dependent problem.") .def( "createModel", [](py::object py_kernel, qcor::PauliOperator &obs, const int n_params) { qcor::qsim::QuantumSimulationModel model; auto nq = obs.nBits(); auto kernel_functor = std::make_shared<qcor::PyKernelFunctor>( py_kernel, nq, n_params); model.observable = &obs; model.user_defined_ansatz = kernel_functor; return std::move(model); }, "") .def( "createModel", [](py::object py_kernel, qcor::PauliOperator &obs, Loading Loading
python/examples/qsim_vqe.py 0 → 100644 +31 −0 Original line number Diff line number Diff line from qcor import * # Define the deuteron hamiltonian H = -2.1433 * X(0) * X(1) - 2.1433 * \ Y(0) * Y(1) + .21829 * Z(0) - 6.125 * Z(1) + 5.907 # 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 : float): X(q[0]) Ry(q[1], theta) CX(q[1], q[0]) # Create the problem model, provide the state # prep circuit, Hamiltonian and note how many qubits # and variational parameters num_params = 1 problemModel = qsim.ModelBuilder.createModel(ansatz, H, num_params) # Create the NLOpt derivative free optimizer optimizer = createOptimizer('nlopt') # Create the VQE workflow workflow = qsim.getWorkflow('vqe', {'optimizer': optimizer}) # Execute and print the result result = workflow.execute(problemModel) energy = result['energy'] print(energy) No newline at end of file
python/py-qcor.cpp +33 −20 Original line number Diff line number Diff line Loading @@ -119,7 +119,6 @@ class PyObjectiveFunction : public qcor::ObjectiveFunction { PyObjectiveFunction(py::object q, qcor::PauliOperator &qq, const int n_dim, const std::string &helper_name) : py_kernel(q) { // Set the OptFunction dimensions _dim = n_dim; Loading Loading @@ -359,6 +358,20 @@ PYBIND11_MODULE(_pyqcor, m) { return qcor::qsim::ModelBuilder::createModel(obs, ham_func); }, "Return the Model for a time-dependent problem.") .def( "createModel", [](py::object py_kernel, qcor::PauliOperator &obs, const int n_params) { qcor::qsim::QuantumSimulationModel model; auto nq = obs.nBits(); auto kernel_functor = std::make_shared<qcor::PyKernelFunctor>( py_kernel, nq, n_params); model.observable = &obs; model.user_defined_ansatz = kernel_functor; return std::move(model); }, "") .def( "createModel", [](py::object py_kernel, qcor::PauliOperator &obs, Loading