Q Sharp

Last updated
Q#
Paradigm Quantum, functional, imperative
Designed by Microsoft Research (quantum architectures and computation group; QuArC)
Developer Microsoft
First appearedDecember 11, 2017 (2017-12-11) [1]
Typing discipline Static, strong
Platform Common Language Infrastructure
License MIT License [2]
Filename extensions .qs
Website docs.microsoft.com/en-us/quantum
Influenced by
C#, F#, Python

Q# (pronounced as Q sharp) is a domain-specific programming language used for expressing quantum algorithms. [3] It was initially released to the public by Microsoft as part of the Quantum Development Kit. [4]

Contents

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. [1] [5] [6]

History

Historically, Microsoft Research had two teams interested in quantum computing: the QuArC team based in Redmond, Washington, [7] 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. [8] [9]

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. [10] On December 11, 2017, Microsoft released Q# as a part of the Quantum Development Kit. [4]

At Build 2019, Microsoft announced that it would be open-sourcing the Quantum Development Kit, including its Q# compilers and simulators. [11]

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#. [12]

Bettina Heim currently leads the Q# language development effort. [13] [14]

Usage

Q# is available as a separately downloaded extension for Visual Studio, [15] 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. [16]

The Quantum Development Kit includes a quantum simulator capable of running Q# and simulated 30 logical qubits. [17] [18]

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. [19]

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 the list at the article on quantum logic gates. [20]

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. [21]

Documentation and resources

Currently, the resources available for Q# are scarce, but the official documentation is published: Microsoft Developer Network: Q#. 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 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: microsoft/QuantumKatas with links to resources, and answers to the problems.

Syntax

Q# is syntactically related to both C# and F# yet also has some significant differences.

Similarities with C#

Similarities with F#

Differences

Example

The following source code is a multiplexer from the official Microsoft Q# library repository.

// Copyright (c) Microsoft Corporation.// Licensed under the MIT License.namespaceMicrosoft.Quantum.Canon{openMicrosoft.Quantum.Intrinsic;openMicrosoft.Quantum.Arithmetic;openMicrosoft.Quantum.Arrays;openMicrosoft.Quantum.Diagnostics;openMicrosoft.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)operationMultiplexOperationsFromGenerator<'T>(unitaryGenerator:(Int,(Int->('T=>UnitisAdj+Ctl))),index:LittleEndian,target:'T):UnitisCtl+Adj{let(nUnitaries,unitaryFunction)=unitaryGenerator;letunitaryGeneratorWithOffset=(nUnitaries,0,unitaryFunction);ifLength(index!)==0{fail"MultiplexOperations failed. Number of index qubits must be greater than 0.";}ifnUnitaries>0{letauxiliary=[];AdjointMultiplexOperationsFromGeneratorImpl(unitaryGeneratorWithOffset,auxiliary,index,target);}}/// # Summary/// Implementation step of `MultiplexOperationsFromGenerator`./// # See Also/// - Microsoft.Quantum.Canon.MultiplexOperationsFromGeneratorinternaloperationMultiplexOperationsFromGeneratorImpl<'T>(unitaryGenerator:(Int,Int,(Int->('T=>UnitisAdj+Ctl))),auxiliary:Qubit[],index:LittleEndian,target:'T):Unit{body(...){letnIndex=Length(index!);letnStates=2^nIndex;let(nUnitaries,unitaryOffset,unitaryFunction)=unitaryGenerator;letnUnitariesLeft=MinI(nUnitaries,nStates/2);letnUnitariesRight=MinI(nUnitaries,nStates);letleftUnitaries=(nUnitariesLeft,unitaryOffset,unitaryFunction);letrightUnitaries=(nUnitariesRight-nUnitariesLeft,unitaryOffset+nUnitariesLeft,unitaryFunction);letnewControls=LittleEndian(Most(index!));ifnUnitaries>0{ifLength(auxiliary)==1andnIndex==0{// Termination case(ControlledAdjoint(unitaryFunction(unitaryOffset)))(auxiliary,target);}elifLength(auxiliary)==0andnIndex>=1{// Start caseletnewauxiliary=Tail(index!);ifnUnitariesRight>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.letcontrols=[Tail(index!)]+auxiliary;usenewauxiliary=Qubit();useandauxiliary=Qubit[MaxI(0,Length(controls)-2)];within{ApplyAndChain(andauxiliary,controls,newauxiliary);}apply{ifnUnitariesRight>0{MultiplexOperationsFromGeneratorImpl(rightUnitaries,[newauxiliary],newControls,target);}within{(ControlledX)(auxiliary,newauxiliary);}apply{MultiplexOperationsFromGeneratorImpl(leftUnitaries,[newauxiliary],newControls,target);}}}}}adjointauto;controlled(controlRegister,...){MultiplexOperationsFromGeneratorImpl(unitaryGenerator,auxiliary+controlRegister,index,target);}adjointcontrolledauto;}/// # 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.operationMultiplexOperationsBruteForceFromGenerator<'T>(unitaryGenerator:(Int,(Int->('T=>UnitisAdj+Ctl))),index:LittleEndian,target:'T):UnitisAdj+Ctl{letnIndex=Length(index!);letnStates=2^nIndex;let(nUnitaries,unitaryFunction)=unitaryGenerator;foridxOpin0..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.MultiplexOperationsFromGeneratorfunctionMultiplexerFromGenerator(unitaryGenerator:(Int,(Int->(Qubit[]=>UnitisAdj+Ctl)))):((LittleEndian,Qubit[])=>UnitisAdj+Ctl){returnMultiplexOperationsFromGenerator(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.MultiplexOperationsBruteForceFromGeneratorfunctionMultiplexerBruteForceFromGenerator(unitaryGenerator:(Int,(Int->(Qubit[]=>UnitisAdj+Ctl)))):((LittleEndian,Qubit[])=>UnitisAdj+Ctl){returnMultiplexOperationsBruteForceFromGenerator(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.internaloperationApplyAndChain(auxRegister:Qubit[],ctrlRegister:Qubit[],target:Qubit):UnitisAdj{ifLength(ctrlRegister)==0{X(target);}elifLength(ctrlRegister)==1{CNOT(Head(ctrlRegister),target);}else{EqualityFactI(Length(auxRegister),Length(ctrlRegister));letcontrols1=ctrlRegister[0..0]+auxRegister;letcontrols2=Rest(ctrlRegister);lettargets=auxRegister+[target];ApplyToEachA(ApplyAnd,Zipped3(controls1,controls2,targets));}}}


Related Research Articles

<span class="mw-page-title-main">Quantum computing</span> Technology that uses quantum mechanics

A quantum computer is a computer that exploits quantum mechanical phenomena. On small scales, physical matter exhibits properties of both particles and waves, and quantum computing leverages this behavior using specialized hardware. Classical physics cannot explain the operation of these quantum devices, and a scalable quantum computer could perform some calculations exponentially faster than any modern "classical" computer. In particular, a large-scale quantum computer could break widely used encryption schemes and aid physicists in performing physical simulations; however, the current state of the art is largely experimental and impractical, with several obstacles to useful applications.

<span class="mw-page-title-main">Qubit</span> Basic unit of quantum information

In quantum computing, a qubit or quantum bit is a basic unit of quantum information—the quantum version of the classic binary bit physically realized with a two-state device. A qubit is a two-state quantum-mechanical system, one of the simplest quantum systems displaying the peculiarity of quantum mechanics. Examples include the spin of the electron in which the two levels can be taken as spin up and spin down; or the polarization of a single photon in which the two spin states can also be measured as horizontal and vertical linear polarization. In a classical system, a bit would have to be in one state or the other. However, quantum mechanics allows the qubit to be in a coherent superposition of multiple states simultaneously, a property that is fundamental to quantum mechanics and quantum computing.

In logic circuits, the Toffoli gate, also known as the CCNOT gate (“controlled-controlled-not”), invented by Tommaso Toffoli, is a CNOT gate with two control qubits and one target qubit. That is, the target qubit will be inverted if the first and second qubits are both 1. It is a universal reversible logic gate, which means that any classical reversible circuit can be constructed from Toffoli gates.

<span class="mw-page-title-main">Quantum circuit</span> Model of quantum computing

In quantum information theory, a quantum circuit is a model for quantum computation, similar to classical circuits, in which a computation is a sequence of quantum gates, measurements, initializations of qubits to known values, and possibly other actions. The minimum set of actions that a circuit needs to be able to perform on the qubits to enable quantum computation is known as DiVincenzo's criteria.

<span class="mw-page-title-main">Quantum logic gate</span> Basic circuit in quantum computing

In quantum computing and specifically the quantum circuit model of computation, a quantum logic gate is a basic quantum circuit operating on a small number of qubits. Quantum logic gates are the building blocks of quantum circuits, like classical logic gates are for conventional digital circuits.

