Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
62cc7af
rng library being generated and model almost done
Jonhas-qtm Jun 30, 2025
0745130
rng interface integrated into sim
Jonhas-qtm Jun 30, 2025
69c0c28
adding support to check for JOB_shotnum
Jonhas-qtm Jun 30, 2025
60fe89d
integrated into build files to build pecos_rng_pcg
Jonhas-qtm Jul 1, 2025
5bb562c
modified makefile to build pecos rng library across different envs
Jonhas-qtm Jul 2, 2025
cb944f6
modified workflow to build rng lib
Jonhas-qtm Jul 2, 2025
9772169
missed including nanobind explictly in Install build dependencies step
Jonhas-qtm Jul 2, 2025
dc94ab8
added docstrings and types to parameters and return type
Jonhas-qtm Jul 2, 2025
b65acac
pre-commit changes
Jonhas-qtm Jul 2, 2025
c55d8ac
more pre-commit changes
Jonhas-qtm Jul 2, 2025
7de750f
pre-commit green
Jonhas-qtm Jul 2, 2025
f04b414
fixed up github action
Jonhas-qtm Jul 2, 2025
1bb5a58
cleaned up makefile and how to pecos_rng library and modified github …
Jonhas-qtm Jul 3, 2025
9dd3f8d
pre-commit changes
Jonhas-qtm Jul 3, 2025
c8dfa7a
added test case for rng
Jonhas-qtm Jul 5, 2025
79f1f6c
pre-commit changes for test
Jonhas-qtm Jul 7, 2025
7f09226
changed from unittest to pytest
Jonhas-qtm Jul 7, 2025
6e137f9
modified ruff to ignore specific ruff check to assure appropriate bui…
Jonhas-qtm Jul 8, 2025
63658c2
modified workflow to pick appropriate repo
Jonhas-qtm Jul 8, 2025
30e32a4
github action chanegs
Jonhas-qtm Jul 8, 2025
ef6d281
lib change didnt go through? fixing
Jonhas-qtm Jul 8, 2025
7d8968e
moving stuff around for ci
Jonhas-qtm Jul 8, 2025
44a9e86
forgot to fix sdist action
Jonhas-qtm Jul 8, 2025
63ef723
removed uv usage in release
Jonhas-qtm Jul 8, 2025
937bfa0
removed uv for build_sdist_quantum_pecos
Jonhas-qtm Jul 8, 2025
a7e8aac
made changes to python-test to see if pecos rng is built proper
Jonhas-qtm Jul 8, 2025
8f9d2d2
success with python-release action. Adding prefix path to cmake to se…
Jonhas-qtm Jul 8, 2025
8ed5a42
uv not finding paths properly running new cmmd
Jonhas-qtm Jul 8, 2025
3910fab
changes
Jonhas-qtm Jul 8, 2025
3910464
moved things around for test
Jonhas-qtm Jul 8, 2025
65c41ed
pre-commit changes
Jonhas-qtm Jul 8, 2025
6774eb2
Workspace Makefile fix?
ciaranra Jul 8, 2025
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
24 changes: 18 additions & 6 deletions .github/workflows/python-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,16 @@ jobs:
- name: Install pecos-rslib
run: pip install ./pecos-rslib-wheel/*.whl

- name: Install build dependencies
- name: Install pecos RNG build dependencies
run: |
python -m pip install --upgrade pip
pip install build
pip install build nanobind

- name: Build PECOS RNG
run: |
cd clibs/pecos-rng && mkdir build && cd build/ && cmake ..
make && cd .. && pip install .
env:
NANOBIND_DIR: python -m nanobind --include_dir

- name: Build quantum-pecos SDist
run: |
Expand Down Expand Up @@ -249,10 +255,16 @@ jobs:
- name: Install pecos-rslib
run: pip install ./pecos-rslib-wheel/*.whl

- name: Install build dependencies
- name: Install pecos rng build dependencies
run: |
pip install build nanobind

- name: Build PECOS RNG
run: |
python -m pip install --upgrade pip
pip install build
cd clibs/pecos-rng && mkdir build && cd build/ && cmake ..
make && cd .. && pip install .
env:
NANOBIND_DIR: python -m nanobind --include_dir

- name: Build quantum-pecos wheel
run: |
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ jobs:
uv lock --project .
uv sync --project .

- name: Install pecos rng build dependencies
run: |
uv pip install build nanobind scikit-build-core cmake ninja

- name: Build PECOS RNG
run: |
export NANOBIND_CMAKE_DIR=$(uv run -- python -m nanobind --cmake_dir)
export NANOBIND_DIR=$(uv run -- python -m nanobind --include_dir)
cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. -DCMAKE_PREFIX_PATH=$NANOBIND_CMAKE_DIR
cmake --build . && cd ..
uv pip install .

- name: Install pecos-rslib with maturin
run: |
cd python/pecos-rslib
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down Expand Up @@ -177,3 +176,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# Prevent subdirectory virtual environments
clibs/*/.venv/
clibs/*/uv.lock
33 changes: 30 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Try to autodetect if python3 or python is the python executable used.
PYTHONPATH := $(shell which python 2>/dev/null || which python3 2>/dev/null)

SHELL=bash

# Requirements
Expand All @@ -20,27 +19,37 @@ installreqs: ## Install Python project requirements to root .venv
@echo "Installing requirements..."
uv sync

.PHONY: buildrng
buildrng:
@echo "Building and installing RNG library..."
uv pip install nanobind
cd clibs/pecos-rng && CC=gcc CXX=g++ uv pip install --python $(shell uv run which python) -e .

# Building development environments
# ---------------------------------
.PHONY: build
build: installreqs ## Compile and install for development
@unset CONDA_PREFIX && cd python/pecos-rslib/ && uv run maturin develop --uv
$(MAKE) buildrng
@unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all]

.PHONY: build-basic
build-basic: installreqs ## Compile and install for development but do not include install extras
@unset CONDA_PREFIX && cd python/pecos-rslib/ && uv run maturin develop --uv
$(MAKE) buildrng
@unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .

.PHONY: build-release
build-release: installreqs ## Build a faster version of binaries
@unset CONDA_PREFIX && cd python/pecos-rslib/ && uv run maturin develop --uv --release
$(MAKE) buildrng
@unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all]

.PHONY: build-native
build-native: installreqs ## Build a faster version of binaries with native CPU optimization
@unset CONDA_PREFIX && cd python/pecos-rslib/ && RUSTFLAGS='-C target-cpu=native' \
&& uv run maturin develop --uv --release
$(MAKE) buildrng
@unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all]

# Documentation
Expand Down Expand Up @@ -146,6 +155,12 @@ clean-unix:
@find . -type d -name "junit" -exec rm -rf {} +
@find python -name "*.so" -delete
@find python -name "*.pyd" -delete
@# Clean clibs build artifacts
@find clibs -type d -name "build" -exec rm -rf {} +
@find clibs -type d -name "dist" -exec rm -rf {} +
@find clibs -type d -name "*.egg-info" -exec rm -rf {} +
@find clibs -type d -name ".venv" -exec rm -rf {} +
@find clibs -name "uv.lock" -delete
@# Clean all target directories in crates (in case they were built independently)
@find crates -type d -name "target" -exec rm -rf {} +
@find python -type d -name "target" -exec rm -rf {} +
Expand All @@ -167,6 +182,12 @@ clean-windows-ps:
@powershell -Command "Get-ChildItem -Path . -Recurse -Directory -Filter '.hypothesis' | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
@powershell -Command "Get-ChildItem -Path . -Recurse -Directory -Filter 'junit' | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
@powershell -Command "Get-ChildItem -Path python -Recurse -File -Include '*.so','*.pyd' | Remove-Item -Force -ErrorAction SilentlyContinue"
@# Clean clibs build artifacts
@powershell -Command "Get-ChildItem -Path clibs -Recurse -Directory -Filter 'build' | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
@powershell -Command "Get-ChildItem -Path clibs -Recurse -Directory -Filter 'dist' | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
@powershell -Command "Get-ChildItem -Path clibs -Recurse -Directory -Filter '*.egg-info' | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
@powershell -Command "Get-ChildItem -Path clibs -Recurse -Directory -Filter '.venv' | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
@powershell -Command "Get-ChildItem -Path clibs -Recurse -File -Filter 'uv.lock' | Remove-Item -Force -ErrorAction SilentlyContinue"
@# Clean all target directories in crates
@powershell -Command "Get-ChildItem -Path crates -Recurse -Directory -Filter 'target' | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
@powershell -Command "Get-ChildItem -Path python -Recurse -Directory -Filter 'target' | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
Expand All @@ -187,6 +208,12 @@ clean-windows-cmd:
-@for /f "delims=" %%d in ('dir /s /b /ad .hypothesis 2^>nul') do @rd /s /q "%%d" 2>nul
-@for /f "delims=" %%d in ('dir /s /b /ad junit 2^>nul') do @rd /s /q "%%d" 2>nul
-@for /f "delims=" %%f in ('dir /s /b python\*.so python\*.pyd 2^>nul') do @del "%%f" 2>nul
-@REM Clean clibs build artifacts
-@for /f "delims=" %%d in ('dir /s /b /ad clibs\build 2^>nul') do @rd /s /q "%%d" 2>nul
-@for /f "delims=" %%d in ('dir /s /b /ad clibs\dist 2^>nul') do @rd /s /q "%%d" 2>nul
-@for /f "delims=" %%d in ('dir /s /b /ad clibs\*.egg-info 2^>nul') do @rd /s /q "%%d" 2>nul
-@for /f "delims=" %%d in ('dir /s /b /ad clibs\.venv 2^>nul') do @rd /s /q "%%d" 2>nul
-@for /f "delims=" %%f in ('dir /s /b clibs\uv.lock 2^>nul') do @del "%%f" 2>nul
-@REM Clean all target directories in crates
-@for /f "delims=" %%d in ('dir /s /b /ad crates\target 2^>nul') do @rd /s /q "%%d" 2>nul
-@for /f "delims=" %%d in ('dir /s /b /ad python\target 2^>nul') do @rd /s /q "%%d" 2>nul
Expand All @@ -201,10 +228,10 @@ pip-install-uv: ## Install uv using pip and create a venv. (Recommended to inst
@echo "Creating venv and installing dependencies..."
uv sync

.PONY: dev
.PHONY: dev
dev: clean build test ## Run the typical sequence of commands to check everything is running correctly

.PONY: devl ## Run the commands to make sure everything runs + lint
.PHONY: devl ## Run the commands to make sure everything runs + lint
devl: dev lint

# Help
Expand Down
34 changes: 34 additions & 0 deletions clibs/pecos-rng/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# CMakeLists.txt
cmake_minimum_required(VERSION 3.15...3.27)
project(pecos_rng LANGUAGES C CXX)
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")


if (CMAKE_VERSION VERSION_LESS 3.18)
set(DEV_MODULE Development)
else()
set(DEV_MODULE Development.Module)
endif()

# Find Python (for Python 3.13)
find_package(Python REQUIRED COMPONENTS Interpreter ${DEV_MODULE})

# Detect the installed nanobind package and import it into CMake
# This executes a Python command to get the nanobind CMake directory
execute_process(
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE nanobind_ROOT
)

set(CMAKE_PREFIX_PATH "${nanobind_ROOT}" ${CMAKE_PREFIX_PATH})

# This sets up nanobind
find_package(nanobind CONFIG REQUIRED)

# Add your C source file as a library
add_library(rng_pcg STATIC src/rng_pcg.c) # Compile test.c as a static library
nanobind_add_module(pecos_rng src/wrapper.cpp)
target_link_libraries(pecos_rng PRIVATE rng_pcg)
# Install the module so it can be found by pip install
install(TARGETS pecos_rng DESTINATION "pecos_pcg")
Empty file added clibs/pecos-rng/README.md
Empty file.
10 changes: 10 additions & 0 deletions clibs/pecos-rng/pecos_pcg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Python Package responsible for generating random numbers using pcg_rng."""

from .pecos_rng import (
pcg32_boundedrand,
pcg32_frandom,
pcg32_random,
pcg32_srandom,
)

__all__ = ["pcg32_boundedrand", "pcg32_frandom", "pcg32_random", "pcg32_srandom"]
22 changes: 22 additions & 0 deletions clibs/pecos-rng/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[build-system]
requires = ["scikit-build-core[pyproject]", "nanobind"]
build-backend = "scikit_build_core.build"

[project]
name = "pecos-pcg"
version = "0.1.0"
description = "A Python package with a nanobind-based C++ extension"
license = { text = "MIT" }
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: C++",
"License :: OSI Approved :: MIT License"
]

[tool.scikit-build]
wheel.packages = ["pecos_pcg"]
# Optional: set cmake minimum version or other CMake arguments
cmake.minimum-version = "3.18"
cmake.verbose = true
96 changes: 96 additions & 0 deletions clibs/pecos-rng/src/rng_pcg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* PCG Random Number Generation for C.
*
* Copyright 2014 Melissa O'Neill <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For additional information about the PCG random number generation scheme,
* including its license and other licensing options, visit
*
* http://www.pcg-random.org
*/

