Skip to content

InfoTCube/Qubit.NET

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

37 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Qubit.NET logo

Qubit.NET

🧠 C# Quantum Computing Simulation Library

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.


βœ… Requirements

  • .NET 6.0 or newer
  • System.Numerics (for complex numbers β€” included in .NET)

πŸ“₯ Setup

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.


πŸš€ 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

🧰 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

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.


πŸŒ€ 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();

string results = Simulator.Run(qc, 1000)[0].GetStringResult();
Console.WriteLine(results);

🎲 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();

πŸ“Œ Future Roadmap

  • Entanglement entropy measurements
  • Noise simulation (decoherence, damping)
  • Circuit export in QASM

πŸ’‘ 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

About

C# Quantum Computing Simulation Library

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages