Lectures onType Theory
Chapter 185
Chapter 185Optional

Typed Quantum Circuits and Proto-Quipper-M

Prerequisites. Direct starred prerequisites: Chapter 184. No later core chapter depends on this route.

A program of the quantum lambda calculus of chapter 184 owns quantum data. Its configuration [Q,L,M] contains a register, and reducing the term changes that register. Suppose instead we want a program that describes a circuit: a value that can be stored, printed, applied twice, or transformed, without executing anything. The first attempt is to write a function that produces a term, but the calculus of chapter 184 has no type of circuits. The only way to apply a gate there is to reduce a configuration, and the result is a changed register rather than a description.

The obstruction sharpens once the description is parameterized. Consider the family repn := “apply the gate H to the wire n times”, whose size is selected by a classical natural number. Two facts must hold simultaneously: n may be inspected, duplicated, and compared, since it controls a recursion; and the wire may not be duplicated, by theorem 184.4. A single linear context cannot carry both. Typing n linearly forbids the recursion that reads it twice — once for the test and once for the recursive call — while typing the wire duplicably reintroduces cloning.

The calculus of this chapter resolves this by splitting the judgment: one context of parameters, available at circuit-generation time and freely duplicable, and one context of states, the wires, which are linear. The splitting is not a convention imposed on the programmer; it is forced by the two requirements just displayed, and every construct of the language is placed on one side of it.

Parameters and states

Definition 185.1 — Types

Fix a set of wire type constants α. The types of Proto-Quipper-M are A,B ::= α0A+BIABAB::=  !AnatlistACirc(T,U),P,R ::= 0P+RIPR !A::= natlistPCirc(T,U),T,U ::= αITU. The P,R are the parameter types, a subgrammar of the types; the T,U are the simple types, which describe wire bundles. Note what each grammar omits: a parameter type never contains a bare wire constant α or a linear function, and a simple type contains no !, no sum, and no circuit.

The three grammars encode the split. A value of parameter type exists at generation time and may be duplicated; a value of simple type is a bundle of wires in the circuit being built; and Circ(T,U), itself a parameter type, is the type of a completed circuit from interface T to interface U — a description, duplicable precisely because it is not a wire.

Definition 185.2 — Terms, values, and configurations

Terms and values are M,N ::= xclet x=M in NleftA,BMrightA,BM case M of {left xNright yP}M;NM,N let x,y=M in NλxA.MMNlift Mforce M boxTMapply(M,N)(,C,),V,W ::= xcleftA,BVrightA,BVV,W λxA.Mlift M(,C,), where ranges over labels, the names of wires, and (,C,) is a boxed circuit: a circuit C together with its input and output label tuples. A label context Q assigns a wire type constant to each of finitely many labels. A configuration is a pair (C,M) of a circuit under construction and a term. Typing judgments have the form Φ;QM:A, with Φ a context of parameter types and Q a label context.

Definition 185.3 — Labelled circuits

Let M be a symmetric monoidal category (definition 184.25) together with an interpretation [[]] of wire type constants as objects, extended to label contexts by [[Q]]:=Q[[Q()]]. The category ML of labelled circuits has label contexts as objects and, as morphisms QQ, the morphisms [[Q]][[Q]] of M. Identities and composition are those of M, and the symmetric monoidal structure is the unique one making [[]]:MLM symmetric monoidal; for label contexts with disjoint domains, QQQQ.

Nothing about quantum mechanics has been used. M is an arbitrary symmetric monoidal category, and a “circuit” is a morphism in it; the quantum instance is one choice among many, and it is deferred to section 185.4 so that the theorems below are visibly independent of it.

Boxing and unboxing

The phase distinction determines two operations. A circuit is built by running a function on fresh wires and recording what happened; that is boxing. A recorded circuit is inserted into the circuit under construction; that is application.

Definition 185.4 — The two circuit operations

