Commit 2d9245e1 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Start to work on porting examples to standard lib



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 6e304b6b
Loading
Loading
Loading
Loading
+3 −34
Original line number Diff line number Diff line
@@ -2,41 +2,10 @@

#include <vector>
#include "qcor_observable.hpp"
#include <qcor_common>

// Util to reset all qubits in FTQC mode:
// TODO: we can add this as a *native* method so that the
// simulator can just do a wavefunction reset.
__qpu__ void ResetAll(qreg q) {
  for (int i = 0; i < q.size(); ++i) {
    if (Measure(q[i])) {
      X(q[i]);
    }
  }
}

// Measure tensor product of Paulis operators.
// Note: the bases must be elementary (I, X, Y, Z) Pauli ops
__qpu__ void MeasureP(qreg q, std::vector<qcor::PauliOperator> bases, int& out_parity) {
  int oneCount = 0;
  for (int i = 0; i < bases.size(); ++i) {
    auto pauliOp = bases[i];
    const std::string pauliStr = pauliOp.toString().substr(6);
    const auto bitIdx = std::stoi(pauliStr.substr(1));
    // TODO: fix XASM compiler to handle char literal
    if (pauliStr.rfind("X", 0) == 0) {
      H(q[bitIdx]);
    }
    
    if (pauliStr.rfind("Y", 0) == 0) {
      Rx(q[bitIdx], M_PI_2);
    }
    if (Measure(q[bitIdx])) {
      oneCount++;
    }
  }
  out_parity = oneCount - 2 * (oneCount / 2);
}

// TODO: investigate why `ftqc::MeasureP` is corrupted during codegen.
using namespace ftqc;
__qpu__ void EstimateTermExpectation(qreg q, const std::function<void(qreg)>& statePrep, std::vector<qcor::PauliOperator> bases, int nSamples, double& out_energy) {
  double sum = 0.0;
  for (int i = 0; i < nSamples; ++i) {

lib/impl/common.hpp

0 → 100644
+39 −0
Original line number Diff line number Diff line
#pragma once
#include <qalloc>
#include <vector>
#include "qcor_observable.hpp"

#ifdef _QCOR_FTQC_RUNTIME 
namespace ftqc {
__qpu__ void ResetAll(qreg q) {
  for (int i = 0; i < q.size(); ++i) {
    if (Measure(q[i])) {
      X(q[i]);
    }
  }
}

// FTQC "sync" Pauli measurement: returns the parity output
__qpu__ void MeasureP(qreg q, std::vector<qcor::PauliOperator> bases,
                      int &out_parity) {
  int oneCount = 0;
  for (int i = 0; i < bases.size(); ++i) {
    auto pauliOp = bases[i];
    const std::string pauliStr = pauliOp.toString().substr(6);
    const auto bitIdx = std::stoi(pauliStr.substr(1));
    // TODO: fix XASM compiler to handle char literal
    if (pauliStr.rfind("X", 0) == 0) {
      H(q[bitIdx]);
    }

    if (pauliStr.rfind("Y", 0) == 0) {
      Rx(q[bitIdx], M_PI_2);
    }
    if (Measure(q[bitIdx])) {
      oneCount++;
    }
  }
  out_parity = oneCount - 2 * (oneCount / 2);
}
}
#endif
 No newline at end of file

lib/qcor_common

0 → 100644
+2 −0
Original line number Diff line number Diff line
#pragma once
#include "impl/common.hpp"
+2 −1
Original line number Diff line number Diff line
@@ -138,7 +138,8 @@ def main(argv=None):
        sys.argv.remove(qrtName)
        sys.argv.remove('-qrt')
        sys.argv += ['-D__internal__qcor__compile__qrt__mode=\"'+qrtName+'\"']   

        if qrtName == 'ftqc': 
            sys.argv += ['-D_QCOR_FTQC_RUNTIME']
    # Get the filename we are compiling or the object file
    filename = ''
    fileType = ''