Qubit.NET
1.0.0
See the version list below for details.
dotnet add package Qubit.NET --version 1.0.0
NuGet\Install-Package Qubit.NET -Version 1.0.0
<PackageReference Include="Qubit.NET" Version="1.0.0" />
<PackageVersion Include="Qubit.NET" Version="1.0.0" />
<PackageReference Include="Qubit.NET" />
paket add Qubit.NET --version 1.0.0
#r "nuget: Qubit.NET, 1.0.0"
#:package Qubit.NET@1.0.0
#addin nuget:?package=Qubit.NET&version=1.0.0
#tool nuget:?package=Qubit.NET&version=1.0.0
<img src="./img/qubitnet.png" alt="Qubit.NET logo" style="height: 250px"/>
Qubit.NET
๐ง C# Quantum Computing Simulation Library
Qubit.NET is a lightweight quantum circuit simulation library written in C#. It lets you build quantum circuits, initialize qubits, apply common quantum gates, and measure results โ all on a classical computer. Perfect for learning, prototyping, or integrating quantum logic into .NET applications.
The state vector holds 2โฟ complex amplitudes, so memory is the limit: 20 qubits โ 16 MB, 24 โ 256 MB, 26 โ 1 GB (the hard ceiling, set by the CLR's 2 GB single-array limit).
๐ฅ Install
dotnet add package Qubit.NET
Zero dependencies. Targets .NET Standard 2.1, .NET 8 and .NET 10.
๐ฎ Unity
Qubit.NET ships a netstandard2.1 build, so it works in Unity 2021.2+. Either install it
through NuGetForUnity, or drop
lib/netstandard2.1/Qubit.NET.dll from the package into Assets/Plugins/.
Unity has no Console, so use the string-returning APIs:
Debug.Log(qc.ToDiagram()); // instead of qc.Draw()
Debug.Log(QuantumGates.Format(QuantumGates.H)); // instead of QuantumGates.Print(...)
๐ Quick Start
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
Draw() prints a colored ASCII diagram to the console โ ToDiagram() returns the same
thing as a string:
q2 (0): โโโโโโโโโโโ[+]โโ[X]โโ[M]โ
| | |
q1 (0): โโโโโโ[+]โโโ@โโโโ|โโโ[M]โ
| | | |
q0 (0): โ[H]โโโ@โโโโ@โโโ[X]โโ[M]โ
๐งฐ Features
๐งฉ Qubit Initialization
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, given as amplitudes ฮฑ and ฮฒ:
// |ฯโฉ = (|0โฉ + i|1โฉ) / โ2
qc.Initialize(0, new Complex(1 / Math.Sqrt(2), 0), new Complex(0, 1 / Math.Sqrt(2)));
โ ๏ธ The state must be normalized โ
|ฮฑ|ยฒ + |ฮฒ|ยฒ = 1โ or anArgumentExceptionis thrown.
โ ๏ธ Initialization can only be done before any gate is applied to that qubit.
This is internally tracked using a private_isQubitModifiedarray.
๐ Gate Application
Qubit.NET includes several built-in quantum gates:
โ Single-Qubit 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);
โ Two-Qubit Gates
| 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);
โ Three-Qubit Gates
| 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);
โ Custom Gate Support
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);
๐ Measurement
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.
โ๏ธ Simulation
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()
Example:
QuantumCircuit qc = new QuantumCircuit(2);
qc.H(0);
qc.CNOT(0, 1);
qc.Measure();
MeasurementResult result = Simulator.Run(qc, 1000)[0];
Console.WriteLine(result); // {'00': 512, '11': 488}
Console.WriteLine(result.Counts["00"]); // 512
Console.WriteLine(result.Probability("11")); // 0.488
Console.WriteLine(result.MostFrequent); // 00
Console.WriteLine(result.Shots); // 1000
Simulator.Run never modifies the circuit you hand it, so you can run the same circuit as
many times as you like.
๐ Classical bits and feedforward
Measuring writes into a classical bit โ one per qubit, so measuring qubit q fills bit q
unless you say otherwise with MeasureInto. When then conditions later gates on that bit,
which is what mid-circuit measurement and error correction need.
qc.MeasureInto(qubit: 0, classicalBit: 0);
qc.When(classicalBit: 0, value: 1, c => c.X(2)); // X(2) runs only if bit 0 came out 1
Conditional gates are always recorded, so Simulator.Run re-evaluates the condition on
every shot against that shot's own outcomes.
Quantum teleportation in full:
var qc = new QuantumCircuit(3);
qc.Ry(0, theta); // the message on qubit 0
qc.H(1); // entangle qubits 1 and 2
qc.CNOT(1, 2);
qc.CNOT(0, 1); // Bell-basis measurement of qubits 0 and 1
qc.H(0);
qc.MeasureInto(0, 0);
qc.MeasureInto(1, 1);
qc.When(1, 1, c => c.X(2)); // corrections
qc.When(0, 1, c => c.Z(2));
// qubit 2 now holds the state qubit 0 started in
Qubit.NET deliberately has no separate
ClassicalRegistertype. The classical register in Qiskit exists mainly to express feedforward and result layout;WhenandMeasureIntocover both without making every circuit declare two registers up front.
๐ฒ Randomness source
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();
๐งช Built-in algorithms
using Qubit.NET.Circuits;
// Grover search: finds the marked item in O(sqrt(N))
var grover = Algorithms.Grover(2, c => c.CZ(0, 1)); // marks |11>
Console.WriteLine(grover.ToHistogram()); // 11 | ####...#### 1.000
// Bernstein-Vazirani: recovers a hidden bit string in a single query
var bv = Algorithms.BernsteinVazirani([true, false, true]);
Console.WriteLine(bv.Measure(2, 1, 0)); // 101
// Deutsch-Jozsa, teleportation, superdense coding, QFT
var dj = Algorithms.DeutschJozsa(3, c => c.CNOT(0, 3));
var tp = Algorithms.Teleportation(c => c.Ry(0, 0.9));
var sd = Algorithms.SuperdenseCoding(true, false);
qc.QFT(); // Quantum Fourier Transform, in place
Plus BellStates.PhiPlus/PhiMinus/PsiPlus/PsiMinus/GHZ().
๐ค OpenQASM export
Export to OpenQASM 2.0 and run your circuit on real hardware through Qiskit or IBM Quantum:
using Qubit.NET.Export;
File.WriteAllText("teleport.qasm", Algorithms.Teleportation(c => c.Ry(0, 0.9)).ToQasm());
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];
ry(0.9) q[0];
h q[1];
cx q[1],q[2];
cx q[0],q[1];
h q[0];
measure q[0] -> c[0];
measure q[1] -> c[1];
if (c[1]==1) x q[2];
if (c[0]==1) z q[2];
# Qiskit
qc = QuantumCircuit.from_qasm_file("teleport.qasm")
๐ Inspecting the state
using Qubit.NET.Visualization;
var (x, y, z) = qc.BlochVector(0); // Bloch sphere coordinates โ drive a Unity gizmo
qc.QubitProbability(0); // P(qubit 0 = |1>), tracing out the rest
Console.WriteLine(qc.ToHistogram()); // ASCII bar chart of outcome probabilities
A qubit entangled with others sits inside the sphere โ maximally entangled means the origin, which makes entanglement something you can actually see.
โก Performance
Gates are applied in place, so a circuit allocates one state vector regardless of how many gates you apply, and gate application is parallelized above ~16 qubits.
| Circuit | 16 qubits | 20 qubits | 22 qubits |
|---|---|---|---|
| Hadamard on every qubit | 2.4 ms | 36 ms | 147 ms |
| GHZ (H + CNOT chain) | 2.3 ms | 34 ms | 142 ms |
| Measure all qubits | 2.7 ms | 44 ms | 188 ms |
<sub>BenchmarkDotNet, .NET 10, Ryzen desktop. Reproduce with
dotnet run -c Release --project benchmarks/Qubit.NET.Benchmarks.</sub>
Memory is the real limit โ the state vector holds 2โฟ complex amplitudes at 16 bytes each:
| Qubits | State vector |
|---|---|
| 16 | 1 MB |
| 20 | 16 MB |
| 24 | 256 MB |
| 26 | 1 GB (max) |
๐ Future Roadmap
- Circuit export in QASM
- Mid-circuit measurement and classical feedforward
- Bloch sphere coordinates and probability histograms
- Built-in algorithms (Grover, DeutschโJozsa, BernsteinโVazirani, QFT, teleportation)
- Noise simulation (decoherence, damping)
- Entanglement entropy measurements
- QASM import
- Multi-controlled gates and circuit inverses
๐ก Contributions
Pull requests, suggestions, and feature requests are welcome!
Feel free to fork and extend the library.
๐ค Author
Created by Tymoteusz Marzec
Find me on GitHub: @InfoTCube
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
See CHANGELOG.md