Commit 0772472c authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added a FTQC extension for Pauli measurement



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent d638841c
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
#include "ftqc/vqe.hpp"

// Compile with:
// qcor -qpu qpp -qrt ftqc -I<qcor/examples/shared> deuteron.cpp 

__qpu__ void ansatz(qreg q, double theta) {
  X(q[0]);
  Ry(q[1], theta);
  CX(q[1], q[0]);
}

__qpu__ void test(qreg q, double theta, std::vector<qcor::PauliOperator> bases) {
  ansatz(q, theta);
  MeasureP(q, bases);
}

int main(int argc, char **argv) {
  // Allocate 2 qubits
  auto q = qalloc(2);

  // auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) -
  //          6.125 * Z(1);

  std::vector<qcor::PauliOperator> ops { X(0), X(1)};
  test(q, M_PI_4, ops);
  q.print();
}
+25 −0
Original line number Diff line number Diff line
#pragma once

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

// 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) {
  for (int i = 0; i < bases.size(); ++i) {
    auto pauliOp = bases[i];
    const std::string pauliStr = pauliOp.toString().substr(6);
    std::cout << "Pauli: " << pauliStr << "\n";
    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);
    }
    std::cout << "Measure q[" << bitIdx << "]\n";
    Measure(q[bitIdx]);
  }
}