원시데이터 수집 시스템
선정목록으로

QWIRE Practice: Formal Verification of Quantum Circuits in Coq

관련 필름·전자재료 · (11) 배리어 필름

기본 정보

저자
  1. Robert W. Rand — University of Pennsylvania
  2. Jennifer Paykin — University of Pennsylvania
  3. Steve Zdancewic — University of Pennsylvania
DOI
10.4204/eptcs.266.8
저널
arXiv (Cornell University)
발행년도
2018
발행기관
Cornell University
피인용수
36
라이선스
cc-by-nc-nd
논문 유형
–
언어
–
원문 페이지
–
토픽
Advancements in Photolithography Techniques
OpenAlex ID
W2792039802

평가 내용

관련도
관련
사업분야
필름·전자재료 · (11) 배리어 필름
부 분야
–
R&D 측면
–
확신도
–
근거 용어
–
판단 근거
임베딩 유사도 기반 평가로 '필름·전자재료 > 배리어 필름' 분야에 선정된 논문입니다.
R&D 활용 포인트
–
평가 입력
–

초록

We describe an embedding of the QWIRE quantum circuit language in the Coq proof assistant. This allows programmers to write quantum circuits using high-level abstractions and to prove properties of those circuits using Coq's theorem proving features. The implementation uses higher-order abstract syntax to represent variable binding and provides a type-checking algorithm for linear wire types, ensuring that quantum circuits are well-formed. We formalize a denotational semantics that interprets QWIRE circuits as superoperators on density matrices, and prove the correctness of some simple quantum programs.

본문 49,776자