<span class="mw-page-title-main">Fredkin gate</span> Universal reversible logic gate, applied in quantum computing

The Fredkin gate is a computational circuit suitable for reversible computing, invented by Edward Fredkin. It is universal, which means that any logical or arithmetic operation can be constructed entirely of Fredkin gates. The Fredkin gate is a circuit or device with three inputs and three outputs that transmits the first bit unchanged and swaps the last two bits if, and only if, the first bit is 1.

Superconducting quantum computing is a branch of solid state quantum computing that implements superconducting electronic circuits using superconducting qubits as artificial atoms, or quantum dots. For superconducting qubits, the two logic states are the ground state and the excited state, denoted respectively. Research in superconducting quantum computing is conducted by companies such as Google, IBM, IMEC, BBN Technologies, Rigetti, and Intel. Many recently developed QPUs use superconducting architecture.

Quantum programming is the process of designing or assembling sequences of instructions, called quantum circuits, using gates, switches, and operators to manipulate a quantum system for a desired outcome or results of a given experiment. Quantum circuit algorithms can be implemented on integrated circuits, conducted with instrumentation, or written in a programming language for use with a quantum computer or a quantum processor.

<span class="mw-page-title-main">Superdense coding</span> Two-bit quantum communication protocol

In quantum information theory, superdense coding is a quantum communication protocol to communicate a number of classical bits of information by only transmitting a smaller number of qubits, under the assumption of sender and receiver pre-sharing an entangled resource. In its simplest form, the protocol involves two parties, often referred to as Alice and Bob in this context, which share a pair of maximally entangled qubits, and allows Alice to transmit two bits to Bob by sending only one qubit. This protocol was first proposed by Charles H. Bennett and Stephen Wiesner in 1970 and experimentally actualized in 1996 by Klaus Mattle, Harald Weinfurter, Paul G. Kwiat and Anton Zeilinger using entangled photon pairs. Superdense coding can be thought of as the opposite of quantum teleportation, in which one transfers one qubit from Alice to Bob by communicating two classical bits, as long as Alice and Bob have a pre-shared Bell pair.

<span class="mw-page-title-main">Controlled NOT gate</span> Quantum logic gate

In computer science, the controlled NOT gate, controlled-X gate, controlled-bit-flip gate, Feynman gate or controlled Pauli-X is a quantum logic gate that is an essential component in the construction of a gate-based quantum computer. It can be used to entangle and disentangle Bell states. Any quantum circuit can be simulated to an arbitrary degree of accuracy using a combination of CNOT gates and single qubit rotations. The gate is sometimes named after Richard Feynman who developed an early notation for quantum gate diagrams in 1986.

<span class="mw-page-title-main">Quantum neural network</span> Quantum Mechanics in Neural Networks

Quantum neural networks are computational neural network models which are based on the principles of quantum mechanics. The first ideas on quantum neural computation were published independently in 1995 by Subhash Kak and Ron Chrisley, engaging with the theory of quantum mind, which posits that quantum effects play a role in cognitive function. However, typical research in quantum neural networks involves combining classical artificial neural network models with the advantages of quantum information in order to develop more efficient algorithms. One important motivation for these investigations is the difficulty to train classical neural networks, especially in big data applications. The hope is that features of quantum computing such as quantum parallelism or the effects of interference and entanglement can be used as resources. Since the technological implementation of a quantum computer is still in a premature stage, such quantum neural network models are mostly theoretical proposals that await their full implementation in physical experiments.

<span class="mw-page-title-main">One-way quantum computer</span> Method of quantum computing

The one-way quantum computer, also known as measurement-based quantum computer (MBQC), is a method of quantum computing that first prepares an entangled resource state, usually a cluster state or graph state, then performs single qubit measurements on it. It is "one-way" because the resource state is destroyed by the measurements.

Quantum block codes are useful in quantum computing and in quantum communications. The encoding circuit for a large block code typically has a high complexity although those for modern codes do have lower complexity.

In quantum computing, the quantum phase estimation algorithm is a quantum algorithm to estimate the phase corresponding to an eigenvalue of a given unitary operator. Because the eigenvalues of a unitary operator always have unit modulus, they are characterized by their phase, and therefore the algorithm can be equivalently described as retrieving either the phase or the eigenvalue itself. The algorithm was initially introduced by Alexei Kitaev in 1995.