The typing rules for the two circuit constructs are Φ;M:!(TU)Φ;boxTM:Circ(T,U), Φ;Q1M:Circ(T,U)Φ;Q2N:TΦ;Q1,Q2apply(M,N):U, together with the rules for the modality, Φ;M:AΦ;lift M:!A,Φ;QM:!AΦ;Qforce M:A.

Three side conditions carry the discipline, and each is visible in the rules. The argument of box is typed in an empty label context, so a circuit description may not capture a wire of the surrounding circuit; that is the exact prohibition that makes Circ(T,U) a parameter type. The argument is also duplicable, of type !(TU), so that boxing may run it on freshly generated labels. And apply splits the label context, so that the wires fed to a circuit are not also used elsewhere.

Definition 185.5 — Circuit generation

Evaluation is a big-step relation (C,M)(C,V) on configurations. The rule for boxing is (idQ, (force M))(D,V)(C,boxTM)(C,(,D,V)), where Q is a fresh label context of shape T and its labels: the body is run on a separate circuit that starts as the identity, and the resulting circuit D is packaged as a value. The rule for application appends the recorded circuit to the circuit under construction, renaming its labels to the supplied ones. The remaining rules are the call-by-value rules for the functional constructs, leaving C untouched.

Example 185.6 — A family of circuits selected by a number

Let α be a wire constant and let g:!(αα) be a gate. Define, by recursion on a parameter of type nat, rep:=λnnat. lift (λwα. case n of {0w  m+1(force g)w}),w:=(force(rep m))w, so that rep:nat !(αα), and put repbox:=λnnat. boxα(rep n) of type natCirc(α,α).

The variable n is used twice in the body — in the test and in the recursive call — which is permitted because nat is a parameter type and n is declared in Φ. The variable w is used once, which is enforced because α is a wire type and w is declared in the label context. This is the split announced in the chapter opening, now visible in a single derivation.

Evaluating repbox 3 generates a fresh label 0 of type α, runs the body on it, and records the three gate applications: D = 0 g 1 g 2 g 3, so that the value is the boxed circuit (0,D,3) of type Circ(α,α). Its typed interface is one input wire of type α and one output wire of type α, independently of n; the number 3 is visible in the size of D and not in its type. A type that recorded the width would have to mention a term, and no type of definition 185.1 does.

Example 185.7 — A rejected term

The term λwα. boxα(lift(λvα.w)) is not typable. The body of the box must be typed in the empty label context by definition 185.4, but it mentions w, which is in the label context. The rejection is exactly right: the boxed circuit would be a description that secretly refers to a wire of the surrounding circuit, and applying it twice would use that wire twice.

Exercise 185.1

★☆☆ Compute the type and the generated circuit of repbox 0 and repbox 1, and state which part of the answer depends on n and which does not.

Exercise 185.2

★★☆ For each of the following terms, say whether it is typable and name the premise that decides the question: (a) λnnat.n,n;(b) λwα.w,w; (c) λcCirc(α,α).c,c;(d) λwα.apply(repbox 2,w).

Safety and soundness

Definition 185.8 — Well-typed configuration

Let Q,Q be label contexts. A configuration (C,M) is well typed with input labels Q, output labels Q, and type A, written Q(C,M):A;Q, when there is a label context Q disjoint from Q with C:QQQ in ML and ;QM:A.

The definition says exactly which wires belong to whom: the circuit built so far consumes the input labels and produces QQ, of which the term owns Q and the environment owns Q.

Theorem 185.9 — Safety and soundness for Proto-Quipper-M; imported

Let Q(C,M):A;Q be well typed.

  1. (Subject reduction) If (C,M)(C,V) then Q(C,V):A;Q.

  2. (Error freeness) (C,M)⇓̸Error.

  3. (Termination) There are C and V with (C,M)(C,V).

  4. (Soundness) Interpreting a well-typed configuration by [[(C,M)]] := [[Q]] C [[QQ]]  [[Q]][[Q]] [[M]]id [[A]][[Q]], we have [[(C,M)]]=[[(C,V)]] whenever (C,M)(C,V).

  5. (Computational adequacy) At the observable types, equality of denotations implies that the two configurations evaluate to the same boxed circuit.

