Loading lib/qsim/impls/qsim_impl.cpp +29 −27 Original line number Diff line number Diff line Loading @@ -135,10 +135,11 @@ bool IterativeQpeWorkflow::initialize(const HeterogeneousMap ¶ms) { std::shared_ptr<CompositeInstruction> IterativeQpeWorkflow::constructQpeCircuit( std::shared_ptr<Observable> obs, int k, double omega, bool measure) const { auto provider = xacc::getIRProvider("quantum"); auto kernel = provider->createComposite("__TEMP__QPE__LOOP__"); const auto nbQubits = obs->nBits(); // Ancilla qubit is the last one in the register. const size_t ancBit = nbQubits; // Hadamard on ancilla qubit kernel->addInstruction(provider->createInstruction("H", ancBit)); Loading @@ -149,6 +150,7 @@ std::shared_ptr<CompositeInstruction> IterativeQpeWorkflow::constructQpeCircuit( auto trotterCir = method.create_ansatz(obs.get(), {{"dt", trotterStepSize}}).circuit; // std::cout << "Trotter circ:\n" << trotterCir->toString() << "\n"; // Controlled-U auto ctrlKernel = std::dynamic_pointer_cast<CompositeInstruction>( xacc::getService<xacc::Instruction>("C-U")); Loading @@ -159,15 +161,12 @@ std::shared_ptr<CompositeInstruction> IterativeQpeWorkflow::constructQpeCircuit( // Apply C-U^n int power = 1 << (k - 1); // std::cout << "Power = " << power << "\n"; for (int i = 0; i < power; ++i) { for (int j = 0; j < num_steps; ++j) { for (int i = 0; i < power * num_steps; ++i) { for (int instId = 0; instId < ctrlKernel->nInstructions(); ++instId) { // We need to clone the instruction since it'll be repeated. kernel->addInstruction(ctrlKernel->getInstruction(instId)->clone()); } } } // Rz on ancilla qubit // Global phase due to identity pauli Loading @@ -193,11 +192,9 @@ std::shared_ptr<CompositeInstruction> IterativeQpeWorkflow::constructQpeCircuit( void IterativeQpeWorkflow::HamOpConverter::fromObservable(Observable *obs) { translation = 0.0; for (auto &term : obs->getSubTerms()) { translation += std::abs(term->coefficient()); } stretch = 0.5 / translation; } Loading Loading @@ -251,22 +248,26 @@ IterativeQpeWorkflow::execute(const QuantumSimulationModel &model) { qpu->execute(temp_buffer, kernel); // temp_buffer->print(); // Estimate the phase value's bit at this iteration, // i.e. get the most-probable measure bit. const bool bitResult = [&temp_buffer]() { if (!temp_buffer->getMeasurementCounts().empty()) { // If the QPU returns bitstrings: if (xacc::container::contains(temp_buffer->getMeasurements(), "0")) { if (xacc::container::contains(temp_buffer->getMeasurements(), "1")) { return temp_buffer->computeMeasurementProbability("1") > temp_buffer->computeMeasurementProbability("0"); } else { return temp_buffer->computeMeasurementProbability("1") > temp_buffer->computeMeasurementProbability("0"); } else { return false; } } else { assert(xacc::container::contains(temp_buffer->getMeasurements(), "1")); } else { assert( xacc::container::contains(temp_buffer->getMeasurements(), "1")); return true; } } else { } else { // If the QPU returns *expected* Z value: return temp_buffer->getExpectationValueZ() < 0.0; } }(); Loading @@ -274,11 +275,12 @@ IterativeQpeWorkflow::execute(const QuantumSimulationModel &model) { if (bitResult) { omega_coef = omega_coef + 0.5; } // std::cout << "Iter " << iterIdx << ": Result = " << bitResult << "; omega_coef = " << omega_coef << "\n"; // std::cout << "Iter " << iterIdx << ": Result = " << bitResult << "; // omega_coef = " << omega_coef << "\n"; } return { {"phase", omega_coef}, {"energy", ham_converter.computeEnergy(omega_coef)}}; return {{"phase", omega_coef}, {"energy", ham_converter.computeEnergy(omega_coef)}}; } std::shared_ptr<QuantumSimulationWorkflow> Loading lib/qsim/impls/tests/CMakeLists.txt +2 −0 Original line number Diff line number Diff line add_test(NAME qsim_vqe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/VqeWithAnsatzCircuit.cpp) add_test(NAME qsim_trotter COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/TrotterTdWorkflow.cpp) add_test(NAME qsim_iqpe_simple COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/IterativeQpeSimple.cpp) add_test(NAME qsim_iqpe_vqe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/IterativeQpeVqe.cpp) lib/qsim/impls/tests/IterativeQpeWorkflow.cpp→lib/qsim/impls/tests/IterativeQpeSimple.cpp +0 −0 File moved. View file Loading
lib/qsim/impls/qsim_impl.cpp +29 −27 Original line number Diff line number Diff line Loading @@ -135,10 +135,11 @@ bool IterativeQpeWorkflow::initialize(const HeterogeneousMap ¶ms) { std::shared_ptr<CompositeInstruction> IterativeQpeWorkflow::constructQpeCircuit( std::shared_ptr<Observable> obs, int k, double omega, bool measure) const { auto provider = xacc::getIRProvider("quantum"); auto kernel = provider->createComposite("__TEMP__QPE__LOOP__"); const auto nbQubits = obs->nBits(); // Ancilla qubit is the last one in the register. const size_t ancBit = nbQubits; // Hadamard on ancilla qubit kernel->addInstruction(provider->createInstruction("H", ancBit)); Loading @@ -149,6 +150,7 @@ std::shared_ptr<CompositeInstruction> IterativeQpeWorkflow::constructQpeCircuit( auto trotterCir = method.create_ansatz(obs.get(), {{"dt", trotterStepSize}}).circuit; // std::cout << "Trotter circ:\n" << trotterCir->toString() << "\n"; // Controlled-U auto ctrlKernel = std::dynamic_pointer_cast<CompositeInstruction>( xacc::getService<xacc::Instruction>("C-U")); Loading @@ -159,15 +161,12 @@ std::shared_ptr<CompositeInstruction> IterativeQpeWorkflow::constructQpeCircuit( // Apply C-U^n int power = 1 << (k - 1); // std::cout << "Power = " << power << "\n"; for (int i = 0; i < power; ++i) { for (int j = 0; j < num_steps; ++j) { for (int i = 0; i < power * num_steps; ++i) { for (int instId = 0; instId < ctrlKernel->nInstructions(); ++instId) { // We need to clone the instruction since it'll be repeated. kernel->addInstruction(ctrlKernel->getInstruction(instId)->clone()); } } } // Rz on ancilla qubit // Global phase due to identity pauli Loading @@ -193,11 +192,9 @@ std::shared_ptr<CompositeInstruction> IterativeQpeWorkflow::constructQpeCircuit( void IterativeQpeWorkflow::HamOpConverter::fromObservable(Observable *obs) { translation = 0.0; for (auto &term : obs->getSubTerms()) { translation += std::abs(term->coefficient()); } stretch = 0.5 / translation; } Loading Loading @@ -251,22 +248,26 @@ IterativeQpeWorkflow::execute(const QuantumSimulationModel &model) { qpu->execute(temp_buffer, kernel); // temp_buffer->print(); // Estimate the phase value's bit at this iteration, // i.e. get the most-probable measure bit. const bool bitResult = [&temp_buffer]() { if (!temp_buffer->getMeasurementCounts().empty()) { // If the QPU returns bitstrings: if (xacc::container::contains(temp_buffer->getMeasurements(), "0")) { if (xacc::container::contains(temp_buffer->getMeasurements(), "1")) { return temp_buffer->computeMeasurementProbability("1") > temp_buffer->computeMeasurementProbability("0"); } else { return temp_buffer->computeMeasurementProbability("1") > temp_buffer->computeMeasurementProbability("0"); } else { return false; } } else { assert(xacc::container::contains(temp_buffer->getMeasurements(), "1")); } else { assert( xacc::container::contains(temp_buffer->getMeasurements(), "1")); return true; } } else { } else { // If the QPU returns *expected* Z value: return temp_buffer->getExpectationValueZ() < 0.0; } }(); Loading @@ -274,11 +275,12 @@ IterativeQpeWorkflow::execute(const QuantumSimulationModel &model) { if (bitResult) { omega_coef = omega_coef + 0.5; } // std::cout << "Iter " << iterIdx << ": Result = " << bitResult << "; omega_coef = " << omega_coef << "\n"; // std::cout << "Iter " << iterIdx << ": Result = " << bitResult << "; // omega_coef = " << omega_coef << "\n"; } return { {"phase", omega_coef}, {"energy", ham_converter.computeEnergy(omega_coef)}}; return {{"phase", omega_coef}, {"energy", ham_converter.computeEnergy(omega_coef)}}; } std::shared_ptr<QuantumSimulationWorkflow> Loading
lib/qsim/impls/tests/CMakeLists.txt +2 −0 Original line number Diff line number Diff line add_test(NAME qsim_vqe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/VqeWithAnsatzCircuit.cpp) add_test(NAME qsim_trotter COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/TrotterTdWorkflow.cpp) add_test(NAME qsim_iqpe_simple COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/IterativeQpeSimple.cpp) add_test(NAME qsim_iqpe_vqe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/IterativeQpeVqe.cpp)
lib/qsim/impls/tests/IterativeQpeWorkflow.cpp→lib/qsim/impls/tests/IterativeQpeSimple.cpp +0 −0 File moved. View file