Documentation

CslibFrontier.Computability.QueryComplexity.Quantum.Composition

Composing quantum query algorithms #

Constructions that build query algorithms (Cslib.Quantum.run) from others: classical control, which realizes classical randomness, parking, checking an output with one more query, and running an algorithm twice.

Classical control. A query algorithm is a deterministic sequence of isometries, but its workspace and initial state are arbitrary, so classical randomness fits inside it. Let the workspace be W × G, where G is a classical register that the algorithm reads but never changes. The isometry controlled V applies V g to the slice ψ_g of the state where the register holds g (Cslib.Quantum.slice, Cslib.Quantum.controlled). The oracle never touches the workspace, so the slices evolve separately: the slice at g of the final state is the final state of the algorithm V g started from the slice at g of the initial state (Cslib.Quantum.slice_run), and measuring adds up the slices (Cslib.Quantum.measureProb_eq_sum_slice, Cslib.Quantum.regProb_eq_sum_slice). So preparing the register in the superposition ∑_g √(p_g) |g⟩ makes every outcome probability the average, with weights p_g, of the outcome probabilities of the algorithms V g: a classical mixture of query algorithms, with the coin replaced by its purification.

Parking. To let algorithms with different numbers of queries share one query count, an algorithm whose state after k queries has control bit 1 can be parked (Cslib.Quantum.park): after its k-th query the control bit is flipped to 0 (Cslib.Quantum.flipCtrl), where the controlled oracle acts as the identity, and nothing happens afterwards (Cslib.Quantum.run_park). Parking does not change the probability of any query index.

Checking an output. If the state after T queries has control bit 0, a Hadamard gate on the control bit (Cslib.Quantum.hadCtrl), one more query and another Hadamard gate write the queried bit x_i of the query index i into the control bit (Cslib.Quantum.check, Cslib.Quantum.run_check). The control bit is then 1 with the probability that measuring the query register gives an index i with x_i = 1 (Cslib.Quantum.measureProb_run_check). On an arbitrary state the same three steps are the bit oracle |i, b, w⟩ ↦ |i, b ⊕ x_i, w⟩ (Cslib.Quantum.bitFlip, Cslib.Quantum.hadCtrl_phaseOracle_hadCtrl_eq_permute).

Running twice. Cslib.Quantum.twice runs an algorithm with T queries twice, on two copies of its registers: the second copy waits in the workspace during the first run, then the copies are exchanged by a permutation of the basis (Cslib.Quantum.permute), and the outcome of the first run waits in the workspace during the second. After 2T queries the state is the product of two copies of the final state of the algorithm (Cslib.Quantum.run_twice), so accepting when either outcome is accepting succeeds with probability 1 - (1 - a)² (Cslib.Quantum.measureProb_run_twice).

These constructions are standard; their formulation in this query model is original.

Main definitions #

Main results #

Named basis configurations, slices and classical control #

structure Cslib.Quantum.QueryBasis (N : ℕ) (W : Type u_3) :
Type u_3

Named computational-basis configuration of Cslib.Quantum.State N W: the queried index in Fin N, the phase-kickback control bit, and the workspace value.

  • index : Fin N

    The queried input position.

  • ctrl : Bool

    The phase-kickback control bit.

  • work : W

    The workspace register value.