Clauses (i)–(v) are Propositions 5.2, 5.3, 5.4, 5.6, and 5.7 of Rios and Selinger, A Categorical Model for a Quantum Circuit Description Language, arXiv:1706.02630, which states them and omits their proofs. The proofs are in Rios’s thesis, On a Categorically Sound Quantum Programming Language for Circuit Description, Dalhousie University, 2021, where subject reduction is Theorem 6.4.2, error freeness is Theorem 6.5.1, and soundness is Theorem 9.2.1, the last proved by rule induction over the evaluation relation with the cases grouped as axioms, inductive rules, substitution rules, and circuit rules. This book imports the five statements at exactly the signature displayed above, with the thesis as the proof owner; the extended abstract alone does not discharge them, and no statement here is inferred from the implementations named in section 185.4.

Lemma 185.10 — The boxing case of soundness

Let ;M:!(TU) and suppose (idQ,(force M))(D,V) for a fresh label context Q of shape T. Then [[(C,boxTM)]]=[[(C,(,D,V))]].

Proof of Lemma 185.10 — The boxing case of soundness

Proof. Both sides have the form [[C]] followed by [[N]]id for the respective terms N, and C is unchanged by the boxing rule, so it suffices to prove [[boxTM]]=[[(,D,V)]] as morphisms I[[Circ(T,U)]]. By definition 185.4 the left side is the name in ML of the morphism [[T]][[U]] determined by M; by definition 185.5 the right side is the name of the morphism recorded by D together with the output labels named by V. The hypothesis (idQ,(force M))(D,V) and clause (iv) of theorem 185.9 applied to that smaller configuration — whose starting circuit is the identity — give [[(idQ,(force M))]]=[[(D,V)]], and the left side of that equation is exactly the morphism named by [[boxTM]]. Taking names of equal morphisms gives the claim. ◻

Lemma 185.10 is not an independent proof of clause (iv): it uses that clause at a smaller configuration. It is written out because it exhibits the mechanism — boxing is the name of a morphism, and generation computes that morphism — which the statement of the theorem does not display.

Remark 185.11 — Five claims, kept apart

Circuit well-typedness (definition 185.8), denotational equality (clause (iv)), adequacy (clause (v)), physical correctness of the gates, and resource estimates for the generated circuit are five different statements. The first three are proved for this calculus by the owners named above. The fourth is not a statement about a language at all. The fifth is a statement about the size of D, which the type Circ(T,U) does not record: by example 185.6 circuits of every size share one type.

The generic theorem and its quantum instance

Every statement above holds for an arbitrary symmetric monoidal category M with a chosen interpretation of wire constants. The quantum instance is obtained by choosing M to be a category of quantum circuits: objects are finite tensors of wire types, and morphisms are circuits built from a fixed gate set, composed and tensored, taken up to the equations of the chosen circuit formalism. Nothing in theorem 185.9 changes; what changes is what a generated value means.

Remark 185.12 — Order of quantification

The generic theorem is proved first and instantiated afterwards, and the order matters. A proof that used a property special to quantum circuits — unitarity of each gate, say, or reversibility — would not establish theorem 185.9 for the classical instance, and would silently attach the language design to one physical model. Conversely, the generic theorem gives no quantum information: it does not say that the generated circuit implements a unitary, because M need not be a category of unitaries.

