Qubit.NET 1.0.0

There is a newer version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Qubit.NET" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Qubit.NET" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Qubit.NET" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Qubit.NET --version 1.0.0
                    
#r "nuget: Qubit.NET, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Qubit.NET@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Qubit.NET&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Qubit.NET&version=1.0.0
                    
Install as a Cake Tool

<img src="./img/qubitnet.png" alt="Qubit.NET logo" style="height: 250px"/>

Qubit.NET

๐Ÿง  C# Quantum Computing Simulation Library

NuGet Downloads CI License: MIT

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 an ArgumentException is thrown.

โš ๏ธ Initialization can only be done before any gate is applied to that qubit.
This is internally tracked using a private _isQubitModified array.


๐ŸŒ€ 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 ClassicalRegister type. The classical register in Qiskit exists mainly to express feedforward and result layout; When and MeasureInto cover 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last Updated
1.0.1 150 8/17/2026
1.0.0 124 8/17/2026

See CHANGELOG.md