Instances For
    theorem Cslib.Quantum.QueryBasis.ext {N : ℕ} {W : Type u_3} {x y : QueryBasis N W} (index : x.index = y.index) (ctrl : x.ctrl = y.ctrl) (work : x.work = y.work) :
    x = y
    theorem Cslib.Quantum.QueryBasis.ext_iff {N : ℕ} {W : Type u_3} {x y : QueryBasis N W} :
    x = y ↔ x.index = y.index ∧ x.ctrl = y.ctrl ∧ x.work = y.work
    @[instance_reducible]
    instance Cslib.Quantum.instDecidableEqQueryBasis {N✝ : ℕ} {W✝ : Type u_3} [DecidableEq W✝] :
    def Cslib.Quantum.instDecidableEqQueryBasis.decEq {N✝ : ℕ} {W✝ : Type u_3} [DecidableEq W✝] (x✝ x✝¹ : QueryBasis N✝ W✝) :
    Decidable (x✝ = x✝¹)

    Equivalence between QueryBasis N W and the raw basis tuple Fin N × Bool × W of State N W.

    @[simp]
    theorem Cslib.Quantum.QueryBasis.equiv_apply {N : ℕ} {W : Type u_3} (q : QueryBasis N W) :
    @[simp]
    theorem Cslib.Quantum.QueryBasis.equiv_symm_apply {N : ℕ} {W : Type u_3} (i : Fin N) (b : Bool) (w : W) :
    equiv.symm (i, b, w) = { index := i, ctrl := b, work := w }
    @[instance_reducible]
    def Cslib.Quantum.slice {N : ℕ} {W : Type u_1} {G : Type u_2} (g : G) (ψ : State N (W × G)) :
    State N W

    The slice ψ_g of a state at the value g of a classical register G in the workspace: the amplitudes of the basis states whose register holds g.

    @[simp]
    theorem Cslib.Quantum.slice_apply {N : ℕ} {W : Type u_1} {G : Type u_2} (g : G) (ψ : State N (W × G)) (i : Fin N) (b : Bool) (w : W) :
    (slice g ψ).ofLp (i, b, w) = ψ.ofLp (i, b, w, g)
    @[simp]
    theorem Cslib.Quantum.slice_add {N : ℕ} {W : Type u_1} {G : Type u_2} (g : G) (ψ φ : State N (W × G)) :
    slice g (ψ + φ) = slice g ψ + slice g φ
    @[simp]
    theorem Cslib.Quantum.slice_smul {N : ℕ} {W : Type u_1} {G : Type u_2} (g : G) (c : ℂ) (ψ : State N (W × G)) :
    slice g (c • ψ) = c • slice g ψ
    @[simp]
    theorem Cslib.Quantum.slice_phaseOracle {N : ℕ} {W : Type u_1} {G : Type u_2} (x : Fin N → Bool) (g : G) (ψ : State N (W × G)) :
    slice g ((phaseOracle x) ψ) = (phaseOracle x) (slice g ψ)
    theorem Cslib.Quantum.norm_sq_eq_sum_slice {N : ℕ} {W : Type u_1} {G : Type u_2} [Fintype W] [Fintype G] (ψ : State N (W × G)) :
    ‖ψ‖ ^ 2 = ∑ g : G, ‖slice g ψ‖ ^ 2

    The squared norm of a state is the sum of the squared norms of its slices.

    noncomputable def Cslib.Quantum.controlled {N : ℕ} {W : Type u_1} {G : Type u_2} [Fintype W] [Fintype G] (V : G → State N W →ₗᵢ[ℂ] State N W) :
    State N (W × G) →ₗᵢ[ℂ] State N (W × G)

    The isometry controlled by a classical register: it applies V g to the slice at g.

    @[simp]
    theorem Cslib.Quantum.slice_controlled {N : ℕ} {W : Type u_1} {G : Type u_2} [Fintype W] [Fintype G] (V : G → State N W →ₗᵢ[ℂ] State N W) (g : G) (ψ : State N (W × G)) :
    slice g ((controlled V) ψ) = (V g) (slice g ψ)
    theorem Cslib.Quantum.slice_run {N : ℕ} {W : Type u_1} {G : Type u_2} [Fintype W] [Fintype G] (V : G → ℕ → State N W →ₗᵢ[ℂ] State N W) (ψ₀ : State N (W × G)) (x : Fin N → Bool) (g : G) (t : ℕ) :
    slice g (run (fun (s : ℕ) => controlled fun (h : G) => V h s) ψ₀ x t) = run (V g) (slice g ψ₀) x t

    Classically controlled algorithms run slice by slice: if the isometry after s queries applies V g s to the slice at g, then the slice at g of the state after t queries is the state after t queries of the algorithm V g started from the slice at g.

    theorem Cslib.Quantum.measureProb_eq_sum_slice {N : ℕ} {W : Type u_1} {G : Type u_2} [Fintype W] [Fintype G] [DecidableEq W] [DecidableEq G] (ψ : State N (W × G)) (S : Finset (Fin N × Bool × W × G)) :
    measureProb ψ S = ∑ g : G, measureProb (slice g ψ) {x : Fin N × Bool × W | match x with | (i, b, w) => (i, b, w, g) ∈ S}

    Outcome probabilities add up over the slices: the probability of an outcome set S is the sum over g of the probability, in the slice at g, of the outcomes whose extension by g lies in S.

    theorem Cslib.Quantum.regProb_eq_sum_slice {N : ℕ} {W : Type u_1} {G : Type u_2} [Fintype W] [Fintype G] (ψ : State N (W × G)) (i : Fin N) :
    regProb ψ i = ∑ g : G, regProb (slice g ψ) i

    The probability of measuring query index i is the sum over the slices.

    @[simp]
    theorem Cslib.Quantum.controlled_id {N : ℕ} {W : Type u_1} {G : Type u_2} [Fintype W] [Fintype G] :

    Scaling #

    theorem Cslib.Quantum.run_smul {N : ℕ} {W : Type u_1} [Fintype W] (U : ℕ → State N W →ₗᵢ[ℂ] State N W) (c : ℂ) (ψ₀ : State N W) (x : Fin N → Bool) (t : ℕ) :
    run U (c • ψ₀) x t = c • run U ψ₀ x t

    Query algorithms are linear in their initial state.

    theorem Cslib.Quantum.regProb_smul {N : ℕ} {W : Type u_1} [Fintype W] (c : ℂ) (ψ : State N W) (i : Fin N) :
    regProb (c • ψ) i = ‖c‖ ^ 2 * regProb ψ i

    Scaling a state by c scales the probability of every query index by ‖c‖².

    Permutations of the basis and parking #

    noncomputable def Cslib.Quantum.permute {N : ℕ} {W : Type u_1} [Fintype W] (e : Equiv.Perm (Fin N × Bool × W)) :

    The isometry permuting the basis states by e: |p⟩ ↦ |e p⟩.

    @[simp]
    theorem Cslib.Quantum.permute_apply {N : ℕ} {W : Type u_1} [Fintype W] (e : Equiv.Perm (Fin N × Bool × W)) (ψ : State N W) (p : Fin N × Bool × W) :
    ((permute e) ψ).ofLp p = ψ.ofLp ((Equiv.symm e) p)
    theorem Cslib.Quantum.permute_mul {N : ℕ} {W : Type u_1} [Fintype W] (a b : Equiv.Perm (Fin N × Bool × W)) (ψ : State N W) :
    (permute (a * b)) ψ = (permute a) ((permute b) ψ)
    theorem Cslib.Quantum.permute_ket {N : ℕ} {W : Type u_1} [Fintype W] [DecidableEq W] (e : Equiv.Perm (Fin N × Bool × W)) (q : Fin N × Bool × W) :
    (permute e) (ket q) = ket (e q)
    def Cslib.Quantum.bitFlip {N : ℕ} {W : Type u_1} (x : Fin N → Bool) :

    The basis permutation of the bit oracle |i, b, w⟩ ↦ |i, b ⊕ x_i, w⟩, which adds the queried bit to the control bit. Hadamard gates around a query realize it (Cslib.Quantum.hadCtrl_phaseOracle_hadCtrl_eq_permute).

    @[simp]
    theorem Cslib.Quantum.bitFlip_apply {N : ℕ} {W : Type u_1} (x : Fin N → Bool) (i : Fin N) (b : Bool) (w : W) :
    (bitFlip x) (i, b, w) = (i, b ^^ x i, w)
    @[simp]
    theorem Cslib.Quantum.bitFlip_symm {N : ℕ} {W : Type u_1} (x : Fin N → Bool) :
    noncomputable def Cslib.Quantum.flipCtrl {N : ℕ} {W : Type u_1} [Fintype W] :

    The flip |i, b, w⟩ ↦ |i, ¬b, w⟩ of the control bit.

    @[simp]
    theorem Cslib.Quantum.flipCtrl_apply {N : ℕ} {W : Type u_1} [Fintype W] (ψ : State N W) (i : Fin N) (b : Bool) (w : W) :
    (flipCtrl ψ).ofLp (i, b, w) = ψ.ofLp (i, !b, w)
    theorem Cslib.Quantum.regProb_flipCtrl {N : ℕ} {W : Type u_1} [Fintype W] (ψ : State N W) (i : Fin N) :

    Flipping the control bit does not change the probability of any query index.

    theorem Cslib.Quantum.phaseOracle_eq_self {N : ℕ} {W : Type u_1} {x : Fin N → Bool} {ψ : State N W} (hψ : ∀ (i : Fin N) (w : W), ψ.ofLp (i, true, w) = 0) :
    (phaseOracle x) ψ = ψ

    The controlled oracle acts as the identity on states with control bit 0.

    noncomputable def Cslib.Quantum.park {N : ℕ} {W : Type u_1} [Fintype W] (k : ℕ) (U : ℕ → State N W →ₗᵢ[ℂ] State N W) :

    The algorithm U parked after k queries: it runs U up to its k-th isometry, then flips the control bit, and then does nothing.

    theorem Cslib.Quantum.run_park {N : ℕ} {W : Type u_1} [Fintype W] {k : ℕ} {U : ℕ → State N W →ₗᵢ[ℂ] State N W} {ψ₀ : State N W} {x : Fin N → Bool} (hctrl : ∀ (i : Fin N) (w : W), (run U ψ₀ x k).ofLp (i, false, w) = 0) {t : ℕ} (hkt : k ≤ t) :
    run (park k U) ψ₀ x t = flipCtrl (run U ψ₀ x k)

    A parked algorithm keeps its state: if the state of U after k queries has control bit 1, then after t ≥ k queries the parked algorithm is in that state with the control bit flipped.

    Agreement and splitting of runs #

    theorem Cslib.Quantum.run_congr {N : ℕ} {W : Type u_1} [Fintype W] {U U' : ℕ → State N W →ₗᵢ[ℂ] State N W} {t : ℕ} (h : ∀ s ≤ t, U s = U' s) (ψ₀ : State N W) (x : Fin N → Bool) :
    run U ψ₀ x t = run U' ψ₀ x t

    Two algorithms whose isometries agree up to t queries have the same state after t queries.

    theorem Cslib.Quantum.run_add {N : ℕ} {W : Type u_1} [Fintype W] {U V : ℕ → State N W →ₗᵢ[ℂ] State N W} {T : ℕ} (h0 : V 0 = LinearIsometry.id) (hV : ∀ (r : ℕ), V (r + 1) = U (T + r + 1)) (ψ₀ : State N W) (x : Fin N → Bool) (s : ℕ) :
    run U ψ₀ x (T + s) = run V (run U ψ₀ x T) x s

    Splitting a run: if V does nothing before its first query and then follows U from the T-th query on, then running U for T + s queries is running V for s queries from the state of U after T queries.

    Checking an output #

    noncomputable def Cslib.Quantum.hadCtrl {N : ℕ} {W : Type u_1} [Fintype W] :

    The Hadamard gate on the control bit: |i, b, w⟩ ↦ (|i, 0, w⟩ + (-1)^b |i, 1, w⟩) / √2.

    theorem Cslib.Quantum.hadCtrl_apply {N : ℕ} {W : Type u_1} [Fintype W] (ψ : State N W) (i : Fin N) (b : Bool) (w : W) :
    (hadCtrl ψ).ofLp (i, b, w) = ↑(√2)⁻¹ * (ψ.ofLp (i, false, w) + (if b = true then -1 else 1) * ψ.ofLp (i, true, w))
    theorem Cslib.Quantum.hadCtrl_phaseOracle_hadCtrl_eq_permute {N : ℕ} {W : Type u_1} [Fintype W] (x : Fin N → Bool) (ψ : State N W) :

    Hadamard gates around a query give the bit oracle: a Hadamard gate on the control bit, a query and another Hadamard gate permute the basis by |i, b, w⟩ ↦ |i, b ⊕ x_i, w⟩.

    theorem Cslib.Quantum.hadCtrl_phaseOracle_hadCtrl {N : ℕ} {W : Type u_1} [Fintype W] {x : Fin N → Bool} {φ : State N W} (hφ : ∀ (i : Fin N) (w : W), φ.ofLp (i, true, w) = 0) (i : Fin N) (b : Bool) (w : W) :
    (hadCtrl ((phaseOracle x) (hadCtrl φ))).ofLp (i, b, w) = if b = x i then φ.ofLp (i, false, w) else 0

    Checking a parked output: if φ has control bit 0, then a Hadamard gate on the control bit, a query and another Hadamard gate write the queried bit into the control bit: the result has amplitude φ(i, 0, w) at |i, x_i, w⟩ and 0 elsewhere.

    noncomputable def Cslib.Quantum.check {N : ℕ} {W : Type u_1} [Fintype W] (T : ℕ) (U : ℕ → State N W →ₗᵢ[ℂ] State N W) :

    The algorithm U followed by a check of its output after T queries: a Hadamard gate on the control bit, one more query and another Hadamard gate, which write the queried bit x_i of the measured index i into the control bit.

    theorem Cslib.Quantum.run_check {N : ℕ} {W : Type u_1} [Fintype W] {T : ℕ} {U : ℕ → State N W →ₗᵢ[ℂ] State N W} {ψ₀ : State N W} {x : Fin N → Bool} (hctrl : ∀ (i : Fin N) (w : W), (run U ψ₀ x T).ofLp (i, true, w) = 0) (i : Fin N) (b : Bool) (w : W) :
    (run (check T U) ψ₀ x (T + 1)).ofLp (i, b, w) = if b = x i then (run U ψ₀ x T).ofLp (i, false, w) else 0

    The state after a check: if the state of U after T queries has control bit 0, then after the check the control bit holds the queried bit x_i of the query index i.

    theorem Cslib.Quantum.measureProb_run_check {N : ℕ} {W : Type u_1} [Fintype W] {T : ℕ} {U : ℕ → State N W →ₗᵢ[ℂ] State N W} {ψ₀ : State N W} {x : Fin N → Bool} (hctrl : ∀ (i : Fin N) (w : W), (run U ψ₀ x T).ofLp (i, true, w) = 0) :
    measureProb (run (check T U) ψ₀ x (T + 1)) {x : Fin N × Bool × W | match x with | (fst, b, snd) => b = true} = ∑ i : Fin N with x i = true, regProb (run U ψ₀ x T) i

    The acceptance probability of a check: if the state of U after T queries has control bit 0, then after the check the control bit is 1 with the probability that measuring the state of U gives a marked query index.

    Running an algorithm twice #

    The basis permutation exchanging the query register, control bit and workspace with a copy of them (QueryBasis N W) held in the workspace.

    noncomputable def Cslib.Quantum.twice {N : ℕ} {W : Type u_1} [Fintype W] (T : ℕ) (U : ℕ → State N W →ₗᵢ[ℂ] State N W) :

    The algorithm U with T queries run twice, on two copies of its registers: the second copy waits in the workspace while the first run makes its T queries; then the copies are exchanged, and the second run makes T more queries while the outcome of the first waits in the workspace.

    noncomputable def Cslib.Quantum.twiceInit {N : ℕ} {W : Type u_1} (ψ₀ : State N W) :
    State N (W × QueryBasis N W)

    The initial state of twice: two copies of ψ₀.

    theorem Cslib.Quantum.norm_twiceInit {N : ℕ} {W : Type u_1} [Fintype W] {ψ₀ : State N W} (hψ₀ : ‖ψ₀‖ = 1) :

    Two copies of a unit state form a unit state.

    theorem Cslib.Quantum.run_twice {N : ℕ} {W : Type u_1} [Fintype W] (T : ℕ) (U : ℕ → State N W →ₗᵢ[ℂ] State N W) (ψ₀ : State N W) (x : Fin N → Bool) (i : Fin N) (b : Bool) (w : W) (q : QueryBasis N W) :
    (run (twice T U) (twiceInit ψ₀) x (T + T)).ofLp (i, b, w, q) = (run U ψ₀ x T).ofLp (i, b, w) * (run U ψ₀ x T).ofLp (QueryBasis.equiv q)

    Running twice gives two independent copies: after T + T queries, the state of twice is the product of two copies of the state of U after T queries.

    theorem Cslib.Quantum.measureProb_run_twice {N : ℕ} {W : Type u_1} [Fintype W] [DecidableEq W] {T : ℕ} {U : ℕ → State N W →ₗᵢ[ℂ] State N W} {ψ₀ : State N W} (hψ₀ : ‖ψ₀‖ = 1) (x : Fin N → Bool) (S : Finset (Fin N × Bool × W)) :
    measureProb (run (twice T U) (twiceInit ψ₀) x (T + T)) {x : Fin N × Bool × W × QueryBasis N W | match x with | (i, b, w, q) => (i, b, w) ∈ S ∨ QueryBasis.equiv q ∈ S} = 1 - (1 - measureProb (run U ψ₀ x T) S) ^ 2

    The acceptance probability of running twice: accepting when the outcome of either run lies in S, twice accepts with probability 1 - (1 - a)², where a is the probability that the outcome of one run lies in S.