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.NETAdd the project to your solution or include the .cs files (QuantumCircuit.cs, QuantumGates.cs, etc.) in your C# project.
usingQubit.Net;//qubits are created in 0 statevarqc=newQuantumCircuit(2);// Apply Hadamard to qubit 0qc.H(0);// Apply CNOT (qubit 0 → control, qubit 1 → target)qc.CNOT(0,1);// Draw a circuitqc.Draw();// Measure full stateConsole.WriteLine($"Measured: {qc.Measure()}");// Possible: 00 or 11You 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,newComplex(1,1),newComplex(2,2));
⚠️ Initialization can only be done before any gate is applied to that qubit.
This is internally tracked using a private_isQubitModifiedarray.
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)varcx=newComplex[,]{{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.
stringresult=qc.Measure();stringresult=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()
QuantumCircuitqc=newQuantumCircuit(2);qc.H(0);qc.CNOT(0,1);qc.Measure();stringresults=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.
usingQubit.NET.Utilities;publicclassFixedRandomSource:IRandomSource{publicdoubleNextDouble()=>0.42;// Always returns the same value}Then you can use it in QuantumCircuit:
QuantumCircuitqc=newQuantumCircuit(2);qc.RandomSource=newFixedRandomSource();- 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
