Commit 270c0d00 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Merge branch 'master' into tnguyen/qsim-examples

parents 83e7f524 e1ea7475
......@@ -20,11 +20,12 @@ Quick Start
-----------
QCOR is available via pre-built deb packages on Ubuntu Bionic (18.04) and Focal (20.04). To install on Bionic
```bash
$ wget -qO- https://aide-qc.github.io/deploy/aide_qc/debian/bionic/PUBLIC-KEY.gpg | apt-key add -
$ wget -qO- "https://aide-qc.github.io/deploy/aide_qc/debian/bionic/aide-qc-bionic.list" > /etc/apt/sources.list.d/aide-qc-bionic.list
$ apt-get update && apt-get install qcor
wget -qO- https://aide-qc.github.io/deploy/aide_qc/debian/PUBLIC-KEY.gpg | sudo apt-key add -
sudo wget -qO- "https://aide-qc.github.io/deploy/aide_qc/debian/$(lsb_release -cs)/aide-qc.list" > /etc/apt/sources.list.d/aide-qc.list
sudo apt-get update
sudo apt-get install qcor
```
For Focal, replace bionic with focal in the above commands.
Note that the above requires you have `lsb_release` installed (usually is, if not, `apt-get install lsb-release`).
QCOR nightly docker images are available that serve up an Eclipse Theia IDE (the same IDE Gitpod uses) on port 3000. To get started, run
```bash
......
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
......@@ -96,16 +96,16 @@ xacc::HeterogeneousMap heterogeneousMapConvert(
namespace qcor {
// PyObjectiveFunction implements ObjectiveFunction to
// enable the utility of pythonic quantum kernels with the
// existing qcor ObjectiveFunction infrastructure. This class
// keeps track of the quantum kernel as a py::object, which it uses
// in tandem with the QCOR QJIT engine to create an executable
// functor representation of the quantum code at runtime. It exposes
// the ObjectiveFunction operator()() overloads to map vector<double>
// x to the correct pythonic argument structure. It delegates to the
// usual helper ObjectiveFunction (like vqe) for execution of the
// actual pre-, execution, and post-processing.
// PyObjectiveFunction implements ObjectiveFunction to
// enable the utility of pythonic quantum kernels with the
// existing qcor ObjectiveFunction infrastructure. This class
// keeps track of the quantum kernel as a py::object, which it uses
// in tandem with the QCOR QJIT engine to create an executable
// functor representation of the quantum code at runtime. It exposes
// the ObjectiveFunction operator()() overloads to map vector<double>
// x to the correct pythonic argument structure. It delegates to the
// usual helper ObjectiveFunction (like vqe) for execution of the
// actual pre-, execution, and post-processing.
class PyObjectiveFunction : public qcor::ObjectiveFunction {
protected:
py::object py_kernel;
......@@ -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;
......@@ -139,8 +138,8 @@ class PyObjectiveFunction : public qcor::ObjectiveFunction {
qjit.write_cache();
}
// Evaluate this ObjectiveFunction at the dictionary of kernel args,
// return the scalar value
// Evaluate this ObjectiveFunction at the dictionary of kernel args,
// return the scalar value
double operator()(const KernelArgDict args) {
// Map the kernel args to a hetmap
xacc::HeterogeneousMap m;
......@@ -179,11 +178,11 @@ class PyObjectiveFunction : public qcor::ObjectiveFunction {
}
};
// PyKernelFunctor is a subtype of KernelFunctor from the qsim library
// that returns a CompositeInstruction representation of a pythonic
// quantum kernel given a vector of parameters x. This will
// leverage the QJIT infrastructure to create executable functor
// representation of the python kernel.
// PyKernelFunctor is a subtype of KernelFunctor from the qsim library
// that returns a CompositeInstruction representation of a pythonic
// quantum kernel given a vector of parameters x. This will
// leverage the QJIT infrastructure to create executable functor
// representation of the python kernel.
class PyKernelFunctor : public qcor::KernelFunctor {
protected:
py::object py_kernel;
......@@ -200,8 +199,8 @@ class PyKernelFunctor : public qcor::KernelFunctor {
qjit.write_cache();
}
// Delegate to QJIT to create a CompositeInstruction representation
// of the pythonic quantum kernel.
// Delegate to QJIT to create a CompositeInstruction representation
// of the pythonic quantum kernel.
std::shared_ptr<xacc::CompositeInstruction> evaluate_kernel(
const std::vector<double> &x) override {
// Translate x into kernel args
......@@ -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,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment