mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
[qsharp/en] update (#5177)
This commit is contained in:
parent
3c1b4e752d
commit
bc3598b1cd
@ -4,6 +4,7 @@ contributors:
|
|||||||
- ["Vincent van Wingerden", "https://github.com/vivanwin"]
|
- ["Vincent van Wingerden", "https://github.com/vivanwin"]
|
||||||
- ["Mariia Mykhailova", "https://github.com/tcNickolas"]
|
- ["Mariia Mykhailova", "https://github.com/tcNickolas"]
|
||||||
- ["Andrew Ryan Davis", "https://github.com/AndrewDavis1191"]
|
- ["Andrew Ryan Davis", "https://github.com/AndrewDavis1191"]
|
||||||
|
- ["Alex Hansen", "https://github.com/sezna"]
|
||||||
filename: LearnQSharp.qs
|
filename: LearnQSharp.qs
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -19,13 +20,14 @@ Q# is a high-level domain-specific language which enables developers to write qu
|
|||||||
// The most important part of quantum programs is qubits.
|
// The most important part of quantum programs is qubits.
|
||||||
// In Q# type Qubit represents the qubits which can be used.
|
// In Q# type Qubit represents the qubits which can be used.
|
||||||
// This will allocate an array of two new qubits as the variable qs.
|
// This will allocate an array of two new qubits as the variable qs.
|
||||||
using (qs = Qubit[2]) {
|
operation QuantumDataTypes() : Unit {
|
||||||
|
use qs = Qubit[2];
|
||||||
|
|
||||||
// The qubits have internal state that you cannot access to read or modify directly.
|
// The qubits have internal state that you cannot access to read or modify directly.
|
||||||
// You can inspect the current state of your quantum program
|
// You can inspect the current state of your quantum program
|
||||||
// if you're running it on a classical simulator.
|
// if you're running it on a classical simulator.
|
||||||
// Note that this will not work on actual quantum hardware!
|
// Note that this will not work on actual quantum hardware!
|
||||||
DumpMachine();
|
Std.Diagnostics.DumpMachine();
|
||||||
|
|
||||||
// If you want to change the state of a qubit
|
// If you want to change the state of a qubit
|
||||||
// you have to do this by applying quantum gates to the qubit.
|
// you have to do this by applying quantum gates to the qubit.
|
||||||
@ -58,6 +60,7 @@ using (qs = Qubit[2]) {
|
|||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
// 2. Classical data types and operators
|
// 2. Classical data types and operators
|
||||||
|
|
||||||
|
function ClassicalDataTypes() : Unit {
|
||||||
// Numbers in Q# can be stored in Int, BigInt or Double.
|
// Numbers in Q# can be stored in Int, BigInt or Double.
|
||||||
let i = 1; // This defines an Int variable i equal to 1
|
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 bi = 1L; // This defines a BigInt variable bi equal to 1
|
||||||
@ -68,7 +71,7 @@ let n = 2 * 10; // = 20
|
|||||||
// Q# does not have implicit type cast,
|
// Q# does not have implicit type cast,
|
||||||
// so to perform arithmetic on values of different types,
|
// so to perform arithmetic on values of different types,
|
||||||
// you need to cast type explicitly
|
// you need to cast type explicitly
|
||||||
let nd = IntAsDouble(2) * 1.0; // = 20.0
|
let nd = Std.Convert.IntAsDouble(2) * 1.0; // = 20.0
|
||||||
|
|
||||||
// Boolean type is called Bool
|
// Boolean type is called Bool
|
||||||
let trueBool = true;
|
let trueBool = true;
|
||||||
@ -98,20 +101,23 @@ mutable xii = true;
|
|||||||
set xii = false;
|
set xii = false;
|
||||||
|
|
||||||
// You can create an array for any data type like this
|
// You can create an array for any data type like this
|
||||||
let xiii = new Double[10];
|
let xiii = [0.0, size = 10];
|
||||||
|
|
||||||
// Getting an element from an array
|
// Getting an element from an array
|
||||||
let xiv = xiii[8];
|
let xiv = xiii[8];
|
||||||
|
|
||||||
// Assigning a new value to an array element
|
// Assigning a new value to an array element
|
||||||
mutable xv = new Double[10];
|
mutable xv = [0.0, size = 10];
|
||||||
set xv w/= 5 <- 1;
|
set xv w/= 5 <- 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
// 3. Control flow
|
// 3. Control flow
|
||||||
|
|
||||||
// If structures work a little different than most languages
|
operation ControlFlow() : Unit {
|
||||||
|
let a = 1;
|
||||||
|
// If expressions support a true branch, elif, and else.
|
||||||
if (a == 1) {
|
if (a == 1) {
|
||||||
// ...
|
// ...
|
||||||
} elif (a == 2) {
|
} elif (a == 2) {
|
||||||
@ -119,14 +125,15 @@ if (a == 1) {
|
|||||||
} else {
|
} else {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
use qubits = Qubit[2];
|
||||||
|
|
||||||
// Foreach loops can be used to iterate over an array
|
// For loops can be used to iterate over an array
|
||||||
for (qubit in qubits) {
|
for qubit in qubits {
|
||||||
X(qubit);
|
X(qubit);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regular for loops can be used to iterate over a range of numbers
|
// Regular for loops can be used to iterate over a range of numbers
|
||||||
for (index in 0 .. Length(qubits) - 1) {
|
for index in 0..Length(qubits) - 1 {
|
||||||
X(qubits[index]);
|
X(qubits[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,18 +143,18 @@ while (index < 10) {
|
|||||||
set index += 1;
|
set index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let success_criteria = true;
|
||||||
// Quantum equivalent of a while loop is a repeat-until-success loop.
|
// Quantum equivalent of a while loop is a repeat-until-success loop.
|
||||||
// Because of the probabilistic nature of quantum computing sometimes
|
// Because of the probabilistic nature of quantum computing sometimes
|
||||||
// you want to repeat a certain sequence of operations
|
// you want to repeat a certain sequence of operations
|
||||||
// until a specific condition is achieved; you can use this loop to express this.
|
// until a specific condition is achieved; you can use this loop to express this.
|
||||||
repeat {
|
repeat {
|
||||||
// Your operation here
|
// Your operation here
|
||||||
}
|
} until (success_criteria) // This could be a measurement to check if the state is reached
|
||||||
until (success criteria) // This could be a measurement to check if the state is reached
|
|
||||||
fixup {
|
fixup {
|
||||||
// Resetting to the initial conditions, if required
|
// Resetting to the initial conditions, if required
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
// 4. Putting it all together
|
// 4. Putting it all together
|
||||||
@ -169,20 +176,21 @@ operation ApplyXGateCA (source : Qubit) : Unit is Adj + Ctl {
|
|||||||
|
|
||||||
|
|
||||||
// To run Q# code, you can put @EntryPoint() before the operation you want to run first
|
// To run Q# code, you can put @EntryPoint() before the operation you want to run first
|
||||||
@EntryPoint()
|
|
||||||
operation XGateDemo() : Unit {
|
operation XGateDemo() : Unit {
|
||||||
using (q = Qubit()) {
|
use q = Qubit();
|
||||||
ApplyXGate(q);
|
ApplyXGate(q);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Here is a simple example: a quantum random number generator.
|
// Here is a simple example: a quantum random number generator.
|
||||||
// We will generate a classical array of random bits using quantum code.
|
// We will generate a classical array of random bits using quantum code.
|
||||||
@EntryPoint()
|
// Callables (functions or operations) named `Main` are used as entry points.
|
||||||
operation QRNGDemo() : Unit {
|
operation Main() : Unit {
|
||||||
mutable bits = new Int[5]; // Array we'll use to store bits
|
mutable bits = [0, size = 5]; // Array we'll use to store bits
|
||||||
using (q = Qubit()) { // Allocate a qubit
|
use q = Qubit();
|
||||||
for (i in 0 .. 4) { // Generate each bit independently
|
{
|
||||||
|
// Allocate a qubit
|
||||||
|
for i in 0..4 {
|
||||||
|
// Generate each bit independently
|
||||||
H(q); // Hadamard gate sets equal superposition
|
H(q); // Hadamard gate sets equal superposition
|
||||||
let result = M(q); // Measure qubit gets 0|1 with 50/50 prob
|
let result = M(q); // Measure qubit gets 0|1 with 50/50 prob
|
||||||
let bit = result == Zero ? 0 | 1; // Convert measurement result to integer
|
let bit = result == Zero ? 0 | 1; // Convert measurement result to integer
|
||||||
@ -196,9 +204,6 @@ operation QRNGDemo() : Unit {
|
|||||||
|
|
||||||
## Further Reading
|
## Further Reading
|
||||||
|
|
||||||
The [Quantum Katas][1] offer great self-paced tutorials and programming exercises to learn quantum computing and Q#.
|
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#.
|
||||||
|
|
||||||
[Q# Documentation][2] is official Q# documentation, including language reference and user guides.
|
[Q# Documentation](https://docs.microsoft.com/quantum/) is official Q# documentation, including language reference and user guides.
|
||||||
|
|
||||||
[1]: https://github.com/microsoft/QuantumKatas
|
|
||||||
[2]: https://docs.microsoft.com/quantum/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user