Learn Qsharp - 9 Code Examples & CST Typing Practice Test
Q# is a domain-specific programming language developed by Microsoft for expressing quantum algorithms. It is designed for quantum computing tasks, such as simulating quantum operations, quantum chemistry, and quantum cryptography, while seamlessly integrating with classical control logic.
View all 9 Qsharp code examples →
Learn QSHARP with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Qsharp
What is Qsharp?
Q# is a domain-specific programming language developed by Microsoft for expressing quantum algorithms. It is designed for quantum computing tasks, such as simulating quantum operations, quantum chemistry, and quantum cryptography, while seamlessly integrating with classical control logic.
What are the primary use cases for Qsharp?
Quantum algorithm development. Simulating quantum circuits. Quantum chemistry computations. Optimization and combinatorial problems. Integrating classical and quantum workflows
What are the strengths of Qsharp?
Purpose-built for quantum computing. High-level abstraction of quantum mechanics. Integration with Microsoft Quantum Development Kit. Strong tooling: simulators, resource estimators, debuggers. Cross-platform support with .NET and Python bindings
What are the limitations of Qsharp?
Requires classical host for orchestration. Cannot directly execute on non-Microsoft quantum hardware without adapters. Limited debugging compared to classical languages. High learning curve for non-quantum developers. Dependent on QDK and simulators for development
How can I practice Qsharp typing speed?
CodeSpeedTest offers 9+ real Qsharp code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.