• No results found

In this section we give a number of examples of common quantum programs in QNQ. The Bell state described in Section 6.1 can be constructed by applying a hadamard unitary transformation to a newly initialized qubit (which puts the qubit into a superposi- tion √1

2∣0⟩+ 1

2∣1⟩) and then applying a controlled-not unitary on another newly initialized

qubit. Intuitively, this program corresponds to the circuit diagram shown here.

bell00 : Box LUnit (Qubit ⊗ Qubit)

bell00 ≡ box () ⇒ let a ∶= H # init 0 in let b ∶= init 0 in

ctrl-X # (a,b) ∣0⟩

∣0⟩ H

We writebox(x1, . . . , xn) ⇒efor pattern matching against input to a box. For example, in the definition ofbell00 box () ⇒ cin is syntactic sugar forbox x ⇒ let () ∶= x in c. Coin flips. Measuring a qubit results in a lowered boolean value, and so, takingLinαto beBox LUnit(Lowerα), we can construct a quantum coin flip inside the linearity monad as follows:

flip : Lin Bool

flip ≡ box () ⇒ meas (H # init 0) ∣0⟩ H meas

flipN : Nat → Lin Bool flipN 0 ≡ return True flipN (n+1) ≡ do b ← flip

if b then flipN n else return False

This program will return True with probability 21n and Falsewith probability 1−21n. Quantum natural numbers with dependent types. An n-qubit quantum natural number is a term of typen⊗Qubit, which we defined in Section 3.5 as follows:

0⊗σ≡LUnit n+1⊗σ≡σ⊗ (n⊗σ)

Notice that n⊗σ is a linear functor on LType’s; forf ∶Boxσ τ we can writelfmapf ∶ ∏nBox(n⊗σ) (n⊗τ) as follows:

lfmap : Box σ τ → Π n, Box (n ⊗ σ) (n ⊗ τ) lfmap f 0 ≡ box () ⇒ ()

lfmap f (n+1) ≡ box (x,xs) ⇒ (f $ x, lfmap n $ xs)

We writeTuple nαfor the corresponding host-language type ofn-tuples. BecauseLower distributes over⊗, we can transform a linearn-tuple of (Lower α)’s into a lowered n-tuple of α’s.

distrN : Π n. Box (n ⊗ Lower α) (Lower (Tuple n α)) distrN 0 ≡ box () ⇒ put ()

distrN (n+1) ≡ box (x,xs) ⇒ let !a ∶= x in

let !as ∶= distrN n $ xs in

put (a,as)

Now we can measure ann-qubit natural number into ann-tuple of boleans by combining

lfmap and distrN.

measN : Π n. Box (n ⊗ Qubit) (Lower (Tuple n Bool)) measN n ≡ distrN ○ lfmap meas

Quantum teleportation. The quantum version of Hello worldis the quantum telepor- tation algorithm, which describes how Alice can transmit the state of a qubit to Bob at a remote location. Despite the name, teleportation does not imply faster-than-light commu- nication, because, in order to transmit the information, Alice must send Bob two classical bits. Figure 6.2 shows the teleportation algorithm, broken up into three parts.

First, Alice and Bob receive two ends of a shared Bell state—this is their shared secret before moving to separate locations. At her secret location, Alice also has a qubit that she wishes to send to Bob. Alice entangles q with her end of the Bell state by applying a controlled X gate followed by a Hadamard gate. Notice that all three qubits are entangled at this point. Next, Alice measures her two qubits. Because of the no-cloning theorem, Alice cannot both send her qubit’s state to Bob and also keep her own copy of the qubit, so measurement degrades her original qubit. However, the state of Bob’s qubit has now changed as the result of the measurement.

Supposing thatq was originally in the statec0∣0⟩+c1∣1⟩, we can now work out the state

of the system after Alice is done with her measurement:

- If Alice measures ∣0⟩ ∣0⟩, then Bob’s qubit is in the state c0∣0⟩ +c1∣1⟩—the state of the

original qubit.

- If Alice measures∣0⟩ ∣1⟩, then Bob’s qubit is in the statec0∣1⟩+c1∣0⟩—the negation of the

original qubit.

- If Alice measures ∣1⟩ ∣0⟩, then Bob’s qubit is in the state c0∣0⟩ −c1∣1⟩.

- If Alice measures ∣1⟩ ∣1⟩, then Bob’s qubit is in the state c0∣1⟩ −c1∣0⟩.

If Bob knows the results of Alice’s measurement, he can apply a particular sequence of unitary transformations to correct the state of his qubit to return it to the original position. For example, if Bob knows the result was∣0⟩ ∣1⟩, then he can apply anX unitary to transform the qubit back into the statec0∣0⟩ +c1∣1⟩.

Thus, in the final part of the teleportation algorithm, Alice sends her two classical bits to Bob, who then applies his corrections depending on the state of those bits.

alice : Box (Qubit ⊗ Qubit) (Lower Bool ⊗ Lower Bool) alice ≡ box (q,a) ⇒ let (q,a) ∶= CNOT # (q,a) in

(meas (H # q), meas a) bob : Bool → Bool → Box Qubit Qubit bob x y ≡ if x && y then Z ○ X

else if x && ¬ y then X

else if ¬x && y then Z

else id

teleport : Box Qubit Qubit

teleport ≡ box q ⇒ let (a,b) ∶= bell00 $ () in let (!x,!y) ∶= alice $ (q,a) in

bob x y $ b y x bell00 ∣0⟩ ∣0⟩ H alice H meas meas bob x y

QFT : Π (n : Nat), Box (n ⊗ Qubit) (n ⊗ Qubit) QFT 0 ≡ box () ⇒ () QFT 1 ≡ box (x,()) ⇒ (H # x, ()) QFT (n'+2) ≡ box (x,xs) ⇒ let xs ∶= QFT (n'+1) $ xs let xs ∶= rotations n' (n'+1) $ (x,xs) in (H # x, xs)

rotations : Π (n m : Nat), Box (n+2 ⊗ Qubit) (n+2 ⊗ Qubit) rotations 0 ≡ box x ⇒ x

rotations 1 ≡ box x ⇒ x

rotations (n'+2) ≡ box (c,x,xs) ⇒ let (c,xs) ∶= rotations (n'+1) m $ (c,xs) in let (c,x) ∶= ctrl (R (m-n'+1)) $ (c,x) in

(c,x,xs)

Figure 6.3: Quantum Fourier transform in QNQ

Quantum Fourier transform (QFT) The quantum Fourier transform (QFT) computes the discrete Fourier transform on nqubits inO(n2) time, and is a key component in many quantum algorithms, including Shor’s factorization algorihtm. In comparison, the classical Fourier transform takes O(n2n).

The QFT is defined by induction on the number of input qubits, as shown in Figure 6.3. At the inductive step, the circuit calls out to the helper function rotations, which applies a sequence of controlled rotation gates R m, which are indexed by a natural number m. This example is adapted from an introduction to Quipper, to which we refer the reader for full details of the algorithm (Greenet al., 2013a). However, because Quipper does not have dependent quantum types, it cannot express the fact that the QFT has the same number of input and output qubits. Instead, Quipper uses lists and other data types for qubits, that must be instantiated at circuit generation time.