Learn QSHARP with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
Q# Counter and Theme Toggle - Basic
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterBasic() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI() : Unit {
Message($"Counter: {count}");
Message($"Theme: {(if isDark then "Dark" else "Light")}");
}
operation Increment() : Unit {
set count += 1;
UpdateUI();
}
operation ToggleTheme() : Unit {
set isDark = not isDark;
UpdateUI();
}
UpdateUI();
Increment();
ToggleTheme();
}
}
Basic counter with theme toggle using Q# operations.
2
Q# Counter and Theme Toggle - With Decrement
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterWithDecrement() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI() : Unit {
Message($"Counter: {count}");
Message($"Theme: {(if isDark then "Dark" else "Light")}");
}
operation Increment() : Unit { set count += 1; UpdateUI(); }
operation Decrement() : Unit { set count -= 1; UpdateUI(); }
operation ToggleTheme() : Unit { set isDark = not isDark; UpdateUI(); }
UpdateUI();
Increment();
Increment();
Decrement();
ToggleTheme();
}
}
Adds a decrement operation.
3
Q# Counter and Theme Toggle - Reset Example
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterWithReset() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI() : Unit { Message($"Counter: {count}"); Message($"Theme: {(if isDark then "Dark" else "Light")}"); }
operation Increment() : Unit { set count += 1; UpdateUI(); }
operation Reset() : Unit { set count = 0; UpdateUI(); }
operation ToggleTheme() : Unit { set isDark = not isDark; UpdateUI(); }
UpdateUI();
Increment();
Increment();
Reset();
ToggleTheme();
}
}
Adds a reset operation to reset the counter.
4
Q# Counter and Theme Toggle - Inline Toggle
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterInlineToggle() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI() : Unit { Message($"Counter: {count}"); Message($"Theme: {(if isDark then "Dark" else "Light")}"); }
operation IncrementAndToggle() : Unit { set count += 1; set isDark = not isDark; UpdateUI(); }
UpdateUI();
IncrementAndToggle();
}
}
Toggles theme inline during increment or decrement.
5
Q# Counter and Theme Toggle - Conditional Display
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterConditional() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI() : Unit {
Message($"Counter: {count}");
Message($"Theme: {(if isDark then "Dark" else "Light")}");
if (count == 3) { Message("Reached 3!"); }
}
operation Increment() : Unit { set count += 1; UpdateUI(); }
operation ToggleTheme() : Unit { set isDark = not isDark; UpdateUI(); }
UpdateUI();
Increment();
Increment();
Increment();
ToggleTheme();
}
}
Displays extra message when counter reaches 3.
6
Q# Counter and Theme Toggle - Loop Simulation
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterLoop() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI() : Unit { Message($"Counter: {count}"); Message($"Theme: {(if isDark then "Dark" else "Light")}"); }
operation Increment() : Unit { set count += 1; UpdateUI(); }
operation ToggleTheme() : Unit { set isDark = not isDark; UpdateUI(); }
for (i in 1..3) { Increment(); }
ToggleTheme();
}
}
Uses a for loop to increment counter multiple times.
7
Q# Counter and Theme Toggle - Parameterized Update
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterParam() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI(c: Int) : Unit { Message($"Counter: {c}"); Message($"Theme: {(if isDark then "Dark" else "Light")}"); }
operation Increment() : Unit { set count += 1; UpdateUI(count); }
operation ToggleTheme() : Unit { set isDark = not isDark; UpdateUI(count); }
UpdateUI(count);
Increment();
ToggleTheme();
}
}
Passes count as parameter to UpdateUI operation.
8
Q# Counter and Theme Toggle - Combined Increment and Toggle
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterCombined() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI() : Unit { Message($"Counter: {count}"); Message($"Theme: {(if isDark then "Dark" else "Light")}"); }
operation IncrementAndToggle() : Unit { set count += 1; set isDark = not isDark; UpdateUI(); }
UpdateUI();
IncrementAndToggle();
IncrementAndToggle();
}
}
Combines increment and toggle in a single operation.
9
Q# Counter and Theme Toggle - With Reset and Loop
namespace QuantumExamples {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
operation CounterLoopReset() : Unit {
mutable count = 0;
mutable isDark = false;
operation UpdateUI() : Unit { Message($"Counter: {count}"); Message($"Theme: {(if isDark then "Dark" else "Light")}"); }
operation Increment() : Unit { set count += 1; UpdateUI(); }
operation Reset() : Unit { set count = 0; UpdateUI(); }
operation ToggleTheme() : Unit { set isDark = not isDark; UpdateUI(); }
for (i in 1..5) { Increment(); }
Reset();
ToggleTheme();
}
}
Combines loop increments and reset operation.