running the quantum katas - eth z · aka.ms/quantum-katas download the quantum katas open...
TRANSCRIPT
![Page 1: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/1.jpg)
INSTALL GUIDE
aka.ms/qdk-install
Install .NET Core SDK
Install Visual Studio Code + Extension
INSTALLING THE QUANTUM DEVELOPMENT KIT
Install Project Templates
Download the Quantum Katas
Open a kata folder in VS Code
RUNNING THE QUANTUM KATAS
Run dotnet test
KATA DOWNLOAD
aka.ms/quantum-katas
While everyone settles in, feel free to get
started.
![Page 2: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/2.jpg)
![Page 3: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/3.jpg)
![Page 4: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/4.jpg)
![Page 5: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/5.jpg)
Practical Cost Estimates
![Page 6: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/6.jpg)
Fe₂S₂
!3,000
1
Progress made through
• Better quantum algorithms
• More complete cost estimates
• Improved classical statistics
![Page 7: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/7.jpg)
Practical Cost Estimates
Debugging Quantum Algorithms
![Page 8: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/8.jpg)
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
—Brian Kernighan
![Page 9: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/9.jpg)
Practical Cost Estimates
Debugging Quantum Algorithms
Algorithms to Quantum Applications
![Page 10: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/10.jpg)
full-stack
![Page 11: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/11.jpg)
![Page 12: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/12.jpg)
![Page 13: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/13.jpg)
![Page 14: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/14.jpg)
![Page 15: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/15.jpg)
![Page 16: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/16.jpg)
I’ll be showing Visual Studio Code today, but if you’re using Windows 10 and would prefer to use Visual Studio 2017, great!
Full documentation for using Visual Studio 2017 is available at aka.ms/quantum-katas.
![Page 17: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/17.jpg)
OPERATIONS
FUNCTIONS
•
•
•
•
•
docs.microsoft.com/quantum
![Page 18: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/18.jpg)
Q# Operations and Functions
C# Host Program
Q# Operations and Functions
Q# Operations and Functions
host program
![Page 19: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/19.jpg)
projects
SOURCE CODE
project fileclassical (C#) source
quantum (Q#) source
COMPILED CODE
bin/Debug/
obj/compiled quantum app
![Page 20: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/20.jpg)
projects
SOURCE CODE
project fileclassical (C#) source
quantum (Q#) source
COMPILED CODE
bin/Debug/
obj/compiled quantum app
You may also see a solution file(*.sln).
This collects one or more related projects for loading in Visual Studio 2017.
![Page 21: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/21.jpg)
dotnet
![Page 22: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/22.jpg)
dotnet dotnet helpdotnet cmd --help
dotnet newMakes a new project.
dotnet buildDownloads all the packages needed by a project and runs the compiler.
dotnet runRuns the project, rebuilding if necessary.
dotnet test
dotnet add packageInstalls a new package into the project.
![Page 23: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/23.jpg)
Let’s start by seeing how Q# and C# work
together to make a complete program.
![Page 24: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/24.jpg)
Int
Double
Bool
Result
Pauli
Range
String
![Page 25: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/25.jpg)
Qubit
•
•
•
![Page 26: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/26.jpg)
Qubit states can be represented as vectors,
0 ≔10, 1 ≔
01.
Operations then transform qubit states by matrix multiplication.
E.g.:
𝐻 ≔1 11 −1
/ 2, 𝑇 ≔1 0
0 𝑒𝑖𝜋4
.
CONCEPTUAL INTRODUCTION AT
aka.ms/quantum-docs/the-qubit
![Page 27: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/27.jpg)
Let’s make things quantum by generating
some random numbers using the
|+⟩ ≔ (|0⟩ + |1⟩) / √2 state.
![Page 28: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/28.jpg)
operation Result {
body {
mutable Zero;
using Qubit {
;
set ;
;
}
return
}
}
// Prepare in |0⟩.
// 𝐻|0⟩ = |+⟩.
// Measure ⟨0|.
// Reset to |0⟩.
// Return to// classical host.
![Page 29: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/29.jpg)
Let’s look closer at what’s going on when we call the Hoperation.
operation NextRandomBit() : Result {
body {
mutable result = Zero;
using (qubits = Qubit[1]) {
;
set result = M(qubits[0]);
Reset(qubits[0]);
}
return result;
}
}
// Prepare in |0⟩.
// 𝐻|0⟩ = |+⟩.
// Measure ⟨0|.
// Reset to |0⟩.
// Return to// classical host.
![Page 30: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/30.jpg)
INPUT IS A STATE
INPUT HAS A STATE
•
•
•
![Page 31: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/31.jpg)
![Page 32: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/32.jpg)
operation NextRandomBit() : Result {
body {
mutable result = Zero;
using Qubit {
H(qubits[0]);
set result = M(qubits[0]);
;
}
return result;
}
}
// Prepare in |0⟩.
// H|0⟩ = |+⟩.
// Measure ⟨0|.
// Reset to |0⟩.
// Return to// classical host.
This works because we agree to always prepare qubits in the same state, labeled |0⟩.
![Page 33: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/33.jpg)
1 0 0 00 1 0 00 0 0 10 0 1 0
CONCEPTUAL INTRODUCTION AT
aka.ms/quantum-docs/multiple-qubits
![Page 34: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/34.jpg)
We now have everything we need to
make and measure pairs of entangled
qubits.
![Page 35: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/35.jpg)
Q# can help us think conceptually about
algorithms.
![Page 36: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/36.jpg)
![Page 37: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/37.jpg)
![Page 38: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/38.jpg)
![Page 39: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/39.jpg)
![Page 40: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/40.jpg)
MOV cx,10loop:
; …dec cxjnz loop
![Page 41: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/41.jpg)
quantum
think
![Page 42: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/42.jpg)
let PhaseIncrement = With(QFT, Increment);
![Page 43: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/43.jpg)
// U⁺ is an example of the adjoint functor.
operation (left : Qubit, right : Qubit) : () {
Controlled
adjoint
}
// Can undo the operation with (Adjoint EPR).
![Page 44: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/44.jpg)
operation (
(Qubit[] => () : ),
(Qubit[] => () : ),
Qubit[]
) : () {
body {
outer // 𝑈
inner // 𝑉
(Adjoint outer) // 𝑈⁺
}
}
Communicates the algorithmic pattern 𝑈𝑉𝑈⁺.
E.g.:let PhaseIncrement
= With(QFT, Increment);
![Page 45: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/45.jpg)
![Page 46: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/46.jpg)
// Using type parameters, we can express
// that a concept applies across different types.
function <'T, 'U> 'T 'U 'T {
let
return
}
let // pair : (Int, String)
// Returns 3.
![Page 47: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/47.jpg)
SINGLETON–TUPLE EQUIVALENCE
function Map<'T, 'U>(fn : 'T -> 'U, in : 'T[]) : 'U[] {let n = Length(arr);mutable out = new 'U[n];for (idx in 0..n – 1) { set out[idx] = fn(in[idx]); }return out;
}
'T and ('T) are exactly the same type for all 'T.Thus, all functions take and return a single tuple.
EXAMPLE
![Page 48: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/48.jpg)
operation <'T>( 'T => () ) : () {
body {
for (idx in 0..Length(targets) – 1) { op }
}
}
('T => () : Adjoint, Controlled)
EXAMPLE
![Page 49: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/49.jpg)
operation QuantumSearch(nIterations : Int, markedQubit : Qubit, databaseRegister: Qubit[]) : () {
body {
StatePreparationOracle(markedQubit, databaseRegister);
// Loop over Grover iterates.
for (idx in 0..nIterations - 1) {
ReflectMarked(markedQubit);
ReflectStart(markedQubit, databaseRegister);
}
}
}
FULL SAMPLE AT
aka.ms/quantum-samples/DatabaseSearch
We can’t directly pass |ψ⟩, but we can pass an operation 𝑂 that prepares |ψ⟩when given qubits in |0⟩.
![Page 50: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/50.jpg)
We can make teleport much more
reusable with first-class operations.
![Page 51: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/51.jpg)
{
}
let Fold _
// Returns 12.
// Returns [11; 13; 15].
The same features also make it easier to express some kinds of classical processing in terms of composition and combinations of functions.
![Page 52: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/52.jpg)
operation
target : Qubit[]
{ … }
__
target
We can make new functions and operations by specifying a subset of the inputs to a function or operation.
This lets us quickly combine existing callablesto work well together.
![Page 53: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/53.jpg)
function ComposeImpl<'T, 'U, 'V>(outer : ('U -> 'V), inner : ('T -> 'U), target : 'T) : 'V
{ return outer(inner(target)); }
function Compose<'T, 'U, 'V>
(outer : ('U -> 'V), inner : ('T -> 'U)): ('T -> 'V) {
return ComposeImpl(outer, inner, _);
}
EXAMPLE
![Page 54: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/54.jpg)
operation ApplyTwice<'T>(op : 'T => (), target : 'T) : () {
body { op(target); op(target); }
}function SquareOp<'T>(op : 'T => ()) : ('T => ()) {
return ApplyTwice(op, _);
}
EXAMPLE
SquareOp has type ('T => ()) -> ('T => ()).
![Page 55: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/55.jpg)
operation (
('T => () : ),
('T => () : ),
) : () {
body {
outer // 𝑈
inner // 𝑉
(Adjoint outer) // 𝑈⁺
}
}
Can quickly express ideas like “conjugate the operation 𝑈 by a Hadamard on each qubit.”
let Conjugate = WithCA(ApplyToEach(H, _),_
);
Conjugate(U);
![Page 56: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/56.jpg)
We can quickly combine different
operations together using partial
application.
![Page 57: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/57.jpg)
![Page 58: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/58.jpg)
Read the Docs.
OPERATION AND FUNCTION REFERENCE
aka.ms/qsharp-ref
![Page 59: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/59.jpg)
INTERPOLATED STRINGS
function Hello() : () {let x = 42;Message($"Hello, {x}."); // Prints “Hello, 42.” to the console.
}
$"{expr}" inserts diagnostic information about expr into a String for use with Message.
EXAMPLE
![Page 60: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/60.jpg)
ANTHROPIC PRINCIPLE
u (msg);TeleportMessage (msg, there);(Adjoint u)(there);
AssertQubit(Zero, there);
Observed effects must be compatible with their being an observer.
EXAMPLE
We used AssertQubit earlier to test the validity of our teleport operation.
![Page 61: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/61.jpg)
operation IdentityTeleport(q : Qubit[]) : () {body { using (aux = Qubit[1]) {
Teleport(q[0], aux[0]);SWAP(q[0], aux[0]);
} }}
operation TeleportationTest() : () {body {
// Process assertions are a// special case of state assertions.AssertOperationsEqualReferenced(
IdentityTeleport, NoOp, 1);}
}
EXAMPLE
FULL SAMPLE AT
aka.ms/quantum-samples/UnitTesting
![Page 62: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/62.jpg)
•
•
•
The only observable effect of functions is their return value.
DumpMachine and Message return (), which has only one valid value ().
This allows for calls to be stripped with no observable consequences.
![Page 63: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/63.jpg)
Let’s see DumpMachine in action.
![Page 64: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/64.jpg)
![Page 65: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/65.jpg)
KATA DOWNLOAD AND INSTRUCTIONS
aka.ms/quantum-katas
Download the Quantum Katas
Open QuantumKatas.vscode-workspace.
RUNNING THE QUANTUM KATAS
Run Tasks: Run Test Task from theCommand Palette.
![Page 66: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/66.jpg)
KATA DOWNLOAD AND INSTRUCTIONS
aka.ms/quantum-katas
Download the Quantum Katas
Open QuantumKatas.vscode-workspace.
RUNNING THE QUANTUM KATAS
Run Tasks: Run Test Task from theCommand Palette.
• If you get stuck in Visual Studio Code, search the Command Palette.Ctrl + Shift + P or ⌘ + Shift + P
• ReferenceImplementation.qshas the solutions — don’t look unless you get really stuck!
• Your quick references have a lot of useful information, including function and operation summaries and links to documentation.
HINTS
![Page 67: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/67.jpg)
KATA DOWNLOAD AND INSTRUCTIONS
aka.ms/quantum-katas
Download the Quantum Katas
Open QuantumKatas.vscode-workspace.
RUNNING THE QUANTUM KATAS
Run Tasks: Run Test Task from theCommand Palette.
![Page 68: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/68.jpg)
KATA DOWNLOAD AND INSTRUCTIONS
aka.ms/quantum-katas
Download the Quantum Katas
Open QuantumKatas.vscode-workspace.
RUNNING THE QUANTUM KATAS
Run Tasks: Run Test Task from theCommand Palette.
![Page 69: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/69.jpg)
KATA DOWNLOAD AND INSTRUCTIONS
aka.ms/quantum-katas
Download the Quantum Katas
Open QuantumKatas.vscode-workspace.
RUNNING THE QUANTUM KATAS
Run Tasks: Run Test Task from theCommand Palette.
![Page 70: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/70.jpg)
KATA DOWNLOAD AND INSTRUCTIONS
aka.ms/quantum-katas
Download the Quantum Katas
Open QuantumKatas.vscode-workspace.
RUNNING THE QUANTUM KATAS
Run Tasks: Run Test Task from theCommand Palette.
![Page 71: RUNNING THE QUANTUM KATAS - ETH Z · aka.ms/quantum-katas Download the Quantum Katas Open QuantumKatas.vscode-workspace. RUNNING THE QUANTUM KATAS Run Tasks: Run Test Task from the](https://reader035.vdocuments.us/reader035/viewer/2022062317/5ec14ee98bf3fd09670a5757/html5/thumbnails/71.jpg)