In quantum computing, the quantum Fourier transform (QFT) is a linear transformation on quantum bits, and is the quantum analogue of the discrete Fourier transform. The quantum Fourier transform is a part of many quantum algorithms, notably Shor's algorithm for factoring and computing the discrete logarithm, the quantum phase estimation algorithm for estimating the eigenvalues of a unitary operator, and algorithms for the hidden subgroup problem. The quantum Fourier transform was discovered by Don Coppersmith. With small modifications to the QFT, it can also be used for performing fast integer arithmetic operations such as addition and multiplication.

Linear optical quantum computing or linear optics quantum computation (LOQC), also photonic quantum computing (PQC), is a paradigm of quantum computation, allowing (under certain conditions, described below) universal quantum computation. LOQC uses photons as information carriers, mainly uses linear optical elements, or optical instruments (including reciprocal mirrors and waveplates) to process quantum information, and uses photon detectors and quantum memories to detect and store quantum information.

Quantum image processing (QIMP) is using quantum computing or quantum information processing to create and work with quantum images.

Quantum Computation Language (QCL) is one of the first implemented quantum programming languages. The most important feature of QCL is the support for user-defined operators and functions. Its syntax resembles the syntax of the C programming language and its classical data types are similar to primitive data types in C. One can combine classical code and quantum code in the same program.

In quantum computing, a qubit is a unit of information analogous to a bit in classical computing, but it is affected by quantum mechanical properties such as superposition and entanglement which allow qubits to be in some ways more powerful than classical bits for some tasks. Qubits are used in quantum circuits and quantum algorithms composed of quantum logic gates to solve computational problems, where they are used for input/output and intermediate computations.

<span class="mw-page-title-main">Phase kickback</span>

In quantum computing, phase kickback refers to the fact that controlled operations have effects on their controls, in addition to on their targets, and that these effects correspond to phasing operations. The phase of one qubit is effectively transferred to another qubit during a controlled operation, creating entanglement and computational advantages that enable various popular quantum algorithms and protocols.

References

  1. 1 2 "Microsoft's Q# quantum programming language out now in preview". Ars Technica. 12 Dec 2017. Retrieved 2024-09-04.
  2. "Introduction to Q#" (PDF). University of Washington.
  3. 1 2 QuantumWriter. "The Q# Programming Language". docs.microsoft.com. Retrieved 2017-12-11.
  4. 1 2 "Announcing the Microsoft Quantum Development Kit" . Retrieved 2017-12-11.
  5. "Microsoft makes play for next wave of computing with quantum computing toolkit". Ars Technica. 25 Sep 2017. Retrieved 2024-09-04.
  6. "Quantum Computers Barely Exist—Here's Why We're Writing Languages for Them Anyway". MIT Technology Review. 22 Dec 2017. Retrieved 2024-09-04.
  7. "Solving the quantum many-body problem with artificial neural networks". Microsoft Azure Quantum. 15 February 2017.
  8. Scott Aaronson's blog, 2013, 'Microsoft: From QDOS to QMA in less than 35 years', https://scottaaronson.blog/?p=1471
  9. "What are the Q# programming language & QDK? - Azure Quantum". learn.microsoft.com. 12 January 2024.
  10. "Microsoft announces quantum computing programming language" . Retrieved 2017-12-14.
  11. Microsoft is open-sourcing its Quantum Development Kit
  12. Krill, Paul (29 Sep 2020). "Microsoft taps LLVM for quantum computing". InfoWorld. Retrieved 2024-09-04.
  13. "The Women of QuArC". 30 March 2019.
  14. "Intro to Q# - Intro to Quantum Software Development". stem.mitre.org.
  15. QuantumWriter. "Setting up the Q# development environment". docs.microsoft.com. Retrieved 2017-12-14.
  16. Coppock, Mark (26 Feb 2018). "Microsoft's quantum computing language is now available for MacOS". Digital Trends. Retrieved 2024-09-04.
  17. Akdogan, Erman (23 October 2022). "Quantum computing is coming for finance & crypto". Medium.
  18. Melanson, Mike (16 Dec 2017). "This Week in Programming: Get Quantum with Q Sharp". The New Stack. Retrieved 2024-09-04.
  19. "This Week in Programming: Get Quantum with Q Sharp". The New Stack. 16 December 2017.
  20. "Qubit Gate - an overview". www.sciencedirect.com.
  21. "Microsoft previews quantum computing development kit". CIO.
  22. "Types in Q# - Microsoft Quantum". docs.microsoft.com. 27 July 2022.