Commit 3252ea01 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Fixing bug in Circuit evaluate params for gates with more than 1 param


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent dc9f16bc
Pipeline #75335 passed with stage
in 4 minutes and 52 seconds
......@@ -291,6 +291,7 @@ PYBIND11_MODULE(_pyxacc, m) {
const std::vector<std::string> &)) &
xacc::CompositeInstruction::addVariables,
"")
.def("getVariables", &xacc::CompositeInstruction::getVariables, "")
.def("expand", &xacc::CompositeInstruction::expand, "")
.def("eval", &xacc::CompositeInstruction::operator(), "")
.def("name", &xacc::CompositeInstruction::name, "")
......
......@@ -81,34 +81,53 @@ Circuit::operator()(const std::vector<double> &params) {
}
std::vector<InstPtr> flatten;
InstructionIterator iter(shared_from_this());
while (iter.hasNext()) {
auto inst = iter.next();
if (!inst->isComposite()) {
flatten.emplace_back(inst);
}
InstructionIterator iter(shared_from_this());
while (iter.hasNext()) {
auto inst = iter.next();
if (!inst->isComposite()) {
flatten.emplace_back(inst);
}
}
auto evaluatedCircuit = std::make_shared<Circuit>("evaled_" + name());
// Walk the IR Tree, handle functions and instructions differently
for (auto inst : flatten) {
// If a concrete Gate, then check that it
// is parameterized and that it has a string parameter
if (inst->isParameterized() && inst->getParameter(0).isVariable()) {
InstructionParameter p = inst->getParameter(0);
std::stringstream s;
s << p.toString();
double val;
parsingUtil->evaluate(p.toString(), variables, params, val);
auto updatedInst = std::dynamic_pointer_cast<Gate>(inst)->clone();
updatedInst->setParameter(0, val);
if (inst->isParameterized()) {
auto updatedInst = std::dynamic_pointer_cast<Gate>(inst)->clone();
for (int i = 0; i < inst->nParameters(); i++) {
if (inst->getParameter(i).isVariable()) {
InstructionParameter p = inst->getParameter(i);
std::stringstream s;
s << p.toString();
double val;
parsingUtil->evaluate(p.toString(), variables, params, val);
updatedInst->setParameter(i, val);
} else {
auto a = inst->getParameter(i);
updatedInst->setParameter(i, a);
}
updatedInst->setBits(inst->bits());
updatedInst->setParameter(0, val);
evaluatedCircuit->addInstruction(updatedInst);
} else {
evaluatedCircuit->addInstruction(inst);
}
evaluatedCircuit->addInstruction(updatedInst);
} else {
evaluatedCircuit->addInstruction(inst);
}
// If a concrete Gate, then check that it
// is parameterized and that it has a string parameter
// if (inst->isParameterized() && inst->getParameter(0).isVariable()) {
// InstructionParameter p = inst->getParameter(0);
// std::stringstream s;
// s << p.toString();
// double val;
// parsingUtil->evaluate(p.toString(), variables, params, val);
// auto updatedInst = std::dynamic_pointer_cast<Gate>(inst)->clone();
// updatedInst->setParameter(0, val);
// updatedInst->setBits(inst->bits());
// evaluatedCircuit->addInstruction(updatedInst);
// } else {
// evaluatedCircuit->addInstruction(inst);
// }
}
return evaluatedCircuit;
}
......
......@@ -252,6 +252,20 @@ TEST(GateFunctionTester, checkDepth) {
EXPECT_EQ(3, g->depth());
}
TEST(GateFunctionTester, checkU3Eval) {
xacc::qasm(R"(
.compiler xasm
.circuit qubit2_depth1
.parameters x0,x1
.qbit q
U(q[0], x0, -3.14159/2, 3.14159/2 );
U(q[0], 0, 0, x1);
)");
auto f = xacc::getCompiled("qubit2_depth1");
auto ff = f->operator()({1.0, 1.0});
std::cout << "F: " << ff->toString() << "\n";
}
int main(int argc, char **argv) {
xacc::Initialize();
::testing::InitGoogleTest(&argc, argv);
......
......@@ -13,7 +13,7 @@
add_subdirectory(vqe)
add_subdirectory(rdm)
add_subdirectory(vqe-energy)
add_subdirectory(ddcl)
add_subdirectory(ml)
file(GLOB PYDECORATORS ${CMAKE_CURRENT_SOURCE_DIR}/vqe/python/*.py
${CMAKE_CURRENT_SOURCE_DIR}/vqe-energy/python/*.py
${CMAKE_CURRENT_SOURCE_DIR}/ddcl/python/*.py)
......
# *******************************************************************************
# Copyright (c) 2019 UT-Battelle, LLC.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v.10 which accompany this distribution.
# The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# https://eclipse.org/org/documents/edl-v10.php
#
# Contributors:
# Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
add_subdirectory(ddcl)
#add_subdirectory(rbm_classification)
......@@ -68,6 +68,7 @@ void DDCL::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const {
[&, this](const std::vector<double> &x, std::vector<double> &dx) {
// Evaluate and add measurements to all qubits
auto evaled = kernel->operator()(x);
std::set<std::size_t> uniqueBits;
InstructionIterator iter(evaled);
while(iter.hasNext()) {
......@@ -79,7 +80,6 @@ void DDCL::execute(const std::shared_ptr<AcceleratorBuffer> buffer) const {
}
}
for (auto b : uniqueBits) {
// for (std::size_t i = 0; i < buffer->size(); i++) {
auto m = provider->createInstruction("Measure",
std::vector<std::size_t>{b});
evaled->addInstruction(m);
......
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