# Q Sharp

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Q_Sharp
> Markdown URL: https://mediated.wiki/source/Q_Sharp.md
> Source: https://en.wikipedia.org/wiki/Q_Sharp
> Source revision: 1356892715
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Programming language for quantum algorithms}}
{{Correct title|title=Q# (programming language)|reason=hash}}
{{third-party|date=September 2018}}
{{Infobox programming language
| title = Q#
| released = {{Start date|2017|12|11}}<ref name="AT Q#">{{Cite web|url=https://arstechnica.com/gadgets/2017/12/microsofts-q-quantum-programming-language-out-now-in-preview/|title=Microsoft's Q# quantum programming language out now in preview |website=Ars Technica |date=12 Dec 2017 |access-date=2024-09-04|language=en-US}}</ref>
| developer = [Microsoft](/source/Microsoft)
| designer = [Microsoft Research](/source/Microsoft_Research) (quantum architectures and computation group; QuArC)
| influenced by = [C#](/source/C_Sharp_(programming_language)), [F#](/source/F_Sharp_(programming_language)), [Python](/source/Python_(programming_language))
| File extensions = .qs
| platform = [Common Language Infrastructure](/source/Common_Language_Infrastructure)
| paradigm = [Quantum](/source/Quantum_programming), [functional](/source/functional_programming), [imperative](/source/imperative_programming)
| typing = [Static](/source/static_typing), [strong](/source/Strong_and_weak_typing)
| license = [MIT License](/source/MIT_License)<ref>{{cite web |title=Introduction to Q# |url=https://courses.cs.washington.edu/courses/cse490q/20au/hws/qsharp-intro.pdf |publisher=[University of Washington](/source/University_of_Washington)}}</ref>
| website = {{URL|https://learn.microsoft.com/en-us/azure/quantum/}}
| repo = {{URL|[https://github.com/Microsoft/Quantum}}
}}
{{Portal|Computer programming|Free and open-source software}}
'''Q#''' (pronounced ''Q sharp'') is a [domain-specific programming language](/source/Domain-specific_language) used for expressing [quantum algorithms](/source/Quantum_algorithm).<ref name=":1">{{Cite web|url=https://docs.microsoft.com/en-us/quantum/quantum-qr-intro?view=qsharp-preview|title=The Q# Programming Language|last=QuantumWriter|website=docs.microsoft.com|language=en-us|access-date=2017-12-11}}</ref> It was initially released to the public by [Microsoft](/source/Microsoft) as part of the Quantum Development Kit.<ref name=":0">{{Cite news|url=https://cloudblogs.microsoft.com/quantum/2017/12/11/announcing-microsoft-quantum-development-kit/|title=Announcing the Microsoft Quantum Development Kit|access-date=2017-12-11|language=en-US}}</ref>

Q# works in conjunction with classical languages such as C#, Python and F#, and is designed to allow the use of traditional programming concepts in quantum computing, including functions with variables and branches as well as a syntax-highlighted development environment with a quantum debugger.<ref name="AT Q#"></ref><ref name="AT Ignite">{{Cite web|url=https://arstechnica.com/gadgets/2017/09/microsoft-quantum-toolkit/|title=Microsoft makes play for next wave of computing with quantum computing toolkit |date=25 Sep 2017 |website=Ars Technica |access-date=2024-09-04|language=en-US}}</ref><ref>{{Cite web|url=https://www.technologyreview.com/2017/12/22/3662/quantum-computers-barely-exist-heres-why-were-writing-languages-for-them-anyway/|title=Quantum Computers Barely Exist—Here's Why We're Writing Languages for Them Anyway |date=22 Dec 2017 |website=MIT Technology Review |access-date=2024-09-04|language=en-US}}</ref>

==History==
Historically, [Microsoft Research](/source/Microsoft_Research) had two teams interested in quantum computing: the QuArC team based in Redmond, Washington,<ref>{{cite web |title=Solving the quantum many-body problem with artificial neural networks |url=https://cloudblogs.microsoft.com/quantum/2017/02/15/solving-the-quantum-many-body-problem-with-artificial-neural-networks/ |website=Microsoft Azure Quantum |date=15 February 2017}}</ref> directed by [Krysta Svore](/source/Krysta_Svore), that explored the construction of quantum circuitry, and Station Q initially located in [Santa Barbara](/source/Santa_Barbara%2C_California) and directed by [Michael Freedman](/source/Michael_Freedman), that explored [topological quantum computing](/source/topological_quantum_computing).<ref>[Scott Aaronson](/source/Scott_Aaronson)'s blog, 2013, 'Microsoft: From QDOS to QMA in less than 35 years', https://scottaaronson.blog/?p=1471</ref><ref>{{cite web |title=What are the Q# programming language & QDK? - Azure Quantum |url=https://learn.microsoft.com/en-us/azure/quantum/overview-what-is-qsharp-and-qdk |website=learn.microsoft.com |date=12 January 2024 |language=en-us}}</ref>

During a [Microsoft Ignite](/source/Microsoft_Ignite) Keynote on September 26, 2017, Microsoft announced that they were going to release a new programming language geared specifically towards quantum computers.<ref>{{Cite news|url=https://cloudblogs.microsoft.com/quantum/2017/09/26/microsoft-announces-quantum-computing-programming-language/|title=Microsoft announces quantum computing programming language|access-date=2017-12-14|language=en-US}}</ref> On December 11, 2017, Microsoft released Q# as a part of the Quantum Development Kit.<ref name=":0" />

At [Build 2019](/source/Build_2019), Microsoft announced that it would be open-sourcing the Quantum Development Kit, including its Q# [compiler](/source/compiler)s and simulators.<ref>{{Cite web |url=https://venturebeat.com/2019/05/06/microsoft-open-sourcing-quantum-development-kit/ |title=Microsoft is open-sourcing its Quantum Development Kit |access-date=2020-12-12 |archive-date=2021-01-23 |archive-url=https://web.archive.org/web/20210123111336/https://venturebeat.com/2019/05/06/microsoft-open-sourcing-quantum-development-kit/ |url-status=dead }}</ref>

To support Q#, Microsoft developed Quantum Intermediate Representation (QIR) in 2023 as a common interface between programming languages and target quantum processors. The company also announced a compiler extension that generates QIR from Q#.<ref name="QIR">{{Cite web |title=Microsoft taps LLVM for quantum computing |url=https://www.infoworld.com/article/2260508/microsoft-taps-llvm-for-quantum-computing.html |access-date=2024-09-04 |date=29 Sep 2020 |last1=Krill |first1=Paul |website=InfoWorld |language=en-US}}</ref>

[Bettina Heim](/source/Bettina_Heim) used to lead the Q# language development effort.<ref>{{cite web | url=https://devblogs.microsoft.com/qsharp/the-women-of-quarc/ | title=The Women of QuArC | date=30 March 2019 }}</ref><ref>{{cite web |title=Intro to Q# - Intro to Quantum Software Development |url=https://stem.mitre.org/quantum/software-tools/intro-qsharp.html |website=stem.mitre.org}}</ref>

==Usage==
Q# is available as a separately downloaded extension for [Visual Studio](/source/Visual_Studio),<ref>{{Cite web|url=https://docs.microsoft.com/en-us/quantum/quantum-installconfig?view=qsharp-preview|title=Setting up the Q# development environment|last=QuantumWriter|website=docs.microsoft.com|language=en-us|access-date=2017-12-14}}</ref> but it can also be run as an independent tool from the [command line](/source/command_line) or Visual Studio Code. Q# was introduced on Windows and is available on MacOS and Linux.<ref>{{cite web |last1=Coppock |first1=Mark |title=Microsoft's quantum computing language is now available for MacOS |url=https://www.digitaltrends.com/computing/microsoft-quantum-computing-q-available-macos-linux/ |website=Digital Trends |access-date=2024-09-04 |language=en |date=26 Feb 2018}}</ref>

The Quantum Development Kit includes a [quantum simulator](/source/quantum_simulator) capable of running Q# and simulated 30 logical qubits.<ref>{{cite web |last1=Akdogan |first1=Erman |title=Quantum computing is coming for finance & crypto |url=https://medium.datadriveninvestor.com/quantum-computing-is-coming-for-finance-crypto-b56c0255cb09 |website=Medium |language=en |date=23 October 2022}}</ref><ref>{{cite web |last1=Melanson |first1=Mike |title=This Week in Programming: Get Quantum with Q Sharp |url=https://thenewstack.io/week-programming-get-quantum-q-sharp/ |website=The New Stack |access-date=2024-09-04 |language=en |date=16 Dec 2017}}</ref>

In order to invoke the quantum simulator, another [.NET programming language](/source/.NET_Framework), usually [C#](/source/C_Sharp_(programming_language)), is used, which provides the (classical) input data for the simulator and reads the (classical) output data from the simulator.<ref>{{cite web |title=This Week in Programming: Get Quantum with Q Sharp |url=https://thenewstack.io/week-programming-get-quantum-q-sharp/ |website=The New Stack |date=16 December 2017}}</ref>

==Features==
A primary feature of Q# is the ability to create and use [qubits](/source/Qubit) for algorithms. As a consequence, some of the most prominent features of Q# are the ability to [entangle](/source/Quantum_entanglement) and introduce [superpositioning](/source/Quantum_superposition) to qubits via [controlled NOT gates](/source/Controlled_NOT_gate) and [Hadamard gates](/source/Hadamard_gate), respectively, as well as [Toffoli Gates](/source/Toffoli_gate), [Pauli X, Y, Z Gate](/source/Pauli_matrices), and many more which are used for a variety of operations (See [quantum logic gate](/source/quantum_logic_gate)s).{{fact|date=January 2025}}

The hardware stack that will eventually come together with Q# is expected to implement Qubits as [topological qubits](/source/Topological_quantum_computer). The quantum simulator that is shipped with the Quantum Development Kit today is capable of processing up to 32 qubits on a user machine and up to 40 qubits on [Azure](/source/Microsoft_Azure).<ref>{{cite web |title=Microsoft previews quantum computing development kit |url=https://www2.cio.com.au/article/631142/microsoft-previews-quantum-computing-development-kit/ |website=CIO |access-date=2022-10-30 |archive-date=2022-10-30 |archive-url=https://web.archive.org/web/20221030190417/https://www2.cio.com.au/article/631142/microsoft-previews-quantum-computing-development-kit/ |url-status=dead }}</ref>

==Documentation and resources==
Currently, the resources available for Q# are scarce, but the official documentation is published: [https://docs.microsoft.com/en-us/quantum/?view=qsharp-preview Microsoft Developer Network: Q#]. [https://github.com/Microsoft/Quantum/ Microsoft Quantum Github repository] is also a large collection of sample programs implementing a variety of Quantum algorithms and their tests.

Microsoft has also hosted a Quantum Coding contest on [Codeforces](/source/Codeforces), called [https://web.archive.org/web/20181119064628/https://codeforces.com/msqs2018 Microsoft Q# Coding Contest - Codeforces], and also provided related material to help answer the questions in the blog posts, plus the detailed solutions in the tutorials.

Microsoft hosts a set of learning exercises to help learn Q# on GitHub: [https://github.com/Microsoft/QuantumKatas microsoft/QuantumKatas] with links to resources, and answers to the problems.

==Syntax==
Q# is syntactically related to both [C#](/source/C_Sharp_(programming_language)) and [F#](/source/F_Sharp_(programming_language)) yet also has some significant differences.

===Similarities with C#===
* Uses {{Code|namespace}} for code isolation
* All statements end with a {{Code|;}}
* Curly braces are used for statements of scope
* [Single line comment](/source/Comment_(computer_programming))s are done using {{Code|//}}
* Variable data types such as {{Code|Int}} {{Code|Double}} {{Code|String}} and {{Code|Bool}} are similar, although capitalised (and Int is 64-bit)<ref>{{Cite web|url=https://docs.microsoft.com/en-us/quantum/user-guide/language/types|title=Types in Q# - Microsoft Quantum|website=docs.microsoft.com|date=27 July 2022 }}</ref>
* Qubits are allocated and disposed inside a {{Code|using}} block.
* [Lambda function](/source/Lambda_function_(computer_programming))s are defined using the <code>=&gt;</code> operator.
* Results are returned using the {{Code|return}} keyword.

===Similarities with F#===
* Variables are declared using either {{Code|let}} or {{Code|mutable}}<ref name=":1" />
* [First-order function](/source/First-order_function)s 
* Modules, which are imported using the {{Code|open}} keyword
* The datatype is declared after the variable name
* The range operator {{Code|..}}
* {{Code|for … in}} loops
* Every operation/function has a return value, rather than {{Code|void}}. Instead of {{Code|void}}, an empty [Tuple](/source/Tuple) {{Code|()}} is returned.
* Definition of record datatypes (using the {{Code|newtype}} keyword, instead of {{Code|type}}).

===Differences===
* Functions are declared using the {{Code|function}} keyword
* Operations on the quantum computer are declared using the {{Code|operation}} keyword
* Lack of multiline comments
* Asserts instead of throwing exceptions
* Documentation is written in [Markdown](/source/Markdown) instead of [XML](/source/XML)-based documentation tags

==Example==
{{Over-quotation|section|date=January 2025}}
The following source code is a [multiplexer](/source/multiplexer) from the official Microsoft Q# library repository.

<syntaxhighlight lang="fsharp">
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Canon {
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Arithmetic;
    open Microsoft.Quantum.Arrays;
    open Microsoft.Quantum.Diagnostics;
    open Microsoft.Quantum.Math;

    /// # Summary
    /// Applies a multiply-controlled unitary operation $U$ that applies a
    /// unitary $V_j$ when controlled by n-qubit number state $\ket{j}$.
    ///
    /// $U = \sum^{N-1}_{j=0}\ket{j}\bra{j}\otimes V_j$.
    ///
    /// # Input
    /// ## unitaryGenerator
    /// A tuple where the first element `Int` is the number of unitaries $N$,
    /// and the second element `(Int -> ('T => () is Adj + Ctl))`
    /// is a function that takes an integer $j$ in $[0,N-1]$ and outputs the unitary
    /// operation $V_j$.
    ///
    /// ## index
    /// $n$-qubit control register that encodes number states $\ket{j}$ in
    /// little-endian format.
    ///
    /// ## target
    /// Generic qubit register that $V_j$ acts on.
    ///
    /// # Remarks
    /// `coefficients` will be padded with identity elements if
    /// fewer than $2^n$ are specified. This implementation uses
    /// $n-1$ auxiliary qubits.
    ///
    /// # References
    /// - [ *Andrew M. Childs, Dmitri Maslov, Yunseong Nam, Neil J. Ross, Yuan Su*,
    ///      arXiv:1711.10980](https://arxiv.org/abs/1711.10980)
    operation MultiplexOperationsFromGenerator<'T>(unitaryGenerator : (Int, (Int -> ('T => Unit is Adj + Ctl))), index: LittleEndian, target: 'T) : Unit is Ctl + Adj {
        let (nUnitaries, unitaryFunction) = unitaryGenerator;
        let unitaryGeneratorWithOffset = (nUnitaries, 0, unitaryFunction);
        if Length(index!) == 0 {
            fail "MultiplexOperations failed. Number of index qubits must be greater than 0.";
        }
        if nUnitaries > 0 {
            let auxiliary = [];
            Adjoint MultiplexOperationsFromGeneratorImpl(unitaryGeneratorWithOffset, auxiliary, index, target);
        }
    }

    /// # Summary
    /// Implementation step of `MultiplexOperationsFromGenerator`.
    /// # See Also
    /// - Microsoft.Quantum.Canon.MultiplexOperationsFromGenerator
    internal operation MultiplexOperationsFromGeneratorImpl<'T>(unitaryGenerator : (Int, Int, (Int -> ('T => Unit is Adj + Ctl))), auxiliary: Qubit[], index: LittleEndian, target: 'T)
    : Unit {
        body (...) {
            let nIndex = Length(index!);
            let nStates = 2^nIndex;

            let (nUnitaries, unitaryOffset, unitaryFunction) = unitaryGenerator;

            let nUnitariesLeft = MinI(nUnitaries, nStates / 2);
            let nUnitariesRight = MinI(nUnitaries, nStates);

            let leftUnitaries = (nUnitariesLeft, unitaryOffset, unitaryFunction);
            let rightUnitaries = (nUnitariesRight - nUnitariesLeft, unitaryOffset + nUnitariesLeft, unitaryFunction);

            let newControls = LittleEndian(Most(index!));

            if nUnitaries > 0 {
                if Length(auxiliary) == 1 and nIndex == 0 {
                    // Termination case

                    (Controlled Adjoint (unitaryFunction(unitaryOffset)))(auxiliary, target);
                } elif Length(auxiliary) == 0 and nIndex >= 1 {
                    // Start case
                    let newauxiliary = Tail(index!);
                    if nUnitariesRight > 0 {
                        MultiplexOperationsFromGeneratorImpl(rightUnitaries, [newauxiliary], newControls, target);
                    }
                    within {
                        X(newauxiliary);
                    } apply {
                        MultiplexOperationsFromGeneratorImpl(leftUnitaries, [newauxiliary], newControls, target);
                    }
                } else {
                    // Recursion that reduces nIndex by 1 and sets Length(auxiliary) to 1.
                    let controls = [Tail(index!)] + auxiliary;
                    use newauxiliary = Qubit();
                    use andauxiliary = Qubit[MaxI(0, Length(controls) - 2)];
                    within {
                        ApplyAndChain(andauxiliary, controls, newauxiliary);
                    } apply {
                        if nUnitariesRight > 0 {
                            MultiplexOperationsFromGeneratorImpl(rightUnitaries, [newauxiliary], newControls, target);
                        }
                        within {
                            (Controlled X)(auxiliary, newauxiliary);
                        } apply {
                            MultiplexOperationsFromGeneratorImpl(leftUnitaries, [newauxiliary], newControls, target);
                        }
                    }
                }
            }
        }
        adjoint auto;
        controlled (controlRegister, ...) {
            MultiplexOperationsFromGeneratorImpl(unitaryGenerator, auxiliary + controlRegister, index, target);
        }
        adjoint controlled auto;
    }

    /// # Summary
    /// Applies multiply-controlled unitary operation $U$ that applies a
    /// unitary $V_j$ when controlled by n-qubit number state $\ket{j}$.
    ///
    /// $U = \sum^{N-1}_{j=0}\ket{j}\bra{j}\otimes V_j$.
    ///
    /// # Input
    /// ## unitaryGenerator
    /// A tuple where the first element `Int` is the number of unitaries $N$,
    /// and the second element `(Int -> ('T => () is Adj + Ctl))`
    /// is a function that takes an integer $j$ in $[0,N-1]$ and outputs the unitary
    /// operation $V_j$.
    ///
    /// ## index
    /// $n$-qubit control register that encodes number states $\ket{j}$ in
    /// little-endian format.
    ///
    /// ## target
    /// Generic qubit register that $V_j$ acts on.
    ///
    /// # Remarks
    /// `coefficients` will be padded with identity elements if
    /// fewer than $2^n$ are specified. This version is implemented
    /// directly by looping through n-controlled unitary operators.
    operation MultiplexOperationsBruteForceFromGenerator<'T>(unitaryGenerator : (Int, (Int -> ('T => Unit is Adj + Ctl))), index: LittleEndian, target: 'T)
    : Unit is Adj + Ctl {
        let nIndex = Length(index!);
        let nStates = 2^nIndex;
        let (nUnitaries, unitaryFunction) = unitaryGenerator;
        for idxOp in 0..MinI(nStates,nUnitaries) - 1 {
            (ControlledOnInt(idxOp, unitaryFunction(idxOp)))(index!, target);
        }
    }

    /// # Summary
    /// Returns a multiply-controlled unitary operation $U$ that applies a
    /// unitary $V_j$ when controlled by n-qubit number state $\ket{j}$.
    ///
    /// $U = \sum^{2^n-1}_{j=0}\ket{j}\bra{j}\otimes V_j$.
    ///
    /// # Input
    /// ## unitaryGenerator
    /// A tuple where the first element `Int` is the number of unitaries $N$,
    /// and the second element `(Int -> ('T => () is Adj + Ctl))`
    /// is a function that takes an integer $j$ in $[0,N-1]$ and outputs the unitary
    /// operation $V_j$.
    ///
    /// # Output
    /// A multiply-controlled unitary operation $U$ that applies unitaries
    /// described by `unitaryGenerator`.
    ///
    /// # See Also
    /// - Microsoft.Quantum.Canon.MultiplexOperationsFromGenerator
    function MultiplexerFromGenerator(unitaryGenerator : (Int, (Int -> (Qubit[] => Unit is Adj + Ctl)))) : ((LittleEndian, Qubit[]) => Unit is Adj + Ctl) {
        return MultiplexOperationsFromGenerator(unitaryGenerator, _, _);
    }

    /// # Summary
    /// Returns a multiply-controlled unitary operation $U$ that applies a
    /// unitary $V_j$ when controlled by n-qubit number state $\ket{j}$.
    ///
    /// $U = \sum^{2^n-1}_{j=0}\ket{j}\bra{j}\otimes V_j$.
    ///
    /// # Input
    /// ## unitaryGenerator
    /// A tuple where the first element `Int` is the number of unitaries $N$,
    /// and the second element `(Int -> ('T => () is Adj + Ctl))`
    /// is a function that takes an integer $j$ in $[0,N-1]$ and outputs the unitary
    /// operation $V_j$.
    ///
    /// # Output
    /// A multiply-controlled unitary operation $U$ that applies unitaries
    /// described by `unitaryGenerator`.
    ///
    /// # See Also
    /// - Microsoft.Quantum.Canon.MultiplexOperationsBruteForceFromGenerator
    function MultiplexerBruteForceFromGenerator(unitaryGenerator : (Int, (Int -> (Qubit[] => Unit is Adj + Ctl)))) : ((LittleEndian, Qubit[]) => Unit is Adj + Ctl) {
        return MultiplexOperationsBruteForceFromGenerator(unitaryGenerator, _, _);
    }

    /// # Summary
    /// Computes a chain of AND gates
    ///
    /// # Description
    /// The auxiliary qubits to compute temporary results must be specified explicitly.
    /// The length of that register is `Length(ctrlRegister) - 2`, if there are at least
    /// two controls, otherwise the length is 0.
    internal operation ApplyAndChain(auxRegister : Qubit[], ctrlRegister : Qubit[], target : Qubit)
    : Unit is Adj {
        if Length(ctrlRegister) == 0 {
            X(target);
        } elif Length(ctrlRegister) == 1 {
            CNOT(Head(ctrlRegister), target);
        } else {
            EqualityFactI(Length(auxRegister), Length(ctrlRegister));
            let controls1 = ctrlRegister[0..0] + auxRegister;
            let controls2 = Rest(ctrlRegister);
            let targets = auxRegister + [target];
            ApplyToEachA(ApplyAnd, Zipped3(controls1, controls2, targets));
        }
    }
}
</syntaxhighlight>

==References==
{{Reflist}}

==External links==
*{{Official website|https://docs.microsoft.com/en-us/azure/quantum/}}
*{{GitHub|microsoft/qsharp-language}} (now deprecated)
*{{GitHub|microsoft/qsharp}} (Modern QDK repository)

{{quantum computing}}
{{Common Language Infrastructure}}
{{Microsoft FOSS}}
{{Microsoft development tools}}

Category:Microsoft free software
Category:Microsoft programming languages
Category:Quantum programming
Category:Programming languages created in 2017
Category:Software using the MIT license

---
Adapted from the Wikipedia article [Q Sharp](https://en.wikipedia.org/wiki/Q_Sharp) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Q_Sharp?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