#include <math.h>
#include "rng_pcg.h"

// RNG state structure
typedef struct pcg_state_setseq_64 {
uint64_t state;
uint64_t inc;
} pcg32_random_t;

// global RNG state
static pcg32_random_t pcg32_global = {
0x853c49e6748fea9bULL,
0xda3e39cb94b95bdbULL
};

// default multi[plier]
#define PCG_DEFAULT_MULTIPLIER_64 6364136223846793005ULL

// helper functions
static inline uint32_t pcg_rotr_32(uint32_t value, unsigned int urot) {
int rot = (int)urot;
return (value >> rot) | (value << ((-rot) & 31));
}

static inline void pcg_setseq_64_step_r(pcg32_random_t* rng) {
rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64 + rng->inc;
}

static inline uint32_t pcg_output_xsh_rr_64_32(uint64_t state) {
return pcg_rotr_32(((state >> 18u) ^ state) >> 27u, state >> 59u);
}

static inline uint32_t pcg32_random_r(pcg32_random_t* rng) {
const uint64_t oldstate = rng->state;
pcg_setseq_64_step_r(rng);
return pcg_output_xsh_rr_64_32(oldstate);
}

static inline uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t ubound) {
int32_t bound = (int32_t)ubound;
uint32_t threshold = -bound % bound;
for (;;) {
const uint32_t r = pcg32_random_r(rng);
if (r >= threshold)
return r % bound;
}
}