본문 펼치기
# Q WIRE Practice: Formal Verification of Quantum Circuits in Coq Robert Rand ``` rrand@seas.upenn.edu ``` Jennifer Paykin ``` jpaykin@seas.upenn.edu ``` University of Pennsylvania Steve Zdancewic ``` stevez@cis.upenn.edu ``` We describe an embedding of the QWIRE quantum circuit language in the Coq proof assistant. This allows programmers to write quantum circuits using high-level abstractions and to prove properties of those circuits using Coq’s theorem proving features. The implementation uses higher-order abstract syntax to represent variable binding and provides a type-checking algorithm for linear wire types, ensuring that quantum circuits are well-formed. We formalize a denotational semantics that interprets QWIRE circuits as superoperators on density matrices, and prove the correctness of some simple quantum programs. ## 1 Introduction The last few years have witnessed the emergence of lightweight, scalable, and expressive quantum circuit languages such as Quipper [10] and LIQ _Ui_ ∣⟩ [22]. These languages adopt the QRAM model of quantum computation, in which a classical computer sends instructions to a quantum computer and receives back measurement results. Quipper and LIQ _Ui_ ∣⟩ programs classically produce circuits that can be executed on a quantum computer, simulated on a classical computer, or compiled using classical techniques to smaller, faster circuits. Since both languages are embedded inside general-purpose classical host languages (Haskell and F#), they can be used to build useful abstractions on top of quantum circuits, allowing for general purpose quantum programming. As is the case with classical programs, however, quantum programs in these languages will invariably have bugs. Since quantum circuits are inherently expensive to run (either simulated or on a real quantum computer) and are difficult or impossible to debug at runtime, numerous techniques have been developed to verify properties of quantum programs. The first step towards guaranteeing bug-free quantum programs is ensuring that every program corresponds to a valid quantum computation, meaning that a simulator or quantum computer running that program will not crash. In many cases this property can be enforced using type systems, as in the quantum lambda calculus, which uses linear types and guarantees type safety [20]. Along these lines, Proto-Quipper [19] adds linear types to a subset of Quipper, though this approach has not been extended to the full Quipper language. Beyond simply ensuring quantum mechanical soundness, we might wish to statically analyze specific programs or families of programs and prove that their semantics matches a formal specification. Doing so requires a formal semantics for programs, such as unit vectors or density matrices. The specification can then be verified using model-checking [9], Hoare logic [23], proof assistants [3], or other techniques. This work is supported in part by ONR MURI No. FA9550-16-1-0082 and NSF Grant No. CCF- 1421193. Bob Coecke and Aleks Kissinger (Eds.): 14th International Conference on Quantum Physics and Logic (QPL) [EPTCS 266, 2018, pp. 119–132, doi:10.4204/EPTCS.266.8](http://dx.doi.org/10.4204/EPTCS.266.8) © Rand, Paykin & Zdancewic This work is licensed under the [Creative Commons Attribution License.](http://creativecommons.org) 120 QWIRE _Practice_ Finally, one may wish to verify that two particular programs (or program fragments) have the same semantics. LIQ _Ui_ ∣⟩, in particular, has focused on efficient compilation of quantum circuits; similar projects have explored verified compilation passes in the case of reversible circuits [1]. The QWIRE programming language [14] is a small quantum circuit language embedded in a classical host language, which provides three core features: (1) a platform for high level quantum computing, with the expressiveness of embedded languages like Quipper [10] and LIQ _Ui_ ∣⟩[22]; (2) a linear type system that guarantees that generated circuits are well-formed and respect the laws of quantum mechanics; and (3) a concrete denotational semantics, specified in terms of density matrices, for proving properties and equivalences of quantum circuits. In this paper we report on a ongoing effort to implement QWIRE in the Coq theorem prover [6] and formalize its denotational semantics, thereby providing a framework to formally verify the correctness of quantum programs and quantum program transformations. <sup>1</sup> We include two forms of the language, one using Coq variables and higher-order abstract syntax for ease of programming, and a simpler version without these features, for verification purposes, along with a simple translation from the former to the latter. We then use this bridge to prove properties about higher-order programs using QWIRE’s denotational semantics. The paper makes the following contributions: - We implement the QWIRE programming language in the Coq proof assistant, incorporating features such as dependently-typed circuits and proof-carrying code; - We present a type checking algorithm for the linear type system of QWIRE using a representation of linear contexts based on the Linearity Monad [15]; - We formalize a denotational semantics for QWIRE circuits, interpreting them as superoperators on density matrices [13]; and - We show how these semantics can be used to verify quantum programs, including a quantum coin flip, a protocol with _dynamic lifting_, and a simple unitary circuit. Throughout the paper, we will assume some level of familiarity with both functional programming and proof assistants in the style of Coq or Agda. Readers unfamiliar with either of these concepts should consult the the introductory chapter of _Software_ _Foundations_ [17], which introduces the Coq language and theorem prover [6] used here. ## 2 Introduction to Q WIRE programming We start with some examples of QWIRE circuits implemented in Coq. The following circuit implements a quantum coin flip: <sup>2</sup> ``` Definition coin_flip : Box One Bit. ``` `box_` `()` ⇒ `gate_` `x` ← `init0` `@();` `gate_` `y` ← `H` `@x;` `gate_` `z` ← `meas` `@y;` ``` output z. ``` ∣0⟩ _<u>H</u>_ 1The Coq development for this paper is available at: `[https://github.com/jpaykin/QWIRE/tree/QPL2017](https://github.com/jpaykin/QWIRE/tree/QPL2017)` . 2Unless otherwise indicated, all QWIRE circuit definitions end in an implicit `Defined` . _Rand, Paykin & Zdancewic_ 121 The type `Box` `One` `Bit` is the high-level interface to QWIRE circuits, representing a “boxed” circuit with no input wires (represented by the unit type `One` ) and a `Bit` -valued output wire. We use Coq notations and tactics to construct the circuit. The `box_` tactic creates a boxed circuit from a function that takes an input pattern of wires and produces a circuit that uses those wires; `box_` also calls the linear type-checker on the result, to ensure that the circuit is well formed. In this example, the `coin_flip` circuit has no inputs, so the input pattern to the box is the empty pattern (). The body of the circuit is made up of a sequence of gate applications, where `init0`, `H`, and `meas` are all gates. These gates are applied to input patterns, whose types are determined by the gate being applied. In `coin_flip`, the pattern () has type `One`, `x` and `y` have type `Qubit`, and `z` has type `Bit` . The output of each gate is bound in the remainder of the circuit, and the circuit is terminated by an `output` statement. QWIRE ensures that wires in a circuit are treated linearly, meaning that every wire is used exactly once as input and once as output. For example, the linear type system enforces the no-cloning property, rejecting the following bogus circuit. `Definition` `clone` `W` `:` `Box` `W` `(W` ⊗ `W).` `box_` `w` ⇒ `output` `(w,w).` `(*` `Linear` `type` `checker` `cannot` `be` `satisfied.` `*)` `Abort.` QWIRE is compositional, meaning that a boxed circuit can be reused inside another circuit. Notice that only wires are treated linearly in QWIRE; a boxed circuit is a first-class Coq expression, and can be used as many times as necessary. Consider the following circuit, which flips a coin up to _n_ times, returning 1 if and only if the coin always lands on heads, which occurs with probability 2 <sup><u>1</u></sup> <sup>_<u>n</u>_</sup> <sup>.</sup> `Fixpoint` `coin_flips` `(n` `:` N `)` `:` `Box` `One` `Bit.` `box_` `()` ⇒ `match` `n` `with` `(*` `n:` N `is` `either` `0` `or` `the` `successor` `of` `some` `n'` `*)` `|` `0` ⇒ `gate_` `x` ← `new1` `@();` `output` `x` `|` `S` `n'` ⇒ `let_` `c` ← `unbox` `(coin_flips` `n')` `();` `gate_` `q` ← `init0` `@();` `gate_` `(c,q)` ← `bit_ctrl` `H` `@(c,q);` `coin` `flips` `n` ~~`'`~~ ~~●~~ `gate_` `()` ← `discard` `@c;` ∣0⟩ _<u>H</u>_ `gate_` `b` ← `meas` `@q;` ``` output b end. ``` `coin` `flips` `n` ~~`'`~~ ~~●~~ ∣0⟩ _<u>H</u>_ The `unbox` operator feeds the input pattern () into the boxed circuit `coin_flips n` ', and the `let_` operation binds the output of the recursive call in the rest of the circuit. The `let_` and `unbox` operations are separate constructs; for example, `box_ w` ⇒ `unbox b w` is just the _η_ -expansion of a boxed circuit `b` . QWIRE also allows for _dynamic lifting_ [14], in which measurement results from the quantum circuit are dynamically processed as classical data inside of Coq. This `lift` operation measures a qubit (or any collection of wires) and produces a boolean (or a tuple of booleans). We use the result of the measurement to decide which gates to apply in the remainder of the circuit. The following variation on `coin_flips` uses dynamic lifting and calls the unary `coin_flip` circuit from the start of this section. `Fixpoint` `coin_flips'` `(n` `:` N `)` `:` `Box` `One` `Bit.` `box_` `()` ⇒ `match` `n` `with` `|` `0` ⇒ `gate_` `q` ← `new1` `@();` `output` `q` `|` `S` `n'` ⇒ `let_` `q` ← `unbox` `(coin_flips'` `n')` `();` `lift_` `x` ← `q;` ``` if x then unbox coin_flip () ``` `else` `gate_` `q` ← `new0` `@();` `output` `q` ``` end. ``` Further examples can be found in the online development, as well as in the original QWIRE paper [14]. 122 QWIRE _Practice_ ## 3 Implementing Q WIRE in Coq At its core, a QWIRE circuit is a sequence of gates applied to wires. Each wire is described by a _wire type_ `W`, which is either the unit type (has no data), a bit or qubit, or a tuple of wire types. In Coq we represent wire types as an inductively defined data type `WType` as follows: `Inductive` `WType` `:` = `One` `|` `Bit` `|` `Qubit` `|` `Tensor` `:` `WType` → `WType` → `WType.` We use the Coq notation `` `W1` ⊗ `W2` '' for `Tensor W1` `W2` . Gates are indexed by a pair of wire types—a gate of type `Gate W1` `W2` takes an input wire of type `W1` and outputs a wire of type `W2` . In our setting, gates will include a universal set of unitary gates, as well as initialization, measurement, and control. <sup>3</sup> `Inductive` `Unitary` `:` `WType` → `Set` `:` = `(*` `other` `unitary` `gates` `omitted` `*)` ``` | H : Unitary Qubit (* Hadamard gate *) ``` `|` `control` `:` ∀ `{W}` `(U` `:` `Unitary` `W),` `Unitary` `(Qubit` ⊗ `W)` `|` `transpose` `:` ∀ `{W}` `(U` `:` `Unitary` `W),` `Unitary` `W.` `Inductive` `Gate` `:` `WType` → `WType` → `Set` `:` = `|` `U` `:` ∀ `{W}` `(u` `:` `Unitary` `W),` `Gate` `W` `W` ``` | init : Gate One Qubit | meas : Gate Qubit Bit | discard : Gate Bit One. ``` The curly braces surrounding the type argument `W` indicate that `W` is an implicit argument, meaning that it can be automatically inferred from the other arguments. We further define `U` to be a _coercion_ from unitaries to gates, meaning that for `u` : `Unitary W`, we can simply write `u` for `U u` : `Gate W W` . An open circuit (one with free input wires) has Coq type `Circuit Γ W`, where Γ is a typing context of input wires and _W_ is an output wire type. As an example, the circuit `gate_ w1` ' ← `H @w1` ; `gate_ w2` ' ← `meas @w2` ; `output` ( `w2` ', `w1` ') has type `Circuit` ( `w1` : `Qubit`, `w2` : `Qubit` ) ( `Bit` ⊗ `Qubit` ). A typing context of type `Ctx` is a partial map from variables (represented concretely as natural numbers) to wire types, which is represented as `list` ( `option WType` ). In this representation, the variable _i_ is mapped to _W_ if the _i_ th element in the list is `Some` _W_, and is undefined if the _i_ th element is `None` . The _disjoint merge_ operation ⋓ ensures that the same wire cannot be used in two separate parts of a circuit. Mathematically, it is defined on two typing contexts as follows: `[]` ⋓ `Γ2` = `Γ2` `Γ1` ⋓ `[]` = `Γ1` `None` `::` `Γ1` ⋓ `None` `::` `Γ2` = `None` `::` `(Γ1` ⋓ `Γ2)` `Some` `W` `::` `Γ1` ⋓ `None` `::` `Γ2` = `Some` `W` `::` `(Γ1` ⋓ `Γ2)` `None` `::` `Γ1` ⋓ `Some` `W` `::` `Γ2` = `Some` `W` `::` `(Γ1` ⋓ `Γ2)` Since disjoint merge is a partial function we represent it in Coq as a relation on possibly invalid contexts, `OCtx` = `Invalid` _|_ `Valid Ctx` . For convenience, most operations on contexts are lifted to work with `OCtx` values, and so the type signature of the merge operation ⋓ is `OCtx` → `OCtx` → `OCtx` . Wires in a context `Γ` can be collected into a pattern `Pat Γ W` to construct the wire type `W` . A pattern is just a tuple of wires of base types, meaning that all variables in a pattern have type `Bit` or `Qubit` . We use Coq’s dependent types to express logical predicates that constrain how patterns can be constructed. 3The set of gates need not be fixed; Rennela and Staton [18] explore how to use gates to extend QWIRE with recursive types. _Rand, Paykin & Zdancewic_ 123 `Inductive` `Circuit'` `:` `OCtx` → `WType` → `Set` `:` = `|` `output'` `:` ∀ `{Γ` `W},` `Pat` `Γ` `W` → `Circuit'` `Γ` `W` `|` `gate'` `:` ∀ `{Γ` `Γ1` `Γ2` `W1` `W2` `W},` `is_valid` `(Γ1` ⋓ `Γ)` → `is_valid` `(Γ2` ⋓ `Γ)` → `Gate` `W1` `W2` → `Pat` `Γ1` `W1` → `Pat` `Γ2` `W2` → `Circuit'` `(Γ2` ⋓ `Γ)` `W` → `Circuit'` `(Γ1` ⋓ `Γ)` `W` `|` `lift'` `:` ∀ `{Γ1` `Γ2` `W` `W'},` `is_valid` `(Γ1` ⋓ `Γ2)` → `Pat` `Γ1` `W` → `(interpret` `W` → `Circuit'` `Γ2` `W')` → `Circuit'` `(Γ1` ⋓ `Γ2)` `W'.` Figure 1: Definition of QWIRE circuits using an explicit representation of variable binding. We call this type `Circuit’`, reserving the name `Circuit` for the higher-order abstract syntax representation (Figure 2). `Inductive` `Pat` `:` `OCtx` → `WType` → `Set` `:` = ``` | unit : Pat (Valid []) One ``` `|` `qubit` `:` ∀ `(x` `:` N `)` `(Γ` `:` `Ctx),` `SingletonCtx` `x` `Qubit` `Γ` → `Pat` `(Valid` `Γ)` `Qubit` `|` `bit` `:` ∀ `(x` `:` N `)` `(Γ` `:` `Ctx),` `SingletonCtx` `x` `Bit` `Γ` → `Pat` `(Valid` `Γ)` `Bit` `|` `pair` `:` ∀ `Γ1` `Γ2` `W1` `W2,` `is_valid(Γ1` ⋓ `Γ2)` → `Pat` `Γ1` `W1` → `Pat` `Γ2` `W2` → `Pat` `(Γ1` ⋓ `Γ2)` `(W1` ⊗ `W2).` The `pair` constructor (for which we use notation ( `p1`, `p2` )) ensures that the wires in `p1` are disjoint from those in `p2` by calling out to the `is_valid` predicate, which checks whether the result of the merge is well-defined. The `qubit` and `bit` patterns are variable constructors that are only valid in contexts that contain the exact variable being introduced. The predicate `SingletonCtx x W Γ` ensures that the context Γ contains only the single wire _x_ of type _W_ . Circuits Like patterns, circuits are indexed by an input context and an output type. There are only three syntactic forms for circuits: _output_, _gate application_, and _dynamic lifting_ . Figure 1 defines circuits as an inductive data type indexed by the input typing context and the output wire type. An output circuit `output p` is just a pattern. A gate application, which we write with syntactic sugar as `gate_ p2` ← `g @p1` ; `C`, is made up of a gate `g` : `Gate W1` `W2`, an input pattern `p1` : `Pat Γ1` `W1`, an output pattern `p2` : `Pat Γ2` `W2`, and a circuit `C` : `Circuit` ' ( `Γ1` ⋓ `Γ2` ) `W` '. The intended meaning is that `p1` is the input to the gate `g`, and its output is bound to `p2` in the continuation `C` . Thus the variables `Γ2` that made up `p2` are also free in C, whereas the variables `Γ1` in `p1` are no longer available in C, enforcing linearity. The `is_valid` predicates enforce that all of the context arguments to circuits must be well-defined. The lift operation, which we write `lift_ x` ← `p` ; `C` takes as input a pattern `p` : `Pat Γ1` `W` and a function `fun x` ⇒ `C` that takes the classical interpretation of that data and produces another circuit. The intended semantics is that the circuit will measure the wires `p` (if they are not already bit-valued) and pass the result as ordinary Coq data to the function. In particular, both bits and qubits will result in boolean values being provided to the continuation, and tensors will be interpreted as pairs. A boxed circuit, written `box_ p` ⇒ `C` is a pair of a pattern and a circuit. `Inductive` `Box'` `:` `WType` → `WType` → `Set` `:` = `|` `box'` `:` ∀ `{W1` `W2` `Γ},` `Pat` `Γ` `W1` → `Circuit'` `Γ` `W2` → `Box'` `W1` `W2.` Composition and Higher-Order Abstract Syntax The minimal embedding of QWIRE in Figure 1 lacks a number of features of the language, including composition and unboxing. The `unbox` operator takes in a boxed circuit and an input pattern and produces a circuit. It has the following signature: ``` Definition unbox {Γ W1 W2} (b : Box' W1 W2) (p : Pat Γ W1) : Circuit' Γ W2. ``` 124 QWIRE _Practice_ `Inductive` `Circuit` `:` `OCtx` → `WType` → `Set` `:` = `|` `output` `:` ∀ `{Γ` `Γ'` `w},` `(Γ` = `Γ')` → `Pat` `Γ` `w` → `Circuit` `Γ'` `w` `|` `gate` `:` ∀ `{Γ` `Γ1` `Γ1'` `w1` `w2` `w},` `is_valid` `Γ1'` → `(Γ1'` = `Γ1` ⋓ `Γ)` → `Gate` `w1` `w2` → `Pat` `Γ1` `w1` → `(` ∀ `{Γ2` `Γ2'},` `is_valid` `Γ2'` → `(Γ2'` = `Γ2` ⋓ `Γ)` → `Pat` `Γ2` `w2` → `Circuit` `Γ2'` `w)` → `Circuit` `Γ1'` `w` `|` `lift` `:` ∀ `{Γ1` `Γ2` `Γ` `w` `w'},` `is_valid` `Γ` → `(Γ` = `Γ1` ⋓ `Γ2)` → `Pat` `Γ1` `w` → `(interpret` `w` → `Circuit` `Γ2` `w')` → `Circuit` `Γ` `w'.` Figure 2: A definition of QWIRE circuits using higher-order abstract syntax. The intended _β_ -reduction rule should have the form `unbox` ( `box_ p` ⇒ `C` ) `p` ' = `C` [ `p` '/ `p` ] where `C` [ `p` '/ `p` ] is a substitution of the variables in `p` ' for those in `p` in the circuit `C` . The traditional solution to this problem involves defining a number of substitution functions and proving appropriate typing relations for each operation. Although this approach is viable, it is often tedious and introduces a large amount of complexity, especially in linear systems. An alternative approach is the technique of higher-order abstract syntax (HOAS) [16], in which variable bindings in an embedded language are represented as functions in the host language. This means that the language designer does not have to define substitution functions and prove their correctness, and that variables in the embedded language have the same weight as variables in the host language. The HOAS approach to boxed circuits treats a box as a function from patterns to circuits: `Inductive` `Box` `:` `WType` → `WType` → `Set` `:` = `|` `box` `:` `(` ∀ `{Γ},` `Pat` `Γ` `W1` → `Circuit` `Γ` `W2)` → `Box` `W1` `W2.` The `unbox` operation then simply destructs the box and applies the function appropriately. `Definition` `unbox` `{Γ` `W1` `W2}` `(b` `:` `Box` `W1` `W2)` `(p` `:` `Pat` `Γ` `W1)` `:` `Circuit` `Γ` `W2` `:` = `match` `b` `with` `box` `f` ⇒ `f` `p` `end.` We can take a similar approach for the binding pattern in gate application, as shown in Figure 2. In this setting, output of a gate application is represented by a function from the output pattern to a new circuit. The other difference between the HOAS presentation of circuits and the “flat” representation `Circuit` ' in the previous section is that in addition to proofs of validity about merged contexts, we introduce fresh arguments for the output context of each circuit. This is due to a technical limitation of Coq pattern matching, and the result makes it possible for us to write the examples in Section 2. With this machinery in place, we can define composition as a meta-operation on circuits. For conciseness, we give its definition as a sequence of _β_ -reduction rules, omitting the proof arguments. ``` Fixpoint compose {Γ1 Γ1' W Γ W'} (c : Circuit Γ1 W) ``` `(f` `:` ∀ `{Γ2` `Γ2'},` `(Γ2'` = `Γ2` ⋓ `Γ)` → `is_valid` `Γ2'` → `Pat` `Γ2` `W` → `Circuit` `Γ2'` `W')` `:` `is_valid` `Γ1'` → `(Γ1'` = `Γ1` ⋓ `Γ)` → `Circuit` `Γ1'` `W'.` `compose` `(output` `p)` `f` = `f` `p` `compose` `(gate` `g` `p1` `h)` `f` = `gate` `g` `p1` `(fun` `p2` ⇒ `compose` `(h` `p2)` `f)` `compose` `(lift` `p` `h)` `f` = `lift` `p` `(fun` `x` ⇒ `compose` `(h` `x)` `f)` The Q WIRE type checker In type-checking QWIRE circuits, we are asked to solve equations of the form `Γ1` ⋓⋯⋓ `Γn` = `Γ1` ' ⋓⋯⋓ `Γm` ' and `is_valid` ( `Γ1` ⋓⋯⋓ `Γn` ), in order to enforce the linearity of wires. _Rand, Paykin & Zdancewic_ 125 The first of these goals can be discharged with a automated proof tactic for solving systems of commutative monoids. In the higher-order abstract syntax version of circuits, these predicates may also contain _evars_, existentially quantified Coq variables. By starting at the leaves of the typing derivation, we can ensure that every equation of the form `Γ1` ⋓⋯⋓ `Γn` = `Γ1` ' ⋓⋯⋓ `Γm` ' has at most one evar. We can then cancel out all variables that appear on both sides of the equation, and unify the evar with what remains on the opposite side. Once repeated applications of `monoid` have replaced all existential variables with regular Coq variables, we can prove goals of the form `is_valid` ( `Γ1` ⋓⋯⋓ `Γn` ). We take advantage of the fact that a set of finite contexts is disjoint if and only if the contexts are all pairwise disjoint. Our custom tactic for solving these goals extracts all proofs of pairwise disjointness from hypotheses and then reduces the goal to a conjunction of pairwise disjointness claims `is_valid` ( `Γi` ⋓ `Γj` ). It then applies proofs of these claim, where available, from the hypotheses. ## 4 Denotational Semantics 4.1 The Matrix Library The denotational semantics of QWIRE is implemented using a matrix library created specifically for this purpose. Matrices are simply functions from pairs of natural numbers to complex numbers. <sup>4</sup> `Definition` `Matrix` `(m` `n` `:` N `)` `:` = N → N → C `.` The arguments _m_ and _n_, which are the dimensions of the matrix, are not used directly in the definition, but they are useful to define certain operations on matrices, such as the Kronecker product and matrix multiplication, which depend on these dimensions. They are also useful as an informal annotation that aids the programmer. We say a matrix is well-formed when it is zero-valued outside of its domain. `Definition` `WF_Matrix` `{m` `n}` `(M` `:` `Matrix` `m` `n)` `:` P `:` = ∀ `i` `j,` `i` ≥ `m` ∨ `i` ≥ `n` → `M` `i` `j` = `0.` This library is designed to facilitate reasoning about and computing with matrices. Treating matrices as functions allows us to easily express otherwise complicated matrix operations. Consider the definitions of Kronecker product (⊗) and complex conjugate transpose (†), where `Cconj` is the complex conjugate: `Definition` `kron` `{m` `n` `o` `p}` `(A` `:` `Matrix` `m` `n)` `(B` `:` `Matrix` `o` `p)` `:` `Matrix` `(m*n)` `(o*p)` `:` = `fun` `x` `y` ⇒ `A` `(x` / `o)` `(y` / `p)` `*` `B` `(x` `mod` `o)` `(y` `mod` `p).` `Definition` `ctrans` `{m` `n}` `(A` `:` `Matrix` `m` `n)` `:` `Matrix` `n` `m` `:` = `fun` `x` `y` ⇒ `Cconj` `(A` `y` `x).` We represent complex numbers using an adaptation of Coquelicot’s `Complex` library [4]. This representation has the advantage of being a straightforward extension of the Coq real numbers, allowing us to easily extend the Coq Standard Library’s powerful Linear Real Arithmetic solver ( `lra` ) to both complex numbers and matrices. The tactics we define (termed `clra` for complex numbers and `mlra` for matrices) allow us to trivially prove that complex conjugate and complex conjugate transpose are involutive: `Lemma` `conj_involutive` `:` ∀ `(c` `:` C `),` `Cconj` `(Cconj` `c)` = `c.` `Proof.` `intros.` `clra.` `Qed.` `Lemma` `ctrans_involutive` `:` ∀ `{m` `n}` `(A` `:` `Matrix` `m` `n),` `A††` = `A.` `Proof.` `intros.` `mlra.` `Qed.` 4As a Coq technicality, note that matrices are only equal up to functional extensionality. 126 QWIRE _Practice_ 4.2 Density Matrices QWIRE programs are interpreted as superoperators over density matrices, following the denotational semantics described by Paykin _et_ _al._ [14]. We use a density matrix representation over the standard unit vector representation (used for example by Boender _et_ _al._ [3]) because density matrices represent probability distributions (introduced via measurement) over quantum states directly, as opposed to the unit vector representation which must be embedded inside a probability monad. We start with some preliminary definitions. A unitary matrix is a well-formed square matrix _A_ such that _A_ <sup>†</sup> × _A_ is the identity. `Definition` `is_unitary` `{n}` `(A` `:` `Matrix` `n` `n)` `:` = `WF_Matrix` `A` ∧ `A†` × `A` = `Id` `n.` A pure state of a quantum system is one that corresponds to a unit vector ∣ _φ_ ⟩. An equivalent representation is that of square matrices _ρ_ such that _ρ_ × _ρ_ = _ρ_ . `Definition` `Pure_State` `{n}` `(` _ρ_ `:` `Matrix` `n` `n)` `:` P `:` = `WF_Matrix` _ρ_ ∧ _ρ_ = _ρ_ × _ρ_ `.` A density matrix, or mixed state, is a linear combination of pure states representing the probability of each pure state. `Inductive` `Mixed_State` `{n}` `(` _ρ_ `:` `Matrix` `n` `n)` `:` P `:` = `|` `Pure_S` `:` ∀ _ρ_ `,` `Pure_State` _ρ_ → `Mixed_State` _ρ_ `|` `Mix_S` `:` ∀ `(p` `:` R `)` _ρ_ `1` _ρ_ `2,` `0` < `p` < `1` → `Mixed_State` _ρ_ `1` → `Mixed_State` _ρ_ `2` → `Mixed_State` `(p` `.*` _ρ_ `1` `.` + `(1-p)` `.*` _ρ_ `2).` Note that every mixed state is also well-formed, since scaling and addition preserve well-formedness. A superoperator is a function on square matrices that takes mixed states to mixed states. `Definition` `Superoperator` `m` `n` `:` = `Matrix` `m` `m` → `Matrix` `n` `n.` `Definition` `WF_Superoperator` `m` `n` `(f` `:` `Superoperator` `m` `n)` `:` = ∀ `(` _ρ_ `:` `Matrix` `m` `m),` `Mixed_State` _ρ_ → `Mixed_State` `(f` _ρ_ `).` Any _m_ × _n_ matrix can be lifted to a superoperator from _n_ to _m_ as follows: `Definition` `super` `{m` `n}` `(A` `:` `Matrix` `m` `n)` `:` `Superoperator` `n` `m` `:` = `fun` _ρ_ ⇒ `A` × _ρ_ × `A†.` 4.3 Denotation of Q WIRE Types, Contexts and Gates In order to interpret circuits as superoperators over density matrices, we will also give types, contexts, gates, and patterns interpretations in linear algebra. For clarity we write �−� for the denotation of a variety of QWIRE objects, which we express via a Coq type class. `Class` `Denote` `source` `target` `:` = `{` `denote` `:` `source` → `target` `}.` `Notation` `"` - `x` - `"` `:` = `(denote` `x)` `(at` `level` `10).` We interpret every wire type as the number of primitive ( `Bit` or `Qubit` ) wires in that type, so - `Qubit` ⊗ ( `One` ⊗ `Bit` )� = 2. Contexts and `OCtx` s are similarly denoted by the number of wires they contain. Every gate of type `Unitary W` corresponds to a unitary matrix of dimension 2 <sup>�</sup> <sup>_W_</sup> <sup>�</sup> × 2 <sup>�</sup> <sup>_W_</sup> <sup>�</sup> . We omit the implemented gate set and their corresponding matrices here, but in the development we prove that every denoted unitary satisfies the `is_unitary` predicate defined above. `Lemma` `unitary_gate_unitary` `:` ∀ `{W}` `(u` `:` `Unitary` `W),` `is_unitary` - `u` - `.` A gate of type `Gate` `W1` `W2` corresponds to a superoperator as follows: _Rand, Paykin & Zdancewic_ 127 `Definitionmatch` `g` `withdenote_gate` `{W1` `W2}` `(g` `:` `Gate` `W1` `W2)` `:` `Superoperator` `2` <sup>∧</sup> - `W1` - `2` <sup>∧</sup> - `W2` - `:` = ``` match g with ``` `||` `Uinit0u` `|` `new0` ⇒⇒ `supersuper` �∣0 `u` ⟩� `|` `init1` `|` `new1` ⇒ `super` ∣1⟩ `|` `meas` ⇒ `fun` _ρ_ ⇒ `super` ∣0⟩⟨0∣ _ρ_ `.` + `super` ∣1⟩⟨1∣ _ρ_ `|` `discard` ⇒ `fun` _ρ_ ⇒ `super` ⟨0∣ _ρ_ `.` + `super` ⟨1∣ _ρ_ ``` end. ``` `Instance{|` `denoteDenote_Gate:` = `denote_gate{W1` `W2}` `|}.:` `Denote` `(Gate` `W1` `W2)` `(Superoperator` `2` <sup>∧</sup> - `W1` - `2` <sup>∧</sup> - `W2` - `)` `:` = `{|` `denote` `:` = `denote_gate` `|}.` When applying a gate to a subset of a quantum system, however, we will need a generalization of the `denote_gate` operation that applies the gate to the first part of a quantum system. `Definition` `denote_gate'` `n` `{W1` `W2}` `(g` `:` `Gate` `W1` `W2)` `:` `Superoperator` `2` <sup>∧</sup> - `W1` - `*2` <sup>∧</sup> `n` `2` <sup>∧</sup> - `W2` - `*2` <sup>∧</sup> `n.` What we previously wrote as `denote_gate` is simply `denote_gate` ' 0. Patterns and Flat Circuits Patterns of type `Pat Γ W` are interpreted as permutation matrices of dimension 2 <sup>�Γ�</sup> × 2 <sup>�</sup> <sup>_W_</sup> <sup>�</sup> . These matrices are constructed via multiple applications of a `swap` matrix, as follows: In general we will want to swap ⎛1 0 0 0⎞ the positions of two arbitrary qubits in a system; to swap qubit 0 with qubit 2 in a 0 0 1 0 `swap` = 3-qubit system, we invoke `swap2` `0` `2` = ( _I_ 2 ⊗ `swap` )( `swap` ⊗ _I_ 2)( _I_ 2 ⊗ `swap` ). ⎜⎜⎜0 1 0 0⎟⎟⎟ First, every pattern is interpreted as a list of indices indicating the wire ⎝0 0 0 1⎠ number (counting from 0) that each variable refers to. So the pattern (3,0) : `Pat` [ `Some Qubit`, `None`, `None`, `Some Bit` ] ( `Bit` ⊗ `Qubit` ) corresponds to the list [1;0], because variable 3 corresponds the wire numbered 1 and variable 0 corresponds to wire 0 in the circuit. This list `ls` is then turned into an association list [(0 _,_ 1);(1 _,_ 0)], mapping variable `i` to `ls` [ `i` ]. These pairs are then interpreted as a series of calls to `swap2` . As for gates, the function `denote_pat_in` follows a similar algorithm, but allows us to interpret a pattern `p` inside a larger context of variables. Its signature is as follows: `Definition` `denote_pat_in` `Γ'` `{Γ` `W}` `(p` `:` `Pat` `Γ` `W):` `Matrix` `2` <sup>∧</sup> - `Γ` ⋓ `Γ'` - `(2` <sup>∧</sup> - `W` - `*` `2` <sup>∧</sup> - `Γ'` - `).` `Instance{|` `denoteDenote_Pat:` = `denote_pat_in{Γ` `W}` `:` `Denote(Valid(Pat[])Γ` `W)|}.(Matrix` `2` <sup>∧</sup> - `Γ` - `2` <sup>∧</sup> - `W` - `)` `:` = Finally, we interpret circuits as superoperators on density matrices. To do this, we start with the “flat” representation `Circuit` ' that does not use higher-order abstract syntax. The higher-order abstract syntax representation is useful for defining the meta-level operations on circuits, but the “flat” circuits from Figure 1 use concrete variables, thus making them a useful intermediate representation for the semantics. Consider an output circuit `output` ' `p`, where `p` : `Pat Γ W` . The interpretation of this circuit is the superoperator obtained from the denotation of `p` : - `output` ' `p` - = `super` - `p` �. Next, consider `gate_ p2` ← `g @p1` ; `C`, where `g` : `Gate W1` `W2`, `pi` : `Pat Γi` `Wi` and `C` : `Circuit` ' ( `Γ2` ⋓ `Γ` ) `W` . The interpretation of `C` has type `Superoperator` 2 <sup>∧</sup> - `Γ2` ⋓ `Γ` - 2 <sup>∧</sup> - `W` �, so we need to compose - `C` - with a superoperator from 2 <sup>∧</sup> - `Γ1` �*2 <sup>∧</sup> - `Γ` - to 2 <sup>∧</sup> - `Γ2` �*2 <sup>∧</sup> - `Γ` - which we obtain by composing `denote_gate` ' with the results of `denote_pat_in`, to rearrange the quantum system appropriately: - `gate'` `g` `p1` `p2` `C'` - = - `C'` - - `super` `(` - `p2` - `†` ⊗ `Id` `2` <sup>∧</sup> - `Γ` - `)` - `denote_gate'` - `Γ` - `g` - `super` `(` - `p1` - ⊗ `Id` `2` <sup>∧</sup> - `Γ` - `)` ⎞ ⎟⎟⎟ ⎠ `swap` = ⎛ ⎜⎜⎜ ⎝ 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 128 QWIRE _Practice_ Finally, consider a lift circuit, `lift’` `p` `f`, where `p` : `Pat Γ1` `W` and `f` : `interpret W` → `Circuit` ' `Γ2` `W` '. When `W` is a qubit, `interpret W` = `bool`, and the lift operation would measure the qubit and sum over the results of � `f true` - and � `f false` �. More generally, consider `Definition` `f'` `:` `interpret` `W` → `Superoperator` `2` <sup>∧</sup> - `Γ1` ⋓ `Γ2` - `2` <sup>∧</sup> - `Γ2` `:` = `fun` `x` ⇒ - `f` `x` - - `(super` `((kets` `x)†` ⊗ `Id` `2` <sup>∧</sup> - `Γ2` - `))` - `(super` `(denote_pat_in` `Γ2` `p))` Here, `kets` _{_ `W` _}_ : `interpret W` → `Matrix` 2 <sup>∧</sup> - `W` - 1 is the basis representation of the input value of type `interpret W` . By transposing `kets x` and expanding it via the `super` operation, we pick out the partial density matrix corresponding to that measurement branch. Next, since all wire types are finite, we can enumerate all values of type `interpret W` in a list via the operation `get_interpretations W` . By mapping `f` ' over this list, we obtain each of the actual measurement branches as superoperators. Now we can simply perform pointwise addition of the superoperators, and compose with the pattern `p` to organize the wires in order: - `lift'` `p` `f` - = `fold_left` `Splus` `(map` `f'` `(get_interpretations` `W))` `SZero` A flat box is also interpreted as a superoperator: - `box` ' `p C` - = � `C` - - `super` - `p` �† HOAS Circuits To denote the HOAS version of circuits, we first map them to our representation of “flat” circuits, which involves instantiating the output patterns of HOAS gates with a particular concrete pattern. We do this via an operation `fresh` ~~`p`~~ `at` that takes as input a context and a type, and produces a pattern of that type whose domain ( `fresh_pat_ctx` ) is disjoint from the input context. ``` Definition fresh_pat (Γ : OCtx) (W : WType) : Pat (fresh_pat_ctx Γ W) W. ``` Using `fresh_pat` we can define a function that converts HOAS circuits and boxes into flat circuits and boxes. We leave off implicit proof and `OCtx` arguments for legibility. `Program` `Fixpoint` `from_HOAS` `{Γ` `W}` `(c` `:` `Circuit` `Γ` `W)` `:` `Circuit'` `Γ` `W` `:` = ``` match c with ``` `|` `output` `p` ⇒ `output'` `p` `|` `gate` `g` `p1` `f` ⇒ `gate'` `g` `p1` `p2` `(from_HOAS` `(f` `(fresh_pat` `_` `_)))` `|` `lift` `p` `f` ⇒ `lift'` `p` `(fun` `x` ⇒ `from_HOAS` `(f` `x))` ``` end. ``` `Program` `Definition` `from_HOAS_Box` `{W1` `W2}` `(b` `:` `Box` `W1` `W2)` `:` `Flat_Box` `W1` `W2` `:` = `match` `b` `with` `box` `f` ⇒ `let` `p` `:` = `fresh_pat` `[]` `W1` `in` `box'` `p` `(from_HOAS` `(f` `p))` `end.` The denotation of a HOAS circuit is exactly the denotation of its corresponding flat circuit. ## 5 A Taste of Verification When a circuit is closed, that is when it has no input, it represents a preparation of a quantum state. In many cases, a programmer may know what state their program should prepare, and our verification framework allows them to compare the denotation of the circuit with the desired density matrix directly. Consider, for instance, the coin flip circuit in Section 2. In the online development we prove that the 1/2 0 denotation of `coin_flip` corresponds to the matrix `even_toss` = ( 0 1/2 <sup>) as follows:</sup> `Lemma` `flip_toss` `:` - `coin_flip` - `(Id` `1)` = `even_toss.` The Coq proof script used to prove this lemma is only twelve lines long, and calls out to a number of specialized tactics, including `Msimpl`, `Csimpl` and `Csolve`, designed to simplify and prove equality between matrices and complex numbers. _Rand, Paykin & Zdancewic_ 129 The combination of Coq and QWIRE truly shines in its ability to prove more complex properties and prove properties of _families_ of circuits, not merely circuits themselves. For example, consider the Deutsch-Jozsa problem, where we want to verify a property of a family of circuits with any number of input qubits, that holds for every appropriate unitary matrix _U_ _f_ . This sort of property requires induction over the number of inputs and the full power of a proof assistant. Open circuits, which correspond to arbitrary superoperators, are even more interesting from the perspective of verification. Consider the following circuit which composes a unitary gate with its transpose: ``` Definition unitary_trans {W} (U : Unitary W) : Box W W. ``` `box_` `p` ⇒ `gate_` `p` ← `U` `@p;` `gate_` `p` ← `transpose` `U` `@p;` ``` output p. ``` `Lemma` `unitary_trans_id` `:` ∀ `W` `(U` `:` `Unitary` `W)` _ρ_ `,` <u>U</u> <u>U</u> <sup>†</sup> `WF_Matrix` `(2` <sup>∧</sup> - `W` - `)` `(2` <sup>∧</sup> - `W` - `)` _ρ_ → - `unitary_trans` `U` - _ρ_ = - `id_circ` `W` - _ρ_ `.` This correctness of this circuit holds for any unitary gate regardless of its input size. The proof of this lemma takes fewer than 20 lines of code, and proceeds using common facts from linear algebra, such as the fact that `Id n` × `A` = `A`, along with fact that the denotation of `U` is in fact unitary, that is, - `U` �† � `U` - = `Id` . We also rely on the fact that the denotation of every pattern in this circuit is actually an identity matrix. Another useful sanity check ensures that the denotation of a `lift` circuit is equivalent to applying a measurement gate. For example, consider `lift` ~~`m`~~ `eas`, which measures a qubit via `lift` and then reconstructs a bit-valued wire from the result of the measurement. The resulting circuit is equivalent to the circuit that simply applies a measurement gate. ## 6 Related and Future Work ``` Definition lift m eas : Box Qubit Bit. ``` `box_` `q` ⇒ `lift_` `x` ← `q;` `gate_` `p` ← `(if` `x` `then` `new1` `else` `new0)` `@();` ``` output p. ``` `Lemma` `lift` `meas_correct` `:` ∀ _ρ_ `,` `WF_Matrix` _ρ_ → - `lift` ~~`m`~~ `eas` - _ρ_ = - `boxed_gate` `meas` - _ρ_ `.` The Coq development described in this paper is very much still a work in progress. There are a small number of lemmas in the underlying matrix library that we have not yet formally proved, although they are known facts about linear algebra. In addition, we have not yet formally proved that the denotation of every circuit is a well-formed superoperator over density matrices, or that the denotation of the composition of two circuits is equal to the composition of their denotations. After completing this work, there are a number of exciting areas to explore in the future. Verified Compilation By verifying the equivalence of circuit transformations like `unitary_trans_id` from Section 5, we see QWIRE as a prime language in which to compile quantum programs. The area of verified compilers has gained a lot of traction in recent years, inspired by the success of the CompCert C compiler [12]. Towards this direction, Amy _et al._ [1] developed a verified, lightly-optimizing compiler for reversible circuits, which can encompass unitary quantum circuits. We can also provide insight into a relationship between QWIRE circuits and QASM [7], a quantum assembly-like language that has gained widespread use in quantum simulators and IBM’s Quantum Experience [11]. The largest difference between QWIRE and QASM is our use of variable binding, using abstract variables such as _x_, _y_, and _z_ and allowing qubits to be renamed as in `let_ y` ← `output x` ; `C` '. In 130 QWIRE _Practice_ comparison, QASM operates on a concrete set of quantum registers ( _e.g.,_ qubits 1, 2, or 3) that cannot be renamed. In the development we provide a version of QWIRE that similarly operates directly on named qubits. Our denotational semantics extends directly to this representation, and we can compile from QWIRE to these “assembly-level” circuits. Future work could formally establish a relationship between the assembly-level version of QWIRE and QASM, allowing us to prove properties of QASM programs, and prove that compilation is sound with respect to the denotational semantics. A More Efficient Backend Unfortunately, we have so far struggled to prove properties of (nonparametric) circuits with more than a few qubits, indicating that scalability will be a challenge moving forward. A key contributor to the scalability of our theorems is the underlying matrix library, which is not currently optimized in any significant way. Of course, any quantum simulator will be intractable on large enough circuits, as density matrices are always exponential in the size of the corresponding circuit. In the near future, we will be transitioning the linear algebra back-end of our development to one of several existing projects, in the hopes of making it more efficient. Boender _et_ _al._ [3] present one candidate, a library developed reasoning about the correctness of quantum protocols using pure states. Another candidate is the Coq Effective Algebra Library (CoqEAL) [5], which is designed specifically to allow matrix computation inside Coq. This library allows easy translation between it matrices and those of the Mathematical Components library [2], which in turn was designed with an eye towards verification. The two libraries together may substantially increase our ability to run and reason about QWIRE programs. We can also simulate QWIRE programs by extraction to OCaml. Theory of Q WIRE In this paper we focus on the denotational semantics of QWIRE, but many other aspects of the meta-theory are left to be explored. Rennela and Staton [18] present a categorical model of EWire, a close variant of QWIRE, as an enriched category. Their model also allows for additional features such as quantum data types in the style of Quipper [10]. The equational theory of quantum circuits is another area left for future work. For example, Staton [21] presents an axiomatization of the relationship between measurement, qubit initialization, and a limited set of unitary gates. In the future we hope to adapt Staton’s work to QWIRE and thereby reason syntactically, rather than semantically, about the equivalence of QWIRE circuits. An equational theory is also key to integrating QWIRE with dependent types [14]. Verifying Higher-Order Programs The use of dependent types was a driving factor for both the development of QWIRE and the choice to embed it in Coq specifically. While dependent types power Coq’s verification capabilities, they’re also key to our representation of circuits. For example, in the development we implement a dependently-typed version of the Quantum Fourier Transform, as described in the introduction to QWIRE [14]. This paper hasn’t focused on dependent types, or high level programming in QWIRE generally, but both will feature heavily in future QWIRE development. Greater use of the host language and classical programming abstractions will also allow to further push the boundaries of quantum verification. While current QWIRE programs can be thought of as “simply circuits,” such direct proofs will become increasingly difficult as we add on layers of abstraction—just as it would be nearly impossible to prove interesting program properties by reasoning about the underlying classical circuits. This will require new approaches to quantum verification, from quantum weakest precondition reasoning [8] (expanded into a Hoare-like logic in [23, 24]), to the forms of equational reasoning described above. _Rand, Paykin & Zdancewic_ 131 ## References [1] Matthew Amy, Martin Roetteler & Krysta M Svore (2017): _Verified compilation of space-efficient reversible_ _circuits_ . In: _International Conference on Computer Aided Verification_ [, Springer, pp. 3–21, doi:10.1007/978-](http://dx.doi.org/10.1007/978-3-319-63390-9_1) [3-319-63390-9](http://dx.doi.org/10.1007/978-3-319-63390-9_1) ~~1~~ . [2] Assia Mahboubi and Enrico Tassi (2016): _Mathematical_ _Components_ . Electronic resource, available from `[https://math-comp.github.io/mcb/book.pdf](https://math-comp.github.io/mcb/book.pdf)` . [3] Jaap Boender, Florian Kamm¨uller & Rajagopal Nagarajan (2015): _Formalization of Quantum Protocols using_ _Coq_ . In Chris Heunen, Peter Selinger & Jamie Vicary, editors: Proceedings of the 12th International Workshop on _Quantum Physics and Logic,_ Oxford, U.K., July 15-17, 2015, _Electronic Proceedings in Theoretical_ _Computer Science_ [195, Open Publishing Association, pp. 71–83, doi:10.4204/EPTCS.195.6.](http://dx.doi.org/10.4204/EPTCS.195.6) [4] Sylvie Boldo, Catherine Lelay & Guillaume Melquiond (2015): _Coquelicot_ . `[http://coquelicot.](http://coquelicot.saclay.inria.fr/)` `[saclay.inria.fr/](http://coquelicot.saclay.inria.fr/)` . [5] Guillaume Cano, Cyril Cohen, Maxime D´en`es, Anders M¨ortberg & Vincent Siles (2016): _CoqEAL - The Coq_ _Effective Algebra Library_ . `[https://github.com/CoqEAL/CoqEAL](https://github.com/CoqEAL/CoqEAL)` . [6] Coq Development Team (2017): _The_ _Coq_ _Proof_ _Assistant_ _Reference_ _Manual,_ _Version_ _8.6_ . Electronic re source, available from `[http://coq.inria.fr](http://coq.inria.fr)` . [7] Andrew Cross: _qasm-tools:_ _An_ _interoperable_ _open-source_ _software_ _tool_ _chain_ _for_ _studying_ _fault-tolerant_ _quantum circuits_ . [8] Ellie D’Hondt & Prakash Panangaden (2006): _Quantum weakest preconditions_ . _Mathematical Structures in_ _Computer Science_ [16(03), pp. 429–451, doi:10.1017/S0960129506005251.](http://dx.doi.org/10.1017/S0960129506005251) [9] Simon J. Gay, Rajagopal Nagarajan & Nikolaos Papanikolaou (2008): _QMC:_ _A_ _Model_ _Checker_ _for_ _Quan-_ _tum_ _Systems_ . In Aarti Gupta & Sharad Malik, editors: _Computer_ _Aided_ _Verification:_ _20th_ _International_ _Conference, CAV 2008_ [, Springer Berlin Heidelberg, Princeton, NJ, USA, pp. 543–547, doi:10.1007/978-3-](http://dx.doi.org/10.1007/978-3-540-70545-1_51) [540-70545-1](http://dx.doi.org/10.1007/978-3-540-70545-1_51) ~~5~~ 1. [10] Alexander Green, Peter LeFanu Lumsdaine, Neil J. Ross, Peter Selinger & Benoˆıt Valiron (2013): _Quipper: A_ _Scalable Quantum Programming Language_ . In: _Proceedings of the 34th ACM SIGPLAN Conference on Pro-_ _gramming Language Design and Implementation_ [, PLDI 2013, pp. 333–342, doi:10.1145/2491956.2462177.](http://dx.doi.org/10.1145/2491956.2462177) [11] IBM (2017): _IBM Quantum Experience_ . Available at `[http://research.ibm.com/ibm-q/qx/](http://research.ibm.com/ibm-q/qx/)` . [12] Xavier Leroy (2004): _The_ _CompCert_ _verified_ _compiler_ . _Development_ _available_ _at_ _http://compcert._ _inria._ _fr_ 2009. [13] Michael A Nielsen & Isaac L Chuang (2010): _Quantum computation and quantum information_ . Cambridge [university press, doi:10.1017/CBO9780511976667.](http://dx.doi.org/10.1017/CBO9780511976667) [14] Jennifer Paykin, Robert Rand & Steve Zdancewic (2017): _QWIRE: A Core Language for Quantum Circuits_ . In: _Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages_, POPL [2017, ACM, New York, NY, USA, pp. 846–858, doi:10.1145/3009837.3009894.](http://dx.doi.org/10.1145/3009837.3009894) [15] Jennifer Paykin & Steve Zdancewic (2017): _The Linearity Monad_ . In: _Proceedings of the 10th ACM SIG-_ _PLAN International Symposium on Haskell_ [, ACM, pp. 117–132, doi:10.1145/3122955.3122965.](http://dx.doi.org/10.1145/3122955.3122965) [16] F. Pfenning & C. Elliott (1988): _Higher-order Abstract Syntax_ . In: _Proceedings of the ACM SIGPLAN 1988_ _Conference on Programming Language Design and Implementation_, PLDI ’88, ACM, New York, NY, USA, [pp. 199–208, doi:10.1145/53990.54010.](http://dx.doi.org/10.1145/53990.54010) [17] Benjamin C. Pierce, Arthur Azevedo de Amorim, Chris Casinghino, Marco Gaboardi, Michael Greenberg, Cˇatˇalin Hrit¸cu, Vilhelm Sj¨oberg & Brent Yorgey (2016): _Software Foundations_ . Electronic textbook. Version 4.0. `[http://www.cis.upenn.edu/~bcpierce/sf](http://www.cis.upenn.edu/~bcpierce/sf)` . [18] Mathys Rennela & Sam Staton (2017): _Classical control and quantum circuits in enriched category theory_ . In: _Proceedings of the 33rd Conference on the Mathematical Foundations of Programming Semantics, MFPS_ . 132 QWIRE _Practice_ [19] Neil J. Ross (2015): _Algebraic_ _and_ _Logical_ _Methods_ _in_ _Quantum_ _Computation_ . Ph.D. thesis, Dalhousie University. [20] Peter Selinger & Benoˆıt Valiron (2009): _Quantum_ _lambda_ _calculus_ . In Simon Gay & Ian Mackie, editors: _Semantic_ _Techniques_ _in_ _Quantum_ _Computation_, Cambridge University Press, pp. 135–172, [doi:10.1017/CBO9781139193313.005.](http://dx.doi.org/10.1017/CBO9781139193313.005) [21] Sam Staton (2015): _Algebraic_ _Effects,_ _Linearity,_ _and_ _Quantum_ _Programming_ _Languages_ . In: _Proceedings_ _of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages_, POPL [’15, ACM, New York, NY, USA, pp. 395–406, doi:10.1145/2676726.2676999.](http://dx.doi.org/10.1145/2676726.2676999) [22] Dave Wecker & Krysta M Svore (2014): _LIQUiD: A software design architecture and domain-specific lan-_ _guage for quantum computing_ . _arXiv:1402.4467 [quant-ph]_ . [23] Mingsheng Ying (2011): _Floyd–hoare_ _logic_ _for_ _quantum_ _programs_ . _ACM_ _Transactions_ _on_ _Programming_ _Languages and Systems (TOPLAS)_ [33(6), p. 19, doi:10.1145/2049706.2049708.](http://dx.doi.org/10.1145/2049706.2049708) [24] Mingsheng Ying, Shenggang Ying & Xiaodi Wu (2017): _Invariants of quantum programs:_ _characterisations_ _and_ _generation_ . In: _Proceedings_ _of_ _the_ _44th_ _ACM_ _SIGPLAN_ _Symposium_ _on_ _Principles_ _of_ _Programming_ _Languages_ [, ACM, pp. 818–832, doi:10.1145/3093333.3009840.](http://dx.doi.org/10.1145/3093333.3009840)

