Commit 466463da authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added some Q#-like examples



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent b3028cf3
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
#include <qalloc>
// Compile with: qcor -qpu qpp -qrt ftqc repeat-until-success.cpp
// Execute: ./a.out
// We should get the print out conditioned by the measurement.
// If not using the "ftqc" QRT, this will cause errors since the Measure results
// are not available yet.

__qpu__ void rus(qreg q, int maxIter) {
  using qcor::xasm;
  // Note: control = q[0], eigenstate = q[1]
  // U = Z; P = X
  bool measuredZero = false;
  bool measuredOne = false;
  X(q[1]);
  for (int i = 0; i < maxIter; ++i) {
    std::cout << "Iter: " << i << "\n";
    H(q[0]);
    CZ(q[0], q[1]);
    H(q[0]);
    Measure(q[0]);
    if (q.cReg(0)) {
      // Fix up: if measure = 1, reset the control qubit back to 0.
      // Note: the eigenstate qubit remains coherent during this RUS loop.
      X(q[0]);
    }

    measuredZero = measuredZero || (q.cReg(0) == false);
    measuredOne = measuredOne || (q.cReg(0) == true);
    if (measuredZero && measuredOne) {
      std::cout << "Success after " << i << " iterations.\n";
      break;
    }
  }

  if (!measuredZero || !measuredOne) {
    std::cout << "Failed!!!\n";
  }
}

int main() {
  auto q = qalloc(2);
  rus(q, 100);
}
+21 −18
Original line number Diff line number Diff line
@@ -5,27 +5,30 @@
// If not using the "ftqc" QRT, this will cause errors since the Measure results
// are not available yet.

__qpu__ void bell(qreg q) {
__qpu__ void bell(qreg q, int nbRuns) {
  using qcor::xasm;
  for (int i = 0; i < nbRuns; ++i) {
    H(q[0]);
    CX(q[0], q[1]);
  for (int i = 0; i < 2; i++)
    X(q[i]);
  
    Measure(q[0]);
  
    Measure(q[1]);
    if (q.cReg(0) && q.cReg(1)) {
      std::cout << "Iter " << i << ": Matched!\n";
    } else {
      // Should only happen if using a real (noisy) backend.
      std::cout << "Iter " << i << ": Not matched!\n";
    }
    // Reset qubits
    if (q.cReg(0)) {
    std::cout << "Q0 = 1 !\n";
      X(q[0]);
    }
  
  Measure(q[1]);
    if (q.cReg(1)) {
    std::cout << "Q1 = 1 !\n";
      X(q[1]);
    }
  }
}

int main() {
  qcor::set_verbose(true);
  auto q = qalloc(2);
  bell(q);
  bell(q, 100);
}
+6 −2
Original line number Diff line number Diff line
@@ -186,8 +186,12 @@ void XasmTokenCollector::collect(clang::Preprocessor &PP,
      ss << qrt_visitor.get_new_src();

    } else {
      // this was a classical code string
      ss << visitor.result.first;
      // this was a classical code string;
      // hence, append the original source line.
      // IMPORTANT: the ANTLR tokenized string may be different,
      // e.g. it may parse '&&' as two tokens hence adding a space
      // which will mess up the C++ code.
      ss << line;
    }
  }
}