Qubit.NET is a lightweight quantum circuit simulation library written in C#. It allows users to simulate quantum circuits up to 30 qubits, initialize qubits, apply common quantum gates, and measure results β all using a classical computer. Perfect for learning, prototyping, or integrating quantum logic into .NET applications.
- .NET 6.0 or newer
System.Numerics
(for complex numbers β included in .NET)
Clone or download the repository:
git clone https://github.com/InfoTCube/Qubit.Net.git
cd Qubit.NET
Add the project to your solution or include the .cs
files (QuantumCircuit.cs
, QuantumGates.cs
, etc.) in your C# project.
using Qubit.Net;
//qubits are created in 0 state
var qc = new QuantumCircuit(2);
// Apply Hadamard to qubit 0
qc.H(0);
// Apply CNOT (qubit 0 β control, qubit 1 β target)
qc.CNOT(0, 1);
// Draw a circuit
qc.Draw();
// Measure full state
Console.WriteLine($"Measured: {qc.Measure()}"); // Possible: 00 or 11
You can initialize any qubit to one of the predefined basis states:
|0β©
βState.Zero
|1β©
βState.One
|+β©
βState.Plus
|ββ©
βState.Minus
qc.Initialize(0, State.Minus);
or in any custom state
qc.Initialize(0, new Complex(1, 1), new Complex(2, 2));
β οΈ Initialization can only be done before any gate is applied to that qubit.
This is internally tracked using a private_isQubitModified
array.
Qubit.NET includes several built-in quantum gates:
Method | Description |
---|---|
I(q) |
Identity |
H(q) |
Hadamard |
X(q) |
Pauli-X (NOT) |
Y(q) |
Pauli-Y |
Z(q) |
Pauli-Z |
S(q) |
Phase gate (βZ) |
Sdag(q) |
Conjugate transpose of S (Sβ ) |
T(q) |
T gate (fourth root of Z) |
Tdag(q) |
Conjugate transpose of T (Tβ ) |
Rx(q, ΞΈ) |
Rotation around X-axis by angle ΞΈ |
Ry(q, ΞΈ) |
Rotation around Y-axis by angle ΞΈ |
Rz(q, ΞΈ) |
Rotation around Z-axis by angle ΞΈ |
SX(q) |
Square-root of Pauli-X (βX) |
SY(q) |
Square-root of Pauli-Y (βY) |
SZ(q) |
Square-root of Pauli-Z (βZ), aka S gate |
U3(q, ΞΈ, Ο, Ξ») |
General single-qubit rotation gate |
qc.H(0);
qc.X(1);
Method | Description |
---|---|
CNOT(c, t) |
Controlled-NOT gate |
CY(c, t) |
Controlled-Y gate |
CZ(c, t) |
Controlled-Z gate |
CH(c, t) |
Controlled-Hadamard gate |
CRx(c, t, ΞΈ) |
Controlled-Rx gate |
CRy(c, t, ΞΈ) |
Controlled-Ry gate |
CRz(c, t, ΞΈ) |
Controlled-Rz gate |
CU3(c, t, ΞΈ, Ο, Ξ») |
Controlled-U3 gate |
SWAP(q1, q2) |
SWAP gate (exchanges qubits) |
qc.CNOT(0, 1);
Method | Description |
---|---|
Toffoli(c1, c2, t) |
Toffoli (CC-NOT) gate |
Fredkin(c, t1, t2) |
Fredkin (C-SWAP) gate |
qc.Toffoli(0, 1, 2);
qc.Fredkin(0, 1, 2);
You can custom gates for 1-4 qubits. Remember that matrix must be a square matrix of size 2^n x 2^n, where n is number of qubits involved. The matrix must be unitary β πβ π = πΌ
// Equivalent to CNOT(0, 1)
var cx = new Complex[,]
{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 0, 1 },
{ 0, 0, 1, 0 }
};
qc.Custom(cx, 0, 1);
Measure the entire quantum system and get a classical bitstring (e.g. "00"
, "11"
).
You can get one result using basic vector state real-time simulator. You can also perform partial measurements to observe only selected qubits, yielding a shorter bitstring corresponding to the measured subset - the bits in the result are ordered exactly as the qubit indices are listed in the argument.
string result = qc.Measure();
string result = qc.Measure(0, 2);
The measurement collapses the quantum state probabilistically based on the amplitudes.
The Simulator
class provides functionality to simulate quantum circuits and measure the results. It allows you to run a quantum circuit multiple times and analyze the measurement outcomes. It returns an array of measurments for each qc.Measure()
QuantumCircuit qc = new QuantumCircuit(2);
qc.H(0);
qc.CNOT(0, 1);
qc.Measure();
string results = Simulator.Run(qc, 1000)[0].GetStringResult();
Console.WriteLine(results);
Qubit.NET uses a pluggable randomness system through the IRandomSource interface. By default, it uses a pseudo-random generator (PseudoRandomSource). You can swap this out for your custom implementation.
using Qubit.NET.Utilities;
public class FixedRandomSource : IRandomSource
{
public double NextDouble() => 0.42; // Always returns the same value
}
Then you can use it in QuantumCircuit:
QuantumCircuit qc = new QuantumCircuit(2);
qc.RandomSource = new FixedRandomSource();
- Entanglement entropy measurements
- Noise simulation (decoherence, damping)
- Circuit export in QASM
Pull requests, suggestions, and feature requests are welcome!
Feel free to fork and extend the library.
Created by Tymoteusz Marzec
Find me on GitHub: @InfoTCube