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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/branch-protection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ jobs:
install-optionals: true
install-from-sdist: false
qiskit-pycache: false
mimalloc: false
- id: "Python newest"
python: ${{ needs.config.outputs.python-new }}
install-optionals: false
Expand All @@ -127,6 +128,7 @@ jobs:
- id: "Python oldest"
python: ${{ needs.config.outputs.python-old }}
install-optionals: true
mimalloc: false
- id: "Python newest"
python: ${{ needs.config.outputs.python-new }}
install-optionals: false
Expand All @@ -146,6 +148,7 @@ jobs:
- id: "Python oldest"
python: ${{ needs.config.outputs.python-old }}
install-optionals: true
mimalloc: false
- id: "Python newest"
python: ${{ needs.config.outputs.python-new }}
install-optionals: false
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ on:
structures when building Qiskit.
type: boolean
default: true
mimalloc:
description: >
Whether to build with mimalloc or not
type: boolean
default: true

jobs:
unit-tests:
Expand Down Expand Up @@ -61,6 +66,9 @@ jobs:
- name: Set gate-caching settings for build
if: ${{ !inputs.qiskit-pycache }}
run: 'echo "QISKIT_NO_CACHE_GATES=1" >> $GITHUB_ENV'
- name: Set mimalloc for build
if: ${{ inputs.mimalloc }}
run: 'echo "QISKIT_BUILD_WITH_MIMALLOC=1" >> $GITHUB_ENV'
- name: Install Qiskit from sdist
if: ${{ inputs.install-from-sdist }}
run: |
Expand Down
11 changes: 9 additions & 2 deletions .github/workflows/test-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ on:
Python version to currently test
type: string
required: true

install-optionals:
description: >
Decides whether we install optyional dependencies
type: boolean
default: false

runner:
description: >
Describes the system this workflow should run on.
type: string
required: true
mimalloc:
description: >
Whether to build with mimalloc or not
type: boolean
default: true

jobs:
tests-mac:
name: ${{ inputs.runner }} - Python ${{ inputs.python-version }}
Expand All @@ -42,6 +46,9 @@ jobs:
stestr | "${{ runner.os }}"
stestr
path: .stestr
- name: Set mimalloc for build
if: ${{ inputs.mimalloc }}
run: 'echo "QISKIT_BUILD_WITH_MIMALLOC=1" >> $GITHUB_ENV'
- name: "Install dependencies"
run: |
set -e
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ on:
Python version to currently test
type: string
required: true

runner:
description: >
Describes the system this workflow should run on.
type: string
required: true

install-optionals:
description: >
Decides whether we install optyional dependencies
type: boolean
default: false
mimalloc:
description: >
Whether to build with mimalloc or not
type: boolean
default: true
jobs:
test-windows:
name: ${{ inputs.runner }} - Python ${{ inputs.python-version }}
Expand All @@ -42,6 +45,9 @@ jobs:
stestr | "${{ runner.os }}"
stestr
path: .stestr
- name: Set mimalloc for build
if: ${{ inputs.mimalloc }}
run: 'echo "QISKIT_BUILD_WITH_MIMALLOC=1" >> $GITHUB_ENV'
- name: Install dependencies
run: |
python -m venv test-job
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/wheels-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ jobs:
cat >>"$GITHUB_ENV" <<EOF
CIBW_BEFORE_ALL_LINUX=dnf install -y wget && {package}/tools/install_rust.sh
CIBW_BEFORE_BUILD=bash ./tools/build_pgo.sh $PGO_WORK_DIR $PGO_OUT_PATH
CIBW_ENVIRONMENT=RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function'
CIBW_ENVIRONMENT_MACOS=MACOSX_DEPLOYMENT_TARGET='10.12' RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function'
CIBW_ENVIRONMENT_LINUX=RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function' PATH="\$PATH:\$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true"
CIBW_ENVIRONMENT=RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function' QISKIT_BUILD_WITH_MIMALLOC=1
CIBW_ENVIRONMENT_MACOS=MACOSX_DEPLOYMENT_TARGET='10.12' RUSTUP_TOOLCHAIN=stable RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function' QISKIT_BUILD_WITH_MIMALLOC=1
CIBW_ENVIRONMENT_LINUX=RUSTUP_TOOLCHAIN=stable QISKIT_BUILD_WITH_MIMALLOC=1 RUSTFLAGS='-Cprofile-use=$PGO_OUT_PATH -Cllvm-args=-pgo-warn-missing-function' PATH="\$PATH:\$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true"
EOF
env:
PGO_WORK_DIR: ${{ github.workspace }}/pgo-data
Expand Down
31 changes: 21 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,27 @@ to `build_rust` overrides this default.

