From 62cc7af30a092c63caff32ecfb6d1ab4e4f1bc24 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Mon, 30 Jun 2025 02:41:06 -0400 Subject: [PATCH 01/32] rng library being generated and model almost done --- .gitignore | 1 - lib/pecos_rng/CMakeLists.txt | 34 +++++++ lib/pecos_rng/README.md | 0 lib/pecos_rng/pecos_rng_pcg/__init__.py | 3 + lib/pecos_rng/pyproject.toml | 23 +++++ lib/pecos_rng/src/rng_pcg.c | 96 +++++++++++++++++++ lib/pecos_rng/src/rng_pcg.h | 42 ++++++++ lib/pecos_rng/src/wrapper.cpp | 12 +++ .../src/pecos/engines/cvm/rng_model.py | 36 +++++++ .../src/pecos/engines/hybrid_engine_old.py | 6 ++ 10 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 lib/pecos_rng/CMakeLists.txt create mode 100644 lib/pecos_rng/README.md create mode 100644 lib/pecos_rng/pecos_rng_pcg/__init__.py create mode 100644 lib/pecos_rng/pyproject.toml create mode 100644 lib/pecos_rng/src/rng_pcg.c create mode 100644 lib/pecos_rng/src/rng_pcg.h create mode 100644 lib/pecos_rng/src/wrapper.cpp create mode 100644 python/quantum-pecos/src/pecos/engines/cvm/rng_model.py diff --git a/.gitignore b/.gitignore index 46407a2aa..a8439883f 100644 --- a/.gitignore +++ b/.gitignore @@ -32,7 +32,6 @@ dist/ downloads/ eggs/ .eggs/ -lib/ lib64/ parts/ sdist/ diff --git a/lib/pecos_rng/CMakeLists.txt b/lib/pecos_rng/CMakeLists.txt new file mode 100644 index 000000000..2cd1575c0 --- /dev/null +++ b/lib/pecos_rng/CMakeLists.txt @@ -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 3.13 COMPONENTS Interpreter ${DEV_MODULE} REQUIRED) + +# 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_rng_pcg/") diff --git a/lib/pecos_rng/README.md b/lib/pecos_rng/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/lib/pecos_rng/pecos_rng_pcg/__init__.py b/lib/pecos_rng/pecos_rng_pcg/__init__.py new file mode 100644 index 000000000..2be7669c9 --- /dev/null +++ b/lib/pecos_rng/pecos_rng_pcg/__init__.py @@ -0,0 +1,3 @@ +from .pecos_rng import pcg32_random, pcg32_frandom, pcg32_boundedrand, pcg32_srandom + +__all__ = ['pcg32_random', 'pcg32_frandom', 'pcg32_boundedrand', 'pcg32_srandom'] diff --git a/lib/pecos_rng/pyproject.toml b/lib/pecos_rng/pyproject.toml new file mode 100644 index 000000000..bcaa65271 --- /dev/null +++ b/lib/pecos_rng/pyproject.toml @@ -0,0 +1,23 @@ +[build-system] +requires = ["scikit-build-core[pyproject]", "nanobind"] +build-backend = "scikit_build_core.build" + +[project] +name = "pecos_rng_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_rng_pcg"] +# Optional: set cmake minimum version or other CMake arguments +cmake.minimum-version = "3.18" +cmake.verbose = true + diff --git a/lib/pecos_rng/src/rng_pcg.c b/lib/pecos_rng/src/rng_pcg.c new file mode 100644 index 000000000..c6c5bca21 --- /dev/null +++ b/lib/pecos_rng/src/rng_pcg.c @@ -0,0 +1,96 @@ +/* + * PCG Random Number Generation for C. + * + * Copyright 2014 Melissa O'Neill + * + * 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 +#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); +} diff --git a/lib/pecos_rng/src/rng_pcg.h b/lib/pecos_rng/src/rng_pcg.h new file mode 100644 index 000000000..5cfb759f5 --- /dev/null +++ b/lib/pecos_rng/src/rng_pcg.h @@ -0,0 +1,42 @@ +/* + * PCG Random Number Generation for C. + * + * Copyright 2014 Melissa O'Neill + * + * 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 + +#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 diff --git a/lib/pecos_rng/src/wrapper.cpp b/lib/pecos_rng/src/wrapper.cpp new file mode 100644 index 000000000..1d774beac --- /dev/null +++ b/lib/pecos_rng/src/wrapper.cpp @@ -0,0 +1,12 @@ +#include +#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"); +} \ No newline at end of file diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py new file mode 100644 index 000000000..e94fe97e7 --- /dev/null +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -0,0 +1,36 @@ +import pecos_rng_pcg +from typing import Optional + +class RNGModel: + def __init__(self, seed:int=0, current_bound: Optional[int]=0) -> None: + self.current_bound = current_bound + self.count = 0 + self.last_rand = 0 + self.seed = self.set_seed(seed) + + def set_seed(self, seed:int) -> None: + self.seed = seed + pecos_rng_pcg.pcg32_srandom(seed) + + def set_bound(self, bound:int) -> None: + self.current_bound = bound + + def rng_random(self) -> int: + if self.current_bound == 0: + rng_num = pecos_rng_pcg.pcg32_random() + else: + rng_num = pecos_rng_pcg.pcg32_boundedrand(self.current_bound) + self.count+=1 + self.last_rand = rng_num + return rng_num + + def set_index(self, index: int) -> None: + if self.count > index: + raise BufferError("rngindex called after specified already generated") + # number after from the stream will be the idx of interest + while self.count < index: + self.rng_random() + + def eval_func(self, op): + pass + diff --git a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py index 26593a0f7..9865e28ec 100644 --- a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py +++ b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py @@ -25,6 +25,7 @@ import numpy as np from pecos.engines.cvm.binarray import BinArray +from pecos.engines.cvm.rng_model import RNGModel from pecos.engines.cvm.classical import eval_condition, eval_cop, set_output from pecos.engines.cvm.wasm import eval_cfunc, get_ccop from pecos.error_models.fake_error_model import FakeErrorModel @@ -89,8 +90,11 @@ def __init__( self.seed = None if self.seed: + self.rng_model = RNGModel(self.seed) np.random.seed(self.seed) random.seed(self.seed) + else: + self.rng_model = RNGModel(0) self.ccop = None @@ -239,6 +243,7 @@ def run_circuit( if eval_condition(params.get("cond"), output) and eval_cond2: # Run quantum simulator if symbol == "cop": + print(f'looking at {params}') if ( params.get("cop_type") == "Idle" or params.get("is_transport") @@ -248,6 +253,7 @@ def run_circuit( pass elif params.get("cop_type") == "CFunc": + print(params) eval_cfunc(self, params, output) elif params.get("expr"): From 07451304b2973cf044ca41f578935a0091465a87 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Mon, 30 Jun 2025 05:36:41 -0400 Subject: [PATCH 02/32] rng interface integrated into sim --- .../src/pecos/engines/cvm/rng_model.py | 42 +++++++++++++++++-- .../src/pecos/engines/hybrid_engine_old.py | 14 ++++--- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index e94fe97e7..2eefb8217 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -1,12 +1,16 @@ import pecos_rng_pcg from typing import Optional +from pecos.engines.cvm.binarray import BinArray class RNGModel: def __init__(self, seed:int=0, current_bound: Optional[int]=0) -> None: self.current_bound = current_bound self.count = 0 self.last_rand = 0 - self.seed = self.set_seed(seed) + self.seed = self.set_seed(seed) + + def __str__(self) -> str: + return f'RNG Model with bound {self.current_bound} with count {self.count}' def set_seed(self, seed:int) -> None: self.seed = seed @@ -31,6 +35,38 @@ def set_index(self, index: int) -> None: while self.count < index: self.rng_random() - def eval_func(self, op): - pass + def extract_val(self, param, output): + if param.isdigit(): + val = int(param) + elif '[' in param: + idx_creg = param.split('[') + creg = output[idx_creg[0]] + idx = int(idx_creg[-1][:-1]) + val = int(creg[idx]) + else: + reg = output[param] + val = int(reg) + return val + def eval_func(self, params, output): + func_name = params.get('func') + if func_name == 'RNGseed': + seed_var = params.get('args')[0] + seed = self.extract_val(seed_var, output) + self.set_seed(seed) + elif func_name == 'RNGbound': + bound_var = params.get('args')[0] + bound = self.extract_val(bound_var, output) + self.set_bound(bound) + elif func_name == 'RNGindex': + index_var = params.get('args')[0] + index = self.extract_val(index_var, output) + self.set_index(index) + elif func_name == 'RNGnum': + creg_name = params.get('assign_vars')[0] + creg = output[creg_name] + rng = self.rng_random() + binary_val = BinArray(creg.size, rng) + creg.set(binary_val) + else: + raise ValueError(f'RNG function not supported {func_name}') \ No newline at end of file diff --git a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py index 9865e28ec..19431aa58 100644 --- a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py +++ b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py @@ -141,7 +141,8 @@ def run( # -------------------- self.generate_errors = True error_circuits = error_gen.start(circuit, error_params) - + self.rng_model.count = 0 + # run through the circuits... # --------------------------- for tick_circuit, time, params in circuit.iter_ticks(): @@ -154,7 +155,7 @@ def run( error_circuits = error_gen.generate_tick_errors( tick_circuit, time, - output, + # output, **params, ) errors = error_circuits.get(time, {}) @@ -226,7 +227,6 @@ def run_circuit( """ self.state = state - if removed_locations is None: removed_locations = set() @@ -243,7 +243,6 @@ def run_circuit( if eval_condition(params.get("cond"), output) and eval_cond2: # Run quantum simulator if symbol == "cop": - print(f'looking at {params}') if ( params.get("cop_type") == "Idle" or params.get("is_transport") @@ -253,8 +252,11 @@ def run_circuit( pass elif params.get("cop_type") == "CFunc": - print(params) - eval_cfunc(self, params, output) + cop_name = params.get('func') + if 'RNG' in cop_name: + self.rng_model.eval_func(params, output) + else: + eval_cfunc(self, params, output) elif params.get("expr"): eval_cop(params.get("expr"), output, width=self.regwidth) From 69c0c28ae598fb218d6e88053a7dca1d9ddea1e3 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Mon, 30 Jun 2025 14:58:17 -0400 Subject: [PATCH 03/32] adding support to check for JOB_shotnum --- .../quantum-pecos/src/pecos/engines/cvm/rng_model.py | 10 +++++++--- .../src/pecos/engines/hybrid_engine_old.py | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index 2eefb8217..5dad614d5 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -3,7 +3,8 @@ from pecos.engines.cvm.binarray import BinArray class RNGModel: - def __init__(self, seed:int=0, current_bound: Optional[int]=0) -> None: + def __init__(self, shot_id: int, seed:int=0, current_bound: Optional[int]=0) -> None: + self.shot_id = shot_id self.current_bound = current_bound self.count = 0 self.last_rand = 0 @@ -44,8 +45,11 @@ def extract_val(self, param, output): idx = int(idx_creg[-1][:-1]) val = int(creg[idx]) else: - reg = output[param] - val = int(reg) + if param == 'JOB_shotnum': + val = self.shot_id + else: + reg = output[param] + val = int(reg) return val def eval_func(self, params, output): diff --git a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py index 19431aa58..5cdae256c 100644 --- a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py +++ b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py @@ -104,6 +104,7 @@ def run( self, state: SimulatorProtocol, circuit: QuantumCircuit, + shot_id: int, error_gen: ParentErrorModel | None = None, error_params: dict[str, float | dict[str, float]] | None = None, error_circuits: dict[int, dict[str, QuantumCircuit | set[int]]] | None = None, @@ -142,6 +143,7 @@ def run( self.generate_errors = True error_circuits = error_gen.start(circuit, error_params) self.rng_model.count = 0 + self.rng_model.shot_id = shot_id # run through the circuits... # --------------------------- From 60fe89d89bc57b7aeeec4beaf275ed365581a8bf Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 1 Jul 2025 03:39:53 -0400 Subject: [PATCH 04/32] integrated into build files to build pecos_rng_pcg --- Makefile | 4 +++- pyproject.toml | 3 ++- uv.lock | 13 ++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index e140b6c27..163b7f8d2 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -26,6 +25,9 @@ installreqs: ## Install Python project requirements to root .venv build: installreqs ## Compile and install for development @unset CONDA_PREFIX && cd python/pecos-rslib/ && uv run maturin develop --uv @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] + export NANOBIND_DIR="python3 -m nanobind --include_dir" + cd lib/pecos_rng && mkdir build && cd build/ && cmake .. && cmake --build . && cd .. && uv pip install . + .PHONY: build-basic build-basic: installreqs ## Compile and install for development but do not include install extras diff --git a/pyproject.toml b/pyproject.toml index 98f668951..81a3479bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "pecos-workspace" -version = "0.6.0.dev8" +version = "0.7.0.dev8" [tool.uv.workspace] members = [ @@ -10,6 +10,7 @@ members = [ [dependency-groups] dev = [ + "nanobind", "maturin>=1.2,<2.0", # For building (should match build requirements) "setuptools>=62.6", # Build system "pre-commit", # Git hooks diff --git a/uv.lock b/uv.lock index f07c13aca..08c042d1b 100644 --- a/uv.lock +++ b/uv.lock @@ -886,6 +886,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/2a/e2/5d3f6ada4297caebe1a2add3b126fe800c96f56dbe5d1988a2cbe0b267aa/mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d", size = 4695, upload-time = "2023-02-04T12:11:25.002Z" }, ] +[[package]] +name = "nanobind" +version = "2.7.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d8/7d/f77f2bc2e2a210502a164556f8a742cd0f72f39061b97cb9d73bbd3ff0ab/nanobind-2.7.0.tar.gz", hash = "sha256:f9f1b160580c50dcf37b6495a0fd5ec61dc0d95dae5f8004f87dd9ad7eb46b34", size = 976093, upload-time = "2025-04-18T01:17:37.187Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/96/14/989883082b395146120d34ca7e484a2b24cb73b0e428576a3a4249bd4082/nanobind-2.7.0-py3-none-any.whl", hash = "sha256:73b12d0e751d140d6c1bf4b215e18818a8debfdb374f08dc3776ad208d808e74", size = 241690, upload-time = "2025-04-18T01:17:34.821Z" }, +] + [[package]] name = "networkx" version = "3.4.2" @@ -1038,7 +1047,7 @@ source = { editable = "python/pecos-rslib" } [[package]] name = "pecos-workspace" -version = "0.6.0.dev8" +version = "0.7.0.dev8" source = { virtual = "." } [package.dev-dependencies] @@ -1049,6 +1058,7 @@ dev = [ { name = "mkdocs" }, { name = "mkdocs-material" }, { name = "mkdocstrings", extra = ["python"] }, + { name = "nanobind" }, { name = "pre-commit" }, { name = "ruff" }, { name = "setuptools" }, @@ -1069,6 +1079,7 @@ dev = [ { name = "mkdocs" }, { name = "mkdocs-material" }, { name = "mkdocstrings", extras = ["python"] }, + { name = "nanobind" }, { name = "pre-commit" }, { name = "ruff" }, { name = "setuptools", specifier = ">=62.6" }, From 5bb562c69b2a1e71b7edf9e12aeccff4051b5672 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 2 Jul 2025 01:16:03 -0400 Subject: [PATCH 05/32] modified makefile to build pecos rng library across different envs --- Makefile | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 163b7f8d2..2209c5eeb 100644 --- a/Makefile +++ b/Makefile @@ -19,31 +19,38 @@ installreqs: ## Install Python project requirements to root .venv @echo "Installing requirements..." uv sync +.PHONY: buildrng +buildrng: + @echo "Building and installing RNG library..." + export NANOBIND_DIR="python3 -m nanobind --include_dir" + cd lib/pecos_rng && mkdir build && cd build/ && cmake .. && cmake --build . && cd .. && uv pip install . + # Building development environments # --------------------------------- .PHONY: build build: installreqs ## Compile and install for development @unset CONDA_PREFIX && cd python/pecos-rslib/ && uv run maturin develop --uv @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] - export NANOBIND_DIR="python3 -m nanobind --include_dir" - cd lib/pecos_rng && mkdir build && cd build/ && cmake .. && cmake --build . && cd .. && uv pip install . - + $(MAKE) buildrng .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 @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e . + $(MAKE) buildrng .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 @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] + $(MAKE) buildrng .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 @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] + $(MAKE) buildrng # Documentation # ------------- From cb944f66e19a316ec1019391532ae30c640effa4 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 2 Jul 2025 01:26:26 -0400 Subject: [PATCH 06/32] modified workflow to build rng lib --- .github/workflows/python-release.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 220885109..4cc869ce7 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -253,6 +253,13 @@ jobs: run: | python -m pip install --upgrade pip pip install build + + - name: Build PECOS RNG + run: | + cd lib/pecos_rng && mkdir build && cd build/ && cmake .. + cmake --build . && cd .. && python -m pip install . + env: + NANOBIND_DIR: python -m nanobind --include_dir - name: Build quantum-pecos wheel run: | From 977216945236423e0c38a23e896d02b6a1386b4e Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 2 Jul 2025 11:12:02 -0400 Subject: [PATCH 07/32] missed including nanobind explictly in Install build dependencies step --- .github/workflows/python-release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 4cc869ce7..1c9097113 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -252,7 +252,7 @@ jobs: - name: Install build dependencies run: | python -m pip install --upgrade pip - pip install build + pip install build nanobind - name: Build PECOS RNG run: | From dc94ab8d02279e791d27eec032ef62185c11026b Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 2 Jul 2025 11:29:49 -0400 Subject: [PATCH 08/32] added docstrings and types to parameters and return type --- lib/pecos_rng/pecos_rng_pcg/__init__.py | 2 +- .../src/pecos/engines/cvm/rng_model.py | 40 ++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/pecos_rng/pecos_rng_pcg/__init__.py b/lib/pecos_rng/pecos_rng_pcg/__init__.py index 2be7669c9..773be18e5 100644 --- a/lib/pecos_rng/pecos_rng_pcg/__init__.py +++ b/lib/pecos_rng/pecos_rng_pcg/__init__.py @@ -1,3 +1,3 @@ -from .pecos_rng import pcg32_random, pcg32_frandom, pcg32_boundedrand, pcg32_srandom +from pecos_rng import pcg32_random, pcg32_frandom, pcg32_boundedrand, pcg32_srandom __all__ = ['pcg32_random', 'pcg32_frandom', 'pcg32_boundedrand', 'pcg32_srandom'] diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index 5dad614d5..635370de5 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -3,40 +3,65 @@ from pecos.engines.cvm.binarray import BinArray class RNGModel: + """ + This class is responsible the functionality of generating a sequence + of random numbers + """ def __init__(self, shot_id: int, seed:int=0, current_bound: Optional[int]=0) -> None: + """ + Constructs an RNGModel object + """ self.shot_id = shot_id self.current_bound = current_bound self.count = 0 - self.last_rand = 0 self.seed = self.set_seed(seed) def __str__(self) -> str: + """ + Returning the str representation of the model + """ return f'RNG Model with bound {self.current_bound} with count {self.count}' def set_seed(self, seed:int) -> None: + """ + Setting the seed for generating random numbers + """ self.seed = seed pecos_rng_pcg.pcg32_srandom(seed) def set_bound(self, bound:int) -> None: + """ + Setting the current bound for generating random numbers + """ self.current_bound = bound def rng_random(self) -> int: + """ + Generating a random number and keeping track of how many we have generated + """ if self.current_bound == 0: rng_num = pecos_rng_pcg.pcg32_random() else: rng_num = pecos_rng_pcg.pcg32_boundedrand(self.current_bound) self.count+=1 - self.last_rand = rng_num return rng_num def set_index(self, index: int) -> None: + """ + Setting the index for the random number sequence. The + number after from the stream will be the idx of interest + """ if self.count > index: - raise BufferError("rngindex called after specified already generated") - # number after from the stream will be the idx of interest + error_msg = 'rngindex called after specified already generated' + raise BufferError(error_msg) while self.count < index: self.rng_random() - def extract_val(self, param, output): + def extract_val(self, param:str, output:dict) -> int: + """ + Responsible for extracting the value of interest depending on the type of the + parameter being passed in. + """ if param.isdigit(): val = int(param) elif '[' in param: @@ -52,7 +77,10 @@ def extract_val(self, param, output): val = int(reg) return val - def eval_func(self, params, output): + def eval_func(self, params:dict, output:dict) -> None: + """ + Calling the appropriate functions dependent on RNG Function call passed in + """ func_name = params.get('func') if func_name == 'RNGseed': seed_var = params.get('args')[0] From b65acacd65a5c5fa3559fc18ef0f4e593021ff45 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 2 Jul 2025 11:34:54 -0400 Subject: [PATCH 09/32] pre-commit changes --- .github/workflows/python-release.yml | 2 +- Makefile | 4 +- lib/pecos_rng/pecos_rng_pcg/__init__.py | 4 +- lib/pecos_rng/pyproject.toml | 1 - lib/pecos_rng/src/rng_pcg.c | 2 +- lib/pecos_rng/src/wrapper.cpp | 2 +- .../src/pecos/engines/cvm/rng_model.py | 77 ++++++++++--------- .../src/pecos/engines/hybrid_engine_old.py | 8 +- 8 files changed, 51 insertions(+), 49 deletions(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 1c9097113..51d3331b5 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -253,7 +253,7 @@ jobs: run: | python -m pip install --upgrade pip pip install build nanobind - + - name: Build PECOS RNG run: | cd lib/pecos_rng && mkdir build && cd build/ && cmake .. diff --git a/Makefile b/Makefile index 2209c5eeb..f60f254f3 100644 --- a/Makefile +++ b/Makefile @@ -24,14 +24,14 @@ buildrng: @echo "Building and installing RNG library..." export NANOBIND_DIR="python3 -m nanobind --include_dir" cd lib/pecos_rng && mkdir build && cd build/ && cmake .. && cmake --build . && cd .. && uv pip install . - + # Building development environments # --------------------------------- .PHONY: build build: installreqs ## Compile and install for development @unset CONDA_PREFIX && cd python/pecos-rslib/ && uv run maturin develop --uv @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] - $(MAKE) buildrng + $(MAKE) buildrng .PHONY: build-basic build-basic: installreqs ## Compile and install for development but do not include install extras diff --git a/lib/pecos_rng/pecos_rng_pcg/__init__.py b/lib/pecos_rng/pecos_rng_pcg/__init__.py index 773be18e5..e771e660a 100644 --- a/lib/pecos_rng/pecos_rng_pcg/__init__.py +++ b/lib/pecos_rng/pecos_rng_pcg/__init__.py @@ -1,3 +1,3 @@ -from pecos_rng import pcg32_random, pcg32_frandom, pcg32_boundedrand, pcg32_srandom +from pecos_rng import pcg32_boundedrand, pcg32_frandom, pcg32_random, pcg32_srandom -__all__ = ['pcg32_random', 'pcg32_frandom', 'pcg32_boundedrand', 'pcg32_srandom'] +__all__ = ["pcg32_boundedrand", "pcg32_frandom", "pcg32_random", "pcg32_srandom"] diff --git a/lib/pecos_rng/pyproject.toml b/lib/pecos_rng/pyproject.toml index bcaa65271..65f396e79 100644 --- a/lib/pecos_rng/pyproject.toml +++ b/lib/pecos_rng/pyproject.toml @@ -20,4 +20,3 @@ wheel.packages = ["pecos_rng_pcg"] # Optional: set cmake minimum version or other CMake arguments cmake.minimum-version = "3.18" cmake.verbose = true - diff --git a/lib/pecos_rng/src/rng_pcg.c b/lib/pecos_rng/src/rng_pcg.c index c6c5bca21..8ddb23d2e 100644 --- a/lib/pecos_rng/src/rng_pcg.c +++ b/lib/pecos_rng/src/rng_pcg.c @@ -32,7 +32,7 @@ typedef struct pcg_state_setseq_64 { // global RNG state static pcg32_random_t pcg32_global = { - 0x853c49e6748fea9bULL, + 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }; diff --git a/lib/pecos_rng/src/wrapper.cpp b/lib/pecos_rng/src/wrapper.cpp index 1d774beac..94a258808 100644 --- a/lib/pecos_rng/src/wrapper.cpp +++ b/lib/pecos_rng/src/wrapper.cpp @@ -9,4 +9,4 @@ NB_MODULE(pecos_rng, m) { 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"); -} \ No newline at end of file +} diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index 635370de5..2d99e45bb 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -1,15 +1,18 @@ import pecos_rng_pcg -from typing import Optional + from pecos.engines.cvm.binarray import BinArray + class RNGModel: """ - This class is responsible the functionality of generating a sequence - of random numbers + This class is responsible the functionality of generating a sequence of random numbers. """ - def __init__(self, shot_id: int, seed:int=0, current_bound: Optional[int]=0) -> None: + + def __init__( + self, shot_id: int, seed: int = 0, current_bound: int | None = 0 + ) -> None: """ - Constructs an RNGModel object + Constructs an RNGModel object. """ self.shot_id = shot_id self.current_bound = current_bound @@ -18,87 +21,87 @@ def __init__(self, shot_id: int, seed:int=0, current_bound: Optional[int]=0) -> def __str__(self) -> str: """ - Returning the str representation of the model + Returns the str representation of the model. """ - return f'RNG Model with bound {self.current_bound} with count {self.count}' + return f"RNG Model with bound {self.current_bound} with count {self.count}" - def set_seed(self, seed:int) -> None: + def set_seed(self, seed: int) -> None: """ - Setting the seed for generating random numbers + Setting the seed for generating random numbers. """ self.seed = seed pecos_rng_pcg.pcg32_srandom(seed) - def set_bound(self, bound:int) -> None: + def set_bound(self, bound: int) -> None: """ - Setting the current bound for generating random numbers + Setting the current bound for generating random numbers. """ self.current_bound = bound def rng_random(self) -> int: """ - Generating a random number and keeping track of how many we have generated + Generating a random number and keeping track of how many we have generated. """ if self.current_bound == 0: rng_num = pecos_rng_pcg.pcg32_random() else: rng_num = pecos_rng_pcg.pcg32_boundedrand(self.current_bound) - self.count+=1 + self.count += 1 return rng_num def set_index(self, index: int) -> None: """ Setting the index for the random number sequence. The - number after from the stream will be the idx of interest + number after from the stream will be the idx of interest. """ if self.count > index: - error_msg = 'rngindex called after specified already generated' + error_msg = "rngindex called after specified already generated" raise BufferError(error_msg) while self.count < index: self.rng_random() - - def extract_val(self, param:str, output:dict) -> int: + + def extract_val(self, param: str, output: dict) -> int: """ Responsible for extracting the value of interest depending on the type of the - parameter being passed in. + parameter being passed in. """ if param.isdigit(): val = int(param) - elif '[' in param: - idx_creg = param.split('[') + elif "[" in param: + idx_creg = param.split("[") creg = output[idx_creg[0]] idx = int(idx_creg[-1][:-1]) val = int(creg[idx]) + elif param == "JOB_shotnum": + val = self.shot_id else: - if param == 'JOB_shotnum': - val = self.shot_id - else: - reg = output[param] - val = int(reg) + reg = output[param] + val = int(reg) return val - def eval_func(self, params:dict, output:dict) -> None: + def eval_func(self, params: dict, output: dict) -> None: """ - Calling the appropriate functions dependent on RNG Function call passed in + Calling the appropriate functions dependent on RNG Function call passed in. """ - func_name = params.get('func') - if func_name == 'RNGseed': - seed_var = params.get('args')[0] + func_name = params.get("func") + if func_name == "RNGseed": + seed_var = params.get("args")[0] seed = self.extract_val(seed_var, output) self.set_seed(seed) - elif func_name == 'RNGbound': - bound_var = params.get('args')[0] + elif func_name == "RNGbound": + bound_var = params.get("args")[0] bound = self.extract_val(bound_var, output) self.set_bound(bound) - elif func_name == 'RNGindex': - index_var = params.get('args')[0] + elif func_name == "RNGindex": + index_var = params.get("args")[0] index = self.extract_val(index_var, output) self.set_index(index) - elif func_name == 'RNGnum': - creg_name = params.get('assign_vars')[0] + elif func_name == "RNGnum": + creg_name = params.get("assign_vars")[0] creg = output[creg_name] rng = self.rng_random() binary_val = BinArray(creg.size, rng) creg.set(binary_val) else: - raise ValueError(f'RNG function not supported {func_name}') \ No newline at end of file + error_msg = f"RNG function not supported {func_name}" + raise ValueError(error_msg) diff --git a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py index 5cdae256c..1ebc8e9b8 100644 --- a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py +++ b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py @@ -25,8 +25,8 @@ import numpy as np from pecos.engines.cvm.binarray import BinArray -from pecos.engines.cvm.rng_model import RNGModel from pecos.engines.cvm.classical import eval_condition, eval_cop, set_output +from pecos.engines.cvm.rng_model import RNGModel from pecos.engines.cvm.wasm import eval_cfunc, get_ccop from pecos.error_models.fake_error_model import FakeErrorModel from pecos.errors import NotSupportedGateError @@ -144,7 +144,7 @@ def run( error_circuits = error_gen.start(circuit, error_params) self.rng_model.count = 0 self.rng_model.shot_id = shot_id - + # run through the circuits... # --------------------------- for tick_circuit, time, params in circuit.iter_ticks(): @@ -254,8 +254,8 @@ def run_circuit( pass elif params.get("cop_type") == "CFunc": - cop_name = params.get('func') - if 'RNG' in cop_name: + cop_name = params.get("func") + if "RNG" in cop_name: self.rng_model.eval_func(params, output) else: eval_cfunc(self, params, output) From c55d8ac57c1dc4bbd3a53f3be5b53be8adb0bf3a Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 2 Jul 2025 11:39:27 -0400 Subject: [PATCH 10/32] more pre-commit changes --- .../src/pecos/engines/cvm/rng_model.py | 43 ++++++++----------- .../src/pecos/engines/hybrid_engine_old.py | 1 + 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index 2d99e45bb..05b5ea149 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -1,3 +1,8 @@ +""" +This Module is reponsible for keeping track of the state for generating a sequence +of random numbers. It handles RNG platform function calls that that are handled by the +pcg_rng library. +""" import pecos_rng_pcg from pecos.engines.cvm.binarray import BinArray @@ -7,41 +12,33 @@ class RNGModel: """ This class is responsible the functionality of generating a sequence of random numbers. """ - def __init__( - self, shot_id: int, seed: int = 0, current_bound: int | None = 0 + self, + shot_id: int, + seed: int = 0, + current_bound: int | None = 0, ) -> None: - """ - Constructs an RNGModel object. - """ + """Constructs an RNGModel object.""" self.shot_id = shot_id self.current_bound = current_bound self.count = 0 self.seed = self.set_seed(seed) def __str__(self) -> str: - """ - Returns the str representation of the model. - """ + """Returns the str representation of the model.""" return f"RNG Model with bound {self.current_bound} with count {self.count}" def set_seed(self, seed: int) -> None: - """ - Setting the seed for generating random numbers. - """ + """Setting the seed for generating random numbers.""" self.seed = seed pecos_rng_pcg.pcg32_srandom(seed) def set_bound(self, bound: int) -> None: - """ - Setting the current bound for generating random numbers. - """ + """Setting the current bound for generating random numbers.""" self.current_bound = bound def rng_random(self) -> int: - """ - Generating a random number and keeping track of how many we have generated. - """ + """Generating a random number and keeping track of how many we have generated.""" if self.current_bound == 0: rng_num = pecos_rng_pcg.pcg32_random() else: @@ -50,9 +47,9 @@ def rng_random(self) -> int: return rng_num def set_index(self, index: int) -> None: - """ - Setting the index for the random number sequence. The + """Setting the index for the random number sequence. The number after from the stream will be the idx of interest. + """ if self.count > index: error_msg = "rngindex called after specified already generated" @@ -61,9 +58,9 @@ def set_index(self, index: int) -> None: self.rng_random() def extract_val(self, param: str, output: dict) -> int: - """ - Responsible for extracting the value of interest depending on the type of the + """Responsible for extracting the value of interest depending on the type of the parameter being passed in. + """ if param.isdigit(): val = int(param) @@ -80,9 +77,7 @@ def extract_val(self, param: str, output: dict) -> int: return val def eval_func(self, params: dict, output: dict) -> None: - """ - Calling the appropriate functions dependent on RNG Function call passed in. - """ + """Calling the appropriate functions dependent on RNG Function call passed in.""" func_name = params.get("func") if func_name == "RNGseed": seed_var = params.get("args")[0] diff --git a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py index 1ebc8e9b8..476e449d4 100644 --- a/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py +++ b/python/quantum-pecos/src/pecos/engines/hybrid_engine_old.py @@ -117,6 +117,7 @@ def run( Args: state: Quantum simulator state. circuit: Quantum circuit to execute. + shot_id: Integer representing current shot. error_gen: Optional error model. error_params: Parameters for error generation. error_circuits: Pre-generated error circuits. From 7de750f9f1f3dab235bb66f39dea20fdbc7f5690 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 2 Jul 2025 11:43:45 -0400 Subject: [PATCH 11/32] pre-commit green --- lib/pecos_rng/pecos_rng_pcg/__init__.py | 2 ++ .../src/pecos/engines/cvm/rng_model.py | 23 ++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/pecos_rng/pecos_rng_pcg/__init__.py b/lib/pecos_rng/pecos_rng_pcg/__init__.py index e771e660a..e6d689485 100644 --- a/lib/pecos_rng/pecos_rng_pcg/__init__.py +++ b/lib/pecos_rng/pecos_rng_pcg/__init__.py @@ -1,3 +1,5 @@ +"""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"] diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index 05b5ea149..b30613bdc 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -1,17 +1,17 @@ +"""This Module is responsible for keeping track of the state for generating a sequence of random numbers. + +It handles RNG platform function calls that that are handled by the pcg_rng library. + """ -This Module is reponsible for keeping track of the state for generating a sequence -of random numbers. It handles RNG platform function calls that that are handled by the -pcg_rng library. -""" + import pecos_rng_pcg from pecos.engines.cvm.binarray import BinArray class RNGModel: - """ - This class is responsible the functionality of generating a sequence of random numbers. - """ + """This class is responsible the functionality of generating a sequence of random numbers.""" + def __init__( self, shot_id: int, @@ -47,9 +47,9 @@ def rng_random(self) -> int: return rng_num def set_index(self, index: int) -> None: - """Setting the index for the random number sequence. The - number after from the stream will be the idx of interest. + """Setting the index for the random number sequence. + The number after from the stream will be the idx of interest. """ if self.count > index: error_msg = "rngindex called after specified already generated" @@ -58,10 +58,7 @@ def set_index(self, index: int) -> None: self.rng_random() def extract_val(self, param: str, output: dict) -> int: - """Responsible for extracting the value of interest depending on the type of the - parameter being passed in. - - """ + """Responsible for extracting the value of interest depending on the type of the parameter being passed in.""" if param.isdigit(): val = int(param) elif "[" in param: From f04b4148436fac48d4748cf62e9687b837e7ff4f Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Wed, 2 Jul 2025 11:51:18 -0400 Subject: [PATCH 12/32] fixed up github action --- .github/workflows/python-release.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 51d3331b5..9a5fb02e3 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -201,7 +201,14 @@ jobs: - name: Install build dependencies run: | python -m pip install --upgrade pip - pip install build + pip install build nanobind + + - name: Build PECOS RNG + run: | + cd lib/pecos_rng && mkdir build && cd build/ && cmake .. + cmake --build . && cd .. && python -m pip install . + env: + NANOBIND_DIR: python -m nanobind --include_dir - name: Build quantum-pecos SDist run: | @@ -252,6 +259,7 @@ jobs: - name: Install build dependencies run: | python -m pip install --upgrade pip + apt-get install -y python3-dev pip install build nanobind - name: Build PECOS RNG From 1bb5a58c4a46fe90e62ec9a6e275f67c9c8c5177 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Thu, 3 Jul 2025 12:50:47 -0400 Subject: [PATCH 13/32] cleaned up makefile and how to pecos_rng library and modified github action to take account python version for cmake --- .github/workflows/python-release.yml | 4 ++-- Makefile | 13 +++++++------ {lib/pecos_rng => clibs/pecos-rng}/CMakeLists.txt | 4 ++-- {lib/pecos_rng => clibs/pecos-rng}/README.md | 0 .../pecos-rng/pecos_pcg}/__init__.py | 2 +- {lib/pecos_rng => clibs/pecos-rng}/pyproject.toml | 4 ++-- {lib/pecos_rng => clibs/pecos-rng}/src/rng_pcg.c | 0 {lib/pecos_rng => clibs/pecos-rng}/src/rng_pcg.h | 0 {lib/pecos_rng => clibs/pecos-rng}/src/wrapper.cpp | 0 .../src/pecos/engines/cvm/rng_model.py | 8 ++++---- 10 files changed, 18 insertions(+), 17 deletions(-) rename {lib/pecos_rng => clibs/pecos-rng}/CMakeLists.txt (88%) rename {lib/pecos_rng => clibs/pecos-rng}/README.md (100%) rename {lib/pecos_rng/pecos_rng_pcg => clibs/pecos-rng/pecos_pcg}/__init__.py (65%) rename {lib/pecos_rng => clibs/pecos-rng}/pyproject.toml (90%) rename {lib/pecos_rng => clibs/pecos-rng}/src/rng_pcg.c (100%) rename {lib/pecos_rng => clibs/pecos-rng}/src/rng_pcg.h (100%) rename {lib/pecos_rng => clibs/pecos-rng}/src/wrapper.cpp (100%) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 9a5fb02e3..b69675e0a 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -131,7 +131,7 @@ jobs: uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - + - name: Remove conflicting README.md run: | if [ -f crates/pecos-python/README.md ]; then @@ -206,7 +206,7 @@ jobs: - name: Build PECOS RNG run: | cd lib/pecos_rng && mkdir build && cd build/ && cmake .. - cmake --build . && cd .. && python -m pip install . + cmake -DPYTHON_EXECUTABLE=$(which python) --build . && cd .. && python -m pip install . env: NANOBIND_DIR: python -m nanobind --include_dir diff --git a/Makefile b/Makefile index f60f254f3..b4ba83d48 100644 --- a/Makefile +++ b/Makefile @@ -22,35 +22,36 @@ installreqs: ## Install Python project requirements to root .venv .PHONY: buildrng buildrng: @echo "Building and installing RNG library..." - export NANOBIND_DIR="python3 -m nanobind --include_dir" - cd lib/pecos_rng && mkdir build && cd build/ && cmake .. && cmake --build . && cd .. && uv pip install . + uv pip install nanobind + nanobind_DIR="python3 -m nanobind --include_dir" + cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. && cmake --build . && cd .. && uv pip install . # Building development environments # --------------------------------- .PHONY: build build: installreqs ## Compile and install for development @unset CONDA_PREFIX && cd python/pecos-rslib/ && uv run maturin develop --uv - @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] $(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 - @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e . $(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 - @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] $(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 - @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] $(MAKE) buildrng + @unset CONDA_PREFIX && cd python/quantum-pecos && uv pip install -e .[all] # Documentation # ------------- diff --git a/lib/pecos_rng/CMakeLists.txt b/clibs/pecos-rng/CMakeLists.txt similarity index 88% rename from lib/pecos_rng/CMakeLists.txt rename to clibs/pecos-rng/CMakeLists.txt index 2cd1575c0..b2f3f0760 100644 --- a/lib/pecos_rng/CMakeLists.txt +++ b/clibs/pecos-rng/CMakeLists.txt @@ -11,7 +11,7 @@ else() endif() # Find Python (for Python 3.13) -find_package(Python 3.13 COMPONENTS Interpreter ${DEV_MODULE} REQUIRED) +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 @@ -31,4 +31,4 @@ 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_rng_pcg/") +install(TARGETS pecos_rng DESTINATION "pecos_pcg") diff --git a/lib/pecos_rng/README.md b/clibs/pecos-rng/README.md similarity index 100% rename from lib/pecos_rng/README.md rename to clibs/pecos-rng/README.md diff --git a/lib/pecos_rng/pecos_rng_pcg/__init__.py b/clibs/pecos-rng/pecos_pcg/__init__.py similarity index 65% rename from lib/pecos_rng/pecos_rng_pcg/__init__.py rename to clibs/pecos-rng/pecos_pcg/__init__.py index e6d689485..f8b9eee12 100644 --- a/lib/pecos_rng/pecos_rng_pcg/__init__.py +++ b/clibs/pecos-rng/pecos_pcg/__init__.py @@ -1,5 +1,5 @@ """Python Package responsible for generating random numbers using pcg_rng.""" -from pecos_rng import pcg32_boundedrand, pcg32_frandom, pcg32_random, pcg32_srandom +from .pecos_rng import pcg32_boundedrand, pcg32_frandom, pcg32_random, pcg32_srandom __all__ = ["pcg32_boundedrand", "pcg32_frandom", "pcg32_random", "pcg32_srandom"] diff --git a/lib/pecos_rng/pyproject.toml b/clibs/pecos-rng/pyproject.toml similarity index 90% rename from lib/pecos_rng/pyproject.toml rename to clibs/pecos-rng/pyproject.toml index 65f396e79..72a22f680 100644 --- a/lib/pecos_rng/pyproject.toml +++ b/clibs/pecos-rng/pyproject.toml @@ -3,7 +3,7 @@ requires = ["scikit-build-core[pyproject]", "nanobind"] build-backend = "scikit_build_core.build" [project] -name = "pecos_rng_pcg" +name = "pecos-pcg" version = "0.1.0" description = "A Python package with a nanobind-based C++ extension" license = { text = "MIT" } @@ -16,7 +16,7 @@ classifiers = [ ] [tool.scikit-build] -wheel.packages = ["pecos_rng_pcg"] +wheel.packages = ["pecos_pcg"] # Optional: set cmake minimum version or other CMake arguments cmake.minimum-version = "3.18" cmake.verbose = true diff --git a/lib/pecos_rng/src/rng_pcg.c b/clibs/pecos-rng/src/rng_pcg.c similarity index 100% rename from lib/pecos_rng/src/rng_pcg.c rename to clibs/pecos-rng/src/rng_pcg.c diff --git a/lib/pecos_rng/src/rng_pcg.h b/clibs/pecos-rng/src/rng_pcg.h similarity index 100% rename from lib/pecos_rng/src/rng_pcg.h rename to clibs/pecos-rng/src/rng_pcg.h diff --git a/lib/pecos_rng/src/wrapper.cpp b/clibs/pecos-rng/src/wrapper.cpp similarity index 100% rename from lib/pecos_rng/src/wrapper.cpp rename to clibs/pecos-rng/src/wrapper.cpp diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index b30613bdc..a34d74002 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -4,7 +4,7 @@ """ -import pecos_rng_pcg +from pecos_pcg import pecos_rng from pecos.engines.cvm.binarray import BinArray @@ -31,7 +31,7 @@ def __str__(self) -> str: def set_seed(self, seed: int) -> None: """Setting the seed for generating random numbers.""" self.seed = seed - pecos_rng_pcg.pcg32_srandom(seed) + pecos_rng.pcg32_srandom(seed) def set_bound(self, bound: int) -> None: """Setting the current bound for generating random numbers.""" @@ -40,9 +40,9 @@ def set_bound(self, bound: int) -> None: def rng_random(self) -> int: """Generating a random number and keeping track of how many we have generated.""" if self.current_bound == 0: - rng_num = pecos_rng_pcg.pcg32_random() + rng_num = pecos_rng.pcg32_random() else: - rng_num = pecos_rng_pcg.pcg32_boundedrand(self.current_bound) + rng_num = pecos_rng.pcg32_boundedrand(self.current_bound) self.count += 1 return rng_num From 9dd3f8d48d9fb9d55f2453e3068811d7f6f7f04e Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Thu, 3 Jul 2025 12:55:55 -0400 Subject: [PATCH 14/32] pre-commit changes --- .github/workflows/python-release.yml | 4 ++-- clibs/pecos-rng/pecos_pcg/__init__.py | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index b69675e0a..0937e58f9 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -131,7 +131,7 @@ jobs: uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - + - name: Remove conflicting README.md run: | if [ -f crates/pecos-python/README.md ]; then @@ -202,7 +202,7 @@ jobs: run: | python -m pip install --upgrade pip pip install build nanobind - + - name: Build PECOS RNG run: | cd lib/pecos_rng && mkdir build && cd build/ && cmake .. diff --git a/clibs/pecos-rng/pecos_pcg/__init__.py b/clibs/pecos-rng/pecos_pcg/__init__.py index f8b9eee12..07d18113e 100644 --- a/clibs/pecos-rng/pecos_pcg/__init__.py +++ b/clibs/pecos-rng/pecos_pcg/__init__.py @@ -1,5 +1,10 @@ """Python Package responsible for generating random numbers using pcg_rng.""" -from .pecos_rng import pcg32_boundedrand, pcg32_frandom, pcg32_random, pcg32_srandom +from .pecos_rng import ( + pcg32_boundedrand, + pcg32_frandom, + pcg32_random, + pcg32_srandom, +) # noqa: TID252 __all__ = ["pcg32_boundedrand", "pcg32_frandom", "pcg32_random", "pcg32_srandom"] From c8dfa7a81381e69835787c3d10f6d199a06d30c3 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Sat, 5 Jul 2025 00:19:52 -0400 Subject: [PATCH 15/32] added test case for rng --- python/tests/pecos/unit/test_rng.py | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 python/tests/pecos/unit/test_rng.py diff --git a/python/tests/pecos/unit/test_rng.py b/python/tests/pecos/unit/test_rng.py new file mode 100644 index 000000000..02c3d4470 --- /dev/null +++ b/python/tests/pecos/unit/test_rng.py @@ -0,0 +1,51 @@ +import unittest +import sys +from pecos.engines.cvm.rng_model import RNGModel + +import random + +class TestRNG(unittest.TestCase): + + def test_set_seed(self): + rng = RNGModel(shot_id = 0) + seed = 42 + rng.set_seed(seed) + self.assertEqual(rng.seed, seed) + + def test_random_number(self): + rng = RNGModel(shot_id = 0) + random = rng.rng_random() + self.assertTrue(isinstance(random, int)) + + def test_bounded_random(self): + rng = RNGModel(shot_id = 0) + rng.set_seed(42) + bound = 16 + rng.set_bound(bound) + self.assertEqual(rng.current_bound, bound) + + random_number = rng.rng_random() + self.assertTrue(random_number < bound) + + def test_set_idx(self): + rng = RNGModel(shot_id = 0) + rng.set_seed(42) + idx = 4 + rng.set_index(idx) + self.assertEqual(rng.count, idx) + + def test_multiple_bounded_rand(self): + rng = RNGModel(shot_id = 0) + rng.set_seed(42) + + for _ in range(100): + random_bound = random.randint(1, 2**32-1) + rng.set_bound(random_bound) + random_number = rng.rng_random() + self.assertTrue(0 <= random_number < random_bound) + + +if __name__ == '__main__': + unittest.main() + + From 79f1f6cd89e67c355c6b2d20883808868ec118bb Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Mon, 7 Jul 2025 12:06:42 -0400 Subject: [PATCH 16/32] pre-commit changes for test --- clibs/pecos-rng/pecos_pcg/__init__.py | 2 +- .../src/pecos/engines/cvm/rng_model.py | 2 +- python/tests/pecos/unit/test_rng.py | 54 ++++++++++--------- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/clibs/pecos-rng/pecos_pcg/__init__.py b/clibs/pecos-rng/pecos_pcg/__init__.py index 07d18113e..7c2210c3d 100644 --- a/clibs/pecos-rng/pecos_pcg/__init__.py +++ b/clibs/pecos-rng/pecos_pcg/__init__.py @@ -5,6 +5,6 @@ pcg32_frandom, pcg32_random, pcg32_srandom, -) # noqa: TID252 +) # noqa: TID252 __all__ = ["pcg32_boundedrand", "pcg32_frandom", "pcg32_random", "pcg32_srandom"] diff --git a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py index a34d74002..de760a639 100644 --- a/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py +++ b/python/quantum-pecos/src/pecos/engines/cvm/rng_model.py @@ -10,7 +10,7 @@ class RNGModel: - """This class is responsible the functionality of generating a sequence of random numbers.""" + """This class is responsible for the functionality of generating a sequence of random numbers.""" def __init__( self, diff --git a/python/tests/pecos/unit/test_rng.py b/python/tests/pecos/unit/test_rng.py index 02c3d4470..037598df6 100644 --- a/python/tests/pecos/unit/test_rng.py +++ b/python/tests/pecos/unit/test_rng.py @@ -1,51 +1,57 @@ +"""Testing module for the RNG Model.""" + +import random import unittest -import sys + from pecos.engines.cvm.rng_model import RNGModel -import random class TestRNG(unittest.TestCase): + """This class is responsible for testing RNG platform calls.""" - def test_set_seed(self): - rng = RNGModel(shot_id = 0) + def test_set_seed(self) -> None: + """Verifies that a seed is set properly for our RNG model.""" + rng = RNGModel(shot_id=0) seed = 42 rng.set_seed(seed) - self.assertEqual(rng.seed, seed) + assert rng.seed == seed - def test_random_number(self): - rng = RNGModel(shot_id = 0) + def test_random_number(self) -> None: + """Verifies that the random number generated is an int type.""" + rng = RNGModel(shot_id=0) random = rng.rng_random() - self.assertTrue(isinstance(random, int)) + assert isinstance(random, int) - def test_bounded_random(self): - rng = RNGModel(shot_id = 0) + def test_bounded_random(self) -> None: + """Verifies that a single generated random number is within bounds.""" + rng = RNGModel(shot_id=0) rng.set_seed(42) bound = 16 rng.set_bound(bound) - self.assertEqual(rng.current_bound, bound) + assert rng.current_bound == bound random_number = rng.rng_random() - self.assertTrue(random_number < bound) - - def test_set_idx(self): - rng = RNGModel(shot_id = 0) + assert 0 <= random_number < bound + + def test_set_idx(self) -> None: + """Verifies that the idx is set properly for our model.""" + rng = RNGModel(shot_id=0) rng.set_seed(42) idx = 4 rng.set_index(idx) - self.assertEqual(rng.count, idx) + assert rng.count == idx - def test_multiple_bounded_rand(self): - rng = RNGModel(shot_id = 0) + def test_multiple_bounded_rand(self) -> None: + """For several randomly generated number, with a random bound, verifies that its appropriate.""" + rng = RNGModel(shot_id=0) rng.set_seed(42) for _ in range(100): - random_bound = random.randint(1, 2**32-1) + random_bound = random.randint(1, 2**32 - 1) # noqa: S311 rng.set_bound(random_bound) random_number = rng.rng_random() - self.assertTrue(0 <= random_number < random_bound) - - -if __name__ == '__main__': - unittest.main() + assert 0 <= random_number < random_bound +if __name__ == "__main__": + unittest.main() From 7f092263d498e8eccba1ab941841ab9abf148953 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Mon, 7 Jul 2025 12:32:50 -0400 Subject: [PATCH 17/32] changed from unittest to pytest --- python/tests/pecos/unit/test_rng.py | 90 +++++++++++++---------------- 1 file changed, 41 insertions(+), 49 deletions(-) diff --git a/python/tests/pecos/unit/test_rng.py b/python/tests/pecos/unit/test_rng.py index 037598df6..22e8a2ab0 100644 --- a/python/tests/pecos/unit/test_rng.py +++ b/python/tests/pecos/unit/test_rng.py @@ -1,57 +1,49 @@ """Testing module for the RNG Model.""" import random -import unittest from pecos.engines.cvm.rng_model import RNGModel -class TestRNG(unittest.TestCase): - """This class is responsible for testing RNG platform calls.""" - - def test_set_seed(self) -> None: - """Verifies that a seed is set properly for our RNG model.""" - rng = RNGModel(shot_id=0) - seed = 42 - rng.set_seed(seed) - assert rng.seed == seed - - def test_random_number(self) -> None: - """Verifies that the random number generated is an int type.""" - rng = RNGModel(shot_id=0) - random = rng.rng_random() - assert isinstance(random, int) - - def test_bounded_random(self) -> None: - """Verifies that a single generated random number is within bounds.""" - rng = RNGModel(shot_id=0) - rng.set_seed(42) - bound = 16 - rng.set_bound(bound) - assert rng.current_bound == bound - +def test_set_seed() -> None: + """Verifies that a seed is set properly for our RNG model.""" + rng = RNGModel(shot_id=0) + seed = 42 + rng.set_seed(seed) + assert rng.seed == seed + +def test_random_number() -> None: + """Verifies that the random number generated is an int type.""" + rng = RNGModel(shot_id=0) + random = rng.rng_random() + assert isinstance(random, int) + +def test_bounded_random() -> None: + """Verifies that a single generated random number is within bounds.""" + rng = RNGModel(shot_id=0) + rng.set_seed(42) + bound = 16 + rng.set_bound(bound) + assert rng.current_bound == bound + + random_number = rng.rng_random() + assert 0 <= random_number < bound + +def test_set_idx() -> None: + """Verifies that the idx is set properly for our model.""" + rng = RNGModel(shot_id=0) + rng.set_seed(42) + idx = 4 + rng.set_index(idx) + assert rng.count == idx + +def test_multiple_bounded_rand() -> None: + """For several randomly generated number, with a random bound, verifies that its appropriate.""" + rng = RNGModel(shot_id=0) + rng.set_seed(42) + + for _ in range(100): + random_bound = random.randint(1, 2**32 - 1) # noqa: S311 + rng.set_bound(random_bound) random_number = rng.rng_random() - assert 0 <= random_number < bound - - def test_set_idx(self) -> None: - """Verifies that the idx is set properly for our model.""" - rng = RNGModel(shot_id=0) - rng.set_seed(42) - idx = 4 - rng.set_index(idx) - assert rng.count == idx - - def test_multiple_bounded_rand(self) -> None: - """For several randomly generated number, with a random bound, verifies that its appropriate.""" - rng = RNGModel(shot_id=0) - rng.set_seed(42) - - for _ in range(100): - random_bound = random.randint(1, 2**32 - 1) # noqa: S311 - rng.set_bound(random_bound) - random_number = rng.rng_random() - assert 0 <= random_number < random_bound - - -if __name__ == "__main__": - unittest.main() + assert 0 <= random_number < random_bound From 6e137f91b28cca4cef0da0d87f9b34a606232916 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 01:57:34 -0400 Subject: [PATCH 18/32] modified ruff to ignore specific ruff check to assure appropriate building of png library --- clibs/pecos-rng/pecos_pcg/__init__.py | 2 +- python/tests/pecos/unit/test_rng.py | 4 ++++ ruff.toml | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/clibs/pecos-rng/pecos_pcg/__init__.py b/clibs/pecos-rng/pecos_pcg/__init__.py index 7c2210c3d..5580f3ca4 100644 --- a/clibs/pecos-rng/pecos_pcg/__init__.py +++ b/clibs/pecos-rng/pecos_pcg/__init__.py @@ -5,6 +5,6 @@ pcg32_frandom, pcg32_random, pcg32_srandom, -) # noqa: TID252 +) __all__ = ["pcg32_boundedrand", "pcg32_frandom", "pcg32_random", "pcg32_srandom"] diff --git a/python/tests/pecos/unit/test_rng.py b/python/tests/pecos/unit/test_rng.py index 22e8a2ab0..d342adc13 100644 --- a/python/tests/pecos/unit/test_rng.py +++ b/python/tests/pecos/unit/test_rng.py @@ -12,12 +12,14 @@ def test_set_seed() -> None: rng.set_seed(seed) assert rng.seed == seed + def test_random_number() -> None: """Verifies that the random number generated is an int type.""" rng = RNGModel(shot_id=0) random = rng.rng_random() assert isinstance(random, int) + def test_bounded_random() -> None: """Verifies that a single generated random number is within bounds.""" rng = RNGModel(shot_id=0) @@ -29,6 +31,7 @@ def test_bounded_random() -> None: random_number = rng.rng_random() assert 0 <= random_number < bound + def test_set_idx() -> None: """Verifies that the idx is set properly for our model.""" rng = RNGModel(shot_id=0) @@ -37,6 +40,7 @@ def test_set_idx() -> None: rng.set_index(idx) assert rng.count == idx + def test_multiple_bounded_rand() -> None: """For several randomly generated number, with a random bound, verifies that its appropriate.""" rng = RNGModel(shot_id=0) diff --git a/ruff.toml b/ruff.toml index cc535ce30..36fffc649 100644 --- a/ruff.toml +++ b/ruff.toml @@ -91,6 +91,7 @@ ignore = [ "ANN", "D", ] +"clibs/pecos-rng/pecos_pcg/__init__.py" = ["TID252"] [lint.pycodestyle] From 63658c20d8d641062fe18777afd2fc563167a944 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 01:59:55 -0400 Subject: [PATCH 19/32] modified workflow to pick appropriate repo --- .github/workflows/python-release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 0937e58f9..009dbf64b 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -205,7 +205,7 @@ jobs: - name: Build PECOS RNG run: | - cd lib/pecos_rng && mkdir build && cd build/ && cmake .. + cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. cmake -DPYTHON_EXECUTABLE=$(which python) --build . && cd .. && python -m pip install . env: NANOBIND_DIR: python -m nanobind --include_dir From 30e32a45aa25d35112f5aff2aa29995d6579259f Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:13:09 -0400 Subject: [PATCH 20/32] github action chanegs --- .github/workflows/python-release.yml | 4 ++-- .github/workflows/python-test.yml | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 009dbf64b..49dc55846 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -259,13 +259,13 @@ jobs: - name: Install build dependencies run: | python -m pip install --upgrade pip - apt-get install -y python3-dev + sudo apt-get install -y python3-dev pip install build nanobind - name: Build PECOS RNG run: | cd lib/pecos_rng && mkdir build && cd build/ && cmake .. - cmake --build . && cd .. && python -m pip install . + make && cd .. && uv pip install . env: NANOBIND_DIR: python -m nanobind --include_dir diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index d056d495a..002f96062 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -68,6 +68,15 @@ jobs: run: | uv lock --project . uv sync --project . + sudo apt-get install -y python3-dev + uv pip install build nanobind + + - name: Build PECOS RNG + run: | + cd lib/pecos_rng && mkdir build && cd build/ && cmake .. + make && cd .. && python -m pip install . + env: + NANOBIND_DIR: python -m nanobind --include_dir - name: Install pecos-rslib with maturin run: | From ef6d281042128d78df502322014fb5957799f852 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:16:28 -0400 Subject: [PATCH 21/32] lib change didnt go through? fixing --- .github/workflows/python-release.yml | 3 +-- .github/workflows/python-test.yml | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 49dc55846..e93a4e0ff 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -259,12 +259,11 @@ jobs: - name: Install build dependencies run: | python -m pip install --upgrade pip - sudo apt-get install -y python3-dev pip install build nanobind - name: Build PECOS RNG run: | - cd lib/pecos_rng && mkdir build && cd build/ && cmake .. + cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. make && cd .. && uv pip install . env: NANOBIND_DIR: python -m nanobind --include_dir diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 002f96062..a171a6606 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -68,12 +68,11 @@ jobs: run: | uv lock --project . uv sync --project . - sudo apt-get install -y python3-dev uv pip install build nanobind - name: Build PECOS RNG run: | - cd lib/pecos_rng && mkdir build && cd build/ && cmake .. + cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. make && cd .. && python -m pip install . env: NANOBIND_DIR: python -m nanobind --include_dir From 7d8968e95e8fad515f806f46aee7090ec7a9f979 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:20:38 -0400 Subject: [PATCH 22/32] moving stuff around for ci --- .github/workflows/python-release.yml | 5 ++--- .github/workflows/python-test.yml | 5 ++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index e93a4e0ff..0735eb68d 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -256,10 +256,9 @@ 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 nanobind + uv pip install build nanobind - name: Build PECOS RNG run: | diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index a171a6606..c059c5f58 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -68,8 +68,11 @@ jobs: run: | uv lock --project . uv sync --project . + + - name: Install pecos rng build dependencies + run: | uv pip install build nanobind - + - name: Build PECOS RNG run: | cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. From 44a9e86e6a41bad5d4a729755c4f149ce548d106 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:22:00 -0400 Subject: [PATCH 23/32] forgot to fix sdist action --- .github/workflows/python-release.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 0735eb68d..58a5d4152 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -198,15 +198,14 @@ 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 nanobind + uv pip install build nanobind - name: Build PECOS RNG run: | cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. - cmake -DPYTHON_EXECUTABLE=$(which python) --build . && cd .. && python -m pip install . + make && cd .. && uv pip install . env: NANOBIND_DIR: python -m nanobind --include_dir From 63ef72395ca3b3f1e6aca316b72eec9aa82741d2 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:28:47 -0400 Subject: [PATCH 24/32] removed uv usage in release --- .github/workflows/python-release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index 58a5d4152..ba294bd79 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -257,12 +257,12 @@ jobs: - name: Install pecos rng build dependencies run: | - uv pip install build nanobind + pip install build nanobind - name: Build PECOS RNG run: | cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. - make && cd .. && uv pip install . + make && cd .. && pip install . env: NANOBIND_DIR: python -m nanobind --include_dir From 937bfa0941c188eecc498ad0dffcd75a0ca8c446 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:34:55 -0400 Subject: [PATCH 25/32] removed uv for build_sdist_quantum_pecos --- .github/workflows/python-release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-release.yml b/.github/workflows/python-release.yml index ba294bd79..be09eca8b 100644 --- a/.github/workflows/python-release.yml +++ b/.github/workflows/python-release.yml @@ -200,12 +200,12 @@ jobs: - name: Install pecos RNG build dependencies run: | - uv pip install build nanobind + pip install build nanobind - name: Build PECOS RNG run: | cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. - make && cd .. && uv pip install . + make && cd .. && pip install . env: NANOBIND_DIR: python -m nanobind --include_dir From a7e8aace1091aea832021b6a1ca81cc2abd82dec Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:37:56 -0400 Subject: [PATCH 26/32] made changes to python-test to see if pecos rng is built proper --- .github/workflows/python-test.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index c059c5f58..61b0a709f 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -71,14 +71,13 @@ jobs: - name: Install pecos rng build dependencies run: | - uv pip install build nanobind + uv pip install build nanobind scikit-build-core cmake ninja - name: Build PECOS RNG run: | + export NANOBIND_DIR=$(python -m nanobind --include_dir) cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. make && cd .. && python -m pip install . - env: - NANOBIND_DIR: python -m nanobind --include_dir - name: Install pecos-rslib with maturin run: | From 8f9d2d2c2e0e78cf6c92a4a7112e64c51669b1ec Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:44:55 -0400 Subject: [PATCH 27/32] success with python-release action. Adding prefix path to cmake to see if can build with python-test --- .github/workflows/python-test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 61b0a709f..648d5c832 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -75,8 +75,9 @@ jobs: - name: Build PECOS RNG run: | + export NANOBIND_CMAKE_DIR=$(python -m nanobind --cmake_dir) export NANOBIND_DIR=$(python -m nanobind --include_dir) - cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. + cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. -DCMAKE_PREFIX_PATH=$NANOBIND_CMAKE_DIR make && cd .. && python -m pip install . - name: Install pecos-rslib with maturin From 8ed5a4280222d08271640a129e9e6d10fae152cd Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:50:49 -0400 Subject: [PATCH 28/32] uv not finding paths properly running new cmmd --- .github/workflows/python-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 648d5c832..e0e90ed2a 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -75,8 +75,8 @@ jobs: - name: Build PECOS RNG run: | - export NANOBIND_CMAKE_DIR=$(python -m nanobind --cmake_dir) - export NANOBIND_DIR=$(python -m nanobind --include_dir) + 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 make && cd .. && python -m pip install . From 3910fab8b021e896d3f0018246337ef57b8d91e8 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 02:53:45 -0400 Subject: [PATCH 29/32] changes --- .github/workflows/python-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index e0e90ed2a..b42e665b9 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -77,8 +77,8 @@ jobs: 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 - make && cd .. && python -m pip install . + cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. -DCMAKE_PREFIX_PATH=$NANOBIND_CMAKE_DIR && make + cd .. && python -m pip install . - name: Install pecos-rslib with maturin run: | From 3910464592f6998545cacdc4d986000fa3133938 Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 03:20:00 -0400 Subject: [PATCH 30/32] moved things around for test --- .github/workflows/python-test.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index b42e665b9..e752b726a 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -77,8 +77,9 @@ jobs: 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 && make - cd .. && python -m pip install . + 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: | From 65c41edfe4fdbc14743e8b08bd012ae04adb748e Mon Sep 17 00:00:00 2001 From: Jonhas Colina Date: Tue, 8 Jul 2025 03:24:13 -0400 Subject: [PATCH 31/32] pre-commit changes --- .github/workflows/python-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index e752b726a..b374ea298 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -72,13 +72,13 @@ jobs: - 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 .. + cmake --build . && cd .. uv pip install . - name: Install pecos-rslib with maturin From 6774eb296b050a80880a56d484c5f45eabaea159 Mon Sep 17 00:00:00 2001 From: Ciaran Ryan-Anderson Date: Tue, 8 Jul 2025 11:52:11 -0600 Subject: [PATCH 32/32] Workspace Makefile fix? --- .gitignore | 4 ++++ Makefile | 25 +++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index a8439883f..147a22cfd 100644 --- a/.gitignore +++ b/.gitignore @@ -176,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 diff --git a/Makefile b/Makefile index b4ba83d48..d94150ed7 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,7 @@ installreqs: ## Install Python project requirements to root .venv buildrng: @echo "Building and installing RNG library..." uv pip install nanobind - nanobind_DIR="python3 -m nanobind --include_dir" - cd clibs/pecos-rng && mkdir build && cd build/ && cmake .. && cmake --build . && cd .. && uv pip install . + cd clibs/pecos-rng && CC=gcc CXX=g++ uv pip install --python $(shell uv run which python) -e . # Building development environments # --------------------------------- @@ -156,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 {} + @@ -177,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" @@ -197,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 @@ -211,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