-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAncilla.v
417 lines (383 loc) · 12 KB
/
Ancilla.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
Require Import DBCircuits.
Require Import TypeChecking.
Require Import Denotation.
Require Import Composition.
(*************************)
(* Assertion Correctness *)
(*************************)
Inductive not_assert : forall {W1 W2} (g : Gate W1 W2), Prop :=
| na_U : forall W u, not_assert (@U W u)
| na_NOT : not_assert BNOT
| na_init0 : not_assert init0
| na_init1 : not_assert init1
| na_new0 : not_assert new0
| na_new1 : not_assert new1
| na_meas : not_assert meas
| na_discard : not_assert discard.
Inductive ancilla_free {W} : Circuit W -> Prop :=
| af_output : forall p, ancilla_free (output p)
| af_gate : forall W1 W2 (g : Gate W1 W2) p c',
not_assert g ->
(forall p, ancilla_free (c' p)) ->
ancilla_free (gate g p c')
| af_lift : forall p c, (forall b, ancilla_free (c b)) ->
ancilla_free (lift p c).
Inductive ancilla_free_box {W1 W2} : Box W1 W2 -> Prop :=
| af_box : forall c, (forall p, ancilla_free (c p)) -> ancilla_free_box (box c).
Definition valid_ancillae {W} (c : Circuit W) : Prop := forall (Γ Γ0 : Ctx),
(* Γ' == Γ0 ∙ Γ -> *) (* necessary? *)
Γ ⊢ c:Circ -> (* <- is this right? *)
⟨ Γ0 | Γ ⊩ c ⟩ = ⟨! Γ0 | Γ ⊩ c !⟩.
Definition valid_ancillae_box {W1 W2} (c : Box W1 W2) :=
Typed_Box c -> (* why are we including typing judgments? *)
denote_box true c = denote_box false c.
Definition valid_ancillae' {W} (c : Circuit W) := forall (Γ Γ0 : Ctx) ρ,
Γ ⊢ c:Circ -> (* <- is this right? *)
Mixed_State ρ ->
trace (⟨! Γ0 | Γ ⊩ c !⟩ ρ) = 1.
Definition valid_ancillae_box' {W1 W2} (c : Box W1 W2) : Prop := forall ρ,
Typed_Box c ->
Mixed_State ρ ->
trace (denote_box false c ρ) = 1.
Proposition valid_ancillae_equal : forall W (c : Circuit W),
valid_ancillae c <-> valid_ancillae' c.
Proof.
intros.
unfold valid_ancillae, valid_ancillae'.
split.
- intros H Γ Γ0 ρ H0 H1.
rewrite <- H; trivial.
apply mixed_state_trace_1.
(* The following lemma is sufficient:
apply denote_circuit_correct; easy. *)
admit.
- induction c as [| W' W0 g p c IH | IH].
+ reflexivity.
+ intros H Γ Γ0 H'.
replace (gate g p c) with (HOASCircuits.compose (gate g p (fun p' => output p')) c) by auto.
dependent destruction H'.
destruct Γ1 as [|Γ1]; try invalid_contradiction.
erewrite denote_compose with (Γ1:=[]); trivial.
Locate ":Fun".
3:{
intros Γ3 Γ0' p0 H0 H1.
destruct H0.
rewrite merge_nil_r in pf_merge.
subst.
apply (t0 Γ3); trivial.
all: admit. }
(*
rewrite denote_compose with (Γ1:=Γ); trivial.
apply f_equal2.
rewrite (IH (add_fresh_pat W0 Γ)).
reflexivity.
intros. apply (H Γ1 Γ2).
denote_compose.
unfold denote_circuit in *.
rewrite H1.
simpl.
destruct g.
* simpl.
unfold denote_db_circuit.
simpl.
reflexivity.
reflexivity.
matrix_denote. simpl.
intros VA.
unfold valid_ancillae, valid_ancillae'.
induction c.
intros Γ Γ0 ρ H H0.
*)
Abort.
Fact valid_ancillae_box_equal : forall W1 W2 (c : Box W1 W2),
valid_ancillae_box c <-> valid_ancillae_box' c.
Proof.
intros.
destruct c.
Admitted.
(* This relationship should be easy to prove.
Alternatively, we could just define one in terms of the other *)
Fact valid_ancillae_unbox : forall W W' (c : Pat W -> Circuit W'),
(forall p, valid_ancillae (c p)) <-> valid_ancillae_box (box (fun p => c p)).
Proof.
intros.
unfold valid_ancillae, valid_ancillae_box.
unfold denote_box. unfold denote_circuit.
unfold denote_db_box.
unfold hoas_to_db_box.
split.
- intros H T.
specialize (H (add_fresh_pat W []) (add_fresh_state W []) []).
simpl in *.
rewrite size_fresh_ctx in H.
simpl in H.
unfold add_fresh_state, add_fresh_pat in *.
destruct (add_fresh W []) as [p Γ] eqn:E.
simpl in *.
rewrite H.
easy.
unfold Typed_Box in T. simpl in T. apply T.
(* This should be proven in DBCircuits *)
admit.
- intros H p Γ Γ0 T.
simpl in *.
Admitted.
Proposition valid_ancillae_unbox' : forall W W' (c : Box W W') (p : Pat W),
valid_ancillae (unbox c p) <-> valid_ancillae_box c.
Proof.
intros W W' c p.
unfold valid_ancillae, valid_ancillae_box.
unfold denote_box.
unfold denote_db_box.
destruct c.
simpl.
unfold denote_circuit.
simpl.
split.
- intros H.
admit.
Abort.
Lemma id_correct : forall W p, valid_ancillae (@output W p).
Proof.
intros W p.
unfold valid_ancillae.
reflexivity.
Qed.
Lemma update_merge : forall (Γ Γ' :Ctx) W W' v, Γ' == singleton v W ∙ Γ ->
Valid (update_at Γ' v (Some W')) == singleton v W' ∙ Γ.
Proof.
intros Γ Γ' W W' v H.
generalize dependent Γ.
generalize dependent Γ'.
induction v.
- intros Γ' Γ H.
simpl in *.
apply merge_fun_ind in H.
inversion H; subst.
simpl.
constructor.
apply valid_valid.
reflexivity.
inversion H5; subst.
inversion H4; subst.
constructor.
apply valid_valid.
reflexivity.
inversion H4; subst.
constructor.
apply valid_valid.
reflexivity.
- intros Γ' Γ H.
simpl.
destruct Γ.
+ destruct H.
rewrite merge_nil_r in pf_merge. inversion pf_merge.
simpl.
edestruct IHv.
constructor.
2: rewrite merge_nil_r; easy.
apply valid_valid.
rewrite merge_nil_r in pf_merge0.
inversion pf_merge0.
constructor.
apply valid_valid.
rewrite merge_nil_r.
easy.
+ destruct H.
constructor.
apply valid_valid.
unlock_merge. simpl in *.
destruct (merge' (singleton v W) Γ) eqn:E.
rewrite pf_merge in pf_valid. invalid_contradiction.
simpl.
edestruct IHv.
constructor.
2: symmetry in E; unlock_merge; apply E.
apply valid_valid.
unlock_merge.
rewrite <- pf_merge0.
inversion pf_merge.
simpl.
easy.
Qed.
Lemma change_type_singleton : forall v W W', change_type v W' (singleton v W) = singleton v W'.
Proof.
intros v W W'.
unfold change_type.
erewrite update_at_singleton.
reflexivity.
apply singleton_singleton.
apply singleton_singleton.
Qed.
Lemma ancilla_free_valid : forall W (c : Circuit W),
ancilla_free c ->
valid_ancillae c.
Proof.
intros W c AF.
induction c.
+ unfold valid_ancillae. reflexivity.
+ assert (forall p : Pat w2, valid_ancillae (c p)) as VA.
intros p'.
apply H.
dependent destruction AF.
apply H1.
clear H.
unfold valid_ancillae in *.
intros Γ0 Γ1 WT.
dependent destruction WT.
destruct Γ as [|Γ], Γ2 as [|Γ2]; try invalid_contradiction.
erewrite 2 denote_gate_circuit; try apply pf1; try apply t.
destruct g.
- simpl. erewrite VA. reflexivity. eapply t0; [apply pf1|apply t].
- simpl. erewrite VA. reflexivity. eapply t0; [apply pf1|apply t].
- simpl. erewrite VA. reflexivity.
eapply t0.
2: constructor; apply singleton_singleton.
dependent destruction p.
dependent destruction t.
destruct pf1.
rewrite merge_nil_l in pf_merge. inversion pf_merge. subst.
unfold process_gate_state. simpl.
split. validate.
rewrite merge_comm, merge_singleton_append.
easy.
- simpl. erewrite VA. reflexivity.
eapply t0.
2: constructor; apply singleton_singleton.
dependent destruction p.
dependent destruction t.
destruct pf1.
rewrite merge_nil_l in pf_merge. inversion pf_merge. subst.
unfold process_gate_state. simpl.
split. validate.
rewrite merge_comm, merge_singleton_append.
easy.
- simpl. erewrite VA. reflexivity.
eapply t0.
2: constructor; apply singleton_singleton.
dependent destruction p.
dependent destruction t.
destruct pf1.
rewrite merge_nil_l in pf_merge. inversion pf_merge. subst.
unfold process_gate_state. simpl.
split. validate.
rewrite merge_comm, merge_singleton_append.
easy.
- simpl. erewrite VA. reflexivity.
eapply t0.
2: constructor; apply singleton_singleton.
dependent destruction p.
dependent destruction t.
destruct pf1.
rewrite merge_nil_l in pf_merge. inversion pf_merge. subst.
unfold process_gate_state. simpl.
split. validate.
rewrite merge_comm, merge_singleton_append.
easy.
- dependent destruction p.
dependent destruction t.
simpl. erewrite VA. reflexivity.
eapply t0.
2: constructor; apply singleton_singleton.
apply singleton_equiv in s; subst.
unfold process_gate_state. simpl.
split. validate.
unfold change_type.
eapply update_merge.
apply pf1.
- simpl. erewrite VA. reflexivity. eapply t0; [apply pf1|apply t].
- dependent destruction p.
dependent destruction t.
simpl. erewrite VA. reflexivity.
unfold process_gate_state. simpl.
unfold process_gate_pat. simpl.
apply singleton_equiv in s. subst.
erewrite remove_bit_merge'.
apply trim_types_circ.
eapply t0.
2: constructor.
split. validate. rewrite merge_nil_l. easy.
easy.
- dependent destruction AF. inversion H.
- dependent destruction AF. inversion H.
+ dependent destruction AF.
assert (forall b, valid_ancillae (c b)) as VA. intros; apply H; apply H0.
clear H.
unfold valid_ancillae in *.
intros Γ Γ0 WT.
unfold denote_circuit in *.
simpl in *.
replace (size_ctx Γ - 1)%nat with (size_ctx (DBCircuits.remove_pat p Γ)).
erewrite VA.
erewrite VA.
reflexivity.
* dependent destruction WT.
dependent destruction p.
dependent destruction t.
apply singleton_equiv in s. subst.
destruct Γ2 as [|Γ2]; try invalid_contradiction.
erewrite remove_bit_merge'.
apply trim_types_circ.
apply t0.
easy.
* dependent destruction WT.
dependent destruction p.
dependent destruction t.
apply singleton_equiv in s. subst.
destruct Γ2 as [|Γ2]; try invalid_contradiction.
erewrite remove_bit_merge'.
apply trim_types_circ.
apply t0.
easy.
* dependent destruction WT.
dependent destruction p.
dependent destruction t.
apply singleton_equiv in s. subst.
destruct Γ2 as [|Γ2]; try invalid_contradiction.
rewrite (remove_bit_pred Γ2 Γ).
easy.
easy.
Qed.
Lemma ancilla_free_box_valid : forall W W' (c : Box W W'),
ancilla_free_box c ->
valid_ancillae_box c.
Proof.
intros.
destruct H.
apply valid_ancillae_unbox.
intros p.
apply ancilla_free_valid.
apply H.
Qed.
(* ---------------------------------------------*)
(*--------- Tactics for Valid Circuits ---------*)
(* ---------------------------------------------*)
(* It turns out, these generally aren't needed. *)
Lemma valid_denote_true : forall W W' (c : Box W W')
(ρ : Square (2^(⟦W⟧))) (ρ' : Square (2^(⟦W⟧))) (safe : bool),
(* typically ancilla_free_box c, but we'll make it general *)
Typed_Box c ->
valid_ancillae_box c ->
denote_box true c ρ = ρ' ->
denote_box safe c ρ = ρ'.
Proof.
intros W W' c ρ ρ' safe T H D.
destruct safe; trivial.
rewrite <- H; assumption.
Qed.
Lemma valid_denote_false : forall W W' (c : Box W W')
(ρ : Square (2^(⟦W⟧))) (ρ' : Square (2^(⟦W⟧))) (safe : bool),
Typed_Box c ->
valid_ancillae_box c ->
denote_box false c ρ = ρ' ->
denote_box safe c ρ = ρ'.
Proof.
intros W W' c ρ ρ' safe T H D.
destruct safe; trivial.
rewrite H; assumption.
Qed.
Ltac case_safe := apply valid_denote_true;
try solve [type_check; apply ancilla_free_box_valid; repeat constructor].
Ltac case_unsafe := apply valid_denote_false;
try solve [type_check; apply ancilla_free_box_valid; repeat constructor].
(* *)