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
3add29c7
Commit
3add29c7
authored
Nov 15, 2019
by
Mccaskey, Alex
Browse files
uploading python optimizer plugin for pycma module
Signed-off-by:
Alex McCaskey
<
mccaskeyaj@ornl.gov
>
parent
8e580e8f
Pipeline
#80230
passed with stage
in 3 minutes and 38 seconds
Changes
9
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
python/CMakeLists.txt
View file @
3add29c7
...
...
@@ -44,5 +44,6 @@ file(GLOB PYDECORATORS benchmark/vqe/*.py
plugins/aer/*.py
plugins/dwave/*.py
plugins/qiskit/*.py
plugins/observables/*.py
)
plugins/observables/*.py
plugins/optimizers/*.py
)
install
(
FILES
${
PYDECORATORS
}
DESTINATION
${
CMAKE_INSTALL_PREFIX
}
/py-plugins
)
python/examples/pycma_example.py
0 → 100644
View file @
3add29c7
import
xacc
import
numpy
as
np
def
rosen
(
x
,
args
):
xx
=
(
1.
-
x
[
0
])
**
2
+
100
*
(
x
[
1
]
-
x
[
0
]
**
2
)
**
2
return
xx
optimizer
=
xacc
.
getOptimizer
(
'pycma'
,
{
'tolx'
:
1e-12
,
'AdaptSigma'
:
True
,
'CMA_elitist'
:
True
,
'popsize'
:
4
+
np
.
floor
(
2
*
np
.
log
(
2
))})
f
=
xacc
.
OptFunction
(
rosen
,
2
)
r
,
p
=
optimizer
.
optimize
(
f
)
print
(
'Result = '
,
r
,
p
)
python/plugins/chp_monte_carlo.py/chp_mc_accelerator.py
0 → 100644
View file @
3add29c7
import
xacc
from
pelix.ipopo.decorators
import
ComponentFactory
,
Property
,
Requires
,
Provides
,
\
Validate
,
Invalidate
,
Instantiate
@
ComponentFactory
(
"chp_mc_accelerator_factory"
)
@
Provides
(
"accelerator"
)
@
Property
(
"_accelerator"
,
"accelerator"
,
"chp-mc"
)
@
Property
(
"_name"
,
"name"
,
"chp-mc"
)
@
Instantiate
(
"chp_mc_accelerator_instance"
)
class
CHPMCAccelerator
(
xacc
.
Accelerator
):
def
__init__
(
self
):
xacc
.
Accelerator
.
__init__
(
self
)
self
.
shots
=
1024
def
initialize
(
self
,
options
):
if
'shots'
in
options
:
self
.
shots
=
options
[
'shots'
]
def
name
(
self
):
return
'chp-mc'
def
execute
(
self
,
buffer
,
programs
):
python/plugins/optimizers/pycma_optimizer.py
0 → 100644
View file @
3add29c7
import
xacc
from
pelix.ipopo.decorators
import
ComponentFactory
,
Property
,
Requires
,
Provides
,
\
Validate
,
Invalidate
,
Instantiate
@
ComponentFactory
(
"pycma_optimizer_factory"
)
@
Provides
(
"optimizer"
)
@
Property
(
"_optimizer"
,
"optimizer"
,
"pycma"
)
@
Property
(
"_name"
,
"name"
,
"pycma"
)
@
Instantiate
(
"pycma_instance"
)
class
PyCMAOptimizer
(
xacc
.
Optimizer
):
def
__init__
(
self
):
xacc
.
Optimizer
.
__init__
(
self
)
self
.
options
=
{}
self
.
sigma
=
.
1
self
.
hetOpts
=
None
def
name
(
self
):
return
'pycma'
def
setOptions
(
self
,
opts
):
self
.
hetOpts
=
opts
if
'sigma'
in
opts
:
self
.
sigma
=
opts
[
'sigma'
]
if
'maxfevals'
in
opts
:
self
.
options
[
'maxfevals'
]
=
opts
[
'maxfevals'
]
if
'tolx'
in
opts
:
self
.
options
[
'tolx'
]
=
opts
[
'tolx'
]
if
'AdaptSigma'
in
opts
:
self
.
options
[
'AdaptSigma'
]
=
True
if
'CMA_elitist'
in
opts
:
self
.
options
[
'CMA_elitist'
]
=
opts
[
'CMA_elitist'
]
if
'popsize'
in
opts
:
self
.
options
[
'popsize'
]
=
opts
[
'popsize'
]
def
optimize
(
self
,
function
):
import
cma
params
=
function
.
dimensions
()
*
[
0.
]
if
'initial-parameters'
in
self
.
hetOpts
:
params
=
self
.
hetOpts
[
'initial-parameters'
]
es
=
cma
.
CMAEvolutionStrategy
(
params
,
self
.
sigma
,
self
.
options
)
es
.
optimize
(
function
)
return
(
es
.
result
.
fbest
,
es
.
result
.
xbest
)
python/py_optimizer.cpp
View file @
3add29c7
...
...
@@ -15,10 +15,11 @@
void
bind_optimizer
(
py
::
module
&
m
)
{
// Expose Optimizer
py
::
class_
<
xacc
::
Optimizer
,
std
::
shared_ptr
<
xacc
::
Optimizer
>>
(
py
::
class_
<
xacc
::
Optimizer
,
std
::
shared_ptr
<
xacc
::
Optimizer
>
,
PyOptimizer
>
(
m
,
"Optimizer"
,
"The Optimizer interface provides optimization routine implementations "
"for use in algorithms."
)
.
def
(
py
::
init
<>
(),
""
)
.
def
(
"optimize"
,
[
&
](
xacc
::
Optimizer
&
o
,
py
::
function
&
f
,
...
...
@@ -49,5 +50,11 @@ void bind_optimizer(py::module &m) {
py
::
class_
<
xacc
::
OptFunction
>
(
m
,
"OptFunction"
,
""
)
.
def
(
py
::
init
<
std
::
function
<
double
(
const
std
::
vector
<
double
>
&
,
std
::
vector
<
double
>
&
)
>
,
const
int
>
());
const
int
>
())
.
def
(
"dimensions"
,
&
xacc
::
OptFunction
::
dimensions
,
""
)
.
def
(
"__call__"
,
[](
OptFunction
&
o
,
const
std
::
vector
<
double
>
&
x
)
{
std
::
vector
<
double
>
tmpgrad
(
o
.
dimensions
());
return
o
(
x
,
tmpgrad
);
},
""
);
}
\ No newline at end of file
python/py_optimizer.hpp
View file @
3add29c7
...
...
@@ -25,4 +25,24 @@
namespace
py
=
pybind11
;
using
namespace
xacc
;
class
PyOptimizer
:
public
xacc
::
Optimizer
{
public:
/* Inherit the constructors */
using
Optimizer
::
Optimizer
;
const
std
::
string
name
()
const
override
{
PYBIND11_OVERLOAD_PURE
(
const
std
::
string
,
xacc
::
Optimizer
,
name
);
}
const
std
::
string
description
()
const
override
{
return
""
;
}
OptResult
optimize
(
OptFunction
&
function
)
override
{
PYBIND11_OVERLOAD_PURE
(
OptResult
,
xacc
::
Optimizer
,
optimize
,
function
);
}
void
setOptions
(
const
HeterogeneousMap
&
opts
)
override
{
PYBIND11_OVERLOAD_PURE
(
void
,
xacc
::
Optimizer
,
setOptions
,
opts
);
}
};
void
bind_optimizer
(
py
::
module
&
m
);
\ No newline at end of file
python/xacc.py
View file @
3add29c7
...
...
@@ -291,7 +291,7 @@ class PyServiceRegistry(object):
def
initialize
(
self
):
serviceList
=
[
'decorator_algorithm_service'
,
'benchmark_algorithm'
,
'hamiltonian_generator'
,
'ansatz_generator'
,
'accelerator'
,
'irtransformation'
,
'observable'
]
'irtransformation'
,
'observable'
,
'optimizer'
]
xaccLocation
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
self
.
pluginDir
=
xaccLocation
+
'/py-plugins'
if
not
os
.
path
.
exists
(
self
.
pluginDir
):
...
...
@@ -312,6 +312,8 @@ class PyServiceRegistry(object):
contributeService
(
irtName
,
irt
)
for
obsName
,
obs
in
self
.
registry
[
'observable'
].
items
():
contributeService
(
obsName
,
obs
)
for
optName
,
opt
in
self
.
registry
[
'optimizer'
].
items
():
contributeService
(
optName
,
opt
)
def
get_algorithm_services
(
self
,
serviceType
):
tmp
=
self
.
context
.
get_all_service_references
(
serviceType
)
...
...
xacc/optimizer/Optimizer.hpp
View file @
3add29c7
...
...
@@ -53,7 +53,7 @@ public:
template
<
typename
T
>
void
appendOption
(
const
std
::
string
key
,
T
&
value
)
{
options
.
insert
(
key
,
value
);
}
void
setOptions
(
const
HeterogeneousMap
&
opts
)
{
options
=
opts
;
}
virtual
void
setOptions
(
const
HeterogeneousMap
&
opts
)
{
options
=
opts
;
}
virtual
OptResult
optimize
(
OptFunction
&
function
)
=
0
;
};
...
...
xacc/xacc.cpp
View file @
3add29c7
...
...
@@ -365,7 +365,17 @@ std::shared_ptr<Optimizer> getOptimizer(const std::string name) {
error
(
"XACC not initialized before use. Please execute "
"xacc::Initialize() before using API."
);
}
return
xacc
::
getService
<
Optimizer
>
(
name
);
std
::
shared_ptr
<
Optimizer
>
t
;
if
(
xacc
::
hasService
<
Optimizer
>
(
name
))
{
t
=
xacc
::
getService
<
Optimizer
>
(
name
,
false
);
}
else
if
(
xacc
::
hasContributedService
<
Optimizer
>
(
name
))
{
t
=
xacc
::
getContributedService
<
Optimizer
>
(
name
,
false
);
}
if
(
!
t
)
{
xacc
::
error
(
"Invalid Optimizer name, not in service registry - "
+
name
);
}
return
t
;
}
std
::
shared_ptr
<
Optimizer
>
getOptimizer
(
const
std
::
string
name
,
...
...
@@ -428,7 +438,7 @@ getIRTransformation(const std::string &name) {
"xacc::Initialize() before using API."
);
}
std
::
shared_ptr
<
IRTransformation
>
t
;
std
::
shared_ptr
<
IRTransformation
>
t
;
if
(
xacc
::
hasService
<
IRTransformation
>
(
name
))
{
t
=
xacc
::
getService
<
IRTransformation
>
(
name
,
false
);
}
else
if
(
xacc
::
hasContributedService
<
IRTransformation
>
(
name
))
{
...
...
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