Commit 159ce1f5 authored by Daniel Claudino's avatar Daniel Claudino
Browse files

Print circuit and number of terms in commutator


Signed-off-by: default avatarDaniel Claudino <6d3@ornl.gov>
parent 2b073add
...@@ -20,6 +20,6 @@ adapt = xacc.getAlgorithm('adapt-vqe', {'accelerator': qpu, ...@@ -20,6 +20,6 @@ adapt = xacc.getAlgorithm('adapt-vqe', {'accelerator': qpu,
'threshold': 1.0e-2, 'threshold': 1.0e-2,
'print-threshold': 1.0e-10, 'print-threshold': 1.0e-10,
'maxiter': 2, 'maxiter': 2,
'pool': "uccsd"}) 'pool': "singlet-adapted-uccsd"})
# execute # execute
adapt.execute(buffer) adapt.execute(buffer)
...@@ -217,29 +217,34 @@ void ADAPT_VQE::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const { ...@@ -217,29 +217,34 @@ void ADAPT_VQE::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const {
for (int operatorIdx = 0; operatorIdx < commutators.size(); operatorIdx++){ for (int operatorIdx = 0; operatorIdx < commutators.size(); operatorIdx++){
// only compute commutators if they aren't zero // only compute commutators if they aren't zero
if(std::dynamic_pointer_cast<PauliOperator>(commutators[operatorIdx])->getTerms().size() != 0){ int nTermsCommutator = std::dynamic_pointer_cast<PauliOperator>(commutators[operatorIdx])->getTerms().size();
// observe the commutators with the updated circuit ansatz if(nTermsCommutator != 0){
auto grad_vqe = xacc::getAlgorithm(
"vqe", {std::make_pair("observable", commutators[operatorIdx]), // Print number of instructions for computing <observable>
std::make_pair("optimizer", optimizer), xacc::info("Number of instructions for commutator calculation: "
std::make_pair("accelerator", accelerator), + std::to_string(nTermsCommutator));
std::make_pair("ansatz", ansatzInstructions)}); // observe the commutators with the updated circuit ansatz
auto tmp_buffer = xacc::qalloc(buffer->size()); auto grad_vqe = xacc::getAlgorithm(
auto commutatorValue = std::real(grad_vqe->execute(tmp_buffer, x)[0]); "vqe", {std::make_pair("observable", commutators[operatorIdx]),
std::make_pair("optimizer", optimizer),
if(abs(commutatorValue) > _printThreshold){ std::make_pair("accelerator", accelerator),
ss << std::setprecision(12) << "[H," << operatorIdx << "] = " << commutatorValue << "\n"; std::make_pair("ansatz", ansatzInstructions)});
xacc::info(ss.str()); auto tmp_buffer = xacc::qalloc(buffer->size());
ss.str(std::string()); auto commutatorValue = std::real(grad_vqe->execute(tmp_buffer, x)[0]);
}
if(abs(commutatorValue) > _printThreshold){
ss << std::setprecision(12) << "[H," << operatorIdx << "] = " << commutatorValue << "\n";
xacc::info(ss.str());
ss.str(std::string());
}
// update maxCommutator // update maxCommutator
if(abs(commutatorValue) > abs(maxCommutator)){ if(abs(commutatorValue) > abs(maxCommutator)){
maxCommutatorIdx = operatorIdx; maxCommutatorIdx = operatorIdx;
maxCommutator = commutatorValue; maxCommutator = commutatorValue;
} }
gradientNorm += commutatorValue * commutatorValue; gradientNorm += commutatorValue * commutatorValue;
} }
} }
...@@ -266,6 +271,8 @@ void ADAPT_VQE::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const { ...@@ -266,6 +271,8 @@ void ADAPT_VQE::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const {
xacc::info(ss.str() + "\n"); xacc::info(ss.str() + "\n");
ss.str(std::string()); ss.str(std::string());
xacc::info("Final ADAPT-VQE circuit\n" + ansatzInstructions->toString());
return; return;
} else if (iter < _maxIter) { // Add operator and reoptimize } else if (iter < _maxIter) { // Add operator and reoptimize
......
...@@ -126,7 +126,7 @@ public: ...@@ -126,7 +126,7 @@ public:
return pool; return pool;
} }
const std::string name() const override { return "uccsd"; } const std::string name() const override { return "singlet-adapted-uccsd"; }
const std::string description() const override { return ""; } const std::string description() const override { return ""; }
}; };
......
...@@ -61,7 +61,7 @@ TEST(AdaptVQETester, checkSimple) { ...@@ -61,7 +61,7 @@ TEST(AdaptVQETester, checkSimple) {
EXPECT_TRUE(adapt_vqe->initialize({std::make_pair("accelerator",acc), EXPECT_TRUE(adapt_vqe->initialize({std::make_pair("accelerator",acc),
std::make_pair("observable", H), std::make_pair("observable", H),
std::make_pair("optimizer", optimizer), std::make_pair("optimizer", optimizer),
std::make_pair("pool", "uccsd"), std::make_pair("pool", "singlet-adapted-uccsd"),
std::make_pair("gradient-strategy", "parameter-shift-gradient"), std::make_pair("gradient-strategy", "parameter-shift-gradient"),
std::make_pair("nElectrons", 2)})); std::make_pair("nElectrons", 2)}));
......
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