Unverified Commit 6cc4bf0a authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #44 from tnguyen-ornl/tnguyen/qsim-examples

Qsim code clean-up and reorg
parents 874b7474 270c0d00
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -18,3 +18,6 @@ add_test(NAME qrt_ftqc_deuteron COMMAND ${CMAKE_BINARY_DIR}/qcor -c -qrt ftqc ${
add_test(NAME qrt_ftqc_bit_flip_qec_std_lib COMMAND ${CMAKE_BINARY_DIR}/qcor -c -qrt ftqc ${CMAKE_CURRENT_SOURCE_DIR}/ftqc_qrt/error_correcting_code.cpp)
add_test(NAME qrt_ftqc_five_qubit_qec_std_lib COMMAND ${CMAKE_BINARY_DIR}/qcor -c -qrt ftqc ${CMAKE_CURRENT_SOURCE_DIR}/ftqc_qrt/five_qubit_qec_code.cpp)
add_test(NAME qrt_ftqc_steane_qec_std_lib COMMAND ${CMAKE_BINARY_DIR}/qcor -c -qrt ftqc ${CMAKE_CURRENT_SOURCE_DIR}/ftqc_qrt/steane_qec_code.cpp)
add_test(NAME qsim_vqe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/qsim/VqeWithAnsatzCircuit.cpp)
add_test(NAME qsim_trotter COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/qsim/TrotterTdWorkflow.cpp)
add_test(NAME qsim_iqpe_vqe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/qsim/IterativeQpeVqe.cpp)
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ int main(int argc, char **argv) {
  auto problemModel =
      qsim::ModelBuilder::createModel(eigen_state_prep, H, 2, 0);

  // Instantiate an IQPE workflow.
  // Instantiate an IQPE workflow
  auto workflow =
      qsim::getWorkflow("iqpe", {{"time-steps", 8}, {"iterations", 8}});

+56 −0
Original line number Diff line number Diff line
#include "qcor_qsim.hpp"

// Using noise-mitigation observable evaluation (Verified QPE)

// Compile and run with (e.g. using the 5-qubit Yorktown device)
/// $ qcor -qpu -qpu aer[noise-model:<noise JSON>] -shots 8192 -opt-pass
/// two-qubit-block-merging VerifiedQuantumPhaseEstimation.cpp
/// $ ./a.out

// For this simple circuit, the Trotter circuit can be significantly simplified
// by the `two-qubit-block-merging` optimizer (combine Trotter steps and
// decompose the total circuit into a more efficient KAK circuit).

// Note: for simplicity, we use the time-dependent Ising model with only 2
// qubits, hence with QPE, we need a total of 3 qubits. These 3 qubits can be
// directly mapped to the triangular topology of the Yorktown device.
// (Ref: Fig 3 of PhysRevB.101.184305)
int main(int argc, char **argv) {
  // Define the time-dependent Hamiltonian and observable operator using the
  // QCOR API Time-dependent Hamiltonian
  qsim::TdObservable H = [](double t) {
    // Parameters:
    const double Jz = 2 * M_PI * 2.86265 * 1e-3;
    const double epsilon = Jz;
    const double omega = 4.8 * 2 * M_PI * 1e-3;
    return -Jz * Z(0) * Z(1) - epsilon * std::cos(omega * t) * (X(0) + X(1));
  };

  // Observable = average magnetization
  auto observable = (1.0 / 2.0) * (Z(0) + Z(1));

  // Example: build model and TD workflow for Fig. 2 of
  // https://journals.aps.org/prb/pdf/10.1103/PhysRevB.101.184305
  auto problemModel = qsim::ModelBuilder::createModel(observable, H);
  // Trotter step = 3fs, number of steps = 100 -> end time = 300fs
  // Request the Verified QPE observable evaluator:
  auto vqpeEvaluator =
      qsim::getObjEvaluator(observable, "qpe", {{"verified", true}});
  auto workflow =
      qsim::getWorkflow("td-evolution", {{"method", "trotter"},
                                         {"dt", 3.0},
                                         {"steps", 100},
                                         {"evaluator", vqpeEvaluator}});

  // Result should contain the observable expectation value along Trotter steps.
  auto result = workflow->execute(problemModel);
  // Get the observable values (average magnetization)
  const auto obsVals = result.get<std::vector<double>>("exp-vals");

  // Print out for debugging:
  for (const auto &val : obsVals) {
    std::cout << "<Magnetization> = " << val << "\n";
  }

  return 0;
}
+2 −1
Original line number Diff line number Diff line
@@ -25,7 +25,8 @@ int main(int argc, char **argv) {
  // Instantiate a VQE workflow with the nlopt optimizer
  auto workflow = qsim::getWorkflow("vqe", {{"optimizer", optimizer}});

  // Result should contain the observable expectation value along Trotter steps.
  // Result should contain the ground-state energy along with the optimal
  // parameters.
  auto result = workflow->execute(problemModel);

  const auto energy = result.get<double>("energy");
Loading