Skip to content

Zero-Dependency Quantum Simulator written in Rust with custom Complex, Matrix, and System libraries based on the principles of Q#

Notifications You must be signed in to change notification settings

hiibolt/qsharp_rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

QSharp-RS: A Quantum Circuit Simulator in Rust

A comprehensive quantum computing simulator written in Rust, implementing the fundamental concepts from Microsoft's Q# documentation and Quantum Katas. This project serves as both a learning tool for quantum computing concepts and a functional quantum circuit simulator with support for multi-qubit systems, entanglement, and various quantum gates.

๐ŸŽฏ Project Overview

This simulator was built as a hands-on approach to understanding quantum computing fundamentals while learning Rust. It implements a complete quantum computing stack from the ground up, including:

  • Complex number arithmetic with full mathematical operations
  • Matrix operations optimized for quantum computations
  • Quantum state representation using state vectors
  • Complete quantum gate library (Pauli, Hadamard, rotation, phase gates)
  • Multi-qubit systems with entanglement support
  • Quantum measurement simulation with probability visualization

๐Ÿš€ Features

Core Quantum Operations

  • Single-qubit gates: X, Y, Z (Pauli gates), H (Hadamard), S, T, rotation gates (Rx, Ry, Rz, R1)
  • Multi-qubit gates: CNOT, SWAP, and extensible controlled gate framework
  • Quantum state preparation: Support for basis states (|0โŸฉ, |1โŸฉ), superposition states (|+โŸฉ, |-โŸฉ), and arbitrary states
  • Measurement simulation: Visual probability bars and phase information

Mathematical Foundation

  • Complex number operations: Addition, multiplication, division, conjugation, polar conversion
  • Matrix operations: Multiplication, inversion, transposition, tensor products, eigenvalue/eigenvector computation
  • Quantum-specific operations: Inner/outer products, normalization, unitary verification

System Architecture

  • Modular design: Separate modules for complex numbers, matrices, qubits, and quantum systems
  • Entanglement handling: Automatic detection and management of entangled qubit states
  • Memory efficient: Smart state representation that tracks individual qubits vs. entangled systems

๐Ÿ”ฌ Implementation Highlights

Complex Number System

  • Full complex arithmetic with optimized operations
  • Polar/Cartesian coordinate conversion
  • Support for arbitrary complex exponentiation

Matrix Operations

  • Efficient matrix multiplication for quantum gate operations
  • Tensor product implementation for multi-qubit operations
  • Eigenvalue/eigenvector computation for quantum state analysis
  • Inverse tensor product for quantum state decomposition

Quantum State Management

  • Automatic entanglement detection and state consolidation
  • Visual measurement output with probability bars and phase information
  • Support for arbitrary quantum state preparation

Gate Implementation

All major quantum gates are implemented with proper mathematical foundations:

  • Pauli Gates: X (NOT), Y, Z (phase flip)
  • Hadamard Gate: Creates superposition states
  • Phase Gates: S (ฯ€/2 phase), T (ฯ€/4 phase), arbitrary phase rotation
  • Rotation Gates: Arbitrary rotations around X, Y, Z axes
  • Multi-qubit Gates: CNOT, SWAP with extensible framework

๐Ÿงฎ Example Usage

The main.rs file contains numerous examples implementing exercises from the Microsoft Quantum Katas:

// Create a Bell state (entangled pair)
let mut system = System::new();
system.allocate();
system.allocate();
system[0].unwrap_qubit().H();  // Put first qubit in superposition
system.CNOT(0, 1);             // Entangle with second qubit
system.dump();                 // Display the entangled state
// Prepare an arbitrary quantum state
let mut system = System::new();
let q = system.allocate();
q.R_y(theta);  // Rotate to desired amplitude
q.R_1(phi);    // Apply phase rotation
q.measure();   // Display measurement probabilities

๐ŸŽ“ Educational Value

This project implements solutions to exercises from:

  • Microsoft Quantum Katas: Basic quantum operations and multi-qubit systems
  • Quantum State Preparation: Various superposition and entangled states
  • Gate Composition: Building complex operations from fundamental gates
  • Quantum Algorithms: Foundational quantum computing algorithms

Each implementation includes detailed examples showing:

  • How quantum gates affect qubit states
  • Probability calculations for measurement outcomes
  • Phase relationships in quantum superposition
  • Entanglement creation and manipulation

๐Ÿ› ๏ธ Technical Details

Performance Considerations

  • Efficient complex number operations using f32 precision
  • Optimized matrix multiplication for common quantum gate sizes
  • Memory-efficient representation of quantum states
  • Lazy evaluation for entanglement operations

Mathematical Accuracy

  • Proper normalization of quantum states
  • Accurate complex number arithmetic including phase calculations
  • Correct implementation of quantum gate matrices
  • Verified against known quantum computing results

๐Ÿ”ฎ Future Plans

The project was initially intended to expand into:

  • 3D Visualization: Using Bevy engine for quantum state visualization
  • Nix Packaging: Complete development environment setup
  • Extended Gate Library: Implementation of more advanced quantum gates
  • Quantum Algorithm Library: Higher-level quantum algorithms and circuits

๐Ÿšฆ Getting Started

# Clone the repository
git clone <repository-url>
cd qsharp-rs

# Run the quantum simulator examples
cargo run

# Run specific examples
cargo test

The output will show various quantum operations, state preparations, and measurement results with visual probability indicators.


๐Ÿ“š Quick Reference

Important Qubit States

Qubit States Bloch Sphere

Important Gates

Pauli Gates Hadamard Gate Phase Gates Rotation Gates Multi-Qubit Gates

Multi-Qubit Basis States

Multi-Qubit States

Bra-Ket Notation

Bra-Ket Basics

  • You can imagine โŸจx|yโŸฉ as the inner product of |xโŸฉ and |yโŸฉ because they're notationally connected internally via the connector.
  • Likewise, you can imagine |xโŸฉโŸจy| as the outer product because |xโŸฉ and |yโŸฉ are not connected internally, so they're outside the norm.

Gate Representation

This allows you to represent entire gates as a series of Dirac notations without ever technically re-entering matrix territory.

Basic example of how the X (inversion) gate can be represented solely with Dirac notation: X Gate Dirac

This allows fast computation without ever using matrices, which can be seen here flipping |0โŸฉ to |1โŸฉ in Dirac notation: Dirac Computation

Gate Building

I-Gate usage to access only certain qubits

  • Tensor Product with I-Gate as the base of the operation: Push up
  • Tensor Product with I-Gate as the operand of the operation: Push back

Example Gate Usage

Given a system of 3 qubits, in order to ignore the first and third qubits and only apply the X-Gate to the second qubit, you would build a gate as follows: $$(I \otimes X \otimes I) * Q_s$$


๐Ÿ“– References

Primary Resources

Additional Resources

About

Zero-Dependency Quantum Simulator written in Rust with custom Complex, Matrix, and System libraries based on the principles of Q#

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published