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
35e4ea36
Commit
35e4ea36
authored
Jul 30, 2019
by
Mccaskey, Alex
Browse files
adding examples folder, with initial example xacc::qasm() vqe example
Signed-off-by:
Alex McCaskey
<
mccaskeyaj@ornl.gov
>
parent
316726d0
Pipeline
#65113
passed with stage
in 6 minutes and 12 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
CMakeLists.txt
View file @
35e4ea36
...
...
@@ -17,6 +17,7 @@ set(CMAKE_CXX_STANDARD 11)
message
(
STATUS
"C++ version
${
CXX_STANDARD
}
configured."
)
option
(
XACC_BUILD_TESTS
"Build test programs"
OFF
)
option
(
XACC_BUILD_EXAMPLES
"Build example programs"
OFF
)
if
(
FROM_SETUP_PY AND NOT APPLE
)
message
(
STATUS
"Running build from setup.py, linking to static libstdc++"
)
...
...
quantum/CMakeLists.txt
View file @
35e4ea36
...
...
@@ -72,4 +72,8 @@ else()
endif
()
#install(FILES ${QHEADERS} DESTINATION include/quantum/)
install
(
TARGETS
${
LIBRARY_NAME
}
DESTINATION plugins
)
\ No newline at end of file
install
(
TARGETS
${
LIBRARY_NAME
}
DESTINATION plugins
)
if
(
XACC_BUILD_EXAMPLES
)
add_subdirectory
(
examples
)
endif
()
\ No newline at end of file
quantum/examples/CMakeLists.txt
0 → 100644
View file @
35e4ea36
add_subdirectory
(
qasm
)
\ No newline at end of file
quantum/examples/qasm/CMakeLists.txt
0 → 100644
View file @
35e4ea36
add_executable
(
deuteron_from_qasm deuteron.cpp
)
target_link_libraries
(
deuteron_from_qasm PRIVATE xacc xacc-pauli
)
\ No newline at end of file
quantum/examples/qasm/deuteron.cpp
0 → 100644
View file @
35e4ea36
#include
"XACC.hpp"
#include
"Optimizer.hpp"
#include
"PauliOperator.hpp"
#include
"xacc_service.hpp"
int
main
(
int
argc
,
char
**
argv
)
{
xacc
::
Initialize
(
argc
,
argv
);
// Get reference to the Accelerator
// specified by --accelerator argument
auto
accelerator
=
xacc
::
getAccelerator
();
// Allocate some qubits
auto
buffer
=
xacc
::
qalloc
(
2
);
// Create the N=2 deuteron Hamiltonian
auto
H_N_2
=
std
::
make_shared
<
xacc
::
quantum
::
PauliOperator
>
(
"5.907 - 2.1433 X0X1 "
"- 2.1433 Y0Y1"
"+ .21829 Z0 - 6.125 Z1"
);
// JIT map Quil QASM Ansatz to IR
xacc
::
qasm
(
R"(
.compiler quil
.function deuteron_ansatz
X 0
Ry(theta) 1
CNOT 1 0
)"
);
auto
ansatz
=
xacc
::
getCompiled
(
"deuteron_ansatz"
);
// Observe the Ansatz, creating measured kernels
auto
kernels
=
H_N_2
->
observe
(
ansatz
);
// Create the OptFunction to be optimized
xacc
::
OptFunction
vqeOptFunc
(
[
&
](
const
std
::
vector
<
double
>
&
x
)
->
double
{
std
::
vector
<
double
>
coefficients
;
std
::
vector
<
std
::
shared_ptr
<
xacc
::
Function
>>
fsToExec
;
// Get function coefficients (pauli term weights)
// into a list, separate from the identity coeff
double
identityCoeff
=
0.0
;
for
(
auto
&
f
:
kernels
)
{
std
::
complex
<
double
>
coeff
=
f
->
getOption
(
"coefficient"
).
as
<
std
::
complex
<
double
>>
();
if
(
f
->
nInstructions
()
>
ansatz
->
nInstructions
())
{
fsToExec
.
push_back
(
f
->
operator
()(
x
));
coefficients
.
push_back
(
std
::
real
(
coeff
));
}
else
{
identityCoeff
+=
std
::
real
(
coeff
);
}
}
// Execute on the QPU!
auto
buffers
=
accelerator
->
execute
(
buffer
,
fsToExec
);
// Compute the Energy
double
energy
=
identityCoeff
;
for
(
int
i
=
0
;
i
<
buffers
.
size
();
i
++
)
{
energy
+=
buffers
[
i
]
->
getExpectationValueZ
()
*
coefficients
[
i
];
}
// Print the energy at this iteration
std
::
stringstream
ss
;
ss
<<
"E("
<<
x
[
0
];
for
(
int
i
=
1
;
i
<
x
.
size
();
i
++
)
ss
<<
","
<<
x
[
i
];
ss
<<
") = "
<<
energy
;
xacc
::
info
(
ss
.
str
());
return
energy
;
},
ansatz
->
nParameters
());
// Run Optimization routine, default is NLOPT COBYLA
auto
optimizer
=
xacc
::
getService
<
xacc
::
Optimizer
>
(
"nlopt"
);
auto
results
=
optimizer
->
optimize
(
vqeOptFunc
);
// Print the result
std
::
cout
<<
"Energy: "
<<
results
.
first
<<
"
\n
"
;
}
\ No newline at end of file
xacc/XACC.cpp
View file @
35e4ea36
...
...
@@ -550,19 +550,20 @@ void qasm(const std::string &qasmString) {
auto
lines
=
split
(
qasmString
,
'\n'
);
std
::
string
currentFunctionName
=
""
;
for
(
auto
&
l
:
lines
)
{
if
(
l
.
find
(
".compiler"
)
==
std
::
string
::
npos
&&
l
.
find
(
".function"
)
==
std
::
string
::
npos
)
{
function2code
[
currentFunctionName
]
+=
l
+
"
\n
"
;
}
if
(
l
.
find
(
".function"
)
!=
std
::
string
::
npos
)
{
currentFunctionName
=
split
(
l
,
' '
)[
1
];
}
if
(
l
.
find
(
".compiler"
)
==
std
::
string
::
npos
&&
l
.
find
(
".function"
)
==
std
::
string
::
npos
&&
!
l
.
empty
())
{
function2code
[
currentFunctionName
]
+=
l
+
"
\n
"
;
}
if
(
l
.
find
(
".function"
)
!=
std
::
string
::
npos
)
{
currentFunctionName
=
split
(
l
,
' '
)[
1
];
}
}
std
::
string
newQasm
=
""
;
for
(
auto
&
kv
:
function2code
)
{
newQasm
+=
"__qpu__ "
+
kv
.
first
+
"(AcceleratorBuffer b) {
\n
"
+
kv
.
second
+
"
\n
}
\n
"
;
for
(
auto
&
kv
:
function2code
)
{
newQasm
+=
"__qpu__ "
+
kv
.
first
+
"(AcceleratorBuffer b) {
\n
"
+
kv
.
second
+
"
\n
}
\n
"
;
}
std
::
shared_ptr
<
IR
>
ir
;
...
...
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