Q Sharp
{{Short description|Programming lang. 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}}{{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}}
| developer = Microsoft
| designer = Microsoft Research (quantum architectures and computation group; QuArC)
| influenced by = C#, F#, Python
| File extensions = .qs
| platform = Common Language Infrastructure
| paradigm = Quantum, functional, imperative
| license = MIT License{{cite web |title=Introduction to Q# |url=https://courses.cs.washington.edu/courses/cse490q/20au/hws/qsharp-intro.pdf |publisher=University of Washington}}
| website = {{URL|https://learn.microsoft.com/en-us/azure/quantum/}}
| repo = {{URL|[https://github.com/Microsoft/Quantum}}
}}
Q# (pronounced Q sharp) is a domain-specific programming language used for expressing quantum algorithms.{{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}} It was initially released to the public by Microsoft as part of the Quantum Development Kit.{{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}}
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.{{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}}{{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}}
History
Historically, Microsoft Research had two teams interested in quantum computing: the QuArC team based in Redmond, Washington,{{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}} directed by Krysta Svore, that explored the construction of quantum circuitry, and Station Q initially located in Santa Barbara and directed by Michael Freedman, that explored topological quantum computing.Scott Aaronson's blog, 2013, 'Microsoft: From QDOS to QMA in less than 35 years', https://scottaaronson.blog/?p=1471{{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}}
During a Microsoft Ignite Keynote on September 26, 2017, Microsoft announced that they were going to release a new programming language geared specifically towards quantum computers.{{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}} On December 11, 2017, Microsoft released Q# as a part of the Quantum Development Kit.
At Build 2019, Microsoft announced that it would be open-sourcing the Quantum Development Kit, including its Q# compilers and simulators.[https://venturebeat.com/2019/05/06/microsoft-open-sourcing-quantum-development-kit/ Microsoft is open-sourcing its Quantum Development Kit]
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#.{{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}}
Bettina Heim currently leads the Q# language development effort.{{cite web | url=https://devblogs.microsoft.com/qsharp/the-women-of-quarc/ | title=The Women of QuArC | date=30 March 2019 }}{{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}}
Usage
Q# is available as a separately downloaded extension for Visual Studio,{{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}} but it can also be run as an independent tool from the command line or Visual Studio Code. Q# was introduced on Windows and is available on MacOS and Linux.{{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}}
The Quantum Development Kit includes a quantum simulator capable of running Q# and simulated 30 logical qubits.{{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}}{{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}}
In order to invoke the quantum simulator, another .NET programming language, usually C#, is used, which provides the (classical) input data for the simulator and reads the (classical) output data from the simulator.{{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}}
Features
A primary feature of Q# is the ability to create and use qubits for algorithms. As a consequence, some of the most prominent features of Q# are the ability to entangle and introduce superpositioning to qubits via controlled NOT gates and Hadamard gates, respectively, as well as Toffoli Gates, Pauli X, Y, Z Gate, and many more which are used for a variety of operations (See quantum logic gates).{{fact|date=January 2025}}
The hardware stack that will eventually come together with Q# is expected to implement Qubits as topological qubits. 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.{{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}}
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, 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
=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 comments 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){{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 }}
- Qubits are allocated and disposed inside a {{Code|using}} block.
- Lambda functions are defined using the
=>
operator. - Results are returned using the {{Code|return}} keyword.
=Similarities with F#=
- Variables are declared using either {{Code|let}} or {{Code|mutable}}
- First-order functions
- 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 {{Code|()}} is returned.
- Definition of record datatypes (using the {{Code|newtype}} keyword, instead of {{Code|type}}).
=Differences=
Example
{{Over-quotation|section|date=January 2025}}
The following source code is a multiplexer from the official Microsoft Q# library repository.
// 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));
}
}
}
References
{{Reflist}}
External links
- {{Official website|https://docs.microsoft.com/en-us/azure/quantum/}}
- {{GitHub|microsoft/qsharp-language}}
{{quantum computing}}
{{Common Language Infrastructure}}
{{Microsoft FOSS}}
{{Microsoft development tools}}
Category:Free and open-source software
Category:Microsoft free software
Category:Microsoft programming languages