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
895956d2
Commit
895956d2
authored
Jan 08, 2020
by
Mccaskey, Alex
Browse files
implemented internal compiler test for staq compiler with oracle adder synthesis
Signed-off-by:
Alex McCaskey
<
mccaskeyaj@ornl.gov
>
parent
28dfd8cc
Changes
17
Hide whitespace changes
Inline
Side-by-side
python/plugins/aer/aer_accelerator.py
View file @
895956d2
...
...
@@ -49,7 +49,6 @@ class AerAccelerator(xacc.Accelerator):
return
'aer'
def
execute_one_qasm
(
self
,
buffer
,
program
):
print
(
program
.
toString
())
qobjStr
=
self
.
qobj_compiler
.
translate
(
program
)
import
json
from
qiskit
import
Aer
...
...
@@ -65,7 +64,7 @@ class AerAccelerator(xacc.Accelerator):
qobj
=
QasmQobjSchema
().
load
(
qobj_json
[
'qObject'
])
exps
=
qobj
.
experiments
measures
=
{}
qobj
.
experiments
[
0
].
config
.
n_qubits
=
8
qobj
.
experiments
[
0
].
config
.
n_qubits
=
buffer
.
size
()
for
i
in
exps
[
0
].
instructions
:
if
i
.
name
==
"measure"
:
measures
[
i
.
memory
[
0
]]
=
i
.
qubits
[
0
]
...
...
@@ -84,7 +83,8 @@ class AerAccelerator(xacc.Accelerator):
if
len
(
b
)
<
buffer
.
size
():
tmp
=
[
'0'
for
i
in
range
(
buffer
.
size
())]
for
bit_loc
,
qubit
in
measures
.
items
():
tmp
[
len
(
tmp
)
-
1
-
qubit
]
=
list
(
b
)[
bit_loc
]
lb
=
list
(
b
)
tmp
[
len
(
tmp
)
-
1
-
qubit
]
=
lb
[
len
(
b
)
-
bit_loc
-
1
]
bitstring
=
''
.
join
(
tmp
)
buffer
.
appendMeasurement
(
bitstring
,
c
)
...
...
python/plugins/py_plugin_loader.hpp
View file @
895956d2
...
...
@@ -22,7 +22,7 @@ namespace external {
class
PythonPluginLoader
:
public
ExternalLanguagePluginLoader
{
protected:
std
::
shared_ptr
<
py
::
scoped_interpreter
>
guard
;
py
::
scoped_interpreter
*
guard
;
void
*
libpython_handle
;
public:
const
std
::
string
name
()
const
override
{
return
"python"
;
}
...
...
python/plugins/py_plugin_loader.in.cpp
View file @
895956d2
...
...
@@ -18,12 +18,15 @@ namespace xacc {
namespace
external
{
bool
PythonPluginLoader
::
load
()
{
xacc
::
debug
(
"[PyPluginLoader] Loading all contributed Python plugins."
);
if
(
!
XACC_IS_APPLE
){
libpython_handle
=
dlopen
(
"@PYTHON_LIB_NAME@"
,
RTLD_LAZY
|
RTLD_GLOBAL
);
}
guard
=
std
::
make_shared
<
py
::
scoped_interpreter
>
();
py
::
scoped_interpreter
*
guard
=
new
py
::
scoped_interpreter
();
try
{
py
::
module
sys
=
py
::
module
::
import
(
"xacc"
);
sys
.
attr
(
"loaded_from_cpp_dont_finalize"
)
=
true
;
}
catch
(
std
::
exception
&
e
)
{
std
::
cout
<<
"Could not import "
<<
e
.
what
()
<<
"
\n
"
;
return
false
;
...
...
@@ -34,6 +37,8 @@ bool PythonPluginLoader::load() {
bool
PythonPluginLoader
::
unload
()
{
if
(
!
XACC_IS_APPLE
)
{
xacc
::
debug
(
"[PyPluginLoader] Unloading Python plugins"
);
delete
guard
;
int
i
=
dlclose
(
libpython_handle
);
if
(
i
!=
0
)
{
std
::
cout
<<
"error closing python lib: "
<<
i
<<
"
\n
"
;
...
...
python/xacc-py.cpp
View file @
895956d2
...
...
@@ -99,6 +99,8 @@ PYBIND11_MODULE(_pyxacc, m) {
""
);
m
.
def
(
"info"
,
[](
const
std
::
string
s
)
{
xacc
::
info
(
s
);
},
""
);
m
.
def
(
"debug"
,
[](
const
std
::
string
s
)
{
xacc
::
debug
(
s
);
},
""
);
m
.
def
(
"error"
,
[](
const
std
::
string
s
)
{
xacc
::
error
(
s
);
},
""
);
m
.
def
(
"setOption"
,
&
xacc
::
setOption
,
"Set an XACC framework option."
);
...
...
python/xacc.py
View file @
895956d2
...
...
@@ -303,12 +303,16 @@ class PyServiceRegistry(object):
self
.
get_algorithm_services
(
servType
)
for
accName
,
acc
in
self
.
registry
[
'accelerator'
].
items
():
debug
(
"[xacc-py] Contributing "
+
accName
+
" Accelerator"
)
contributeService
(
accName
,
acc
)
for
irtName
,
irt
in
self
.
registry
[
'irtransformation'
].
items
():
debug
(
"[xacc-py] Contributing "
+
irtName
+
" IRTransformation"
)
contributeService
(
irtName
,
irt
)
for
obsName
,
obs
in
self
.
registry
[
'observable'
].
items
():
debug
(
"[xacc-py] Contributing "
+
obsName
+
" Observable"
)
contributeService
(
obsName
,
obs
)
for
optName
,
opt
in
self
.
registry
[
'optimizer'
].
items
():
debug
(
"[xacc-py] Contributing "
+
optName
+
" Optimizer"
)
contributeService
(
optName
,
opt
)
def
get_algorithm_services
(
self
,
serviceType
):
...
...
@@ -423,9 +427,13 @@ def main(argv=None):
initialize
()
loaded_from_cpp_dont_finalize
=
False
def
_finalize
():
Finalize
()
if
not
loaded_from_cpp_dont_finalize
:
Finalize
()
else
:
debug
(
"THIS WAS LOADED FROM CPP DONT FINALIZE"
)
atexit
.
register
(
_finalize
)
...
...
quantum/plugins/staq/CMakeLists.txt
View file @
895956d2
...
...
@@ -56,7 +56,7 @@ else()
endif
()
if
(
XACC_BUILD_TESTS
)
#
add_subdirectory(compiler/tests)
add_subdirectory
(
compiler/tests
)
endif
()
install
(
TARGETS
${
LIBRARY_NAME
}
DESTINATION
${
CMAKE_INSTALL_PREFIX
}
/plugins
)
quantum/plugins/staq/compiler/staq_compiler.cpp
View file @
895956d2
...
...
@@ -180,10 +180,13 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
// Visit Program to find out how many qreg there are and
// use that to build up openqasm xacc function prototype
// std::cout << "HELLO:\n";
// prog->pretty_print(std::cout);
// exit(0);
internal_staq
::
StaqToXasm
translate
;
translate
.
visit
(
*
prog
);
std
::
cout
<<
"XASM:
\n
"
<<
translate
.
ss
.
str
()
<<
"
\n
"
;
//
std::cout << "XASM:\n" << translate.ss.str() << "\n";
std
::
string
kernel
;
if
(
isXaccKernel
)
{
...
...
quantum/plugins/staq/compiler/tests/CMakeLists.txt
View file @
895956d2
...
...
@@ -10,5 +10,15 @@
# Contributors:
# Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
add_xacc_test
(
StaqCompiler
)
target_link_libraries
(
StaqCompilerTester xacc
)
\ No newline at end of file
#add_xacc_test(StaqCompiler)
#target_link_libraries(StaqCompilerTester xacc )
configure_file
(
StaqCompilerTester.in.cpp
${
CMAKE_BINARY_DIR
}
/quantum/plugins/staq/compiler/tests/StaqCompilerTester.cpp
)
add_executable
(
StaqCompilerTester
${
CMAKE_BINARY_DIR
}
/quantum/plugins/staq/compiler/tests/StaqCompilerTester.cpp
)
target_include_directories
(
StaqCompilerTester PRIVATE
${
GTEST_INCLUDE_DIRS
}
)
target_link_libraries
(
StaqCompilerTester PRIVATE xacc
${
GTEST_LIBRARIES
}
)
add_test
(
NAME xacc_StaqCompilerTester COMMAND StaqCompilerTester
)
target_compile_features
(
StaqCompilerTester PRIVATE cxx_std_14
)
\ No newline at end of file
quantum/plugins/staq/compiler/tests/StaqCompilerTester.cpp
→
quantum/plugins/staq/compiler/tests/StaqCompilerTester.
in.
cpp
View file @
895956d2
...
...
@@ -27,6 +27,7 @@ TEST(StaqCompilerTester, checkSimple) {
measure q -> c;
)"
);
auto
hello
=
IR
->
getComposites
()[
0
];
std
::
cout
<<
"HELLO:
\n
"
<<
hello
->
toString
()
<<
"
\n
"
;
...
...
@@ -62,7 +63,7 @@ TEST(StaqCompilerTester, checkOracle) {
OPENQASM 2.0;
include "qelib1.inc";
oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "adder_4.v" }
oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "
@CMAKE_SOURCE_DIR@/quantum/plugins/staq/compiler/tests/
adder_4.v" }
creg result[4];
...
...
quantum/plugins/staq/compiler/tests/adder_4.v
0 → 100644
View file @
895956d2
module
top
(
\
a
[
0
],
\
a
[
1
],
\
a
[
2
],
\
a
[
3
],
\
b
[
0
],
\
b
[
1
],
\
b
[
2
],
\
b
[
3
],
\
c
[
0
],
\
c
[
1
],
\
c
[
2
],
\
c
[
3
]);
input
\
a
[
0
],
\
a
[
1
],
\
a
[
2
],
\
a
[
3
],
\
b
[
0
],
\
b
[
1
],
\
b
[
2
],
\
b
[
3
];
output
\
c
[
0
],
\
c
[
1
],
\
c
[
2
],
\
c
[
3
];
wire
n386
,
n387
,
n388
,
n389
,
n390
,
n391
,
n392
,
n393
,
n394
,
n395
,
n396
,
n397
,
n398
,
n399
,
n400
,
n401
,
n402
,
n403
,
n404
,
n405
,
n406
,
n407
,
n408
,
n409
,
n410
;
assign
n386
=
\
a
[
0
]
&
~
\
b
[
0
]
;
assign
n387
=
~
\
a
[
0
]
&
\
b
[
0
]
;
assign
\
c
[
0
]
=
n386
|
n387
;
assign
n389
=
\
a
[
0
]
&
\
b
[
0
]
;
assign
n390
=
~
\
a
[
1
]
&
~
\
b
[
1
]
;
assign
n391
=
\
a
[
1
]
&
\
b
[
1
]
;
assign
n392
=
~
n390
&
~
n391
;
assign
n393
=
n389
&
~
n392
;
assign
n394
=
~
n389
&
n392
;
assign
\
c
[
1
]
=
n393
|
n394
;
assign
n396
=
n389
&
~
n390
;
assign
n397
=
~
n391
&
~
n396
;
assign
n398
=
~
\
a
[
2
]
&
~
\
b
[
2
]
;
assign
n399
=
\
a
[
2
]
&
\
b
[
2
]
;
assign
n400
=
~
n398
&
~
n399
;
assign
n401
=
n397
&
~
n400
;
assign
n402
=
~
n397
&
n400
;
assign
\
c
[
2
]
=
~
n401
&
~
n402
;
assign
n404
=
~
n397
&
~
n398
;
assign
n405
=
~
n399
&
~
n404
;
assign
n406
=
~
\
a
[
3
]
&
~
\
b
[
3
]
;
assign
n407
=
\
a
[
3
]
&
\
b
[
3
]
;
assign
n408
=
~
n406
&
~
n407
;
assign
n409
=
n405
&
~
n408
;
assign
n410
=
~
n405
&
n408
;
assign
\
c
[
3
]
=
~
n409
&
~
n410
;
endmodule
quantum/plugins/xasm/tests/XASMCompilerTester.cpp
View file @
895956d2
...
...
@@ -293,7 +293,22 @@ Tdg(anc[0]);
}
TEST
(
XASMCompilerTester
,
checkCallingPreviousKernel
)
{
auto
compiler
=
xacc
::
getCompiler
(
"xasm"
);
auto
IR
=
compiler
->
compile
(
R"(__qpu__ void bell(qbit q) {
H(q[0]);
CX(q[0], q[1]);
Measure(q[0]);
Measure(q[1]);
})"
);
auto
IR2
=
compiler
->
compile
(
R"(__qpu__ void call_bell(qbit q) {
bell(q);
})"
);
auto
bell
=
IR2
->
getComposite
(
"call_bell"
);
std
::
cout
<<
bell
->
toString
()
<<
"
\n
"
;
}
int
main
(
int
argc
,
char
**
argv
)
{
...
...
quantum/plugins/xasm/xasm_listener.cpp
View file @
895956d2
...
...
@@ -409,7 +409,6 @@ void XASMListener::exitInstruction(xasmParser::InstructionContext *ctx) {
}
}
std
::
cout
<<
"bnames; "
<<
currentBufferNames
<<
"
\n
"
;
inst
->
setBufferNames
(
currentBufferNames
);
if
(
inForLoop
)
{
...
...
xacc/accelerator/Accelerator.hpp
View file @
895956d2
...
...
@@ -44,6 +44,8 @@ namespace xacc {
class
Accelerator
:
public
Identifiable
{
public:
enum
BitOrder
{
LSB
,
MSB
};
virtual
void
initialize
(
const
HeterogeneousMap
&
params
=
{})
=
0
;
virtual
void
updateConfiguration
(
const
HeterogeneousMap
&
config
)
=
0
;
virtual
void
updateConfiguration
(
const
HeterogeneousMap
&&
config
)
{
...
...
@@ -66,6 +68,10 @@ public:
// preferred for this Accelerator
virtual
const
std
::
string
defaultPlacementTransformation
()
{
return
"default-placement"
;}
virtual
BitOrder
getBitOrder
()
{
return
BitOrder
::
MSB
;
}
// Execute a single program. All results persisted to the buffer
virtual
void
execute
(
std
::
shared_ptr
<
AcceleratorBuffer
>
buffer
,
...
...
xacc/compiler/xacc_internal_compiler.cpp
View file @
895956d2
...
...
@@ -10,6 +10,11 @@ void compiler_InitializeXACC(const char *qpu_backend) {
if
(
!
xacc
::
isInitialized
())
xacc
::
Initialize
();
setAccelerator
(
qpu_backend
);
}
void
setAccelerator
(
const
char
*
qpu_backend
)
{
if
(
qpu
)
{
if
(
qpu_backend
!=
qpu
->
name
())
{
qpu
=
xacc
::
getAccelerator
(
qpu_backend
).
get
();
...
...
@@ -91,16 +96,21 @@ void execute(AcceleratorBuffer **buffers, const int nBuffers,
// buffer names to measurement counts
int
global_reg_size
=
0
;
std
::
map
<
std
::
string
,
int
>
shift_map
;
std
::
vector
<
std
::
string
>
shift_map_names
;
std
::
vector
<
int
>
shift_map_shifts
;
std
::
map
<
std
::
string
,
AcceleratorBuffer
*>
buf_map
;
std
::
map
<
std
::
string
,
std
::
map
<
std
::
string
,
int
>>
buf_counts
;
for
(
auto
&
b
:
bvec
)
{
shift_map
.
insert
({
b
->
name
(),
global_reg_size
});
shift_map_names
.
push_back
(
b
->
name
());
shift_map_shifts
.
push_back
(
global_reg_size
);
buf_map
.
insert
({
b
->
name
(),
b
});
buf_counts
.
insert
({
b
->
name
(),
{}});
global_reg_size
+=
b
->
size
();
}
std
::
cout
<<
"
Creating register of size "
<<
std
::
to_string
(
global_reg_size
)
<<
"
\n
"
;
xacc
::
debug
(
"[xacc_internal_compiler]
Creating register of size "
+
std
::
to_string
(
global_reg_size
)
)
;
auto
tmp
=
xacc
::
qalloc
(
global_reg_size
);
// Update Program bit indices based on new global
...
...
@@ -123,24 +133,29 @@ void execute(AcceleratorBuffer **buffers, const int nBuffers,
unified_buf_names
.
push_back
(
"q"
);
}
next
.
setBufferNames
(
unified_buf_names
);
}
std
::
cout
<<
"HOWDY:
\n
"
<<
program
->
toString
()
<<
"
\n
"
;
// Now execute using the global merged register
execute
(
tmp
.
get
(),
program
,
parameters
);
// Take bit strings and map to buffer individual bit strings
for
(
auto
&
kv
:
tmp
->
getMeasurementCounts
())
{
std
::
cout
<<
"HELLO: "
<<
kv
.
first
<<
", "
<<
kv
.
second
<<
"
\n
"
;
auto
bitstring
=
kv
.
first
;
for
(
auto
&
buff_names_shift
:
shift_map
)
{
if
(
qpu
->
getBitOrder
()
==
Accelerator
::
BitOrder
::
MSB
)
{
std
::
reverse
(
bitstring
.
begin
(),
bitstring
.
end
());
}
for
(
int
j
=
0
;
j
<
shift_map_names
.
size
();
j
++
)
{
auto
shift
=
buff_names_shift
.
second
;
auto
buff_name
=
buff_names_shift
.
first
;
auto
shift
=
shift_map_shifts
[
j
]
;
auto
buff_name
=
shift_map_names
[
j
]
;
auto
buffer
=
buf_map
[
buff_name
];
auto
buffer_bitstring
=
bitstring
.
substr
(
shift
,
shift
+
buffer
->
size
());
auto
buffer_bitstring
=
bitstring
.
substr
(
shift
,
buffer
->
size
());
if
(
qpu
->
getBitOrder
()
==
Accelerator
::
BitOrder
::
MSB
)
{
std
::
reverse
(
buffer_bitstring
.
begin
(),
buffer_bitstring
.
end
());
}
if
(
buf_counts
[
buff_name
].
count
(
buffer_bitstring
))
{
buf_counts
[
buff_name
][
buffer_bitstring
]
+=
kv
.
second
;
...
...
xacc/compiler/xacc_internal_compiler.hpp
View file @
895956d2
...
...
@@ -28,21 +28,17 @@ template <typename T> struct empty_delete {
enum
OptLevel
{
DEFAULT
,
LEVEL1
,
LEVEL2
,
LEVEL3
};
void
compiler_InitializeXACC
(
const
char
*
qpu_backend
);
void
compiler_InitializeXACC
(
const
char
*
qpu_backend
=
"local-ibm"
);
void
setAccelerator
(
const
char
*
qpu_backend
);
// Map kernel source string representing a single
// kernel function to a single CompositeInstruction (src to IR)
// CompositeInstruction *compile(const char *compiler_name, const char
// *kernel_src,
// const char *qpu_name);
CompositeInstruction
*
compile
(
const
char
*
compiler_name
,
const
char
*
kernel_src
);
CompositeInstruction
*
getCompiled
(
const
char
*
kernel_name
);
// Run quantum compilation routines on IR
// void optimize(CompositeInstruction *program, const char *qpu_name,
// const OptLevel opt = DEFAULT);
void
optimize
(
CompositeInstruction
*
program
,
const
OptLevel
opt
=
DEFAULT
);
// Execute on the specified QPU, persisting results to
...
...
xacc/tests/CMakeLists.txt
View file @
895956d2
...
...
@@ -18,4 +18,13 @@ add_xacc_test(Algorithm xacc)
add_xacc_test
(
Heterogeneous xacc
)
target_compile_features
(
HeterogeneousTester PRIVATE cxx_std_14
)
add_xacc_test
(
InternalCompiler xacc
)
\ No newline at end of file
configure_file
(
InternalCompilerTester.in.cpp
${
CMAKE_BINARY_DIR
}
/xacc/tests/InternalCompilerTester.cpp
)
add_executable
(
InternalCompilerTester
${
CMAKE_BINARY_DIR
}
/xacc/tests/InternalCompilerTester.cpp
)
target_include_directories
(
InternalCompilerTester PRIVATE
${
GTEST_INCLUDE_DIRS
}
)
target_link_libraries
(
InternalCompilerTester PRIVATE xacc
${
GTEST_LIBRARIES
}
)
add_test
(
NAME xacc_InternalCompilerTester COMMAND InternalCompilerTester
)
target_compile_features
(
InternalCompilerTester PRIVATE cxx_std_14
)
#add_xacc_test(InternalCompiler xacc)
\ No newline at end of file
xacc/tests/InternalCompilerTester.cpp
→
xacc/tests/InternalCompilerTester.
in.
cpp
View file @
895956d2
...
...
@@ -59,7 +59,11 @@ TEST(InternalCompilerTester, checkMultipleBuffers) {
}
TEST
(
InternalCompilerTester
,
checkStaqAdd
)
{
if
(
!
xacc
::
hasCompiler
(
"staq"
))
{
return
;
}
xacc
::
external
::
load_external_language_plugins
();
setAccelerator
(
"aer"
);
auto
a
=
qalloc
(
4
);
a
.
setName
(
"a"
);
a
.
store
();
...
...
@@ -72,7 +76,7 @@ TEST(InternalCompilerTester, checkStaqAdd) {
c
.
setName
(
"c"
);
c
.
store
();
auto
anc
=
qalloc
(
3
);
auto
anc
=
qalloc
(
3
);
anc
.
setName
(
"anc"
);
anc
.
store
();
...
...
@@ -80,7 +84,7 @@ TEST(InternalCompilerTester, checkStaqAdd) {
OPENQASM 2.0;
include "qelib1.inc";
oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "adder_4.v" }
oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "
@CMAKE_SOURCE_DIR@/quantum/plugins/staq/compiler/tests/
adder_4.v" }
creg result[4];
...
...
@@ -98,8 +102,8 @@ adder a[0],a[1],a[2],a[3],b[0],b[1],b[2],b[3],c[0],c[1],c[2],c[3];
measure c -> result;
})"
;
auto
circuit
=
compile
(
"staq"
,
src
);
std
::
cout
<<
circuit
->
toString
()
<<
"
\n
"
;
xacc
::
AcceleratorBuffer
*
bufs
[
4
]
=
{
a
.
results
(),
b
.
results
(),
c
.
results
(),
anc
.
results
()};
xacc
::
AcceleratorBuffer
*
bufs
[
4
]
=
{
a
.
results
(),
b
.
results
(),
c
.
results
(),
anc
.
results
()};
// std::vector<xacc::AcceleratorBuffer*> bufs{q.results(),r.results()};
execute
(
bufs
,
4
,
circuit
);
...
...
@@ -116,12 +120,20 @@ measure c -> result;
for
(
const
auto
&
kv
:
counts
)
{
printf
(
"%s: %i
\n
"
,
kv
.
first
.
c_str
(),
kv
.
second
);
}
counts
=
anc
.
counts
();
for
(
const
auto
&
kv
:
counts
)
{
printf
(
"%s: %i
\n
"
,
kv
.
first
.
c_str
(),
kv
.
second
);
}
xacc
::
external
::
unload_external_language_plugins
();
}
int
main
(
int
argc
,
char
**
argv
)
{
compiler_InitializeXACC
(
"local-ibm"
);
compiler_InitializeXACC
();
xacc
::
set_verbose
(
true
);
::
testing
::
InitGoogleTest
(
&
argc
,
argv
);
auto
ret
=
RUN_ALL_TESTS
();
//
xacc::Finalize();
// xacc::Finalize();
return
ret
;
}
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