static inline void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq) {
rng->state = 0U;
rng->inc = (initseq << 1u) | 1u;
pcg_setseq_64_step_r(rng);
rng->state += initstate;
pcg_setseq_64_step_r(rng);
}

// public interface to RNG

uint32_t pcg32_random() {
return pcg32_random_r(&pcg32_global);
}

uint32_t pcg32_boundedrand(uint32_t bound) {
return pcg32_boundedrand_r(&pcg32_global, bound);
}

double pcg32_frandom() {
return ldexp(pcg32_random(), -32);
}

void pcg32_srandom(uint64_t seq) {
pcg32_srandom_r(&pcg32_global, 42u, seq);
}
42 changes: 42 additions & 0 deletions clibs/pecos-rng/src/rng_pcg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* PCG Random Number Generation for C.
*
* Copyright 2014 Melissa O'Neill <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For additional information about the PCG random number generation scheme,
* including its license and other licensing options, visit
*
* http://www.pcg-random.org
*/

#pragma once

#include <stdint.h>

#if __cplusplus
extern "C" {
#endif

uint32_t pcg32_random();

uint32_t pcg32_boundedrand(uint32_t bound);

double pcg32_frandom();

void pcg32_srandom(uint64_t seq);

#if __cplusplus
}
#endif
12 changes: 12 additions & 0 deletions clibs/pecos-rng/src/wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <nanobind/nanobind.h>
#include "rng_pcg.h"

namespace nb = nanobind;
using namespace nb::literals;

NB_MODULE(pecos_rng, m) {
m.def("pcg32_random", &pcg32_random,"generate random numbers");
m.def("pcg32_frandom", &pcg32_frandom, "Generate random floating point number");
m.def("pcg32_boundedrand", &pcg32_boundedrand, "Generate bounded random number");
m.def("pcg32_srandom", &pcg32_srandom, "seeded random");
}
Loading
Loading