Commit 209feb6c authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added a simple Q# compile and link example



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent e3bd7ad4
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Core{

    @Attribute()
    newtype Attribute = Unit;

    @Attribute()
    newtype Inline = Unit;

    @Attribute()
    newtype EntryPoint = Unit;

    function Length<'T> (array : 'T[]) : Int { body intrinsic; }

    function RangeStart (range : Range) : Int { body intrinsic; }

    function RangeStep (range : Range) : Int { body intrinsic; }

    function RangeEnd (range : Range) : Int { body intrinsic; }

    function RangeReverse (range : Range) : Range { body intrinsic; }
}

namespace Microsoft.Quantum.Targeting {

    @Attribute()
    newtype TargetInstruction = String;
}
 No newline at end of file
+135 −0
Original line number Diff line number Diff line
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Instructions {

    operation S (qb : Qubit) : Unit {
        body intrinsic;
    }

    operation Rx (theta : Double, qb : Qubit) : Unit {
        body intrinsic;
    }

    operation Rz (theta : Double, qb : Qubit) : Unit {
        body intrinsic;
    }
}

namespace Microsoft.Quantum.Intrinsic {

    open Microsoft.Quantum.Targeting;
    open Microsoft.Quantum.Instructions as Phys;
    
    @Inline()
    function PI() : Double
    {
        return 3.14159265357989;
    }
    
    function IntAsDouble(i : Int) : Double {
        body intrinsic;
    }

    operation X(qb : Qubit) : Unit
    is Adj {
        body intrinsic;
        adjoint self;
	}

    operation Z(qb : Qubit) : Unit
    is Adj {
        body intrinsic;
        adjoint self;
	}

    operation H(qb : Qubit) : Unit
    is Adj {
        body intrinsic;
        adjoint self;
	}

    operation T(qb : Qubit) : Unit 
    is Adj {
        body intrinsic;
	}

    operation CNOT(control : Qubit, target : Qubit) : Unit
    is Adj {
        body intrinsic;
        adjoint self;
	}

    @TargetInstruction("mz")
    operation M(qb : Qubit) : Result {
        body intrinsic;
	}

    operation Measure(bases : Pauli[], qubits : Qubit[]) : Result {
        body intrinsic;
    }

    operation MResetZ(qb : Qubit) : Result
    {
        let res = M(qb);
        if (res == One)
        {
            X(qb);
        }
        return res;
    }

    @Inline()
    operation S(qb : Qubit) : Unit
    is Adj {
        body  (...)
        {
            Phys.S(qb);  
		}
        adjoint (...)
        {
            Phys.S(qb);
            Z(qb);
        }
	}

    @Inline()
    operation Rx(theta : Double, qb : Qubit) : Unit
    is Adj {
        body  (...)
        {
            Phys.Rx(theta, qb);  
		}
        adjoint (...)
        {
            Phys.Rx(-theta, qb);  
		}
	}

    @Inline()
    operation Rz(theta : Double, qb : Qubit) : Unit
    is Adj + Ctl {
        body  (...)
        {
            Phys.Rz(theta, qb);  
		}
        adjoint (...)
        {
            Phys.Rz(-theta, qb);  
		}
        controlled (ctls, ...)
        {
            Phys.Rz(theta / 2.0, qb);
            CNOT(ctls[0], qb);
            Phys.Rz(-theta / 2.0, qb);
            CNOT(ctls[0], qb);
        }
        controlled adjoint (ctls, ...)
        {
            Phys.Rz(-theta / 2.0, qb);
            CNOT(ctls[0], qb);
            Phys.Rz(theta / 2.0, qb);
            CNOT(ctls[0], qb);
        }
	}
}
 No newline at end of file
+20 −0
Original line number Diff line number Diff line
#include <iostream> 
#include <vector>
#include "qcor.hpp"

// Include the external QSharp function.
qcor_include_qsharp(XACC__TestBell__body, int64_t, int64_t)


// Compile with:
// Include both the qsharp source and this driver file 
// in the command line.
// $ qcor source.qs driver.cpp
// Run with:
// $ ./a.out
int main() {
  std::cout << "HOWDY \n";
  auto oneCounts = XACC__TestBell__body(1024);
  std::cout << "Result = " << oneCounts << "\n";
  return 0.0;
}
 No newline at end of file
+22 −0
Original line number Diff line number Diff line
namespace XACC 
{
open Microsoft.Quantum.Intrinsic;
operation TestBell(count : Int) : Int {
    // Simple bell test
    mutable numOnes = 0;
    use q = Qubit[2];
    for test in 1..count {
        H(q[0]);
        CNOT(q[0],q[1]);
        let res = M(q[0]);

        // Count the number of ones we saw:
        if res == One {
            set numOnes += 1;
            X(q[0]);
            X(q[1]);
        }
    }
    return numOnes;
}
}
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -65,3 +65,4 @@ internal_startup startup;
} // namespace qcor

#define qcor_include_qasm(NAME) extern "C" void NAME(qreg);
#define qcor_include_qsharp(NAME, RETURN_TYPE, ...) extern "C" RETURN_TYPE NAME(...);
Loading