-
Notifications
You must be signed in to change notification settings - Fork 26
/
TypeChecking.v
366 lines (327 loc) · 14.9 KB
/
TypeChecking.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
Require Export HOASCircuits.
(** Projecting out elements of tensors **)
Open Scope circ_scope.
Definition wproj {W1 W2} (p : Pat (W1 ⊗ W2)) : Pat W1 * Pat W2 :=
match p with
| pair p1 p2 => (p1, p2)
end.
(*** Typechecking Tactic ***)
(* Prevent compute from unfolding important fixpoints *)
Open Scope circ_scope.
Opaque wproj.
Global Opaque merge.
Global Opaque Ctx.
Global Opaque is_valid.
Fixpoint pair_circ' {W1 W2} (p : Pat W1) (c2 : Circuit W2) : Circuit (W1 ⊗ W2) :=
match c2 with
| output p2 => output (pair p p2)
| gate g p1 f => gate g p1 (fun p2 => pair_circ' p (f p2))
| lift p1 f => lift p1 (fun x => pair_circ' p (f x))
end.
Fixpoint pair_circ {W1 W2} (c1 : Circuit W1) (c2 : Circuit W2) : Circuit (W1 ⊗ W2) :=
match c1 with
| output p1 => pair_circ' p1 c2
| gate g p1 f => gate g p1 (fun p2 => pair_circ (f p2) c2)
| lift p f => lift p (fun b => pair_circ (f b) c2)
end.
Notation "( x , y , .. , z )" := (pair_circ .. (pair_circ x y) .. z) (at level 0) : circ_scope.
(*** Notations ***)
Set Printing Coercions.
(* These tactics construct circuits by calling out to type_check *)
Notation letpair p1 p2 p c := (let (p1,p2) := wproj p in c).
Notation "'box_' p ⇒ C" := (box (fun p => C))
(at level 13) : circ_scope.
Notation "'box_' () ⇒ C" := (box (fun _ => C))
(at level 13) : circ_scope.
Notation "'box_' ( p1 , p2 ) ⇒ C" := (box (fun p => letpair p1 p2 p C))
(at level 13) : circ_scope.
Notation "'box_' ( p1 , p2 , p3 ) ⇒ C" := (box (fun p =>
let (y,p3) := wproj p in
let (p1,p2) := wproj y in C))
(at level 13) : circ_scope.
Notation "'box_' ( p1 , ( p2 , p3 ) ) ⇒ C" := (box (fun x =>
let (p1,y) := wproj x in
let (p2,p3) := wproj y in C))
(at level 13) : circ_scope.
Notation "'box_' ( ( p1 , p2 ) , ( p3 , p4 ) ) ⇒ C" := (box (fun x =>
let (y,z) := wproj x in
let (p1,p2) := wproj y in
let (p3,p4) := wproj z in C))
(at level 13) : circ_scope.
(* Notations for patterns *)
Notation "()" := unit : circ_scope.
(* Now a bit ugly, since we tend to use (a,b) with the newer $ notation *)
Notation "( x ,, y ,, .. ,, z )" := (pair .. (pair x y) .. z) (at level 0) : circ_scope.
(* Notations for circuits *)
Notation comp p c1 c2 := (compose c1 (fun p => c2)).
Notation "'let_' p ← c1 ; c2" := (comp p c1 c2)
(at level 14, right associativity) : circ_scope.
Notation "'let_' () ← c1 ; c2" :=
(compose c1 (fun _ => c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( p1 , p2 ) ← c1 ; c2" :=
(compose c1 (fun x => letpair p1 p2 x c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( ( p1 , p2 ) , p3 ) ← c1 ; c2" :=
(compose c1 (fun x => let (y,p3) := wproj x in
let (p1,p2) := wproj y in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( p1 , p2 , p3 ) ← c1 ; c2" :=
(compose c1 (fun x => let (y,p3) := wproj x in
let (p1,p2) := wproj y in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( ( ( p1 , p2 ) , p3 ) , p4 ) ← c1 ; c2" :=
(compose c1 (fun x => let (y,p4) := wproj x in
let (z,p3) := wproj y in
let (p1,p2) := wproj z in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( p1 , p2 , p3 , p4 ) ← c1 ; c2" :=
(compose c1 (fun x => let (y,p4) := wproj x in
let (z,p3) := wproj y in
let (p1,p2) := wproj z in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( ( ( ( p1 , p2 ) , p3 ) , p4 ) , p5 ) ← c1 ; c2" :=
(compose c1 (fun x => let (y,p5) := wproj x in
let (z,p4) := wproj y in
let (a,p3) := wproj z in
let (p1,p2) := wproj a in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( p1 , p2 , p3 , p4 , p5 ) ← c1 ; c2" :=
(compose c1 (fun x => let (y,p5) := wproj x in
let (z,p4) := wproj y in
let (a,p3) := wproj z in
let (p1,p2) := wproj a in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( p1 , ( p2 , p3 ) ) ← c1 ; c2" :=
(compose c1 (fun x => let (p1,y) := wproj x in
let (p2,p3) := wproj y in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( p1 , ( p2 , ( p3 , p4 ) ) ) ← c1 ; c2" :=
(compose c1 (fun x => let (p1,y) := wproj x in
let (p2,z) := wproj y in
let (p3,p4) := wproj z in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( p1 , ( p2 , ( p3 , ( p4 , p5 ) ) ) ) ← c1 ; c2" :=
(compose c1 (fun x => let (p1,y) := wproj x in
let (p2,z) := wproj y in
let (p3,a) := wproj z in
let (p4,p5) := wproj a in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( ( p1 , p2 ) , ( p3 , p4 ) ) ← c1 ; c2" :=
(compose c1 (fun x => let (y,z) := wproj x in
let (p1, p2) := wproj y in
let (p3, p4) := wproj z in c2))
(at level 14, right associativity) : circ_scope.
Notation "'let_' ( p1 , ( p2 , ( p3 , ( p4 , ( p5 , p6 ) ) ) ) ) ← c1 ; c2" :=
(compose c1 (fun x => let (p1,y) := wproj x in
let (p2,z) := wproj y in
let (p3,a) := wproj z in
let (p4,b) := wproj a in
let (p5,p6) := wproj b in c2))
(at level 14, right associativity) : circ_scope.
Notation "'gate_' p2 ← g @ p ; c2" := (gate g p (fun p2 => c2))
(at level 14, right associativity).
Notation "'gate_' () ← g @ p ; c2" := (gate g p (fun _ => c2))
(at level 14, right associativity).
Notation "'gate_' ( p1 , p2 ) ← g @ p ; c2" :=
(gate g p (fun x => letpair p1 p2 x c2))
(at level 14, right associativity) : circ_scope.
Notation "'gate_' ( p1 , p2 , p3 ) ← g @ p ; c2" :=
(gate g p (fun x => let (y, p3) := wproj x in
let (p1, p2) := wproj y in c2))
(at level 14, right associativity) : circ_scope.
Notation "'gate_' ( ( p1 , p2 ) , p3 ) ← g @ p ; c2" :=
(gate g p (fun x => let (y, p3) := wproj x in
let (p1, p2) := wproj y in c2))
(at level 14, right associativity) : circ_scope.
Notation "'gate_' ( p1 , ( p2 , p3 ) ) ← g @ p ; c2" :=
(gate g p (fun x => let (p1, y) := wproj x in
let (p2, p3) := wproj y in c2))
(at level 14, right associativity) : circ_scope.
Notation "'gate_' ( ( p1 , p2 ) , ( p3 , p4 ) ) ← g @ p ; c2" :=
(gate g p (fun x => let (y, z) := wproj x in
let (p1, p2) := wproj y in
let (p3, p4) := wproj z in c2))
(at level 14, right associativity) : circ_scope.
Notation "'gate_' ( p1 , ( p2 , ( p3 , ( p4 , ( p5 , p6 ) ) ) ) ) ← g @ p ; c2" :=
(gate g p (fun x => let (p1,y) := wproj x in
let (p2,z) := wproj y in
let (p3,a) := wproj z in
let (p4,b) := wproj a in
let (p5,p6) := wproj b in c2))
(at level 14, right associativity) : circ_scope.
Notation "'discard_' p ; c" := (gate discard p (fun _ => c))
(at level 14, right associativity) : circ_scope.
Notation "'discard_' ( p1 , p2 ) ; c" := (gate discard p1 (fun _ => gate discard p2
(fun _ => c)))
(at level 14, right associativity) : circ_scope.
Notation "'discard_' ( p1 , p2 , p3 ) ; c" := (gate discard p1
(fun _ => gate discard p2
(fun _ => gate discard p3
(fun _ => c))))
(at level 14, right associativity) : circ_scope.
Notation "'discard_' ( ( p1 , p2 ) , p3 ) ; c" := (gate discard p1
(fun _ => gate discard p2
(fun _ => gate discard p3
(fun _ => c))))
(at level 14, right associativity) : circ_scope.
Notation "'discard_' ( p1 , ( p2 , p3 ) ) ; c" := (gate discard p1
(fun _ => gate discard p2
(fun _ => gate discard p3
(fun _ => c))))
(at level 14, right associativity) : circ_scope.
Notation "'discard_' ( ( p1 , p2 ) , ( p3 , p4 ) ) ; c" :=
(gate discard p1
(fun _ => gate discard p2
(fun _ => gate discard p3
(fun _ => gate discard p4
(fun _ => c)))))
(at level 14, right associativity) : circ_scope.
Delimit Scope circ_scope with qc.
(* Automation *)
Ltac goal_has_evars :=
match goal with
[|- ?G ] => has_evars G
end.
Ltac invert_patterns :=
repeat match goal with
| [ p : Pat One |- _ ] => dependent destruction p
| [ p : Pat Qubit |- _] => dependent destruction p
| [ p : Pat Bit |- _] => dependent destruction p
| [ p : Pat (_ ⊗ _) |- _ ] => dependent destruction p
| [ H : Types_Pat ?Γ () |- _ ] => is_var Γ; inversion H; subst
| [ H : Types_Pat ?Γ (qubit _) |- _ ] => is_var Γ; inversion H; subst
| [ H : Types_Pat ?Γ (bit _) |- _ ] => is_var Γ; inversion H; subst
| [ H : Types_Pat ?Γ (pair _ _) |- _ ] => is_var Γ; dependent destruction H
end.
Create HintDb typed_db.
Ltac type_check_once :=
intros;
try match goal with
| [|- Typed_Box _ ] => solve [eauto with typed_db] (* extensible *)
| [|- @Typed_Box ?W1 ?W2 ?c] => unfold Typed_Box in *; try unfold c
end;
intros;
simpl in *;
subst;
invert_patterns;
repeat match goal with
| [ b : bool |- _ ] => destruct b
| [ H : _ == _ ∙ _ |- _ ] => destruct H
| H: @Types_Circuit _ _ ?c |- @Types_Circuit _ _ ?c
=> exact H
| [ |- @Types_Circuit _ _ _ ] => econstructor; type_check_once
| [ |- @Types_Circuit _ _ (if ?b then _ else _) ]
=> destruct b; type_check_once
(* compose_typing : specify goal. *)
| [ |- @Types_Circuit _ _ _ ] => eapply compose_typing; type_check_once
| [ H : forall a b, Types_Pat _ _ -> Types_Circuit _ _ |- Types_Circuit _ _]
=> apply H; type_check_once
| [ H : @Types_Pat _ _ ?p |- @Types_Pat _ _ ?p ]
=> exact H
| [ H : @Types_Pat ?Γ1 _ ?p |- @Types_Pat ?Γ2 _ ?p ]
(* in case they don't line up exactly *)
=> replace Γ2 with Γ1; [exact H | monoid]
| [ |- Types_Pat _ _ ] => econstructor; type_check_once
| [ |- ?Γ == ?Γ1 ∙ ?Γ2 ] => has_evars (Γ1 ⋓ Γ2 ⋓ Γ)
| [ |- _ == _ ∙ _ ] => solve_merge
end;
(* Runs monoid iff a single evar appears in context *)
match goal with
| [|- is_valid ?Γ] => tryif (has_evar Γ)
then (idtac (*"can't validate"; print_goal*))
else (idtac (*"validate"; print_goal*); validate)
| [|- ?G ] => tryif (has_evars G)
then (idtac (*"can't monoid"; print_goal*))
else (idtac (*"monoid"; print_goal*); monoid)
end.
(* Useful for debugging *)
Ltac type_check_num :=
let pre := numgoals in idtac "Goals before: " pre "";
[> type_check_once..];
let post := numgoals in idtac "Goals after: " post "";
tryif (guard post < pre) then type_check_num else idtac "done".
(* Easiest solution *)
Ltac type_check := let n := numgoals in do n [> type_check_once..].
(** Example **)
Ltac destruct_merges :=
repeat match goal with
| [ H : _ == _ ∙ _ |- _ ] => destruct H
end.
(* Three typing derivations for a simple circuit *)
(* Corresponds to thesis figure 9.1 *)
Definition cnot12 : Square_Box (Qubit ⊗ Qubit ⊗ Qubit) :=
box_ (p0,p1,p2) ⇒
gate_ (p3,p4) ← CNOT @(p1,,p2);
output (p0,,p3,,p4).
(* In functional syntax
Definition entangle23 : Square_Box (Qubit ⊗ Qubit ⊗ Qubit) :=
box_ (p0,p1,p2) ⇒
let_ (p3,p4) ← CNOT $ (p1,p2);
(p0,p3,p4).
*)
Lemma cnot12_WT_manual : Typed_Box cnot12.
Proof.
(* manual - no evars *)
unfold Typed_Box, cnot12.
intros Γ p TP. simpl.
dependent destruction TP.
dependent destruction TP1.
rename Γ0 into Γ, Γ1 into Γ0. rename Γ into Γ1.
rename p3 into p1.
rename TP1_1 into TP0, TP1_2 into TP1.
apply @types_gate with (Γ := Γ0) (Γ1 := Γ1 ⋓ Γ2); try solve_merge.
- (* type input pattern `(p1,p2)` *)
apply types_pair with (Γ1 := Γ1) (Γ2 := Γ2); try solve_merge.
+ apply TP1. (* types p1 *)
+ apply TP2. (* types p2 *)
- (* types `output (p0, p3, p4)` *)
intros Γ Γ' p M TP.
dependent destruction TP.
apply (@types_output _ _ _ _ eq_refl).
(* types (p0, p3, p4) *)
apply types_pair with (Γ1 := Γ0 ⋓ Γ3) (Γ2 := Γ4); try solve_merge.
+ (* types (p0, p3) *)
apply types_pair with (Γ1 := Γ0) (Γ2 := Γ3); try solve_merge.
* apply TP0. (* types p0 *)
* apply TP3. (* types p3 *)
+ apply TP4. (* types p4 *)
Qed.
Lemma cnot12_WT_evars : Typed_Box cnot12.
Proof.
(* manual with evars *)
unfold Typed_Box, cnot12.
intros; simpl.
invert_patterns.
eapply types_gate.
1: {
eapply @types_pair. (* types (p1, p2) *)
4: eauto. (* types p2 *)
3: eauto. (* types p1 *)
2: monoid. (* unifies ?Γ = Γ1 ⋓ Γ2 *)
1: validate. (* solves is_valid (Γ1 ⋓ Γ2) *)
}
2: { (* 3 *)
split. (* _ == _ ∙ _ *)
2: monoid. (* unifies Γ0 ⋓ Γ1 ⋓ Γ2 = Γ1 ⋓ Γ2 ⋓ ?Γ *)
1: validate. (* solves is_valid (Γ0 ⋓ Γ1 ⋓ Γ2) *)
}
1: { (* 2 *)
intros; simpl.
invert_patterns.
eapply @types_output.
1: monoid.
destruct_merges; subst.
eapply @types_pair.
4: eauto. (* types p4 *)
3: {
eapply @types_pair. (* types (p0,p3) *)
4: eauto. (* types p3 *)
3: eauto. (* types p0 *)
2: monoid. (* unifies ?Γ = Γ0 ⋓ Γ3 *)
1: validate. (* solves is_valid (Γ1 ⋓ Γ2) *)
}
2: monoid. (* unifies Γ3 ⋓ Γ4 ⋓ Γ0 = Γ0 ⋓ Γ3 ⋓ Γ4 *)
1: validate. (* solves is_valid (Γ1 ⋓ Γ2) *)
}
Qed.