Commit 0f5114d3 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Fixed up QCOR examples for FTQC runtime



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 466463da
Loading
Loading
Loading
Loading
+26 −24
Original line number Diff line number Diff line
@@ -5,39 +5,41 @@
// 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 Repeat-Until-Success pattern to prepare a quantum state.
// https://docs.microsoft.com/en-us/quantum/user-guide/using-qsharp/control-flow#rus-to-prepare-a-quantum-state
__qpu__ void PrepareStateUsingRUS(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]);
  // Note: target = q[0], aux = q[1]
  H(q[1]);
  // We limit the max number of RUS iterations.
  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]);
    }
    Tdg(q[1]);
    CNOT(q[0], q[1]);
    T(q[1]);

    measuredZero = measuredZero || (q.cReg(0) == false);
    measuredOne = measuredOne || (q.cReg(0) == true);
    if (measuredZero && measuredOne) {
      std::cout << "Success after " << i << " iterations.\n";
    // In order to measure in the PauliX basis, changes the basis.
    H(q[1]);
    Measure(q[1]);
    bool outcome = q.cReg(1);
    if (!outcome) {
      // Success (until (outcome == Zero))
      std::cout << "Success after " << i + 1 << " iterations.\n";
      break;
    }
    // Fix up: Bring the auxiliary and target qubits back to |+> state.
    if (outcome) {
      // Measure 1: |1> state
      X(q[1]);
      H(q[1]);
      X(q[0]);
      H(q[0]);
    } 

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

int main() {
  // qcor::set_verbose(true);
  auto q = qalloc(2);
  rus(q, 100);
  PrepareStateUsingRUS(q, 100);
}
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ __qpu__ void bell(qreg q, int nbRuns) {
    CX(q[0], q[1]);
    Measure(q[0]);
    Measure(q[1]);
    if (q.cReg(0) && q.cReg(1)) {
    if (q.cReg(0) == q.cReg(1)) {
      std::cout << "Iter " << i << ": Matched!\n";
    } else {
      // Should only happen if using a real (noisy) backend.