(* Above the threshold of Proposition 8.10 the amplification fails. A disjunction of k inputs that hold 0, amplified r = 2h + 1 times, reads k r copies of its inputs; when each copy is flipped with probability eps, independently, and 2 k eps > 1, it computes a wrong value with probability at least 1 - rho^h for some rho < 1 that depends on k and eps only. *) From Stdlib Require Import List Bool Arith ZArith Reals Lia Lra. Import ListNotations. Set Warnings "-ambiguous-paths". From TH Require Import TwoHead Net Lev Margin Noise Restore. Set Warnings "ambiguous-paths". Local Open Scope R_scope. (* ---- Bernoulli's inequality and the choice of rho ------------------------------------------------------------------ *) Lemma pow_le_one : forall x n, 0 <= x <= 1 -> x ^ n <= 1. Proof. intros x n [H0 H1]. induction n as [|n IH]; cbn [pow]; [lra|]. pose proof (pow_le x n H0). nra. Qed. Lemma bernoulli : forall a n, 0 <= a -> 1 + INR n * a <= (1 + a) ^ n. Proof. intros a n Ha. induction n as [|n IH]; [cbn; lra|]. rewrite S_INR. cbn [pow]. assert (0 <= INR n * a * a) by (apply Rmult_le_pos; [apply Rmult_le_pos; [apply pos_INR | exact Ha] | exact Ha]). nra. Qed. (* with u = (2 k eps - 1)/(4 k eps), x = 1 - u and c = 1 - eps u: c^(2k) / x < 1 *) Lemma rho_lt : forall k eps, (1 <= k)%nat -> 0 < eps <= 1 -> 1 < 2 * INR k * eps -> let u := (2 * INR k * eps - 1) / (4 * INR k * eps) in 0 < u < 1/2 /\ (1 - eps * u) ^ (2 * k) / (1 - u) < 1. Proof. intros k eps Hk [He0 He1] H u. assert (Hk' : 1 <= INR k) by (apply (le_INR 1); exact Hk). assert (Hp : 1/2 < INR k * eps) by lra. assert (Eu : u * (4 * (INR k * eps)) = 2 * (INR k * eps) - 1) by (unfold u; field; lra). assert (Hu : 0 < u < 1/2). { split. - unfold u. apply Rdiv_lt_0_compat; lra. - apply (Rmult_lt_reg_r (4 * (INR k * eps))); [lra|]. rewrite Eu. lra. } split; [exact Hu|]. set (a := eps * u). assert (Ha : 0 < a < 1) by (unfold a; split; [apply Rmult_lt_0_compat; lra | nra]). (* (1 - a)^(2k) (1 + a)^(2k) <= 1 and (1 + a)^(2k) >= 1 + 2 k a *) assert (AB : (1 - a) ^ (2 * k) * (1 + a) ^ (2 * k) <= 1). { rewrite <- Rpow_mult_distr. apply pow_le_one. split; nra. } pose proof (bernoulli a (2 * k) ltac:(lra)) as Bb. rewrite mult_INR in Bb. cbn [INR] in Bb. assert (HA : 0 <= (1 - a) ^ (2 * k)) by (apply pow_le; lra). assert (C1 : (1 - a) ^ (2 * k) * (1 + 2 * INR k * a) <= 1) by nra. assert (Hd : 1 < (1 + 2 * INR k * a) * (1 - u)). { assert (E2 : 2 * INR k * a = (2 * (INR k * eps) - 1) / 2) by (unfold a; rewrite <- Eu; field). rewrite E2. assert (E3 : 1 - u = (2 * (INR k * eps) + 1) / (4 * (INR k * eps))) by (unfold u; field; lra). rewrite E3. apply (Rmult_lt_reg_r (8 * (INR k * eps))); [lra|]. replace ((1 + (2 * (INR k * eps) - 1) / 2) * ((2 * (INR k * eps) + 1) / (4 * (INR k * eps))) * (8 * (INR k * eps))) with ((2 * (INR k * eps) + 1) * (2 * (INR k * eps) + 1)) by (field; lra). nra. } apply (Rmult_lt_reg_r (1 - u)); [lra|]. unfold Rdiv. rewrite Rmult_assoc, Rinv_l, Rmult_1_r by lra. nra. Qed. Section Fail. Variable Omega : Type. Variable P : (Omega -> Prop) -> R. Hypothesis P_mono : forall A B : Omega -> Prop, (forall w, A w -> B w) -> P A <= P B. Hypothesis P_or : forall A B : Omega -> Prop, P (fun w => A w \/ B w) <= P A + P B. Hypothesis P_empty : P (fun _ => False) = 0. Hypothesis P_true : P (fun _ => True) = 1. Variable X : nat -> Omega -> R. Hypothesis P_indep : forall (I J : nat -> Prop) (A B : Omega -> Prop), (forall g, I g -> J g -> False) -> dep Omega X I A -> dep Omega X J B -> P (fun w => A w /\ B w) = P A * P B. Hypothesis P_compl : forall A, meas Omega X A -> P (fun w => ~ A w) = 1 - P A. (* the output of evaluation g flips when its variable is at least 1, with probability eps *) Variable eps : R. Hypothesis Hflip : forall g, P (fun w => 1 <= X g w) = eps. Lemma eps_range : 0 <= eps <= 1. Proof. rewrite <- (Hflip 0%nat). split; [exact (P_nonneg Omega P P_mono P_empty _) | exact (P_le1 Omega P P_mono P_true _)]. Qed. Lemma Pnone : forall A : Omega -> Prop, (forall w, ~ A w) -> P A = 0. Proof. intros A H. apply Rle_antisym; [rewrite <- P_empty; apply P_mono; intros w Hw; exact (H w Hw)|]. exact (P_nonneg Omega P P_mono P_empty A). Qed. (* the flips among the variables gs *) Definition cnt (gs : list nat) (w : Omega) : nat := length (filter (fb Omega X w) gs). Lemma dep_cnt : forall gs t, dep Omega X (fun g => In g gs) (fun w => (cnt gs w < t)%nat). Proof. intros gs t w w' H. unfold cnt. replace (filter (fb Omega X w') gs) with (filter (fb Omega X w) gs); [reflexivity|]. apply filter_ext_in. intros g Hg. unfold fb. rewrite (H g Hg). reflexivity. Qed. (* fewer than t of the |gs| variables flip with probability at most (1 - eps + eps x)^|gs| / x^(t - 1), 0 < x <= 1 *) Lemma P_few : forall gs t x, NoDup gs -> 0 < x <= 1 -> (1 <= t)%nat -> P (fun w => (cnt gs w < t)%nat) <= (1 - eps + eps * x) ^ length gs / x ^ (t - 1). Proof. pose proof eps_range as He. intros gs. induction gs as [|g gs IH]; intros t x Hnd Hx Ht. - cbn [length pow]. eapply Rle_trans; [apply (P_le1 Omega P P_mono P_true)|]. assert (0 < x ^ (t - 1) <= 1) by (split; [apply pow_lt; lra | apply pow_le_one; lra]). unfold Rdiv. rewrite Rmult_1_l. rewrite <- Rinv_1 at 1. apply Rinv_le_contravar; lra. - inversion Hnd as [|? ? Hg Hgs]; subst. set (c := 1 - eps + eps * x). assert (Hc : 1 - eps <= c <= 1) by (unfold c; split; nra). (* the first variable flips and fewer than t - 1 others do, or it does not and fewer than t others do *) eapply Rle_trans; [apply (P_mono _ (fun w => (1 <= X g w /\ (cnt gs w < t - 1)%nat) \/ (~ 1 <= X g w /\ (cnt gs w < t)%nat)))|]. { intros w Hw. unfold cnt in Hw. cbn [filter] in Hw. destruct (fb Omega X w g) eqn:E. - left. split; [apply fb_iff; exact E|]. cbn [length] in Hw. unfold cnt. lia. - right. split; [intro C; apply fb_iff in C; congruence | exact Hw]. } eapply Rle_trans; [apply P_or|]. rewrite (P_indep (fun g' => g' = g) (fun g' => In g' gs) (fun w => 1 <= X g w) (fun w => (cnt gs w < t - 1)%nat)). 2: { intros g' -> Hin. exact (Hg Hin). } 2: { intros w w' H. rewrite (H g eq_refl). reflexivity. } 2: { apply dep_cnt. } rewrite (P_indep (fun g' => g' = g) (fun g' => In g' gs) (fun w => ~ 1 <= X g w) (fun w => (cnt gs w < t)%nat)). 2: { intros g' -> Hin. exact (Hg Hin). } 2: { intros w w' H. rewrite (H g eq_refl). reflexivity. } 2: { apply dep_cnt. } rewrite Hflip, P_compl, Hflip by apply meas_ge. pose proof (IH t x Hgs Hx Ht) as B1. fold c in B1. cbn [length pow]. fold c. pose proof (pow_le c (length gs) ltac:(lra)) as Hcm. destruct (Nat.eq_dec t 1) as [->|Ht1]. + rewrite (Pnone (fun w => (cnt gs w < 1 - 1)%nat)) by (intros w Hw; lia). cbn [Nat.sub pow] in B1 |- *. unfold Rdiv in *. rewrite Rinv_1, !Rmult_1_r in *. nra. + pose proof (IH (t - 1)%nat x Hgs Hx ltac:(lia)) as B0. fold c in B0. assert (Ex : x ^ (t - 1) = x * x ^ (t - 1 - 1)) by (replace (t - 1)%nat with (S (t - 1 - 1)) at 1 by lia; reflexivity). rewrite Ex in B1 |- *. set (xp := x ^ (t - 1 - 1)) in *. assert (Hxp : 0 < xp) by (apply pow_lt; lra). assert (E : c * c ^ length gs / (x * xp) = eps * (c ^ length gs / xp) + (1 - eps) * (c ^ length gs / (x * xp))) by (unfold c; field; lra). rewrite E. apply Rplus_le_compat; apply Rmult_le_compat_l; lra. Qed. (* ---- a disjunction of inputs that hold 0 ---------------------------------------------------------------------------- *) Definition disj (js : list nat) : tu := TU (-1) (map (fun j => (j, 1%Z)) js). Lemma wsum_ones : forall ins E, (forall j z, In (j, z) ins -> z = 1%Z) -> wsum ins E = Z.of_nat (length (filter (fun p => E (fst p)) ins)). Proof. induction ins as [|[j z] t IH]; intros E H; [reflexivity|]. cbn [wsum filter fst]. rewrite (H j z (or_introl eq_refl)). rewrite IH by (intros j' z' Hin; exact (H j' z' (or_intror Hin))). destruct (E j); cbn [length b2z]; lia. Qed. (* on the replicated input with the copies psi flipped, the r-fold disjunction has pre-activation the number of flipped copies it reads minus (r + 1)/2 *) Lemma pre_disj : forall h js y psi, (forall j, In j js -> (j < length y)%nat /\ nth j y false = false) -> pre (arow (2 * h + 1) (length y) (disj js)) (xorv (rep (2 * h + 1) y) psi) = (Z.of_nat (length (filter (fun p => psi (fst p)) (tin (arow (2 * h + 1) (length y) (disj js))))) - Z.of_nat (h + 1))%Z. Proof. intros h js y psi Hy. unfold pre. rewrite wsum_ones. 2: { intros q z Hin. destruct (in_arow _ _ _ q z Hin) as (c & j & _ & Hj & _). cbn [tin disj] in Hj. apply in_map_iff in Hj as (j' & E & _). injection E as _ <-. reflexivity. } rewrite (filter_ext_in _ (fun p => psi (fst p))). - unfold arow at 1. cbn [tb disj]. replace ((2 * h + 1 - 1) / 2)%nat with h. + rewrite Nat2Z.inj_add, Nat2Z.inj_add, Nat2Z.inj_mul. cbn [Z.of_nat]. lia. + replace (2 * h + 1 - 1)%nat with (h * 2)%nat by lia. rewrite Nat.div_mul by lia. reflexivity. - intros [q z] Hin. cbn [fst]. destruct (in_arow _ _ _ q z Hin) as (c & j & Hc & Hj & ->). cbn [tin disj] in Hj. apply in_map_iff in Hj as (j' & E & Hj'). injection E as -> _. destruct (Hy j Hj') as [Hjl Hj0]. rewrite nth_xorv by (rewrite length_rep; nia). rewrite nth_rep by assumption. rewrite Hj0. reflexivity. Qed. (* Above the threshold: a disjunction of k inputs that hold 0, amplified r = 2h + 1 times, whose kr input copies flip independently with probability eps, outputs 1 with probability at least 1 - rho^h, rho < 1, when 2 k eps > 1 *) Theorem flip_fails : forall k, (1 <= k)%nat -> 1 < 2 * INR k * eps -> exists rho, 0 <= rho < 1 /\ forall h js y (src : nat -> nat), length js = k -> NoDup js -> (forall j, In j js -> (j < length y)%nat /\ nth j y false = false) -> (forall q q', src q = src q' -> q = q') -> 1 - rho ^ h <= P (fun w => Z.leb 0 (pre (arow (2 * h + 1) (length y) (disj js)) (xorv (rep (2 * h + 1) y) (fun q => fb Omega X w (src q)))) = true). Proof. intros k Hk H. pose proof eps_range as He. assert (Hk' : 1 <= INR k) by (apply (le_INR 1); exact Hk). assert (He0 : 0 < eps) by (destruct (Rle_lt_dec eps 0); [nra | assumption]). destruct (rho_lt k eps Hk (conj He0 (proj2 He)) H) as [Hu Hrho]. set (u := (2 * INR k * eps - 1) / (4 * INR k * eps)) in *. set (x := 1 - u). set (c := 1 - eps + eps * x). assert (Ec : c = 1 - eps * u) by (unfold c, x; ring). rewrite <- Ec in Hrho. fold x in Hrho. assert (Hc : 0 <= c <= 1) by (rewrite Ec; split; nra). assert (Hx : 0 < x <= 1) by (unfold x; lra). exists (c ^ (2 * k) / x). split; [split; [apply Rmult_le_pos; [apply pow_le; lra | apply Rlt_le, Rinv_0_lt_compat; lra] | exact Hrho]|]. intros h js y src Hlen Hnd Hy Hsrc. set (r := (2 * h + 1)%nat). assert (Hrow : rowok (length y) (disj js)). { split. - cbn [tin disj]. rewrite map_map. cbn [fst]. rewrite map_id. exact Hnd. - intros j z Hin. cbn [tin disj] in Hin. apply in_map_iff in Hin as (j' & E & Hj'). injection E as <- <-. split; [exact (proj1 (Hy j' Hj')) | left; reflexivity]. } destruct (arow_ok r _ _ Hrow) as [Hnd' _]. set (gs := map (fun p => src (fst p)) (tin (arow r (length y) (disj js)))). assert (Hgs : NoDup gs). { unfold gs. rewrite <- (map_map fst src). apply nodup_map_inj; [intros q q' _ _ E; exact (Hsrc q q' E) | exact Hnd']. } assert (Hlg : length gs = (k * r)%nat). { unfold gs. rewrite length_map, length_tin_arow. cbn [tin disj]. rewrite length_map, Hlen. lia. } (* the disjunction is wrong exactly when at least h + 1 of the copies it reads flip *) eapply Rle_trans; [|apply (P_mono (fun w => ~ (cnt gs w < h + 1)%nat))]. 2: { intros w Hw. unfold r. rewrite pre_disj by exact Hy. unfold cnt, gs, r in Hw. rewrite length_filter_map in Hw. cbv beta in Hw |- *. apply Z.leb_le. lia. } assert (Hcomp : 1 <= P (fun w => ~ (cnt gs w < h + 1)%nat) + P (fun w => (cnt gs w < h + 1)%nat)). { rewrite <- P_true. eapply Rle_trans; [|apply P_or]. apply P_mono. intros w _. destruct (Nat.lt_ge_cases (cnt gs w) (h + 1)); [right | left]; lia. } pose proof (P_few gs (h + 1) x Hgs Hx ltac:(lia)) as B. fold c in B. rewrite Hlg in B. replace (h + 1 - 1)%nat with h in B by lia. assert (Eb : c ^ (k * r) / x ^ h = c ^ k * (c ^ (2 * k) / x) ^ h). { unfold r. replace (k * (2 * h + 1))%nat with (2 * k * h + k)%nat by lia. rewrite pow_add, pow_mult. unfold Rdiv. rewrite Rpow_mult_distr, pow_inv. ring. } rewrite Eb in B. assert (Hck : c ^ k <= 1) by (apply pow_le_one; lra). assert (Hr0 : 0 <= (c ^ (2 * k) / x) ^ h) by (apply pow_le; apply Rmult_le_pos; [apply pow_le; lra | apply Rlt_le, Rinv_0_lt_compat; lra]). assert (0 <= c ^ k) by (apply pow_le; lra). nra. Qed. End Fail.