섹션 본문 104문단 · 32,371자

(제목 없음)

1We describe an embedding of the QWIRE quantum circuit language in the Coq proof assistant.This allows programmers to write quantum circuits using high-level abstractions and to prove properties of those circuits using Coq's theorem proving features.The implementation uses higher-order abstract syntax to represent variable binding and provides a type-checking algorithm for linear wire types, ensuring that quantum circuits are well-formed.We formalize a denotational semantics that interprets QWIRE circuits as superoperators on density matrices, and prove the correctness of some simple quantum programs.

Introduction

2The last few years have witnessed the emergence of lightweight, scalable, and expressive quantum circuit languages such as Quipper [10] and LIQUi ⟩ [22].These languages adopt the QRAM model of quantum computation, in which a classical computer sends instructions to a quantum computer and receives back measurement results.Quipper and LIQUi ⟩ programs classically produce circuits that can be executed on a quantum computer, simulated on a classical computer, or compiled using classical techniques to smaller, faster circuits.Since both languages are embedded inside general-purpose classical host languages (Haskell and F#), they can be used to build useful abstractions on top of quantum circuits, allowing for general purpose quantum programming.

3As is the case with classical programs, however, quantum programs in these languages will invariably have bugs.Since quantum circuits are inherently expensive to run (either simulated or on a real quantum computer) and are difficult or impossible to debug at runtime, numerous techniques have been developed to verify properties of quantum programs.

4The first step towards guaranteeing bug-free quantum programs is ensuring that every program corresponds to a valid quantum computation, meaning that a simulator or quantum computer running that program will not crash.In many cases this property can be enforced using type systems, as in the quantum lambda calculus, which uses linear types and guarantees type safety [20].Along these lines, Proto-Quipper [19] adds linear types to a subset of Quipper, though this approach has not been extended to the full Quipper language.

5Beyond simply ensuring quantum mechanical soundness, we might wish to statically analyze specific programs or families of programs and prove that their semantics matches a formal specification.Doing so requires a formal semantics for programs, such as unit vectors or density matrices.The specification can then be verified using model-checking [9], Hoare logic [23], proof assistants [3], or other techniques.

6Finally, one may wish to verify that two particular programs (or program fragments) have the same semantics.LIQUi ⟩, in particular, has focused on efficient compilation of quantum circuits; similar projects have explored verified compilation passes in the case of reversible circuits [1].

7The QWIRE programming language [14] is a small quantum circuit language embedded in a classical host language, which provides three core features: (1) a platform for high level quantum computing, with the expressiveness of embedded languages like Quipper [10] and LIQUi ⟩ [22]; (2) a linear type system that guarantees that generated circuits are well-formed and respect the laws of quantum mechanics; and (3) a concrete denotational semantics, specified in terms of density matrices, for proving properties and equivalences of quantum circuits.

8In this paper we report on a ongoing effort to implement QWIRE in the Coq theorem prover [6] and formalize its denotational semantics, thereby providing a framework to formally verify the correctness of quantum programs and quantum program transformations. 1 We include two forms of the language, one using Coq variables and higher-order abstract syntax for ease of programming, and a simpler version without these features, for verification purposes, along with a simple translation from the former to the latter.We then use this bridge to prove properties about higher-order programs using QWIRE's denotational semantics.

9The paper makes the following contributions:

10• We implement the QWIRE programming language in the Coq proof assistant, incorporating features such as dependently-typed circuits and proof-carrying code;

11• We present a type checking algorithm for the linear type system of QWIRE using a representation of linear contexts based on the Linearity Monad [15];

12• We formalize a denotational semantics for QWIRE circuits, interpreting them as superoperators on density matrices [13]; and

13• We show how these semantics can be used to verify quantum programs, including a quantum coin flip, a protocol with dynamic lifting, and a simple unitary circuit.

14Throughout the paper, we will assume some level of familiarity with both functional programming and proof assistants in the style of Coq or Agda.Readers unfamiliar with either of these concepts should consult the the introductory chapter of Software Foundations [17], which introduces the Coq language and theorem prover [6] used here.

Introduction to QWIRE programming

15We start with some examples of QWIRE circuits implemented in Coq.The following circuit implements a quantum coin flip: 2Definition coin_flip : Box One Bit.box_ () ⇒ gate_ x ← init0 @(); gate_ y ← H @x; gate_ z ← meas @y; output z.

0⟩ H

16The type Box One Bit is the high-level interface to QWIRE circuits, representing a "boxed" circuit with no input wires (represented by the unit type One) and a Bit-valued output wire.We use Coq notations and tactics to construct the circuit.The box_ tactic creates a boxed circuit from a function that takes an input pattern of wires and produces a circuit that uses those wires; box_ also calls the linear type-checker on the result, to ensure that the circuit is well formed.In this example, the coin_flip circuit has no inputs, so the input pattern to the box is the empty pattern ().

17The body of the circuit is made up of a sequence of gate applications, where init0, H, and meas are all gates.These gates are applied to input patterns, whose types are determined by the gate being applied.In coin_flip, the pattern () has type One, x and y have type Qubit, and z has type Bit.The output of each gate is bound in the remainder of the circuit, and the circuit is terminated by an output statement.

18QWIRE ensures that wires in a circuit are treated linearly, meaning that every wire is used exactly once as input and once as output.For example, the linear type system enforces the no-cloning property, rejecting the following bogus circuit.

19box_ w ⇒ output (w,w).(* Linear type checker cannot be satisfied.*) Abort.

20QWIRE is compositional, meaning that a boxed circuit can be reused inside another circuit.Notice that only wires are treated linearly in QWIRE; a boxed circuit is a first-class Coq expression, and can be used as many times as necessary.Consider the following circuit, which flips a coin up to n times, returning 1 if and only if the coin always lands on heads, which occurs with probability 1 2 n .

21Fixpoint coin_flips (n : N) : Box One Bit.box_ () ⇒ match n with (* n:N is either 0 or the successor of some n' *) | 0 ⇒ gate_ x ← new1 @(); output x | S n' ⇒ let_ c ← unbox (coin_flips n') (); gate_ q ← init0 @(); gate_ (c,q) ← bit_ctrl H @(c,q); gate_ () ← discard @c; gate_ b ← meas @q; output b end.

22The unbox operator feeds the input pattern () into the boxed circuit coin_flips n', and the let_ operation binds the output of the recursive call in the rest of the circuit.The let_ and unbox operations are separate constructs; for example, box_ w ⇒ unbox b w is just the η-expansion of a boxed circuit b.

23QWIRE also allows for dynamic lifting [14], in which measurement results from the quantum circuit are dynamically processed as classical data inside of Coq.This lift operation measures a qubit (or any collection of wires) and produces a boolean (or a tuple of booleans).We use the result of the measurement to decide which gates to apply in the remainder of the circuit.The following variation on coin_flips uses dynamic lifting and calls the unary coin_flip circuit from the start of this section.

24Fixpoint coin_flips' (n : N) : Box One Bit.box_ () ⇒ match n with | 0 ⇒ gate_ q ← new1 @(); output q | S n' ⇒ let_ q ← unbox (coin_flips' n') (); lift_ x ← q; if x then unbox coin_flip () else gate_ q ← new0 @(); output q end.

25Further examples can be found in the online development, as well as in the original QWIRE paper [14].

Implementing QWIRE in Coq

26At its core, a QWIRE circuit is a sequence of gates applied to wires.Each wire is described by a wire type W, which is either the unit type (has no data), a bit or qubit, or a tuple of wire types.In Coq we represent wire types as an inductively defined data type WType as follows:

27We use the Coq notation ``W 1 ⊗ W 2 '' for Tensor W 1 W 2 .

28Gates are indexed by a pair of wire types-a gate of type Gate W 1 W 2 takes an input wire of type W 1 and outputs a wire of type W 2 .In our setting, gates will include a universal set of unitary gates, as well as initialization, measurement, and control.The curly braces surrounding the type argument W indicate that W is an implicit argument, meaning that it can be automatically inferred from the other arguments.We further define U to be a coercion from unitaries to gates, meaning that for u : Unitary W, we can simply write u for U u : Gate W W.

29An open circuit (one with free input wires) has Coq type Circuit Γ W, where Γ is a typing context of input wires and W is an output wire type.As an example, the circuit gate_ w1' ← H @w1; gate_ w2' ← meas @w2; output (w2',w1') has type Circuit (w1:Qubit, w2:Qubit) (Bit ⊗ Qubit).

30A typing context of type Ctx is a partial map from variables (represented concretely as natural numbers) to wire types, which is represented as list (option WType).In this representation, the variable i is mapped to W if the ith element in the list is Some W , and is undefined if the ith element is None.

31The disjoint merge operation ⋓ ensures that the same wire cannot be used in two separate parts of a circuit.Mathematically, it is defined on two typing contexts as follows:

32Since disjoint merge is a partial function we represent it in Coq as a relation on possibly invalid contexts,

33For convenience, most operations on contexts are lifted to work with OCtx values, and so the type signature of the merge operation

34Wires in a context Γ can be collected into a pattern Pat Γ W to construct the wire type W. A pattern is just a tuple of wires of base types, meaning that all variables in a pattern have type Bit or Qubit.We use Coq's dependent types to express logical predicates that constrain how patterns can be constructed.

35Figure 1: Definition of QWIRE circuits using an explicit representation of variable binding.We call this type Circuit', reserving the name Circuit for the higher-order abstract syntax representation (Figure 2).

36The pair constructor (for which we use notation (p1,p2)) ensures that the wires in p1 are disjoint from those in p2 by calling out to the is_valid predicate, which checks whether the result of the merge is well-defined.The qubit and bit patterns are variable constructors that are only valid in contexts that contain the exact variable being introduced.The predicate SingletonCtx x W Γ ensures that the context Γ contains only the single wire x of type W .

37Circuits Like patterns, circuits are indexed by an input context and an output type.There are only three syntactic forms for circuits: output, gate application, and dynamic lifting.Figure 1 defines circuits as an inductive data type indexed by the input typing context and the output wire type.

38An output circuit output p is just a pattern.A gate application, which we write with syntactic sugar as gate_ p2 ← g @p1; C, is made up of a gate g : Gate W 1 W 2 , an input pattern p1 : Pat Γ 1 W 1 , an output pattern p2 : Pat Γ 2 W 2 , and a circuit C : Circuit' (Γ 1 ⋓ Γ 2 ) W'.The intended meaning is that p1 is the input to the gate g, and its output is bound to p2 in the continuation C. Thus the variables Γ 2 that made up p2 are also free in C, whereas the variables Γ 1 in p1 are no longer available in C, enforcing linearity.The is_valid predicates enforce that all of the context arguments to circuits must be well-defined.

39The lift operation, which we write lift_ x ← p; C takes as input a pattern p : Pat Γ 1 W and a function fun x ⇒ C that takes the classical interpretation of that data and produces another circuit.The intended semantics is that the circuit will measure the wires p (if they are not already bit-valued) and pass the result as ordinary Coq data to the function.In particular, both bits and qubits will result in boolean values being provided to the continuation, and tensors will be interpreted as pairs.

40A boxed circuit, written box_ p ⇒ C is a pair of a pattern and a circuit.

41Composition and Higher-Order Abstract Syntax The minimal embedding of QWIRE in Figure 1 lacks a number of features of the language, including composition and unboxing.The unbox operator takes in a boxed circuit and an input pattern and produces a circuit.It has the following signature: The traditional solution to this problem involves defining a number of substitution functions and proving appropriate typing relations for each operation.Although this approach is viable, it is often tedious and introduces a large amount of complexity, especially in linear systems.An alternative approach is the technique of higher-order abstract syntax (HOAS) [16], in which variable bindings in an embedded language are represented as functions in the host language.This means that the language designer does not have to define substitution functions and prove their correctness, and that variables in the embedded language have the same weight as variables in the host language.

42The HOAS approach to boxed circuits treats a box as a function from patterns to circuits:

43The unbox operation then simply destructs the box and applies the function appropriately.

44We can take a similar approach for the binding pattern in gate application, as shown in Figure 2. In this setting, output of a gate application is represented by a function from the output pattern to a new circuit.The other difference between the HOAS presentation of circuits and the "flat" representation Circuit' in the previous section is that in addition to proofs of validity about merged contexts, we introduce fresh arguments for the output context of each circuit.This is due to a technical limitation of Coq pattern matching, and the result makes it possible for us to write the examples in Section 2.

45With this machinery in place, we can define composition as a meta-operation on circuits.For conciseness, we give its definition as a sequence of β -reduction rules, omitting the proof arguments.

46The QWIRE type checker In type-checking QWIRE circuits, we are asked to solve equations of the form

47, in order to enforce the linearity of wires.

48The first of these goals can be discharged with a automated proof tactic for solving systems of commutative monoids.In the higher-order abstract syntax version of circuits, these predicates may also contain evars, existentially quantified Coq variables.By starting at the leaves of the typing derivation, we can ensure that every equation of the form Γ 1 ⋓ ⋯ ⋓ Γ n = Γ 1 ' ⋓ ⋯ ⋓ Γ m ' has at most one evar.We can then cancel out all variables that appear on both sides of the equation, and unify the evar with what remains on the opposite side.Once repeated applications of monoid have replaced all existential variables with regular Coq variables, we can prove goals of the form is_valid (Γ 1 ⋓ ⋯ ⋓ Γ n ).We take advantage of the fact that a set of finite contexts is disjoint if and only if the contexts are all pairwise disjoint.Our custom tactic for solving these goals extracts all proofs of pairwise disjointness from hypotheses and then reduces the goal to a conjunction of pairwise disjointness claims is_valid(Γ i ⋓ Γ j ).It then applies proofs of these claim, where available, from the hypotheses.

The Matrix Library

49The denotational semantics of QWIRE is implemented using a matrix library created specifically for this purpose.Matrices are simply functions from pairs of natural numbers to complex numbers. 4efinition Matrix (m n :

50The arguments m and n, which are the dimensions of the matrix, are not used directly in the definition, but they are useful to define certain operations on matrices, such as the Kronecker product and matrix multiplication, which depend on these dimensions.They are also useful as an informal annotation that aids the programmer.We say a matrix is well-formed when it is zero-valued outside of its domain.

Definition WF_Matrix {m n} (M : Matrix m n) : P

51This library is designed to facilitate reasoning about and computing with matrices.Treating matrices as functions allows us to easily express otherwise complicated matrix operations.Consider the definitions of Kronecker product (⊗) and complex conjugate transpose ( †), where Cconj is the complex conjugate: We represent complex numbers using an adaptation of Coquelicot's Complex library [4].This representation has the advantage of being a straightforward extension of the Coq real numbers, allowing us to easily extend the Coq Standard Library's powerful Linear Real Arithmetic solver (lra) to both complex numbers and matrices.The tactics we define (termed clra for complex numbers and mlra for matrices) allow us to trivially prove that complex conjugate and complex conjugate transpose are involutive:

Density Matrices

52QWIRE programs are interpreted as superoperators over density matrices, following the denotational semantics described by Paykin et al. [14].We use a density matrix representation over the standard unit vector representation (used for example by Boender et al. [3]) because density matrices represent probability distributions (introduced via measurement) over quantum states directly, as opposed to the unit vector representation which must be embedded inside a probability monad.

53We start with some preliminary definitions.A unitary matrix is a well-formed square matrix A such that A † × A is the identity.

Definition is_unitary {n} (

54A pure state of a quantum system is one that corresponds to a unit vector φ ⟩.An equivalent representation is that of square matrices ρ such that ρ × ρ = ρ.

55Definition Pure_State {n} (ρ : Matrix n n) :

56A density matrix, or mixed state, is a linear combination of pure states representing the probability of each pure state.

57Inductive Mixed_State {n} (ρ : Matrix n n) :

58Note that every mixed state is also well-formed, since scaling and addition preserve well-formedness.

59A superoperator is a function on square matrices that takes mixed states to mixed states.Any m × n matrix can be lifted to a superoperator from n to m as follows:

60Definition super {m n} (A : Matrix m n) : Superoperator n m := fun ρ ⇒ A × ρ × A †.

Denotation of QWIRE

61Types, Contexts and Gates In order to interpret circuits as superoperators over density matrices, we will also give types, contexts, gates, and patterns interpretations in linear algebra.For clarity we write − for the denotation of a variety of QWIRE objects, which we express via a Coq type class.

62Class Denote source target := { denote : source → target }.Notation " x " := (denote x) (at level 10).

63We interpret every wire type as the number of primitive (Bit or Qubit) wires in that type, so Qubit ⊗ (One ⊗ Bit) = 2. Contexts and OCtxs are similarly denoted by the number of wires they contain.

64Every gate of type Unitary W corresponds to a unitary matrix of dimension 2 W × 2 W .We omit the implemented gate set and their corresponding matrices here, but in the development we prove that every denoted unitary satisfies the is_unitary predicate defined above.

65Lemma unitary_gate_unitary : ∀ {W} (u : Unitary W), is_unitary u .

66A gate of type Gate W1 W2 corresponds to a superoperator as follows:

67When applying a gate to a subset of a quantum system, however, we will need a generalization of the denote_gate operation that applies the gate to the first part of a quantum system.

68What we previously wrote as denote_gate is simply denote_gate' 0.

69Patterns and Flat Circuits Patterns of type Pat Γ W are interpreted as permutation matrices of dimension 2 Γ × 2 W .These matrices are constructed via multiple applications of a swap matrix, as follows: In general we will want to swap the positions of two arbitrary qubits in a system; to swap qubit 0 with qubit 2 in a 3-qubit system, we invoke swap2 0 2 = (

70First, every pattern is interpreted as a list of indices indicating the wire number (counting from 0) that each variable refers to.So the pattern (3,0) : Pat [Some Qubit, None, None, Some Bit] (Bit ⊗ Qubit) corresponds to the list [1;0], because variable 3 corresponds the wire numbered 1 and variable 0 corresponds to wire 0 in the circuit.This list ls is then turned into an association list [(0,1);(1,0)], mapping variable i to ls[i].These pairs are then interpreted as a series of calls to swap2.

71As for gates, the function denote_pat_in follows a similar algorithm, but allows us to interpret a pattern p inside a larger context of variables.Its signature is as follows:

72Finally, we interpret circuits as superoperators on density matrices.To do this, we start with the "flat" representation Circuit' that does not use higher-order abstract syntax.The higher-order abstract syntax representation is useful for defining the meta-level operations on circuits, but the "flat" circuits from Figure 1 use concrete variables, thus making them a useful intermediate representation for the semantics.

73Consider an output circuit output' p, where p : Pat Γ W. The interpretation of this circuit is the superoperator obtained from the denotation of p: output' p = super p .

74Next, consider gate_ p2 ← g @p1; C, where g :

75which we obtain by composing denote_gate' with the results of denote_pat_in, to rearrange the quantum system appropriately:

76Finally, consider a lift circuit, lift' p f, where p : Pat Γ 1 W and f : interpret W → Circuit' Γ 2 W'.When W is a qubit, interpret W = bool, and the lift operation would measure the qubit and sum over the results of f true and f false .More generally, consider

77Here, kets {W} : interpret W → Matrix 2 ∧ W 1 is the basis representation of the input value of type interpret W. By transposing kets x and expanding it via the super operation, we pick out the partial density matrix corresponding to that measurement branch.Next, since all wire types are finite, we can enumerate all values of type interpret W in a list via the operation get_interpretations W. By mapping f' over this list, we obtain each of the actual measurement branches as superoperators.Now we can simply perform pointwise addition of the superoperators, and compose with the pattern p to organize the wires in order:

78A flat box is also interpreted as a superoperator: box' p C = C ○ super p † HOAS Circuits To denote the HOAS version of circuits, we first map them to our representation of "flat" circuits, which involves instantiating the output patterns of HOAS gates with a particular concrete pattern.We do this via an operation fresh pat that takes as input a context and a type, and produces a pattern of that type whose domain (fresh_pat_ctx) is disjoint from the input context.

79Definition fresh_pat (Γ : OCtx) (W : WType) : Pat (fresh_pat_ctx Γ W) W.

80Using fresh_pat we can define a function that converts HOAS circuits and boxes into flat circuits and boxes.We leave off implicit proof and OCtx arguments for legibility.The denotation of a HOAS circuit is exactly the denotation of its corresponding flat circuit.

A Taste of Verification

81When a circuit is closed, that is when it has no input, it represents a preparation of a quantum state.In many cases, a programmer may know what state their program should prepare, and our verification framework allows them to compare the denotation of the circuit with the desired density matrix directly.Consider, for instance, the coin flip circuit in Section 2. In the online development we prove that the denotation of coin_flip corresponds to the matrix even_toss= The Coq proof script used to prove this lemma is only twelve lines long, and calls out to a number of specialized tactics, including Msimpl, Csimpl and Csolve, designed to simplify and prove equality between matrices and complex numbers.

82The combination of Coq and QWIRE truly shines in its ability to prove more complex properties and prove properties of families of circuits, not merely circuits themselves.For example, consider the Deutsch-Jozsa problem, where we want to verify a property of a family of circuits with any number of input qubits, that holds for every appropriate unitary matrix U f .This sort of property requires induction over the number of inputs and the full power of a proof assistant.

83Open circuits, which correspond to arbitrary superoperators, are even more interesting from the perspective of verification.Consider the following circuit which composes a unitary gate with its transpose:

84Definition unitary_trans {W} (U : Unitary W) : Box W W. box_ p ⇒ gate_ p ← U @p; gate_ p ← transpose U @p; output p. Lemma unitary_trans_id : ∀ W (U : Unitary W) ρ,

85This correctness of this circuit holds for any unitary gate regardless of its input size.The proof of this lemma takes fewer than 20 lines of code, and proceeds using common facts from linear algebra, such as the fact that Id n × A = A, along with fact that the denotation of U is in fact unitary, that is, U † U = Id.

86We also rely on the fact that the denotation of every pattern in this circuit is actually an identity matrix.

87Definition lift meas : Box Qubit Bit.box_ q ⇒ lift_ x ← q; gate_ p ← (if x then new1 else new0) @(); output p. Lemma lift meas_correct : ∀ ρ, WF_Matrix ρ → lift meas ρ = boxed_gate meas ρ.

88Another useful sanity check ensures that the denotation of a lift circuit is equivalent to applying a measurement gate.For example, consider lift meas, which measures a qubit via lift and then reconstructs a bit-valued wire from the result of the measurement.The resulting circuit is equivalent to the circuit that simply applies a measurement gate.

Related and Future Work

89The Coq development described in this paper is very much still a work in progress.There are a small number of lemmas in the underlying matrix library that we have not yet formally proved, although they are known facts about linear algebra.In addition, we have not yet formally proved that the denotation of every circuit is a well-formed superoperator over density matrices, or that the denotation of the composition of two circuits is equal to the composition of their denotations.

90After completing this work, there are a number of exciting areas to explore in the future.

91Verified Compilation By verifying the equivalence of circuit transformations like unitary_trans_id from Section 5, we see QWIRE as a prime language in which to compile quantum programs.The area of verified compilers has gained a lot of traction in recent years, inspired by the success of the CompCert C compiler [12].Towards this direction, Amy et al. [1] developed a verified, lightly-optimizing compiler for reversible circuits, which can encompass unitary quantum circuits.We can also provide insight into a relationship between QWIRE circuits and QASM [7], a quantum assembly-like language that has gained widespread use in quantum simulators and IBM's Quantum Experience [11].The largest difference between QWIRE and QASM is our use of variable binding, using abstract variables such as x, y, and z and allowing qubits to be renamed as in let_ y ← output x; C'.In comparison, QASM operates on a concrete set of quantum registers (e.g., qubits 1, 2, or 3) that cannot be renamed.In the development we provide a version of QWIRE that similarly operates directly on named qubits.Our denotational semantics extends directly to this representation, and we can compile from QWIRE to these "assembly-level" circuits.Future work could formally establish a relationship between the assembly-level version of QWIRE and QASM, allowing us to prove properties of QASM programs, and prove that compilation is sound with respect to the denotational semantics.

92A More Efficient Backend Unfortunately, we have so far struggled to prove properties of (nonparametric) circuits with more than a few qubits, indicating that scalability will be a challenge moving forward.A key contributor to the scalability of our theorems is the underlying matrix library, which is not currently optimized in any significant way.Of course, any quantum simulator will be intractable on large enough circuits, as density matrices are always exponential in the size of the corresponding circuit.

93In the near future, we will be transitioning the linear algebra back-end of our development to one of several existing projects, in the hopes of making it more efficient.Boender et al. [3] present one candidate, a library developed reasoning about the correctness of quantum protocols using pure states.Another candidate is the Coq Effective Algebra Library (CoqEAL) [5], which is designed specifically to allow matrix computation inside Coq.This library allows easy translation between it matrices and those of the Mathematical Components library [2], which in turn was designed with an eye towards verification.The two libraries together may substantially increase our ability to run and reason about QWIRE programs.We can also simulate QWIRE programs by extraction to OCaml.

94Theory of QWIRE In this paper we focus on the denotational semantics of QWIRE, but many other aspects of the meta-theory are left to be explored.Rennela and Staton [18] present a categorical model of EWire, a close variant of QWIRE, as an enriched category.Their model also allows for additional features such as quantum data types in the style of Quipper [10].

95The equational theory of quantum circuits is another area left for future work.For example, Staton [21] presents an axiomatization of the relationship between measurement, qubit initialization, and a limited set of unitary gates.In the future we hope to adapt Staton's work to QWIRE and thereby reason syntactically, rather than semantically, about the equivalence of QWIRE circuits.An equational theory is also key to integrating QWIRE with dependent types [14].

96Verifying Higher-Order Programs The use of dependent types was a driving factor for both the development of QWIRE and the choice to embed it in Coq specifically.While dependent types power Coq's verification capabilities, they're also key to our representation of circuits.For example, in the development we implement a dependently-typed version of the Quantum Fourier Transform, as described in the introduction to QWIRE [14].This paper hasn't focused on dependent types, or high level programming in QWIRE generally, but both will feature heavily in future QWIRE development.

97Greater use of the host language and classical programming abstractions will also allow to further push the boundaries of quantum verification.While current QWIRE programs can be thought of as "simply circuits," such direct proofs will become increasingly difficult as we add on layers of abstraction-just as it would be nearly impossible to prove interesting program properties by reasoning about the underlying classical circuits.This will require new approaches to quantum verification, from quantum weakest precondition reasoning [8] (expanded into a Hoare-like logic in [23,24]), to the forms of equational reasoning described above.

Figure 2 :

98Figure2: A definition of QWIRE circuits using higher-order abstract syntax.

99Definition kron {m n o p} (A : Matrix m n) (B : Matrix o p) : Matrix (m*n) (o*p) := fun x y ⇒ A (x o) (y p) * B (x mod o) (y mod p).Definition ctrans {m n} (A : Matrix m n) : Matrix n m := fun x y ⇒ Cconj (A y x).

100Lemma conj_involutive : ∀ (c : C), Cconj (Cconj c) = c.Proof.intros.clra.Qed.Lemma ctrans_involutive : ∀ {m n} (A : Matrix m n), A † † = A. Proof.intros.mlra.Qed.

Definition

101Superoperator m n := Matrix m m → Matrix n n.Definition WF_Superoperator m n (f : Superoperator m n) := ∀ (ρ : Matrix m m), Mixed_State ρ → Mixed_State (f ρ).

Program

102Fixpoint from_HOAS {Γ W} (c : Circuit Γ W) : Circuit' Γ W := match c with | output p ⇒ output' p | gate g p1 f ⇒ gate' g p1 p2 (from_HOAS (f (fresh_pat _ _))) | lift p f ⇒ lift' p (fun x ⇒ from_HOAS (f x)) end.Program Definition from_HOAS_Box {W 1 W 2 } (b : Box W 1 W 2 ) : Flat_Box W 1 W 2 := match b with box f ⇒ let p := fresh_pat [] W 1 in box' p (from_HOAS (f p)) end.

103Lemma flip_toss : coin_flip (Id 1) = even_toss.

1043

참고문헌 18건

  1. The Coq proof assistant : reference manual, version 6.1 1997
  2. LIQUi|>: A Software Design Architecture and Domain-Specific Language for Quantum Computing. arXiv (Cornell University) · Cornell University · 2014
  3. <i>Quantum Computation and Quantum Information</i> American Journal of Physics · American Institute of Physics · 2002 10.1119/1.1463744
  4. QMC: A Model Checker for Quantum Systems Lecture notes in computer science · Springer Science+Business Media · 2008 10.1007/978-3-540-70545-1_51
  5. Algebraic and Logical Methods in Quantum Computation arXiv (Cornell University) · Cornell University · 2015 10.48550/arxiv.1510.02198
  6. Higher-order abstract syntax ACM SIGPLAN Notices · Association for Computing Machinery · 1988 10.1145/960116.54010
  7. Floyd--hoare logic for quantum programs ACM Transactions on Programming Languages and Systems · Association for Computing Machinery · 2011 10.1145/2049706.2049708
  8. Algebraic Effects, Linearity, and Quantum Programming Languages 2014 10.1145/2676726.2676999
  9. Quantum weakest preconditions Mathematical Structures in Computer Science · Cambridge University Press · 2006 10.1017/s0960129506005251
  10. Quipper 2013 10.1145/2491956.2462177
  11. Formalization of Quantum Protocols using Coq arXiv (Cornell University) · Cornell University · 2015 10.4204/eptcs.195.6
  12. Quantum Lambda Calculus Cambridge University Press eBooks · Cambridge University Press · 2009 10.1017/cbo9781139193313.005
  13. QWIRE: a core language for quantum circuits 2016 10.1145/3009837.3009894
  14. The linearity Monad 2017 10.1145/3122955.3122965
  15. Classical Control and Quantum Circuits in Enriched Category Theory Electronic Notes in Theoretical Computer Science · Elsevier BV · 2018 10.1016/j.entcs.2018.03.027
  16. Quantum Computation and Quantum Information Cambridge University Press eBooks · Cambridge University Press · 2012 10.1017/cbo9780511976667
  17. Invariants of quantum programs: characterisations and generation ACM SIGPLAN Notices · Association for Computing Machinery · 2017 10.1145/3093333.3009840
  18. Higher-order abstract syntax Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation · 1988 10.1145/53990.54010