Commit 73ee9a1e authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added an integration example using Q# standard lib



Overlap calc. by Hadamard test.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent eb9be248
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line

// Demonstrate integration b/w QASM3 kernels with Q# Library code:
// in this case EstimateRealOverlapBetweenStates from the Standard Library (Hadamard test)
// https://github.com/microsoft/QuantumLibraries/blob/main/Standard/src/Characterization/Distinguishability.qs
// Driver to tie them all together:
// - Declare the exported QASM3 callable 
// - Use that as input to the Q# operation

// Compile with:
// qcor -qrt ftqc overlap_calc.qs kernel.qasm driver.cpp

// Note: this driver can be part of QASM3 as well, but
// we don't have the concept of Callable type in QASM3 yet,
// hence, we cannot declare the Q# kernel taking Callable argumements as extern
// in QASM3 yet.
#include "qir-types.hpp"

// QASM3 function wrapping the quantum sub-routine as a QIR Callable
extern "C" ::Callable* qasm_x__callable(); 
extern "C" ::Callable* qasm_h__callable(); 
// Q# functions:
// Compute the overlap b/w states prepared by two ansatz kernels (defined in QASM3)
extern "C" double QCOR__ComputeOverlapBetweenState__body(::Callable *, ::Callable *, int64_t /*n iters*/);

int main() {
  const double overlapped = QCOR__ComputeOverlapBetweenState__body(
      qasm_x__callable(), qasm_h__callable(), 10000);
  // Print out the results:
  // Expected: 1/sqrt(2) b/w |1> and |+> state.
  std::cout << "Overlap = " << overlapped
            << " vs. expected = " << 1.0 / std::sqrt(2.0) << "\n";
  return 0;
}
 No newline at end of file
+12 −0
Original line number Diff line number Diff line
#pragma no_entrypoint;

OPENQASM 3;
include "stdgates.inc";

def qasm_x qubit:qb {
  x qb;
}

def qasm_h qubit:qb {
  h qb;
}
 No newline at end of file
+18 −0
Original line number Diff line number Diff line
namespace QCOR {
  open Microsoft.Quantum.Math;
  open Microsoft.Quantum.Canon;
  open Microsoft.Quantum.Intrinsic;
  open Microsoft.Quantum.Characterization;
  
  /// Given two operations which each prepare copies of a state, estimates
  /// the real part of the overlap between the states prepared by each
  /// operation.
  /// Leverage Q# Characterization library: 
  operation ComputeOverlapBetweenState(preparation1 : (Qubit => Unit is Adj + Ctl), preparation2 : (Qubit => Unit is Adj + Ctl), nMeasurements : Int) : Double {
    // Can use Q# to convert the functor types as well
    let prep1 = ApplyToEachCA(preparation1, _);
    let prep2 = ApplyToEachCA(preparation2, _);
    // This function is part of the Q# standard library
    return EstimateRealOverlapBetweenStates(NoOp<Qubit[]>, prep1, prep2, 1, nMeasurements);
  }
}
 No newline at end of file