### Compile time options

When building qiskit from source there are options available to control how
Qiskit is built. Right now the only option is if you set the environment
variable `QISKIT_NO_CACHE_GATES=1` this will disable runtime caching of
Python gate objects when accessing them from a `QuantumCircuit` or `DAGCircuit`.
This makes a tradeoff between runtime performance for Python access and memory
overhead. Caching gates will result in better runtime for users of Python at
the cost of increased memory consumption. If you're working with any custom
transpiler passes written in Python or are otherwise using a workflow that
repeatedly accesses the `operation` attribute of a `CircuitInstruction` or `op`
attribute of `DAGOpNode` enabling caching is recommended.
When building Qiskit from source there are options available to control how
Qiskit is built. These options are set with the following environment variables:

* `QISKIT_BUILD_WITH_MIMALLOC=1`: this will enable using
[mimalloc](https://github.com/microsoft/mimalloc) as the global allocator for
Qiskit instead of the default system allocator. This improves the runtime and
memory performance of Qiskit but will require having a C compiler installed
when building Qiskit.
* `QISKIT_NO_CACHE_GATES=1`: this will disable runtime caching of
Python gate objects when accessing them from a `QuantumCircuit` or `DAGCircuit`.
This makes a tradeoff between runtime performance for Python access and memory
overhead. Caching gates will result in better runtime for users of Python at
the cost of increased memory consumption. If you're working with any custom
transpiler passes written in Python or are otherwise using a workflow that
repeatedly accesses the `operation` attribute of a `CircuitInstruction` or `op`
attribute of `DAGOpNode` enabling caching is recommended.

These environment variables are only valid when building Qiskit the Python package
with a PEP 517 compatible build tool or calling `setup.py` directly.
Or as a standalone C library with `make c` (`QISKIT_NO_CACHE_GATES` has no effect
when building a standalone C library).

## Issues and pull requests

Expand Down
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ uuid = { version = "1.23", features = ["v4", "fast-rng"], default-features = fal
anyhow = "1.0"
binrw = "0.15"
cbindgen = "0.29.2"
mimalloc = "0.1.48"

# Most of the crates don't need the feature `extension-module`, since only `qiskit-pyext` builds an
# actual C extension (the feature disables linking in `libpython`, which is forbidden in Python
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ ifneq ($(OS), Windows_NT)
OS := $(shell uname -s)
endif

ifeq ($(QISKIT_BUILD_WITH_MIMALLOC), 1)
MIMALLOC := --features=mimalloc
endif

.PHONY: default ruff env lint lint-incr style black test test_randomized pytest pytest_randomized test_ci coverage coverage_erase clean

default: style lint-incr test ;
Expand Down Expand Up @@ -135,7 +139,7 @@ fix_cformat:
# instead.
.PHONY: build-clib build-clib-release build-clib-dev
build-clib:
cargo rustc -p qiskit-cext --crate-type cdylib ${C_LIB_CARGO_FLAGS} -- ${C_LIB_RUSTC_FLAGS}
cargo rustc -p qiskit-cext ${MIMALLOC} --crate-type cdylib ${C_LIB_CARGO_FLAGS} -- ${C_LIB_RUSTC_FLAGS}
build-clib-release: C_LIB_CARGO_FLAGS=--release
build-clib-release: build-clib
build-clib-dev: C_LIB_CARGO_FLAGS=--profile dev
Expand Down
2 changes: 1 addition & 1 deletion asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"build_command": [
"python -m pip install -U build",
"python -m build --outdir {build_cache_dir} --wheel {build_dir}"
"QISKIT_BUILD_WITH_MIMALLOC=1 python -m build --outdir {build_cache_dir} --wheel {build_dir}"
],
"branches": ["main"],
"dvcs": "git",
Expand Down
1 change: 1 addition & 0 deletions crates/cext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ hashbrown.workspace = true
rand.workspace = true
rand_pcg.workspace = true
anyhow.workspace = true
mimalloc = { workspace = true, optional = true}

[features]
python_binding = ["dep:pyo3"]
Expand Down
7 changes: 7 additions & 0 deletions crates/cext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ pub mod transpiler;

pub use exit_codes::ExitCode;

#[cfg(feature = "mimalloc")]
use mimalloc::MiMalloc;

#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

/// Get the C API version of the loaded library.
///
/// If you are dynamically linking against Qiskit, in either a stand-alone or Python-extension
Expand Down
1 change: 1 addition & 0 deletions crates/pyext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ qiskit-cext-vtable = { workspace = true, features = ["python_binding", "addr"] }
qiskit-transpiler.workspace = true
qiskit-quantum-info.workspace = true
qiskit-synthesis.workspace = true
mimalloc = { workspace = true, optional = true }
7 changes: 7 additions & 0 deletions crates/pyext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ pub use qiskit_cext::*;

mod capi;

#[cfg(feature = "mimalloc")]
use mimalloc::MiMalloc;

#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

#[inline(always)]
#[doc(hidden)]
fn add_submodule<F>(m: &Bound<PyModule>, constructor: F, name: &str) -> PyResult<()>
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,16 @@ before-test = "pip install --only-binary=numpy,scipy numpy scipy -c constraints.
# specific job override is needed. For example tier 1 platforms locally override
# the before-build and environment configuration to enable PGO,
# see: .github/workflows/wheels.yml for the jobs where this is done
environment = 'RUSTUP_TOOLCHAIN="stable"'
environment = 'RUSTUP_TOOLCHAIN="stable" QISKIT_BUILD_WITH_MIMALLOC=1'

[tool.cibuildwheel.linux]
before-all = "yum install -y wget && {package}/tools/install_rust.sh"
environment = 'PATH="$PATH:$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true" RUSTUP_TOOLCHAIN="stable"'
environment = 'PATH="$PATH:$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true" RUSTUP_TOOLCHAIN="stable" QISKIT_BUILD_WITH_MIMALLOC=1'
repair-wheel-command = "auditwheel repair -w {dest_dir} {wheel} && pipx run abi3audit --strict --report {wheel}"

[tool.cibuildwheel.macos]
repair-wheel-command = "delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v {wheel} && pipx run abi3audit --strict --report {wheel}"
environment = "MACOSX_DEPLOYMENT_TARGET='10.12' RUSTUP_TOOLCHAIN=stable"
environment = "MACOSX_DEPLOYMENT_TARGET='10.12' RUSTUP_TOOLCHAIN=stable QISKIT_BUILD_WITH_MIMALLOC=1"

[tool.cibuildwheel.windows]
test-command = "cp -r {project}/test . && stestr --test-path test/python run --abbreviate"
Expand Down
11 changes: 11 additions & 0 deletions releasenotes/notes/mimalloc-403d3300aa698fae.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
features_misc:
- The precompiled wheels on PyPI are now built using `mimalloc <https://github.com/microsoft/mimalloc>`__
as the global allocator. This improves the runtime performance of Qiskit and also should
reduce the resident set size when running Qiskit.
build:
- Qiskit can now be optionally built using `mimalloc <https://github.com/microsoft/mimalloc>`__
as the global allocator to improve the runtime performance of Qiskit.
To opt-in to using mimalloc, set the environment variable ``QISKIT_BUILD_WITH_MIMALLOC=1``.
This option can be used when either building the Python package or when using the Makefile
(`make c`) to build a standalone shared library.
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def get_package_dir(package: str) -> Path:
else:
features = ["cache_pygates"]

if os.getenv("QISKIT_BUILD_WITH_MIMALLOC") == "1":
features.append("mimalloc")


setup(
rust_extensions=[
Expand Down
Loading