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.
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
- 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
- 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
- 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
- Full complex arithmetic with optimized operations
- Polar/Cartesian coordinate conversion
- Support for arbitrary complex exponentiation
- 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
- Automatic entanglement detection and state consolidation
- Visual measurement output with probability bars and phase information
- Support for arbitrary quantum state preparation
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
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 probabilitiesThis 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
- 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
- Proper normalization of quantum states
- Accurate complex number arithmetic including phase calculations
- Correct implementation of quantum gate matrices
- Verified against known quantum computing results
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
# Clone the repository
git clone <repository-url>
cd qsharp-rs
# Run the quantum simulator examples
cargo run
# Run specific examples
cargo testThe output will show various quantum operations, state preparations, and measurement results with visual probability indicators.
- 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.
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:
This allows fast computation without ever using matrices, which can be seen here flipping |0โฉ to |1โฉ in Dirac notation:
- 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
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:
- Microsoft Quantum Katas - Home Repository
- Complex Arithmetic Tutorial
- Linear Algebra Tutorial
- Awesome Q# - Curated list of Q# resources
- The Hitchhiker's Guide to Quantum Computing and Q#
- Quirk - Quantum Circuit Simulator - Interactive quantum circuit visualization