Skip to content

Commit c6cd0d5

Browse files
authored
Add structure for multiple Rust crates (Qiskit#9742)
* Add structure for multiplie Rust extension crates This is a precursor to adding an entirely separate and self-contained crate to handle parsing of OpenQASM 2 (not 3 - this is kind of like a trial run for that). It could conceivably still live within `qiskit._accelerate`, but having separate crates helps us enforce more sane API barriers, and has improvements in the incremental build time when working on only one of the Rust extensions. The intent after this commit is still to have `qiskit._accelerate` as an easy catch-all for accelerators for Python. Developers should not need to be concerned with defining a new crate for every new feature, and for the most part `_accelerate` is still logically interoperative within itself (for example, `NLayout` is used all over). This is just laying out the groundwork so more complex crate additions _can_ also be made. Some of the niceties to do with Cargo workspaces only became stabilised in Rust 1.64. In particular, inheritance from the workspace root for things like the package version, Rust version, and dependencies only came in then. For now, the `workspace.packages` key in the root `Cargo.toml` is ignored with a small warning during the build, but I've put it in place mostly to keep track of what we can change once the MSRV becomes 1.64 or greater (likely not til at least Terra 0.26). * Add README.md to crates/accelerate * Correct licence metadata
1 parent 2553376 commit c6cd0d5

28 files changed

+119
-83
lines changed

.github/workflows/coverage.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ jobs:
4545
stestr run
4646
# We set the --source-dir to '.' because we want all paths to appear relative to the repo
4747
# root (we need to combine them with the Python ones), but we only care about `grcov`
48-
# keeping the `src/*` files; we don't care about coverage in dependencies.
49-
grcov . --binary-path target/debug/ --source-dir . --output-type lcov --output-path rust.info --llvm --branch --parallel --keep-only 'src/*'
48+
# keeping the `crates/*` files; we don't care about coverage in dependencies.
49+
grcov . --binary-path target/debug/ --source-dir . --output-type lcov --output-path rust.info --llvm --branch --parallel --keep-only 'crates/*'
5050
env:
5151
QISKIT_TEST_CAPTURE_STREAMS: 1
5252
QISKIT_PARALLEL: FALSE

Cargo.lock

+42-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+9-37
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,14 @@
1-
[package]
2-
name = "qiskit-terra"
1+
[workspace]
2+
members = ["crates/*"]
3+
4+
# This has no meaning until we're on Rust 1.64, but once we get there, the subpackages will be able
5+
# to inherit from this rather than needing to duplicate its content themselves. Until we get there,
6+
# keep this in sync with each subpackage `Cargo.toml`'s `package` key.
7+
[workspace.package]
38
version = "0.24.0"
49
edition = "2021"
5-
# Keep in sync with README.md and rust-toolchain.toml.
6-
rust-version = "1.61"
7-
8-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9-
10-
[lib]
11-
name = "qiskit_accelerate"
12-
crate-type = ["cdylib"]
13-
14-
[dependencies]
15-
rayon = "1.7"
16-
numpy = "0.18.0"
17-
rand = "0.8"
18-
rand_pcg = "0.3"
19-
rand_distr = "0.4.3"
20-
ahash = "0.8.3"
21-
num-complex = "0.4"
22-
num-bigint = "0.4"
23-
rustworkx-core = "0.12"
24-
25-
[dependencies.pyo3]
26-
version = "0.18.1"
27-
features = ["extension-module", "hashbrown", "num-complex", "num-bigint", "indexmap"]
28-
29-
[dependencies.ndarray]
30-
version = "^0.15.6"
31-
features = ["rayon"]
32-
33-
[dependencies.hashbrown]
34-
version = "0.13.2"
35-
features = ["rayon"]
36-
37-
[dependencies.indexmap]
38-
version = "1.9"
39-
features = ["rayon"]
10+
rust-version = "1.61" # Keep in sync with README.md and rust-toolchain.toml.
11+
license = "Apache-2.0"
4012

4113
[profile.release]
4214
lto = 'fat'

MANIFEST.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ include test/python/notebooks/*.ipynb
1818

1919
include Cargo.toml
2020
include Cargo.lock
21-
recursive-include src *
21+
recursive-include crates *

crates/accelerate/Cargo.toml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[package]
2+
name = "qiskit_accelerate"
3+
# The following options can be inherited with (e.g.) `version.workspace = true` once we hit Rust
4+
# 1.64. Until then, keep in sync with the root `Cargo.toml`.
5+
version = "0.24.0"
6+
edition = "2021"
7+
rust-version = "1.61"
8+
license = "Apache-2.0"
9+
10+
[lib]
11+
name = "qiskit_accelerate"
12+
crate-type = ["cdylib"]
13+
14+
[dependencies]
15+
rayon = "1.7"
16+
numpy = "0.18.0"
17+
rand = "0.8"
18+
rand_pcg = "0.3"
19+
rand_distr = "0.4.3"
20+
ahash = "0.8.3"
21+
num-complex = "0.4"
22+
num-bigint = "0.4"
23+
rustworkx-core = "0.12"
24+
25+
# The base version of PyO3 and setting a minimum feature set (e.g. probably just 'extension-module')
26+
# can be done in the workspace and inherited once we hit Rust 1.64.
27+
[dependencies.pyo3]
28+
version = "0.18.1"
29+
features = ["extension-module", "hashbrown", "indexmap", "num-complex", "num-bigint"]
30+
31+
[dependencies.ndarray]
32+
version = "^0.15.6"
33+
features = ["rayon"]
34+
35+
[dependencies.hashbrown]
36+
version = "0.13.2"
37+
features = ["rayon"]
38+
39+
[dependencies.indexmap]
40+
version = "1.9"
41+
features = ["rayon"]

crates/accelerate/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# `qiskit._accelerate`
2+
3+
This crate provides a bits-and-pieces Python extension module for small, self-contained functions
4+
that are used by the main Python-space components to accelerate certain tasks. If you're trying to
5+
speed up one particular Python function by replacing its innards with a Rust one, this is the best
6+
place to put the code. This is _usually_ the right place to put Rust/Python interfacing code.
7+
8+
The crate is made accessible as a private submodule, `qiskit._accelerate`. There are submodules
9+
within that (largely matching the structure of the Rust code) mostly for grouping similar functions.
10+
11+
Some examples of when it might be more appropriate to start a new crate instead of using the
12+
ready-made solution of `qiskit._accelerate`:
13+
14+
* The feature you are developing will have a large amount of domain-specific Rust code and is a
15+
large self-contained module. If it reasonably works in a single Rust file, you probably just want
16+
to put it here.
17+
18+
* The Rust code is for re-use within other Qiskit crates and maintainability of the code will be
19+
helped by using the crate system to provide API boundaries between the different sections.
20+
21+
* You want to start writing your own procedural macros.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

setup.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@
9999
"Documentation": "https://qiskit.org/documentation/",
100100
"Source Code": "https://github.com/Qiskit/qiskit-terra",
101101
},
102-
rust_extensions=[RustExtension("qiskit._accelerate", "Cargo.toml", binding=Binding.PyO3)],
102+
rust_extensions=[
103+
RustExtension("qiskit._accelerate", "crates/accelerate/Cargo.toml", binding=Binding.PyO3)
104+
],
103105
zip_safe=False,
104106
entry_points={
105107
"qiskit.unitary_synthesis": [

0 commit comments

Comments
 (0)