Toward Formalizing The Q# Programming Language (Marshall)
Toward Formalizing The Q# Programming Language (Marshall)
Poster Abstract
Q# is a high-level programming language from Microsoft for writing and running quantum programs.
Like most industrial languages, it was designed without a formal specification, which can naturally
lead to ambiguity in its interpretation. We aim to provide a formal specification and semantics for Q#,
placing the language on a solid mathematical foundation, enabling further evolution of its design and
type system, and leading to research in program correctness and verified compiler implementation.
This poster describes our current progress and outlines the next steps.
1 Introduction
Microsoft’s Q# [Svore et al. 2018] programming language is one of the most sophisticated quantum
programming languages that have emerged in recent years. With a growing code base and popularity
comes the demand for more features and complexity. Hence, Q# faces challenges familiar to many
growing programming languages—maintaining correctness, ease of use, and intuitive understanding.
If the Q# programming language is going to have a lasting impact it will be important to have a
well-specified definition that can serve as a foundation for language extensions, multiple implementations,
and formal verification of both programs and the compiler. This will help ensure that Q# is robust enough
to meet the unique needs of the developing field of quantum software engineering.
A tried-and-tested approach to achieving this ambitious goal is to define an idealized version of the
full language and provide an elaboration from the full language to this core language. In this poster, we
argue that even though Q# is a relatively large language, we can condense it to a small core capturing
most of its interesting features. This core language, which we call λQ# , is the focus of this work.
Preliminary Report. Final version to appear in: This work is licensed under the
QPL 2021 Creative Commons Attribution License.
2 Toward Formalizing the Q# Programming Language
Qubits in Q# are opaque types that act as references to logical qubits. Gate operations are inherently
effectful: a quantum gate application is like a function that takes a qubit reference as input, and returns
a trivial output of type Unit; the referenced qubit is altered by the operation. Passing references to a
function effectively creates an alias. Arrays of qubits are another common scenario where items in the
array are aliased during iteration in a for loop.
While aliasing is necessary for gate application in Q#, it can lead to unsafe behavior in violation of
the no-cloning theorem which forbids duplication of qubits. For example, the following Q# code fragment
demonstrates incorrect use of aliasing, since the CNOT operation cannot be applied with the same qubit as
both the control and the target. Currently, it is not possible for Q# to statically prevent this issue.
use q1 = Qubit ();
let q2 = q1 ;
CNOT ( q1 , q2 );
An informal specification of the Q# language was recently published [Heim 2020; Heim and Q#
Team 2020], but it arguably does not capture all subtle aspects of the language such as aliasing of qubit
references. One goal of our work is to make these subtleties explicit and one of the first applications is to
include a static check to prevent cloning.
earlier. The only allowed operations on qubits are gate application and measurement. Qubits come into
scope with either the use or the borrow keywords. The first provides access to fresh qubits, while the
second may provide mutated and potentially entangled qubits.
Note that in the λQ# grammar below, we ignore the pure expression language by not including any
classical base types except unit. This way we can expose the interesting quantum-classical interface at
play in Q#. Single-qubit unitary operations, U, are typed as qref → cmd. The qubit reference type qref is
inhabited by qubit locations qloc[q] that serve as its values and can be compared for equality.
In λQ# syntax, the unsafe code fragment shown in the previous section can be written as:
or with some syntactic sugar as q1 ← newqref ; let q2 be q1 in ctrlap(q1 ; q2 ; X). This makes it explicit
that q1 and q2 are aliases of the same logical qubit. Extending the type system of λQ# (and later of Q#)
with affine types (à la Rust [Matsakis and Klock II 2014]) will make it possible to statically prevent
cloning in the gate application.
Q# has limited support for parametric polymorphism that we have not tried to tackle yet, but λQ# is
close enough to System F (polymorphic typed λ -calculus) that we expect this to be straightforward.
References
Alan Geller (Sept. 23, 2020). Introducing Quantum Intermediate Representation (QIR). Q# Blog. URL:
https : / / devblogs . microsoft . com / qsharp / introducing - quantum - intermediate -
representation-qir/.
Robert Harper (2016). “Practical Foundations for Programming Languages”. In: 2nd ed. Cambridge,
UK: Cambridge University Press. Chap. 34 - Modernized Algol, pp. 301–312. DOI: 10 . 1017 /
CBO9781316576892.036.
Robert Harper and Chris Stone (2000). “A Type-Theoretic Interpretation of Standard ML”. In: Proof,
Language, and Interaction: Essays in Honor of Robin Milner. Cambridge, MA: MIT Press, pp. 341–
387. URL: https://www.cs.cmu.edu/~rwh/papers/ttisml/ttisml.pdf.
Bettina Heim (2020). “Development of Quantum Applications”. PhD thesis. Zurich: ETH Zurich. Chap. 8:
"Domain-Specific Language Q#". DOI: 10.3929/ethz-b-000468201.
Bettina Heim and Q# Team (2020). Q# Language Specification. URL: https : / / github . com /
microsoft/qsharp-language/tree/main/Specifications/Language#q-language.
Daniel K. Lee, Karl Crary, and Robert Harper (2007). “Towards a Mechanized Metatheory of Standard
ML”. In: Proc. POPL ’07. New York, NY, USA: ACM, pp. 173–184. DOI: 10.1145/1190216.
1190245. URL: https://www.cs.cmu.edu/~dklee/papers/tslf-popl.pdf.
Nicholas D. Matsakis and Felix S. Klock II (2014). “The Rust Language”. In: Proc. ACM SIGAda
Annual Conference on High Integrity Language Technology (HILT ’14). New York, NY, USA: ACM,
pp. 103–104. DOI: 10.1145/2663171.2663188.
Luca Paolini, Luca Roversi, and Margherita Zorzi (2019). “Quantum Programming Made Easy”. In: Proc.
Linearity-TLLA 2018. Waterloo, NSW, Australia: Open Publishing Association, pp. 133–147. DOI:
10.4204/eptcs.292.8.
Frank Pfenning and Carsten Schürmann (1999). “System Description: Twelf—A Meta-Logical Framework
for Deductive Systems”. In: Automated Deduction—CADE-16. Berlin, Heidelberg: Springer, pp. 202–
206. DOI: 10.1007/3-540-48660-7_14. URL: https://www.cs.cmu.edu/~fp/papers/cade99.
pdf.
John C. Reynolds (1981). “The Essence of Algol”. In: Algorithmic Languages: Proceedings of the
International Symposium on Algorithmic Languages. Amsterdam: North-Holland Pub. Co., pp. 345–
372. URL: http://www.cs.cmu.edu/afs/cs/user/crary/www/819-f09/Reynolds81.ps.
Krysta Svore, Alan Geller, Matthias Troyer, John Azariah, Christopher Granade, Bettina Heim, Vadym
Kliuchnikov, Mariia Mykhailova, Andres Paz, and Martin Roetteler (2018). “Q#: Enabling Scalable
Quantum Computing and Development with a High-level DSL”. In: Proc. RWDLS ’18. New York,
NY, USA: ACM, 7:1–7:10. DOI: 10.1145/3183895.3183901. arXiv: 1803.00652.
Jianzhou Zhao, Santosh Nagarakatte, Milo M. K. Martin, and Steve Zdancewic (2012). “Formalizing the
LLVM Intermediate Representation for Verified Program Transformations”. In: Proc. POPL ’12. New
York, NY: ACM, pp. 427–440. DOI: 10.1145/2103656.2103709. URL: https://www.cis.upenn.
edu/~stevez/papers/ZNMZ12.pdf.