2020-08-06 10:42:22 +03:00
---
language: Q#
contributors:
- ["Vincent van Wingerden", "https://github.com/vivanwin"]
- ["Mariia Mykhailova", "https://github.com/tcNickolas"]
2020-08-07 00:15:59 +03:00
- ["Andrew Ryan Davis", "https://github.com/AndrewDavis1191"]
2024-11-15 00:20:21 +03:00
- ["Alex Hansen", "https://github.com/sezna"]
2020-08-06 10:42:22 +03:00
filename: LearnQSharp.qs
---
Q# is a high-level domain-specific language which enables developers to write quantum algorithms. Q# programs can be executed on a quantum simulator running on a classical computer and (in future) on quantum computers.
2024-04-03 14:16:08 +03:00
```c#
2020-08-06 10:42:22 +03:00
// Single-line comments start with //
2020-08-07 00:15:59 +03:00
2020-08-06 10:42:22 +03:00
/////////////////////////////////////
// 1. Quantum data types and operators
2024-11-15 00:20:21 +03:00
// The most important part of quantum programs is qubits.
2020-08-06 10:42:22 +03:00
// In Q# type Qubit represents the qubits which can be used.
// This will allocate an array of two new qubits as the variable qs.
2024-11-15 00:20:21 +03:00
operation QuantumDataTypes() : Unit {
use qs = Qubit[2];
2020-08-06 10:42:22 +03:00
// The qubits have internal state that you cannot access to read or modify directly.
2024-11-15 00:20:21 +03:00
// You can inspect the current state of your quantum program
2020-08-07 00:15:59 +03:00
// if you're running it on a classical simulator.
2020-08-06 10:42:22 +03:00
// Note that this will not work on actual quantum hardware!
2024-11-15 00:20:21 +03:00
Std.Diagnostics.DumpMachine();
2020-08-06 10:42:22 +03:00
2020-08-07 00:15:59 +03:00
// If you want to change the state of a qubit
// you have to do this by applying quantum gates to the qubit.
2024-11-15 00:20:21 +03:00
H(qs[0]); // This changes the state of the first qubit
// from |0⟩ (the initial state of allocated qubits)
// to (|0⟩ + |1⟩) / sqrt(2).
2021-11-02 00:22:59 +03:00
// qs[1] = |1⟩; - this does NOT work, you have to manipulate a qubit by using gates.
2020-08-06 10:42:22 +03:00
// You can apply multi-qubit gates to several qubits.
CNOT(qs[0], qs[1]);
2024-11-15 00:20:21 +03:00
// You can also apply a controlled version of a gate:
2020-08-07 00:26:18 +03:00
// a gate that is applied if all control qubits are in |1⟩ state.
2024-11-15 00:20:21 +03:00
// The first argument is an array of control qubits,
2020-08-07 00:50:39 +03:00
// the second argument is the target qubit.
2024-11-15 00:20:21 +03:00
Controlled Y([qs[0]], qs[1]);
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// If you want to apply an anti-controlled gate
// (a gate that is applied if all control qubits are in |0⟩ state),
2020-08-07 00:26:18 +03:00
// you can use a library function.
2020-08-06 10:42:22 +03:00
ApplyControlledOnInt(0, X, [qs[0]], qs[1]);
2020-08-07 00:26:18 +03:00
// To read the information from the quantum system, you use measurements.
// Measurements return a value of Result data type: Zero or One.
// You can print measurement results as a classical value.
2020-08-06 10:42:22 +03:00
Message($"Measured {M(qs[0])}, {M(qs[1])}");
}
/////////////////////////////////////
// 2. Classical data types and operators
2024-11-15 00:20:21 +03:00
function ClassicalDataTypes() : Unit {
// Numbers in Q# can be stored in Int, BigInt or Double.
let i = 1; // This defines an Int variable i equal to 1
let bi = 1L; // This defines a BigInt variable bi equal to 1
let d = 1.0; // This defines a Double variable d equal to 1
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Arithmetic is done as expected, as long as the types are the same
let n = 2 * 10; // = 20
// Q# does not have implicit type cast,
// so to perform arithmetic on values of different types,
// you need to cast type explicitly
let nd = Std.Convert.IntAsDouble(2) * 1.0; // = 20.0
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Boolean type is called Bool
let trueBool = true;
let falseBool = false;
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Logic operators work as expected
let andBool = true and false;
let orBool = true or false;
let notBool = not false;
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Strings
let str = "Hello World!";
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Equality is ==
let x = 10 == 15; // is false
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Range is a sequence of integers and can be defined like: start..step..stop
let xi = 1..2..7; // Gives the sequence 1,3,5,7
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Assigning new value to a variable:
// by default all Q# variables are immutable;
// if the variable was defined using let, you cannot reassign its value.
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// When you want to make a variable mutable, you have to declare it as such,
// and use the set word to update value
mutable xii = true;
set xii = false;
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// You can create an array for any data type like this
let xiii = [0.0, size = 10];
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Getting an element from an array
let xiv = xiii[8];
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Assigning a new value to an array element
mutable xv = [0.0, size = 10];
set xv w/= 5 < - 1 . 0 ;
}
2020-08-06 10:42:22 +03:00
/////////////////////////////////////
// 3. Control flow
2024-11-15 00:20:21 +03:00
operation ControlFlow() : Unit {
let a = 1;
// If expressions support a true branch, elif, and else.
if (a == 1) {
// ...
} elif (a == 2) {
// ...
} else {
// ...
}
use qubits = Qubit[2];
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// For loops can be used to iterate over an array
for qubit in qubits {
X(qubit);
}
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// Regular for loops can be used to iterate over a range of numbers
for index in 0..Length(qubits) - 1 {
X(qubits[index]);
}
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
// While loops are restricted for use in classical context only
mutable index = 0;
while (index < 10 ) {
set index += 1;
}
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
let success_criteria = true;
// Quantum equivalent of a while loop is a repeat-until-success loop.
// Because of the probabilistic nature of quantum computing sometimes
// you want to repeat a certain sequence of operations
// until a specific condition is achieved; you can use this loop to express this.
repeat {
// Your operation here
} until (success_criteria) // This could be a measurement to check if the state is reached
fixup {
// Resetting to the initial conditions, if required
}
2020-08-06 10:42:22 +03:00
}
/////////////////////////////////////
// 4. Putting it all together
// Q# code is written in operations and functions
operation ApplyXGate(source : Qubit) : Unit {
X(source);
}
2024-11-15 00:20:21 +03:00
// If the operation implements a unitary transformation, you can define
// adjoint and controlled variants of it.
// The easiest way to do that is to add "is Adj + Ctl" after Unit.
2020-08-07 00:26:18 +03:00
// This will tell the compiler to generate the variants automatically.
2024-11-15 00:20:21 +03:00
operation ApplyXGateCA(source : Qubit) : Unit is Adj + Ctl {
2020-08-06 10:42:22 +03:00
X(source);
}
// Now you can call Adjoint ApplyXGateCA and Controlled ApplyXGateCA.
// To run Q# code, you can put @EntryPoint () before the operation you want to run first
operation XGateDemo() : Unit {
2024-11-15 00:20:21 +03:00
use q = Qubit();
ApplyXGate(q);
2020-08-06 10:42:22 +03:00
}
2024-11-15 00:20:21 +03:00
// Here is a simple example: a quantum random number generator.
2020-08-06 10:42:22 +03:00
// We will generate a classical array of random bits using quantum code.
2024-11-15 00:20:21 +03:00
// Callables (functions or operations) named `Main` are used as entry points.
operation Main() : Unit {
mutable bits = [0, size = 5]; // Array we'll use to store bits
use q = Qubit();
{
// Allocate a qubit
for i in 0..4 {
// Generate each bit independently
2020-08-07 00:50:39 +03:00
H(q); // Hadamard gate sets equal superposition
let result = M(q); // Measure qubit gets 0|1 with 50/50 prob
let bit = result == Zero ? 0 | 1; // Convert measurement result to integer
2020-08-07 00:26:18 +03:00
set bits w/= i < - bit ; / / Write generated bit to an array
2020-08-06 10:42:22 +03:00
}
}
2020-08-07 00:26:18 +03:00
Message($"{bits}"); // Print the result
2020-08-06 10:42:22 +03:00
}
```
## Further Reading
2024-11-15 00:20:21 +03:00
The Quantum Katas ([repo](https://github.com/microsoft/qsharp/tree/main/katas) [hosted tutorials ](https://quantum.microsoft.com/en-us/tools/quantum-katas ) offer great self-paced tutorials and programming exercises to learn quantum computing and Q#.
2020-08-06 10:42:22 +03:00
2024-11-15 00:20:21 +03:00
[Q# Documentation ](https://docs.microsoft.com/quantum/ ) is official Q# documentation, including language reference and user guides.