A Verified Optimizer for Quantum Circuits
Total Page:16
File Type:pdf, Size:1020Kb
A Verified Optimizer for Quantum Circuits Kesha Hietala Robert Rand Shih-Han Hung Xiaodi Wu Michael Hicks University of Maryland, College Park, USA Abstract An appealing solution to this problem is to apply rigorous We present voqc, the first fully verified compiler for quan- formal methods to prove that an optimization or algorithm tum circuits, written using the Coq proof assistant. Quan- always does what it is intended to do. As an example, con- tum circuits are expressed as programs in a simple, low-level sider CompCert [26], which is a compiler for C programs language called sqir, which is deeply embedded in Coq. Op- that is written and proved correct using the Coq proof as- timizations and other transformations are expressed as Coq sistant [9]. CompCert includes sophisticated optimizations functions, which are proved correct with respect to a seman- whose proofs of correctness are verified to be valid by Coq’s tics of sqir programs. We evaluate voqc’s verified optimiza- type checker. tions on a series of benchmarks, and it performs comparably In this paper, we apply CompCert’s approach to the quan- to industrial-strength compilers. voqc’s optimizations re- tum setting. We present voqc (pronounced “vox”), a verified duce total gate counts on average by 17.7% on a benchmark optimizer for quantum circuits. voqc takes as input a quan- of 29 circuit programs compared to a 10.7% reduction when tum program written in a language we call sqir (“squire”). using IBM’s Qiskit compiler. While sqir is designed to be a simple quantum intermediate representation, it is not very different from languages such Keywords Formal Verification, Quantum Computing, Op- as PyQuil [36], which are used to construct quantum source timization, Certified Compilation, Programming Languages programs as circuits. voqc applies a series of optimizations to sqir programs, ultimately producing a result that is com- 1 Introduction patible with the specified quantum architecture. For added Programming quantum computers will be challenging, at convenience, voqc provides translators between sqir and least in the near term. Qubits will be scarce, and gate pipelines OpenQASM, the de facto standard format for quantum cir- will need to be short to prevent decoherence. Fortunately, cuits [12]. (Section 2.) optimizing compilers can transform a source algorithm to Like CompCert, voqc is implemented using the Coq proof work with fewer resources. Where compilers fall short, pro- assistant. sqir program ASTs are represented in Coq via a grammers can optimize their algorithms by hand. deep embedding, and optimizations are implemented as Coq Of course, both compiler and by-hand optimizations will functions, which are then extracted to OCaml. We define two inevitably have bugs. For evidence of the former, consider semantics for sqir programs. The simplest denotes every that higher optimization levels of IBM’s Qiskit compiler are quantum circuit as a unitary matrix. However this is only known to have mistakes (as is evident from its issue tracker1). applicable to unitary circuits, i.e., circuits without measure- Kissinger and van de Wetering [24] discovered mistakes in ment. For non-unitary circuits, we provide a denotation of the optimized outputs produced by the circuit compiler by density matrices. Properties of sqir programs, or transforma- Nam et al. [30]. And Nam et al. themselves found that the tions of them, can be proved using whichever semantics is optimization library they compared against sometimes pro- most convenient. As examples, we have proved correctness duced incorrect results. Making mistakes when optimizing properties about several source programs written in sqir, by hand is also to be expected: as put well by Zamdzhiev including GHZ state preparation, quantum teleportation, su- [49], quantum computing can be frustratingly unintuitive. perdense coding, and the Deutsch-Jozsa algorithm. Generally Unfortunately, the very factors that motivate optimizing speaking, sqir is designed to make proofs as easy as possi- quantum compilers make it difficult to test their correctness. ble. For example, we initially contemplated sqir programs Comparing runs of a source program to those of its opti- accessing qubits as Coq variables via higher order abstract mized version may be impractical due to the indeterminacy syntax [32], but we found that proofs were far simpler when of typical quantum algorithms and the substantial expense qubits were accessed as concrete indices into a global register. involved in executing or simulating them. Indeed, resources (Section 3.) may be too scarce, or the qubit connectivity too constrained, We have implemented and proved correct several transfor- to run the program without optimization! mations over sqir programs. In particular, we have developed verified versions of many of the optimizations used in astate- 1 https://github.com/Qiskit/qiskit-terra/issues/2752 of-the-art compiler developed by Nam et al. [30]. We have Draft paper, November 22, 2019, also verified a circuit mapping routine that transforms sqir . programs to satisfy constraints on how qubits may interact 1 Draft paper, November 22, 2019, K. Hietala, R. Rand, S. Hung, X. Wu &M.Hicks on a particular target architecture. These transformations it is measured, at which point the outcome will be 0 with 2 2 were reasonably straightforward to prove correct thanks to probability ⋃︀α⋃︀ and 1 with probability ⋃︀β⋃︀ . Measurement is sqir’s design. (Sections 4 and 5.) not passive: it has the effect of collapsing the state to match We find that voqc performs comparably to unverified, the measured outcome, i.e., either ⋃︀0̃︀ or ⋃︀1̃︀. As a result, all state-of-the-art compilers when run on a benchmark of 29 subsequent measurements return the same answer. circuit programs developed by Amy et al. [5]. These pro- Operators on quantum states are linear mappings. These grams range from 45 and 61,629 gates and use between 5 and mappings can be expressed as matrices, and their application 192 qubits. voqc reduced total gate counts on average by to a state expressed as matrix multiplication. For example, 17.7% compared to 10.7% by IBM’s Qiskit compiler [2]. There the Hadamard operator H is expressed as a matrix ⌋︂1 1 1 . 2 (︀ 1 −1 ⌋︀ is still room for improvement: Nam et al. [30] produced re- 1 1 Applying H to state ⋃︀0̃︀ yields ∐︀ ⌋︂ ; ⌋︂ ̃︀, also written as ⋃︀+̃︀. ductions of 26.5% using additional optimizations we expect 2 2 Many quantum operators are not only linear, they are also we could verify. (Section 6.) unitary—the conjugate transpose (or adjoint) of their matrix voqc is the first fully verified circuit optimizer for a realis- is its own inverse. This ensures that multiplying a qubit tic quantum circuit language. Amy et al. [6] developed a ver- by the operator preserves the qubit’s sum of norms squared. ified optimizing compiler from source Boolean expressions Since a Hadamard is its own adjoint, it is also its own inverse: to reversible circuits, but did not handle general quantum hence H 0 . programs. Rand et al. [34] developed a similar compiler for ⋃︀+̃︀ = ⋃︀ ̃︀ A quantum state with N qubits is represented as vector quantum circuits but without optimizations. Other low-level of length 2N . For example a 2-qubit state is represented as a quantum languages [12, 42] have not been developed with vector α; β;γ;δ where each component corresponds to (the verification in mind, and prior circuit-level optimizations ∐︀ ̃︀ square root of) the probability of measuring 00 , 01 , 10 , [5, 20, 30] have not been formally verified. In concurrent ⋃︀ ̃︀ ⋃︀ ̃︀ ⋃︀ ̃︀ and 11 , respectively. Because of the exponential size of the work, Shi et al. [40] developed CertiQ, which uses symbolic ⋃︀ ̃︀ complex quantum state space, it is not possible to simulate a execution and SMT solving to verify some circuit transfor- 100-qubit quantum computer using even the most powerful mations in the Qiskit compiler. CertiQ is limited to verifying classical computer! correct application of local equivalences, rather than more N -qubit operators are represented general circuit transformations, and sometimes verification as 2N 2N matrices. For example, ⎨ ⎬ fails in which case CertiQ must validate the optimized ("trans- × ⎝ 1 0 0 0 ⎠ the CNOT operator over two qubits ⎝ ⎠ lated") circuits on-line. The PyZX compiler [24] likewise uses ⎝ 0 1 0 0 ⎠ is expressed as the matrix shown at ⎝ ⎠ translation validation to check its rewrites of circuits using ⎝ 0 0 0 1 ⎠ the right. It expresses a controlled ⎝ ⎠ the equational theory of the ZX-Calculus [8]. (Section 7.) ⎝ 0 0 1 0 ⎠ not operation—if the first qubit is 0 ⎪ ⎮ Our work on voqc and sqir constitutes a step toward ⋃︀ ̃︀ then both qubits are mapped to them- developing a full-scale verified compiler toolchain. Next selves, but if the first qubit is 1 then the second qubit is steps include developing certified transformations from high- ⋃︀ ̃︀ negated, e.g., CNOT 00 00 while CNOT 10 11 . level quantum languages to sqir and implementing opti- ⋃︀ ̃︀ = ⋃︀ ̃︀ ⋃︀ ̃︀ = ⋃︀ ̃︀ N -qubit operators can be used to create entanglement, mizations with different objectives, e.g., that aim to reduce which is a situation where two qubits cannot be described the probability that a result is corrupted by quantum noise. independently. For example, while the vector 1; 0; 0; 0 can All code we reference in this paper can be found online at ∐︀ ̃︀ be written as 1; 0 1; 0 where is the tensor product, https://github.com/inQWIRE/SQIRE. ∐︀ ̃︀ ⊗ ∐︀ ̃︀ ⊗ the state ⌋︂1 ; 0; 0; ⌋︂1 cannot be similarly decomposed. We ∐︀ 2 2 ̃︀ say that ⌋︂1 ; 0; 0; ⌋︂1 is an entangled state.