Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/main_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
build-and-test:
runs-on: ubuntu-latest

env:
CONDA_OVERRIDE_CUDA: "12.0" # Mock CUDA version 12.0

steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -20,7 +23,7 @@ jobs:
with:
pixi-version: latest
cache: true
cache-key: ${{ runner.os }}-pixi-v1-${{ hashFiles('**/pixi.lock') }}
cache-key: ${{ runner.os }}-pixi-${{ hashFiles('**/pixi.lock') }}

- name: Check formatting
run: pixi run lint
Expand All @@ -32,5 +35,4 @@ jobs:
run: pixi run main

- name: Run tests
run: pixi run test

run: pixi run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Binary files
build/
build/*
data/
main
*.mojopkg

Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Makefile that forward every target to `pixi run`

.PHONY: all
all:
# @pixi run test
@pixi run main
@pixi run bench

.PHONY: %
%:
@pixi run $@ $(ARGS)
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,49 @@

**A Quantum Circuit Composer & Simulator in Mojo** 🔥⚛️


## Education

This project reimplements and extends the ideas from the following tutorial paper:

> **How to Write a Simulator for Quantum Circuits from Scratch: A Tutorial**
> *Michael J. McGuffin, Jean-Marc Robert, and Kazuki Ikeda*
> Published: 2025-06-09 on [arXiv:2506.08142v1](https://arxiv.org/abs/2506.08142v1) (last accessed: 2025-06-12)


## 🎯 Project Objectives
### 🎯 Project Objectives

* **Mojo Implementation:** Re-implement the approach from the paper in Mojo for more Pythonic synthax and better readability.
* **Learning by Doing:** Gain hands-on experience with quantum circuit simulation to better understand the capabilities and limitations of classical simulation.
* **Performance & Safety:** Leverage Mojo's strong static typing and compilation for blazing-fast and safe operations.
* **Hardware Acceleration:** Utilize Mojo’s universal GPU programming support to accelerate simulations.

### 🔥 Current Implementation

## ⚙️ Environment Setup
The current implementation uses a State Vector approach, which is an efficient method for simulating small-scale quantum circuits (20–30 qubits) with high precision. This approach also enables relatively straightforward exact gradient computations.

Follow these steps to set up your environment and build the binary:
An alternative implementation for the futur could be using the Tensor Network approach. This method is more suitable for larger circuits but offers lower precision and would involves more computationally expensive gradient calculations.

## Usage

### ⚙️ Environment Setup

Follow these steps to set up your environment, build the library and run some examples:

If you don't have Pixi installed yet:
```bash
# If you don't have Pixi installed yet:
curl -sSf https://pixi.sh/install.sh | bash

# Install all project dependencies:
```
Install all project dependencies:
```
pixi install
```

# Build and run examples of the simulator:
Build and run examples of the simulator:
```bash
pixi run main
```


## 🔥 Current Implementation

The current implementation uses a State Vector approach, which is an efficient method for simulating small-scale quantum circuits (20–30 qubits) with high precision. This approach also enables relatively straightforward exact gradient computations.

An alternative implementation for the futur could be using the Tensor Network approach. This method is more suitable for larger circuits but offers lower precision and would involves more computationally expensive gradient calculations.

## 📄 License

This project is open-source and licensed under Apache License 2.0.
32 changes: 20 additions & 12 deletions TODOs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

### Implementations

- 4 / 3 : Implement measurement gates

- 4 / 5 : Implement the computation of statistics (6.5 and 6.6)

- 3 / 3 : Implement naive implementation of the functions to compare performances
- matrix multiplication (but starting from right or smart)
- partial trace

- 5 / 5 : Start adding support for GPU in the base classes if needed (not possible to use SIMD(complexfloat64) anymore, or keep them but seperate them when moving data to GPU)
- struct PureBasisState
- 5 / 5 : Start adding support for GPU in the base classes if needed (not possible to use SIMD(ComplexFloat32) anymore, or keep them but seperate them when moving data to GPU)
- struct StateVector
- struct ComplexMatrix
- struct Gate

- 5 / ? : GPU implementation of:
- qubit_wise_multiply()
- apply_swap()
- partial_trace()
- StateVector.to_density_matrix()

- 4 / 3 : Export benchmark results as plots.

- 2 / 4 : Efficient support for tracking a state statistic like entropy during the execution of the circuit by the simulator.

- 3 / 3 : Implement naive implementation of the functions to compare performances
- matrix multiplication (but starting from right or smart)
- partial trace

### Tests

Expand All @@ -30,8 +30,7 @@
- 5 / 2 : Test for everything that will be implement in GPU
- qubit_wise_multiply()
- apply_swap()
- partial_trace()
- struct PureBasisState's methods
- struct StateVector's methods
- struct ComplexMatrix's methods
- struct Gate's Gate

Expand All @@ -41,14 +40,23 @@

## Droped for now

- 4 / 3 : Implement end of circuit measurement gates with some of those options:
- https://docs.pennylane.ai/en/stable/introduction/measurements.html

- 4 / 4 : Gradient computation with Finite Difference

- 3 / 2 : Use a separate list for things that are not real gate to not slow down the main run logic

- 3 / 3 : Setup automatic Doc generation with pixi but also on github.io repository page

- 3 / 4 : Compile time circuit creation?

- 3 / 4 : Gradient computation with Parameter-Shift

- 3 / 4 : Implement mid circuit measurement gates (Section 7 of paper)

- 3 / 100 : Gradient computation with Adjoint Method

- 2 / 4 : qubit_wise_multiply_extended() but for gates applied to non-adjacent qubits

- 2 / 3 : Implement concurence (2-qubits entanglement metric) computePairwiseQubitConcurrences()
45 changes: 45 additions & 0 deletions benchmarks/all_benchmarks.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from sys import has_accelerator

from bench_simulate_random_circuit import bench_simulate_random_circuit
from bench_qubit_wise_multiply import (
bench_qubit_wise_multiply,
bench_qubit_wise_multiply_inplace,
bench_qubit_wise_multiply_extended,
)
from bench_qubit_wise_multiply_gpu import (
bench_qubit_wise_multiply_inplace_gpu,
)


def main():
print("Running all benchmarks...")
# bench_qubit_wise_multiply()
bench_qubit_wise_multiply_inplace[
min_number_qubits=5,
max_number_qubits=25,
number_qubits_step_size=2,
min_number_layers=5,
max_number_layers=4000,
number_layers_step_size=400,
fixed_number_qubits=11,
fixed_number_layers=20,
]()

@parameter
if not has_accelerator():
print("No compatible GPU found")
else:
bench_qubit_wise_multiply_inplace[
min_number_qubits=5,
max_number_qubits=25,
number_qubits_step_size=2,
min_number_layers=5,
max_number_layers=4000,
number_layers_step_size=400,
fixed_number_qubits=11,
fixed_number_layers=20,
]()

# bench_qubit_wise_multiply_extended()
# bench_simulate_random_circuit()
print("All benchmarks completed.")
Loading