Unverified Commit 325a3814 authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #300 from danclaudino/mc-vqe-devel

Added number of cycles and subset of Hamiltonian terms
parents 46e82e89 4c0325e4
...@@ -16,5 +16,5 @@ target_link_libraries(tnqvm_hpc_virt PRIVATE xacc xacc-quantum-gate) ...@@ -16,5 +16,5 @@ target_link_libraries(tnqvm_hpc_virt PRIVATE xacc xacc-quantum-gate)
configure_file(tnqvm_h4_chain_8qbit_adapt_gs.in.cpp configure_file(tnqvm_h4_chain_8qbit_adapt_gs.in.cpp
${CMAKE_BINARY_DIR}/quantum/examples/hpc_virtualization/tnqvm_h4_chain_8qbit_adapt_gs.cpp) ${CMAKE_BINARY_DIR}/quantum/examples/hpc_virtualization/tnqvm_h4_chain_8qbit_adapt_gs.cpp)
add_executable(h4_chain_8qbit_hpc_virt ${CMAKE_BINARY_DIR}/quantum/examples/hpc_virtualization/tnqvm_h4_chain_8qbit_adapt_gs.cpp) add_executable(chain_hpc_virt ${CMAKE_BINARY_DIR}/quantum/examples/hpc_virtualization/tnqvm_h4_chain_8qbit_adapt_gs.cpp)
target_link_libraries(h4_chain_8qbit_hpc_virt PRIVATE xacc xacc-quantum-gate) target_link_libraries(chain_hpc_virt PRIVATE xacc xacc-quantum-gate xacc-pauli)
(0.022049,0) Y4 Y6 + (0.022049,0) X4 X6 + (0.111499,0) Z0 Z6 + (0.0845405,0) Z4 Z6 + (-0.0255452,0) Y5 Y7 + (-0.0210619,0) X1 Z2 X3 Z4 + (-0.0233845,0) Y1 Z2 Y3 Z7 + (0.0214768,0) X0 Z1 X2 Z7 + (-0.0210619,0) Z0 Y5 Z6 Y7 + (-0.0376584,0) Y0 Y1 Y6 Y7 + (-0.0376584,0) X0 X1 X6 X7 + (0.108983,0) Z0 Z5 + (0.010771,0) Y0 Y1 X4 Z5 Z6 X7 + (0.0112345,0) X4 Z5 X6 Z7 + (0.0845405,0) Z0 Z2 + (0.111941,0) Z1 Z6 + (0.113407,0) Z1 Z5 + (0.022049,0) X0 X2 + (0.022049,0) Y0 Y2 + (0.115986,0) Z1 Z7 + (-0.00245129,0) Z1 Y4 Z5 Y6 + (-0.00245129,0) Z1 X4 Z5 X6 + (-0.0255452,0) X1 X3 + (-0.0255452,0) Y1 Y3 + (0.111499,0) Z2 Z4 + (0.010771,0) X0 X1 X4 Z5 Z6 X7 + (-0.0376584,0) X2 X3 Y4 Y5 + (-0.0376584,0) Y2 Y3 X4 X5 + (-0.0376584,0) Y2 Y3 Y4 Y5 + (0.0696375,0) Z4 Z5 + (-0.00245129,0) X0 Z1 X2 Z5 + (0.026958,0) Y0 Z1 Y2 Y4 Z5 Y6 + (0.026958,0) X0 Z1 X2 X4 Z5 X6 + (0.026958,0) Y0 Z1 Y2 X4 Z5 X6 + (0.0203913,0) Y0 Z1 Y2 Z4 + (-0.0102909,0) Z4 Y5 Z6 Y7 + (0.116851,0) Z2 Z6 + (-0.0248416,0) Y2 Y3 Y5 Y6 + (-0.0132401,0) Y0 Z1 Z2 Y3 X5 X6 + (0.0393455,0) Y0 Y1 Y4 Y5 + (0.0393455,0) X0 X1 Y4 Y5 + (-0.0246282,0) Y1 Z2 Y3 X4 Z5 X6 + (-0.0376584,0) Y0 Y1 X6 X7 + (0.010771,0) Y0 Y1 Y4 Z5 Z6 Y7 + (0.145261,0) Z3 Z7 + (-0.0113881,0) X0 Y1 Y2 X3 + (0.0261613,0) Y1 Z2 Y3 X5 Z6 X7 + (-0.0248416,0) X2 X3 X5 X6 + (0.0794385,0) Z6 Z7 + (-0.00245129,0) Y0 Z1 Y2 Z5 + (0.026958,0) X0 Z1 X2 Y4 Z5 Y6 + (0.0343207,0) X1 X2 Y5 Y6 + (0.010771,0) Y0 Z1 Z2 Y3 X4 X5 + (0.0343207,0) Y1 Y2 X5 X6 + (0.0203913,0) Z0 Y4 Z5 Y6 + (0.0879264,0) Z5 + (-0.0790441,0) Z6 + (-0.0102909,0) Z4 X5 Z6 X7 + (-0.0233845,0) Z3 X5 Z6 X7 + (0.00556213,0) Y4 Z5 Y6 + (-0.0132401,0) X0 Z1 Z2 X3 Y5 Y6 + (0.108983,0) Z1 Z4 + (0.0343207,0) X1 X2 X5 X6 + (0.0393455,0) Y0 Y1 X4 X5 + (-0.0255452,0) X5 X7 + (0.0242674,0) Y0 Z1 Z2 Y3 Y4 Z5 Z6 Y7 + (0.0214768,0) Z3 X4 Z5 X6 + (-0.0376584,0) X0 X1 Y6 Y7 + (0.00556213,0) X4 Z5 X6 + (-0.000703626,0) X1 Z2 X3 Z6 + (-0.000703626,0) Y1 Z2 Y3 Z6 + (0.0214768,0) Z3 Y4 Z5 Y6 + (0.0242674,0) X0 Z1 Z2 X3 X4 Z5 Z6 X7 + (0.010771,0) X0 Z1 Z2 X3 X4 X5 + (-0.0102424,0) Y0 Z1 Z2 Y3 X6 X7 + (-0.0132401,0) X1 X2 X4 Z5 Z6 X7 + (0.0261613,0) X1 Z2 X3 X5 Z6 X7 + (0.0879264,0) Z1 + (-0.0790441,0) Z2 + (0.0776204,0) Z5 Z6 + (0.0261613,0) Y1 Z2 Y3 Y5 Z6 Y7 + (0.130738,0) Z3 Z4 + (-0.331478,0) + (-0.0102424,0) X2 X3 X4 Z5 Z6 X7 + (0.0172666,0) Y1 Z2 Y3 + (-0.0246282,0) X1 Z2 X3 X4 Z5 X6 + (0.0696375,0) Z0 Z1 + (0.0776204,0) Z1 Z2 + (0.0393455,0) X0 X1 X4 X5 + (0.00171566,0) X0 Z1 X2 Z6 + (-0.0102424,0) X0 Z1 Z2 X3 Y6 Y7 + (0.00556213,0) X0 Z1 X2 + (0.0242674,0) Y0 Z1 Z2 Y3 X4 Z5 Z6 X7 + (0.0245002,0) Y0 Y1 X5 X6 + (-0.0102424,0) X0 Z1 Z2 X3 X6 X7 + (-0.0132401,0) Y0 Z1 Z2 Y3 Y5 Y6 + (-0.0246282,0) X1 Z2 X3 Y4 Z5 Y6 + (0.0794385,0) Z2 Z3 + (-0.0376584,0) X2 X3 X4 X5 + (0.0172666,0) X1 Z2 X3 + (-0.0248416,0) X2 X3 Y5 Y6 + (-0.0130301,0) X0 X1 Y2 Y3 + (-0.334612,0) Z3 + (0.120055,0) Z3 Z6 + (-0.0246282,0) Y1 Z2 Y3 Y4 Z5 Y6 + (-0.0248416,0) X1 X2 X6 X7 + (-0.0102424,0) Y2 Y3 Y4 Z5 Z6 Y7 + (-0.0102424,0) Y2 Y3 X4 Z5 Z6 X7 + (0.0203913,0) X0 Z1 X2 Z4 + (0.0245002,0) Y1 Y2 X4 X5 + (0.010771,0) Y0 Z1 Z2 Y3 Y4 Y5 + (0.115986,0) Z3 Z5 + (-0.0210619,0) Y1 Z2 Y3 Z4 + (-0.334612,0) Z7 + (0.120055,0) Z2 Z7 + (0.124321,0) Z0 Z4 + (0.00556213,0) Y0 Z1 Y2 + (0.181365,0) Z4 + (-0.0244182,0) Y4 Y5 Y6 Y7 + (-0.0248416,0) Y2 Y3 X5 X6 + (-0.0132401,0) X0 Z1 Z2 X3 X5 X6 + (0.0242674,0) X0 Z1 Z2 X3 Y4 Z5 Z6 Y7 + (0.010771,0) X0 Z1 Z2 X3 Y4 Y5 + (0.106471,0) Z4 Z7 + (0.130738,0) Z0 Z7 + (0.0112345,0) Y4 Z5 Y6 Z7 + (0.0203913,0) Z0 X4 Z5 X6 + (0.089825,0) Z1 Z3 + (0.0406161,0) Y2 Y3 X6 X7 + (-0.0130301,0) Y4 Y5 X6 X7 + (-0.0244182,0) X4 X5 X6 X7 + (-0.0233845,0) X1 Z2 X3 Z7 + (-0.0130301,0) X4 X5 Y6 Y7 + (0.00171566,0) Z2 Y4 Z5 Y6 + (-0.00101412,0) X1 Z2 X3 Z5 + (-0.0113881,0) X4 Y5 Y6 X7 + (-0.0210619,0) Z0 X5 Z6 X7 + (-0.0113881,0) Y4 X5 X6 Y7 + (0.010771,0) X0 X1 Y4 Z5 Z6 Y7 + (-0.0246282,0) X0 Z1 X2 X5 Z6 X7 + (-0.0102424,0) X2 X3 Y4 Z5 Z6 Y7 + (-0.00101412,0) Y1 Z2 Y3 Z5 + (0.0112345,0) X0 Z1 X2 Z3 + (0.0214768,0) Y0 Z1 Y2 Z7 + (0.0112345,0) Y0 Z1 Y2 Z3 + (-0.0113881,0) Y0 X1 X2 Y3 + (-0.0130301,0) Y0 Y1 X2 X3 + (0.181365,0) Z0 + (-0.0244182,0) Y0 Y1 Y2 Y3 + (0.111941,0) Z2 Z5 + (0.106471,0) Z0 Z3 + (0.0245002,0) X0 X1 X5 X6 + (0.00171566,0) Y0 Z1 Y2 Z6 + (-0.0244182,0) X0 X1 X2 X3 + (0.0245002,0) Y1 Y2 Y4 Y5 + (0.0245002,0) X1 X2 X4 X5 + (0.0245002,0) X1 X2 Y4 Y5 + (-0.0102909,0) Z0 Y1 Z2 Y3 + (-0.0132401,0) Y1 Y2 X4 Z5 Z6 X7 + (0.0245002,0) X0 X1 Y5 Y6 + (-0.000703626,0) Z2 Y5 Z6 Y7 + (0.0245002,0) Y0 Y1 Y5 Y6 + (0.00171566,0) Z2 X4 Z5 X6 + (-0.0102424,0) Y0 Z1 Z2 Y3 Y6 Y7 + (0.0406161,0) X2 X3 X6 X7 + (0.0406161,0) Y2 Y3 Y6 Y7 + (-0.0246282,0) Y0 Z1 Y2 Y5 Z6 Y7 + (-0.0246282,0) Y0 Z1 Y2 X5 Z6 X7 + (0.0261613,0) X1 Z2 X3 Y5 Z6 Y7 + (-0.0246282,0) X0 Z1 X2 Y5 Z6 Y7 + (0.0406161,0) X2 X3 Y6 Y7 + (0.089825,0) Z5 Z7 + (-0.000703626,0) Z2 X5 Z6 X7 + (-0.00101412,0) Z1 X5 Z6 X7 + (0.0172666,0) X5 Z6 X7 + (0.0172666,0) Y5 Z6 Y7 + (-0.0248416,0) X1 X2 Y6 Y7 + (-0.0102909,0) Z0 X1 Z2 X3 + (-0.0132401,0) Y1 Y2 Y4 Z5 Z6 Y7 + (-0.0132401,0) X1 X2 Y4 Z5 Z6 Y7 + (-0.00101412,0) Z1 Y5 Z6 Y7 + (-0.0233845,0) Z3 Y5 Z6 Y7 + (0.0343207,0) Y1 Y2 Y5 Y6 + (-0.0248416,0) Y1 Y2 X6 X7 + (-0.0248416,0) Y1 Y2 Y6 Y7
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
...@@ -13,51 +13,69 @@ ...@@ -13,51 +13,69 @@
#include "Utils.hpp" #include "Utils.hpp"
#include "xacc.hpp" #include "xacc.hpp"
#include "xacc_observable.hpp" #include "xacc_observable.hpp"
#include "xacc_service.hpp"
#include "Circuit.hpp"
#include "PauliOperator.hpp"
// run this with // run this with
// $ mpirun -n N h4_chain_8qbit_hpc_virt --n-virtual-qpus M --n-hydrogens H // <MPI variables> srun -n X -N Y ./path/to/chain_hpc_virt --n-virtual-qpus <n_qpus> --n-hydrogens <n_h> --n-terms <n_terms> --accelerator <acc>
// where N is the number of MPI ranks, M is number of qpus,
// and H is number of hydrogens in the chain
int main(int argc, char **argv) { int main(int argc, char **argv) {
// Initialize and load the python plugins // Initialize
xacc::Initialize(argc, argv); xacc::Initialize(argc, argv);
xacc::external::load_external_language_plugins(); xacc::set_verbose(true);
std::string observable_generator = "pyscf"; xacc::logToFile(true);
xacc::setLoggingLevel(2);
// Process the input arguments // Process the input arguments
std::vector<std::string> arguments(argv + 1, argv + argc); std::vector<std::string> arguments(argv + 1, argv + argc);
auto n_virt_qpus = 2; int n_virt_qpus, n_hydrogen, n_terms = 0, n_cycles = 2;
auto n_hydrogens = 4; std::string acc;
for (int i = 0; i < arguments.size(); i++) { for (int i = 0; i < arguments.size(); i++) {
if (arguments[i] == "--n-virtual-qpus") { if (arguments[i] == "--n-virtual-qpus") {
n_virt_qpus = std::stoi(arguments[i + 1]); n_virt_qpus = std::stoi(arguments[i + 1]);
} }
if (arguments[i] == "--accelerator") {
acc = arguments[i + 1];
}
if (arguments[i] == "--n-hydrogens") { if (arguments[i] == "--n-hydrogens") {
n_hydrogens = std::stoi(arguments[i + 1]); n_hydrogen = std::stoi(arguments[i + 1]);
}
if (arguments[i] == "--n-terms") {
n_terms = std::stoi(arguments[i + 1]);
} }
if (arguments[i] == "--observable-generator") { if (arguments[i] == "--n-cycles") {
observable_generator = arguments[i + 1]; n_cycles = std::stoi(arguments[i + 1]);
} }
} }
// Create the Hydrogen chain geometry // Read in hamiltonian
std::stringstream geom_ss; std::ifstream hfile("@CMAKE_SOURCE_DIR@/quantum/examples/hpc_virtualization/h" + std::to_string(n_hydrogen) + ".txt");
if (observable_generator == "psi4") std::stringstream sss;
geom_ss << "0 1\n"; sss << hfile.rdbuf();
for (int i = 0; i < n_hydrogens; i++) { auto H = xacc::quantum::getObservable("pauli", sss.str());
geom_ss << "H 0.0 0.0 " << (double)i << "\n";
if (n_terms > H->getSubTerms().size()) {
xacc::error("--n-terms needs to be less than " + std::to_string(H->getSubTerms().size()));
} else if (n_terms != 0) {
auto terms = H->getSubTerms();
xacc::quantum::PauliOperator reducedH;
for (int i = 0; i < n_terms; i++) {
reducedH += *std::dynamic_pointer_cast<xacc::quantum::PauliOperator>(terms[i]);
}
H->fromString(reducedH.toString());
//H = xacc::quantum::getObservable("pauli", reducedH.toString());
} }
if (observable_generator == "psi4")
geom_ss << "symmetry c1\n";
// Create the Observable
auto H4 = xacc::quantum::getObservable(
observable_generator, {{"basis", "sto-3g"}, {"geometry", geom_ss.str()}});
// Get reference to the Accelerator // Get reference to the Accelerator
auto accelerator = std::shared_ptr<xacc::Accelerator> accelerator;
xacc::getAccelerator("tnqvm", {{"tnqvm-visitor", "exatn"}}); if (acc == "tnqvm") {
accelerator = xacc::getAccelerator("tnqvm", {{"tnqvm-visitor", "exatn"}});
} else if (acc == "qpp") {
accelerator = xacc::getAccelerator("qpp");
} else if (acc == "aer") {
accelerator = xacc::getAccelerator("aer");
}
// Decorate the accelerator with HPC Virtualization. // Decorate the accelerator with HPC Virtualization.
// This decorator assumes the provided number of input QPUs // This decorator assumes the provided number of input QPUs
...@@ -67,42 +85,31 @@ int main(int argc, char **argv) { ...@@ -67,42 +85,31 @@ int main(int argc, char **argv) {
accelerator = xacc::getAcceleratorDecorator( accelerator = xacc::getAcceleratorDecorator(
"hpc-virtualization", accelerator, {{"n-virtual-qpus", n_virt_qpus}}); "hpc-virtualization", accelerator, {{"n-virtual-qpus", n_virt_qpus}});
// Read the ground state circuit source code auto ansatz = std::dynamic_pointer_cast<xacc::CompositeInstruction>(xacc::getService<xacc::Instruction>("uccsd"));
std::ifstream inFile; ansatz->expand({{"ne", n_hydrogen}, {"nq", 2 * n_hydrogen}});
inFile.open("@CMAKE_SOURCE_DIR@/quantum/examples/hpc_virtualization/h4.qasm"); std::vector<double> zeros(ansatz->nVariables(), 0.0);
std::stringstream strStream; auto evaled = ansatz->operator()(zeros);
strStream << inFile.rdbuf();
const std::string kernelName = "h4_adapt_gs";
std::string h4SrcStr = strStream.str();
std::stringstream ss;
ss << ".compiler staq\n.circuit h4_ansatz\n.qbit q\n" << h4SrcStr;
// Create some qubits and compile the quantum code
auto q = xacc::qalloc(8);
q->setName("q");
xacc::storeBuffer(q);
xacc::qasm(ss.str());
auto ansatz = xacc::getCompiled("h4_ansatz");
// Get the VQE Algorithm and initialize it // Get the VQE Algorithm and initialize it
auto vqe = xacc::getAlgorithm( auto vqe = xacc::getAlgorithm(
"vqe", "vqe",
{{"ansatz", ansatz}, {"observable", H4}, {"accelerator", accelerator}}); {{"ansatz", evaled}, {"observable", H}, {"accelerator", accelerator}});
// Allocate some qubits and execute
double energy = 0.0; double energy = 0.0;
xacc::ScopeTimer timer("mpi_timing", false); for(int i = 0; i < n_cycles; i++){
energy = vqe->execute(q, {})[0]; auto q = xacc::qalloc(2 * n_hydrogen);
auto run_time = timer.getDurationMs(); xacc::ScopeTimer timer("mpi_timing", false);
energy = vqe->execute(q, {})[0];
auto run_time = timer.getDurationMs();
// Print the result // Print the result
if (q->hasExtraInfoKey("rank") ? ((*q)["rank"].as<int>() == 0) : true) { if (q->hasExtraInfoKey("rank") ? ((*q)["rank"].as<int>() == 0) : true) {
std::cout << "Energy: " << energy << " Hartree\n"; std::cout << "Energy: " << energy << " Hartree\n";
std::cout << "Runtime: " << run_time << " ms.\n"; std::cout << "Runtime: " << run_time << " ms.\n";
}
} }
// Finalize and unload the python plugins // Finalize
xacc::external::unload_external_language_plugins();
xacc::Finalize(); xacc::Finalize();
} }
\ No newline at end of file
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <mpi.h> #include <mpi.h>
using namespace boost;
namespace xacc { namespace xacc {
namespace quantum { namespace quantum {
...@@ -39,6 +40,21 @@ void HPCVirtDecorator::initialize(const HeterogeneousMap &params) { ...@@ -39,6 +40,21 @@ void HPCVirtDecorator::initialize(const HeterogeneousMap &params) {
} }
n_virtual_qpus = params.get<int>("n-virtual-qpus"); n_virtual_qpus = params.get<int>("n-virtual-qpus");
} }
// Initialize MPI together with decorator initialization
int initialized, threadSupport;
MPI_Initialized(&initialized);
if (!initialized) {
MPI_Init_thread(&xacc::argc, &xacc::argv, MPI_THREAD_MULTIPLE, &threadSupport);
}
xacc::info("Thread support = " + std::to_string(threadSupport));
if (threadSupport == MPI_THREAD_MULTIPLE) {
xacc::info("Running with MPI_THREAD_MULTIPLE support.");
} else {
xacc::warning("Threading support lesser than MPI_THREAD_MULTIPLE.");
}
} }
void HPCVirtDecorator::updateConfiguration(const HeterogeneousMap &config) { void HPCVirtDecorator::updateConfiguration(const HeterogeneousMap &config) {
...@@ -68,14 +84,6 @@ void HPCVirtDecorator::execute( ...@@ -68,14 +84,6 @@ void HPCVirtDecorator::execute(
// we wish to evaluate the <O> for each by partitioning // we wish to evaluate the <O> for each by partitioning
// their quantum execution across the node sub-groups. // their quantum execution across the node sub-groups.
using namespace boost;
int initialized, provided;
MPI_Initialized(&initialized);
if (!initialized) {
//MPI_Init(&xacc::argc, &xacc::argv);
MPI_Init_thread(&xacc::argc, &xacc::argv, MPI_THREAD_MULTIPLE, &provided);
}
mpi::communicator world; mpi::communicator world;
// Get the rank and size in the original communicator // Get the rank and size in the original communicator
...@@ -85,6 +93,7 @@ void HPCVirtDecorator::execute( ...@@ -85,6 +93,7 @@ void HPCVirtDecorator::execute(
// The number of MPI processes is less than the number of requested virtual // The number of MPI processes is less than the number of requested virtual
// QPUs, just execute as if there is only one virtual QPU and give the QPU // QPUs, just execute as if there is only one virtual QPU and give the QPU
// the whole MPI_COMM_WORLD. // the whole MPI_COMM_WORLD.
xacc::warning("MPI rank < Number of virtual QPUs. Will perform serial execution " + std::to_string(world_size) + "x times.");
void *qpu_comm_ptr = reinterpret_cast<void *>((MPI_Comm)world); void *qpu_comm_ptr = reinterpret_cast<void *>((MPI_Comm)world);
if (!qpuComm) { if (!qpuComm) {
qpuComm = std::make_shared<boost::mpi::communicator>(world); qpuComm = std::make_shared<boost::mpi::communicator>(world);
...@@ -94,6 +103,8 @@ void HPCVirtDecorator::execute( ...@@ -94,6 +103,8 @@ void HPCVirtDecorator::execute(
// just execute // just execute
decoratedAccelerator->execute(buffer, functions); decoratedAccelerator->execute(buffer, functions);
return; return;
} else {
xacc::info("Number of MPI ranks allow parallel execution.");
} }
// Get the color for this rank // Get the color for this rank
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment