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)
configure_file(tnqvm_h4_chain_8qbit_adapt_gs.in.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)
target_link_libraries(h4_chain_8qbit_hpc_virt PRIVATE xacc xacc-quantum-gate)
add_executable(chain_hpc_virt ${CMAKE_BINARY_DIR}/quantum/examples/hpc_virtualization/tnqvm_h4_chain_8qbit_adapt_gs.cpp)
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 @@
#include "Utils.hpp"
#include "xacc.hpp"
#include "xacc_observable.hpp"
#include "xacc_service.hpp"
#include "Circuit.hpp"
#include "PauliOperator.hpp"
// run this with
// $ mpirun -n N h4_chain_8qbit_hpc_virt --n-virtual-qpus M --n-hydrogens H
// where N is the number of MPI ranks, M is number of qpus,
// and H is number of hydrogens in the chain
// <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>
int main(int argc, char **argv) {
// Initialize and load the python plugins
// Initialize
xacc::Initialize(argc, argv);
xacc::external::load_external_language_plugins();
std::string observable_generator = "pyscf";
xacc::set_verbose(true);
xacc::logToFile(true);
xacc::setLoggingLevel(2);
// Process the input arguments
std::vector<std::string> arguments(argv + 1, argv + argc);
auto n_virt_qpus = 2;
auto n_hydrogens = 4;
int n_virt_qpus, n_hydrogen, n_terms = 0, n_cycles = 2;
std::string acc;
for (int i = 0; i < arguments.size(); i++) {
if (arguments[i] == "--n-virtual-qpus") {
n_virt_qpus = std::stoi(arguments[i + 1]);
}
if (arguments[i] == "--accelerator") {
acc = arguments[i + 1];
}
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") {
observable_generator = arguments[i + 1];
if (arguments[i] == "--n-cycles") {
n_cycles = std::stoi(arguments[i + 1]);
}
}
// Create the Hydrogen chain geometry
std::stringstream geom_ss;
if (observable_generator == "psi4")
geom_ss << "0 1\n";
for (int i = 0; i < n_hydrogens; i++) {
geom_ss << "H 0.0 0.0 " << (double)i << "\n";
// Read in hamiltonian
std::ifstream hfile("@CMAKE_SOURCE_DIR@/quantum/examples/hpc_virtualization/h" + std::to_string(n_hydrogen) + ".txt");
std::stringstream sss;
sss << hfile.rdbuf();
auto H = xacc::quantum::getObservable("pauli", sss.str());
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
auto accelerator =
xacc::getAccelerator("tnqvm", {{"tnqvm-visitor", "exatn"}});
std::shared_ptr<xacc::Accelerator> accelerator;
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.
// This decorator assumes the provided number of input QPUs
......@@ -67,42 +85,31 @@ int main(int argc, char **argv) {
accelerator = xacc::getAcceleratorDecorator(
"hpc-virtualization", accelerator, {{"n-virtual-qpus", n_virt_qpus}});
// Read the ground state circuit source code
std::ifstream inFile;
inFile.open("@CMAKE_SOURCE_DIR@/quantum/examples/hpc_virtualization/h4.qasm");
std::stringstream strStream;
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");
auto ansatz = std::dynamic_pointer_cast<xacc::CompositeInstruction>(xacc::getService<xacc::Instruction>("uccsd"));
ansatz->expand({{"ne", n_hydrogen}, {"nq", 2 * n_hydrogen}});
std::vector<double> zeros(ansatz->nVariables(), 0.0);
auto evaled = ansatz->operator()(zeros);
// Get the VQE Algorithm and initialize it
auto vqe = xacc::getAlgorithm(
"vqe",
{{"ansatz", ansatz}, {"observable", H4}, {"accelerator", accelerator}});
{{"ansatz", evaled}, {"observable", H}, {"accelerator", accelerator}});
// Allocate some qubits and execute
double energy = 0.0;
xacc::ScopeTimer timer("mpi_timing", false);
energy = vqe->execute(q, {})[0];
auto run_time = timer.getDurationMs();
for(int i = 0; i < n_cycles; i++){
auto q = xacc::qalloc(2 * n_hydrogen);
xacc::ScopeTimer timer("mpi_timing", false);
energy = vqe->execute(q, {})[0];
auto run_time = timer.getDurationMs();
// Print the result
if (q->hasExtraInfoKey("rank") ? ((*q)["rank"].as<int>() == 0) : true) {
std::cout << "Energy: " << energy << " Hartree\n";
std::cout << "Runtime: " << run_time << " ms.\n";
// Print the result
if (q->hasExtraInfoKey("rank") ? ((*q)["rank"].as<int>() == 0) : true) {
std::cout << "Energy: " << energy << " Hartree\n";
std::cout << "Runtime: " << run_time << " ms.\n";
}
}
// Finalize and unload the python plugins
xacc::external::unload_external_language_plugins();
// Finalize
xacc::Finalize();
}
\ No newline at end of file
}
......@@ -22,6 +22,7 @@
#include <mpi.h>
using namespace boost;
namespace xacc {
namespace quantum {
......@@ -39,6 +40,21 @@ void HPCVirtDecorator::initialize(const HeterogeneousMap &params) {
}
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) {
......@@ -68,14 +84,6 @@ void HPCVirtDecorator::execute(
// we wish to evaluate the <O> for each by partitioning
// 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;
// Get the rank and size in the original communicator
......@@ -85,6 +93,7 @@ void HPCVirtDecorator::execute(
// 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
// 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);
if (!qpuComm) {
qpuComm = std::make_shared<boost::mpi::communicator>(world);
......@@ -94,6 +103,8 @@ void HPCVirtDecorator::execute(
// just execute
decoratedAccelerator->execute(buffer, functions);
return;
} else {
xacc::info("Number of MPI ranks allow parallel execution.");
}
// 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