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
xacc
Commits
3be8e84b
Commit
3be8e84b
authored
Oct 09, 2019
by
Mccaskey, Alex
Browse files
adding ddcl decorator example
Signed-off-by:
Alex McCaskey
<
mccaskeyaj@ornl.gov
>
parent
560c7558
Pipeline
#74520
passed with stage
in 4 minutes and 39 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
python/examples/ddcl_decorator.py
0 → 100644
View file @
3be8e84b
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
quantum/plugins/algorithms/CMakeLists.txt
View file @
3be8e84b
...
...
@@ -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
)
quantum/plugins/algorithms/ddcl/ddcl.cpp
View file @
3be8e84b
...
...
@@ -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
();
...
...
quantum/plugins/algorithms/ddcl/python/wrappedDDCL.py
0 → 100644
View file @
3be8e84b
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
quantum/plugins/algorithms/vqe/python/wrappedVQE.py
View file @
3be8e84b
...
...
@@ -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
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