Implementations, and what they show. A native prototype implementation of Proto-Quipper-M exists, and a full-scale circuit-description language, Quipper, exists with worked developments of teleportation, the quantum Fourier transform, and arithmetic. Both are execution evidence: they show that circuits of the expected shape are produced on the expected inputs. Neither establishes any clause of theorem 185.9, and the prototype’s constructor and tooling coverage is incomplete. An independent mechanized treatment of a related Proto-Quipper core exists in a Hybrid/Coq development, and a separate circuit language, QWIRE, is mechanized in Coq; those developments prove statements about their own systems, and the trust boundary of a host proof assistant is part of what they prove. A modern Proto-Quipper variant adds reversing, control, and a with-computed construct; it is a bounded comparison and does not strengthen the endpoints imported here.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 185.3, then exercise 185.4; the implementation project exercise 185.6 may be attempted at any time.

Exercise 185.3

★★☆ Let swapbox:=boxαα(lift(λpαα.let u,v=p in v,u)). Derive its type, compute the generated circuit and its labelled interface, and then compute the circuit generated by the term that applies swapbox twice to a pair p. State which equation of definition 184.25 makes the second circuit equal to the identity in M, and whether the two generated circuits are equal as data.

Exercise 185.4

★★☆ Locate the parameter/state violation in each of the three terms boxα(lift(λwα.w,w)),λwα.lift w,λnnat.apply(n,n), naming for each the rule of definition 185.4 or definition 185.1 that fails.

Exercise 185.5

★★☆ Compare repbox 3 of example 185.6 with the term of chapter 184 that applies H three times to a qubit of a register. Say precisely what each one produces, what each one consumes, and why the first may be used twice while the second may not.

Exercise 185.6

★★★ Practical project.typed-circuit-builder Implement in Agda or Kappa a typed circuit builder for the fragment of definition 185.1, definition 185.2 containing labels, pairs, linear abstraction, lift, force, box, apply, and natural-number recursion at parameter types. Represent a circuit as a list of gate applications on labels together with input and output label tuples; represent typing contexts as a parameter context and a label context, and enforce that the label context is split at every binary rule and empty at every box.

The invariant to maintain is the one displayed after definition 185.4: the body of a box is typed in an empty label context, and no label occurs twice in the two halves of any context split. The concrete result is a function taking a closed term of type Circ(T,U) and returning the generated circuit together with its input and output label tuples.

Acceptance test. Golden generated circuits: repbox n for n{0,1,3} produces circuits with 0, 1, and 3 gate applications and in every case the interface (0:α)(n:α); swapbox of exercise 185.3 produces a circuit whose output tuple is the reverse of its input tuple; the term of example 185.7 is rejected, with the checker naming the empty-label- context premise; and boxα(lift(λwα.w,w)) is rejected, with the checker naming the context split. A mutation that types the body of box in the ambient label context must accept the term of example 185.7 and then produce a generated circuit whose input tuple does not match its declared interface, which the invariant check must report.

The program builds finite circuits over a fixed gate set. It illustrates theorem 185.9 and proves no clause of it, and a golden circuit is evidence about one input only.

Bibliographic notes

The calculus, the parameter/state distinction, the boxing and application constructs, the category ML of labelled circuits, and the five statements of theorem 185.9 are from Rios and Selinger, A Categorical Model for a Quantum Circuit Description Language, arXiv:1706.02630, whose Table 1 and Table 2 are the source of definition 185.1, definition 185.2 and whose Definition 4.2 is definition 185.3. That paper is an extended abstract and omits the proofs; the proof-complete development is Rios’s thesis, cited at theorem 185.9 with its theorem numbers. The comparison with an executing quantum term is the calculus of chapter 184.

Selinger’s survey of graphical languages for monoidal categories is the reference for the diagrammatic reading of a generated circuit, and Vicary’s notes for the categorical background. The Quipper tutorial [GLR^+13] contains the worked teleportation, Fourier transform, and addition developments that motivate circuit description at scale; the pinned Quipper release, the native Proto-Quipper-M prototype, the QWIRE development, and the Hybrid/Coq formalization of a Proto-Quipper core are retained in the reference library as implementation and comparison evidence. The modern Proto-Quipper with reversing and control [FKRS26] is a bounded comparison whose own conclusion leaves the combined dependent and modal semantics open.

Search the book

Type to search the local edition.