mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Adjusting formatting to clean up carriage return and multi line comment
The line limit is making things a bit skewed, and adding multi line comment
This commit is contained in:
parent
7e28066384
commit
ceefb4adf5
@ -3,6 +3,7 @@ language: Q#
|
|||||||
contributors:
|
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"]
|
||||||
filename: LearnQSharp.qs
|
filename: LearnQSharp.qs
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -13,6 +14,11 @@ This is the new outline
|
|||||||
```C#
|
```C#
|
||||||
// Single-line comments start with //
|
// Single-line comments start with //
|
||||||
|
|
||||||
|
/
|
||||||
|
Multi-line comments
|
||||||
|
like so
|
||||||
|
\
|
||||||
|
|
||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
// 1. Quantum data types and operators
|
// 1. Quantum data types and operators
|
||||||
|
|
||||||
@ -22,27 +28,33 @@ This is the new outline
|
|||||||
using (qs = Qubit[2]) {
|
using (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 if you're running it on a classical simulator.
|
// You can inspect the current state of your quantum program
|
||||||
|
// 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();
|
DumpMachine();
|
||||||
|
|
||||||
// If you want to change the state of a qubit, you have to do this by applying quantum gates to the qubit.
|
// If you want to change the state of a qubit
|
||||||
H(q[0]); // This changes the state of the first qubit from |0⟩ (the initial state of allocated qubits) to (|0⟩ + |1⟩) / sqrt(2).
|
// you have to do this by applying quantum gates to the qubit.
|
||||||
|
H(q[0]); // This changes the state of the first qubit
|
||||||
|
// from |0⟩ (the initial state of allocated qubits) to (|0⟩ + |1⟩) / sqrt(2).
|
||||||
// q[1] = |1⟩; - this does NOT work, you have to manipulate a qubit by using gates.
|
// q[1] = |1⟩; - this does NOT work, you have to manipulate a qubit by using gates.
|
||||||
|
|
||||||
// You can apply multi-qubit gates to several qubits.
|
// You can apply multi-qubit gates to several qubits.
|
||||||
CNOT(qs[0], qs[1]);
|
CNOT(qs[0], qs[1]);
|
||||||
|
|
||||||
// You can also apply a controlled version of a gate: a gate that is applied if all control qubits are in |1⟩ state.
|
/ You can also apply a controlled version of a gate:
|
||||||
// The first argument is an array of control qubits, the second argument is the target qubit.
|
a gate that is applied if all control qubits are in |1⟩ state.
|
||||||
|
\ The first argument is an array of control qubits, the second argument is the target qubit.
|
||||||
Controlled Y([qs[0]], qs[1]);
|
Controlled Y([qs[0]], qs[1]);
|
||||||
|
|
||||||
// If you want to apply an anti-controlled gate (a gate that is applied if all control qubits are in |0⟩ state), you can use a library function.
|
/ If you want to apply an anti-controlled gate
|
||||||
|
(a gate that is applied if all control qubits are in |0⟩ state),
|
||||||
|
\ you can use a library function.
|
||||||
ApplyControlledOnInt(0, X, [qs[0]], qs[1]);
|
ApplyControlledOnInt(0, X, [qs[0]], qs[1]);
|
||||||
|
|
||||||
// To read the information from the quantum system, you use measurements.
|
/ To read the information from the quantum system, you use measurements.
|
||||||
// Measurements return a value of Result data type: Zero or One.
|
Measurements return a value of Result data type: Zero or One.
|
||||||
// You can print measurement results as a classical value.
|
\ You can print measurement results as a classical value.
|
||||||
Message($"Measured {M(qs[0])}, {M(qs[1])}");
|
Message($"Measured {M(qs[0])}, {M(qs[1])}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +69,8 @@ let d = 1.0; // This defines a Double variable d equal to 1
|
|||||||
|
|
||||||
// Arithmetic is done as expected, as long as the types are the same
|
// Arithmetic is done as expected, as long as the types are the same
|
||||||
let n = 2 * 10; // = 20
|
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
|
// Q# does not have implicit type cast,
|
||||||
|
// so to perform arithmetic on values of different types, you need to cast type explicitly
|
||||||
let nd = IntAsDouble(2) * 1.0; // = 20.0
|
let nd = IntAsDouble(2) * 1.0; // = 20.0
|
||||||
|
|
||||||
// Boolean type is called Bool
|
// Boolean type is called Bool
|
||||||
@ -78,9 +91,9 @@ let x = 10 == 15; // is false
|
|||||||
// Range is a sequence of integers and can be defined like: start..step..stop
|
// 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
|
let xi = 1..2..7; // Gives the sequence 1,3,5,7
|
||||||
|
|
||||||
// Assigning new value to a variable:
|
/ Assigning new value to a variable:
|
||||||
// by default all Q# variables are immutable;
|
by default all Q# variables are immutable;
|
||||||
// if the variable was defined using let, you cannot reassign its value.
|
\ if the variable was defined using let, you cannot reassign its value.
|
||||||
|
|
||||||
// When you want to make a variable mutable, you have to declare it as such,
|
// When you want to make a variable mutable, you have to declare it as such,
|
||||||
// and use the set word to update value
|
// and use the set word to update value
|
||||||
@ -126,9 +139,10 @@ while (index < 10) {
|
|||||||
set index += 1;
|
set index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 until a specific condition is achieved; you can use this loop to express this.
|
you want to repeat a certain sequence of operations
|
||||||
|
\ until a specific condition is achieved; you can use this loop to express this.
|
||||||
repeat {
|
repeat {
|
||||||
// Your operation here
|
// Your operation here
|
||||||
}
|
}
|
||||||
@ -146,10 +160,10 @@ operation ApplyXGate(source : Qubit) : Unit {
|
|||||||
X(source);
|
X(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the operation implements a unitary transformation, you can define
|
/ If the operation implements a unitary transformation, you can define
|
||||||
// adjoint and controlled variants of it.
|
adjoint and controlled variants of it.
|
||||||
// The easiest way to do that is to add "is Adj + Ctl" after Unit.
|
The easiest way to do that is to add "is Adj + Ctl" after Unit.
|
||||||
// This will tell the compiler to generate the variants automatically.
|
\ This will tell the compiler to generate the variants automatically.
|
||||||
operation ApplyXGateCA (source : Qubit) : Unit is Adj + Ctl {
|
operation ApplyXGateCA (source : Qubit) : Unit is Adj + Ctl {
|
||||||
X(source);
|
X(source);
|
||||||
}
|
}
|
||||||
@ -169,16 +183,16 @@ operation XGateDemo() : Unit {
|
|||||||
// 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()
|
@EntryPoint()
|
||||||
operation QRNGDemo() : Unit {
|
operation QRNGDemo() : Unit {
|
||||||
mutable bits = new Int[5]; // Array we'll use to store bits
|
mutable bits = new Int[5]; / Array we'll use to store bits
|
||||||
using (q = Qubit()) { // Allocate a qubit
|
using (q = Qubit()) { / Allocate a qubit
|
||||||
for (i in 0 .. 4) { // Generate each bit independently
|
for (i in 0 .. 4) { / Generate each bit independently
|
||||||
H(q); // Apply Hadamard gate to prepare equal superposition
|
H(q); / Apply Hadamard gate prepares equal superposition
|
||||||
let result = M(q); // Measure the qubit to get Zero or One with 50/50 probability
|
let result = M(q); / Measure the qubit to get 0 or 1 with 50/50 prob
|
||||||
let bit = result == Zero ? 0 | 1; // Convert measurement result to an integer
|
let bit = result == Zero ? 0 | 1; / Convert measurement result to an integer
|
||||||
set bits w/= i <- bit; // Write generated bit to an array
|
set bits w/= i <- bit; / Write generated bit to an array
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message($"{bits}"); // Print the result
|
Message($"{bits}"); / Print the result
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user