Commit 3be8e84b authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding ddcl decorator example


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 560c7558
Pipeline #74520 passed with stage
in 4 minutes and 39 seconds
import xacc
# Get the QPU and allocate a single qubit
qpu = xacc.getAccelerator('local-ibm')
qbits = xacc.qalloc(1)
@xacc.qpu(algo='ddcl', accelerator=qpu, optimizer='mlpack', target_dist=[.5,.5], loss='js', gradient='js-parameter-shift')
def f(qbits, x, y, z):
Ry(qbits[0], x)
Ry(qbits[0], y)
Ry(qbits[0], z)
f(qbits, 0, 0, 0)
print(qbits.getInformation('opt-val'))
params = qbits.getAllUnique('parameters')
print(params)
\ No newline at end of file
......@@ -14,5 +14,7 @@ add_subdirectory(vqe)
add_subdirectory(rdm)
add_subdirectory(vqe-energy)
add_subdirectory(ddcl)
file(GLOB PYDECORATORS ${CMAKE_CURRENT_SOURCE_DIR}/vqe/python/*.py ${CMAKE_CURRENT_SOURCE_DIR}/vqe-energy/python/*.py)
file(GLOB PYDECORATORS ${CMAKE_CURRENT_SOURCE_DIR}/vqe/python/*.py
${CMAKE_CURRENT_SOURCE_DIR}/vqe-energy/python/*.py
${CMAKE_CURRENT_SOURCE_DIR}/ddcl/python/*.py)
install(FILES ${PYDECORATORS} DESTINATION ${CMAKE_INSTALL_PREFIX}/py-plugins)
......@@ -103,6 +103,11 @@ void DDCL::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const {
accelerator->execute(tmpBuffer, circuits);
auto buffers = tmpBuffer->getChildren();
for (auto b : buffers) {
b->addExtraInfo("parameters", x);
buffer->appendChild(b->name(), b);
}
// The first child buffer is for the loss function
auto counts = buffers[0]->getMeasurementCounts();
......
from pelix.ipopo.decorators import (ComponentFactory, Property, Requires,
Provides, Instantiate, BindField, UnbindField)
import xacc
import inspect
@ComponentFactory("wrapped_ddcl_factory")
@Provides("decorator_algorithm_service")
@Property("_algorithm", "algorithm", "ddcl")
@Property("_name", "name", "ddcl")
@Requires("_vqe_optimizers", "vqe_optimization", aggregate=True, optional=True)
@Instantiate("wrapped_ddcl_instance")
class WrappedDDCLF(xacc.DecoratorFunction):
def __init__(self):
self.vqe_optimizers = {}
self.observable = None
@BindField("_vqe_optimizers")
def bind_optimizers(self, field, service, svc_ref):
if svc_ref.get_property('vqe_optimizer'):
optimizer = svc_ref.get_property('vqe_optimizer')
self.vqe_optimizers[optimizer] = service
@UnbindField("_vqe_optimizers")
def unbind_optimizers(self, field, service, svc_ref):
if svc_ref.get_property('vqe_optimizer'):
optimizer = svc_ref.get_property('vqe_optimizer')
del vqe_optimizers[optimizer]
def __call__(self, *args, **kwargs):
super().__call__(*args, **kwargs)
execParams = {'accelerator': self.qpu, 'ansatz': self.compiledKernel, 'target_dist':self.kwargs['target_dist'], 'loss':self.kwargs['loss']}
if 'gradient' in self.kwargs:
execParams['gradient'] = self.kwargs['gradient']
optParams = {}
if not isinstance(args[0], xacc.AcceleratorBuffer):
raise RuntimeError(
'First argument of an xacc kernel must be the Accelerator Buffer to operate on.')
buffer = args[0]
ars = args[1:]
if len(ars) > 0:
optParams['initial-parameters'] = list(ars)
if 'options' in self.kwargs:
optParams = self.kwargs['options']
if 'optimizer' not in self.kwargs:
self.kwargs['optimizer'] = 'nlopt'
if self.kwargs['optimizer'] in self.vqe_optimizers:
optimizer = self.vqe_optimizers[self.kwargs['optimizer']]
optimizer.optimize(buffer, optParams, execParams)
else:
execParams['optimizer'] = xacc.getOptimizer(self.kwargs['optimizer'], optParams)
ddcl = xacc.getAlgorithm('ddcl', execParams)
ddcl.execute(buffer)
return
\ No newline at end of file
......@@ -54,7 +54,7 @@ class WrappedVQEF(xacc.DecoratorFunction):
else:
execParams['optimizer'] = xacc.getOptimizer(self.kwargs['optimizer'], optParams)
vqe = xacc.getAlgorithm('vqe', execParams)
vqe.execute(buffer)
vqe = xacc.getAlgorithm('vqe', execParams)
vqe.execute(buffer)
return
\ No newline at end of file
Markdown is supported
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