Commit 838c74e2 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Updated QASM3-Q# QPE example



It's working now.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 8c102992
Loading
Loading
Loading
Loading
+17 −45
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ namespace QCOR
{
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Canon;

operation SWAP(q1 : Qubit, q2: Qubit) : Unit is Adj {
  CNOT(q1, q2);
@@ -9,53 +10,24 @@ operation SWAP(q1 : Qubit, q2: Qubit) : Unit is Adj {
  CNOT(q1, q2);
}

// 1-qubit QFT
operation OneQubitQFT (q : Qubit) : Unit is Adj {
  H(q);
}
// Rotation gate
// Applies a rotation about the |1⟩ state by an angle 
// specified as a dyadic fraction.
operation Rotation (q : Qubit, k : Int) : Unit is Adj+Ctl {
  let angle = 2.0 * 3.14159 / IntAsDouble(1 <<< k);
  // Message($"Angle {angle}");
  Rz(angle, q);
}
// Prepare binary fraction exponent in place (quantum input)
operation BinaryFractionQuantumInPlace (register : Qubit[]) : Unit is Adj {
  OneQubitQFT(register[0]);
  for ind in 1 .. Length(register) - 1 {
      // Message("Apply BinaryFractionQuantumInPlace");
      Controlled Rotation([register[ind]], (register[0], ind + 1));
  }
}
// Reverse the order of qubits
operation ReverseRegister (register : Qubit[]) : Unit is Adj {
    let N = Length(register);
    for ind in 0 .. N / 2 - 1 {
        SWAP(register[ind], register[N - 1 - ind]);
    }
}
// Quantum Fourier transform
// Input: A register of qubits in state |j₁j₂...⟩
// Goal: Apply quantum Fourier transform to the input register
operation QuantumFourierTransform (register : Qubit[]) : Unit is Adj {
  let n = Length(register);
  for i in 0 .. n - 1 {
      BinaryFractionQuantumInPlace(register[i ...]);
  }
  ReverseRegister(register);
operation IQFT(qq: Qubit[]): Unit {
  Message("Hello from Q# IQFT");
  for i in 0 .. Length(qq)/2 - 1 {
    SWAP(qq[i], qq[Length(qq)-i-1]);
  }
    
operation IQFT (register : Qubit[]) : Unit {
  Adjoint QuantumFourierTransform(register);
  for i in 0 .. Length(qq) - 2 {
    H(qq[i]);
    let j = i + 1;
    mutable y = i;
    repeat {
      let theta = -3.14159 / IntAsDouble(1 <<< (j-y));
      // Controlled R1 == CPhase
      Controlled R1([qq[j]], (theta, qq[y]));
      set y = y - 1;
    } until (y < 0);
  }

@EntryPoint() 
operation Dummy(): Unit {
  use qubits = Qubit[3] 
  {
    IQFT(qubits);
  }
  H(qq[Length(qq) -1]);
}
}
 No newline at end of file
+3 −2
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ OPENQASM 3;

const n_counting = 3;

// Declare external quantum subroutine:
// Declare external quantum subroutine (Q#)
def QCOR__IQFT__body qubit[n_counting]:qq extern;

// For this example, the oracle is the T gate 
@@ -36,6 +36,7 @@ for i in [0:n_counting] {
}

// Run inverse QFT 
// This Kernel is from Q#
QCOR__IQFT__body counting;

// Now lets measure the counting qubits
@@ -44,4 +45,4 @@ measure counting -> c;

// Backend is QPP which is lsb, 
// so return should be 100
print(c);
print("Final Bitstring:", c);