Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ORNL Quantum Computing Institute
qcor
Commits
bb05dacb
Commit
bb05dacb
authored
Oct 26, 2020
by
Mccaskey, Alex
Browse files
adding qsim_vqe example
Signed-off-by:
Alex McCaskey
<
mccaskeyaj@ornl.gov
>
parent
bce07606
Pipeline
#122891
passed with stages
in 66 minutes and 16 seconds
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
python/examples/qsim_vqe.py
0 → 100644
View file @
bb05dacb
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
View file @
bb05dacb
...
...
@@ -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
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment