Skip to content

Commit 7a30b15

Browse files
committed
Updated examples:
1. xacc_app: XACC application: User application depends on XACC. 2. exatn_app: ExaTN application: User application depends on ExaTN.
1 parent 7997197 commit 7a30b15

7 files changed

+109
-49
lines changed

CMakeLists.txt

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
2-
project(integration_test VERSION 1.0.0 LANGUAGES CXX)
2+
project(xacc_app VERSION 1.0.0 LANGUAGES CXX)
33

44
set(CMAKE_STANDARD_REQUIRED ON)
5-
set(CMAKE_CXX_STANDARD 14)
5+
set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
77

8-
# Find Xacc
98
find_package(XACC REQUIRED)
109

11-
add_executable(integration_test integration_test.cpp)
12-
target_link_libraries(integration_test PRIVATE xacc::xacc)
10+
add_executable(xacc_app xacc_app.cpp)
11+
target_link_libraries(xacc_app PRIVATE xacc::xacc)

exatn_app.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "exatn.hpp"
2+
3+
#include <vector>
4+
5+
void exatn_simulation() {
6+
7+
auto tensor = std::make_shared<exatn::Tensor>("Tensor",exatn::TensorShape{2,2,2,2,2});
8+
auto success = exatn::createTensor(tensor,exatn::TensorElementType::REAL64); assert(success);
9+
10+
std::vector<double> tensor_data(32,1.0);
11+
success = exatn::initTensorData("Tensor",tensor_data); assert(success);
12+
13+
success = exatn::destroyTensor("Tensor"); assert(success);
14+
15+
exatn::sync();
16+
17+
return;
18+
}
19+
20+
21+
int main(int argc, char** argv) {
22+
23+
//Initialize the ExaTN runtime:
24+
exatn::ParamConf exatn_parameters;
25+
//Set the available CPU Host RAM size to be used by ExaTN:
26+
exatn_parameters.setParameter("host_memory_buffer_size",2L*1024L*1024L*1024L);
27+
#ifdef MPI_ENABLED
28+
int thread_provided;
29+
int mpi_error = MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &thread_provided);
30+
assert(mpi_error == MPI_SUCCESS);
31+
assert(thread_provided == MPI_THREAD_MULTIPLE);
32+
exatn::initialize(exatn::MPICommProxy(MPI_COMM_WORLD),exatn_parameters,"lazy-dag-executor");
33+
#else
34+
exatn::initialize(exatn_parameters,"lazy-dag-executor");
35+
#endif
36+
37+
//Perform the ExaTN simulation defined above:
38+
exatn_simulation();
39+
40+
//Finalize the ExaTN runtime:
41+
exatn::finalize();
42+
#ifdef MPI_ENABLED
43+
mpi_error = MPI_Finalize(); assert(mpi_error == MPI_SUCCESS);
44+
#endif
45+
46+
return 0;
47+
}

integration_test.cpp

-41
This file was deleted.

make.sh

-3
This file was deleted.

make_exatn_app.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rm *.o *.x
2+
g++ `$HOME/.exatn/bin/exatn-config --cxxflags` `$HOME/.exatn/bin/exatn-config --includes` -c exatn_app.cpp -o exatn_app.o
3+
g++ exatn_app.o `$HOME/.exatn/bin/exatn-config --libs` -o exatn_app.x

make_xacc_app.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rm *.o *.x
2+
g++ `$HOME/.exatn/bin/exatn-config --cxxflags` `$HOME/.exatn/bin/exatn-config --includes` -I $HOME/.xacc/include/xacc -c xacc_app.cpp -o xacc_app.o
3+
g++ xacc_app.o -Wl,-rpath,$HOME/.xacc/lib -L $HOME/.xacc/lib -lxacc `$HOME/.exatn/bin/exatn-config --libs` -o xacc_app.x

xacc_app.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "xacc.hpp"
2+
3+
void tnqvm_simulation() {
4+
5+
//Choose the desired quantum accelerator:
6+
auto qpu = xacc::getAccelerator("tnqvm", {std::make_pair("tnqvm-visitor", "exatn")});
7+
8+
//Choose the desired quantum programming language:
9+
auto xasmCompiler = xacc::getCompiler("xasm");
10+
11+
//Compile a quantum kernel into the quantum IR:
12+
auto ir = xasmCompiler->compile(
13+
R"(__qpu__ void ansatz(qbit q, double theta) {
14+
X(q[0]);
15+
Ry(q[1], theta);
16+
CX(q[1], q[0]);
17+
H(q[0]);
18+
H(q[1]);
19+
Measure(q[0]);
20+
Measure(q[1]);
21+
})", qpu);
22+
23+
//Get the generated parameterized quantum circuit:
24+
auto circuit = ir->getComposite("ansatz");
25+
26+
//Perform hybrid quantum/classical computation:
27+
auto angles = xacc::linspace(-3.1415, 3.1415, 20);
28+
for (auto & a: angles) {
29+
auto evaled = (*circuit)({a});
30+
auto qubits = xacc::qalloc(2);
31+
qpu->execute(qubits, evaled);
32+
auto exp_val = qubits->getExpectationValueZ();
33+
std::cout << "<X0X1>(" << a << ") = " << exp_val << "\n";
34+
}
35+
36+
return;
37+
}
38+
39+
40+
int main(int argc, char** argv) {
41+
42+
//Initialize the XACC runtime:
43+
xacc::Initialize(argc, argv);
44+
45+
//Perform the quantum circuit simulation defined above:
46+
tnqvm_simulation();
47+
48+
//Finalize the XACC runtime:
49+
xacc::Finalize();
50+
51+
return 0;
52+
}

0 commit comments

Comments
 (0)