Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 0b79432

Browse files
author
Cassandra Granade
authoredApr 8, 2022
Merge pull request #920 from microsoft/cgranade/expm-rs
Initial support for matrix inverse in qdk_sim_experimental crate
2 parents 16962a9 + b60d691 commit 0b79432

File tree

20 files changed

+1893
-197
lines changed

20 files changed

+1893
-197
lines changed
 

‎src/Simulation/qdk_sim_rs/Cargo.toml

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
# Licensed under the MIT License.
33

44
[package]
5-
name = "qdk_sim_experimental"
5+
name = "qdk_sim_rs"
66
version = "0.0.1-alpha"
77
authors = ["Microsoft"]
88
edition = "2018"
99
license = "MIT"
10-
description = "Experimental simulators for use with the Quantum Development Kit."
10+
description = "Rust-based simulators for use with the Quantum Development Kit."
1111
homepage = "https://github.com/microsoft/qsharp-runtime"
1212
repository = "https://github.com/microsoft/qsharp-runtime"
1313
readme = "README.md"
1414

15+
# Verified with cargo-msrv.
16+
rust-version = "1.51.0"
17+
1518
exclude = [
1619
# Exclude files specific to QDK build pipelines.
1720
"*.template", "*.csx", "*.ps1", "NuGet.Config", "drop",
@@ -21,72 +24,85 @@ exclude = [
2124
"*.egg-info", "qdk_sim_experimental", "setup.py", "*.whl", "pyproject.toml"
2225
]
2326

27+
# Enable LaTeX on docs.rs.
28+
# See https://stackoverflow.com/a/54573800/267841 and
29+
# https://github.com/rust-num/num/pull/226/files for why this works.
30+
[package.metadata.docs.rs]
31+
rustdoc-args = [ "--html-in-header", "docs-includes/header.html", "--html-after-content", "docs-includes/after.html" ]
32+
features = ["document-features"]
33+
2434
[lib]
35+
crate-type = ["rlib", "staticlib", "cdylib"]
2536
name = "qdk_sim"
2637
path = "src/lib.rs"
27-
crate-type = ["rlib", "staticlib", "cdylib"]
2838

2939
# Optional build-time features: we use this to create Python and WASM bindings.
3040
[features]
3141
default = []
32-
wasm = ["web-sys"]
42+
3343
# When Python bindings are enabled, we also need the pyo3 dependency.
44+
## Enables Python bindings for this crate.
3445
python = ["pyo3", "numpy"]
3546

36-
# Enable LaTeX on docs.rs.
37-
# See https://stackoverflow.com/a/54573800/267841 and
38-
# https://github.com/rust-num/num/pull/226/files for why this works.
39-
[package.metadata.docs.rs]
40-
rustdoc-args = [ "--html-in-header", "docs-includes/header.html", "--html-after-content", "docs-includes/after.html" ]
47+
## Ensures that the crate is compatible with usage from WebAssembly.
48+
wasm = ["web-sys"]
4149

4250

4351
[profile.release]
44-
opt-level = 3
4552
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
53+
opt-level = 3
4654
panic = 'unwind'
4755

4856
[dependencies]
49-
ndarray = { version = "0.15.2", features = ["serde"] }
50-
num-complex = { version = "0.4", features = ["serde"] }
51-
num-traits = "0.2"
52-
derive_more = "0.99.10"
53-
itertools = "0.9.0"
54-
rand = { version = "0.7.3", features = ["alloc"] }
55-
serde = { version = "1.0", features = ["derive"] }
56-
serde_json = "1.0"
57-
lazy_static = "1.4.0"
57+
anyhow = "1.0.56"
58+
# We use built to expose compile-time metadata about how this crate
59+
# was built to C and Rust callers.
60+
built = "0.5.0"
61+
cauchy = "0.4.0"
5862
cfg-if = "1.0.0"
59-
num_enum = "0.5.1"
63+
derive_more = "0.99.10"
64+
# We use document-features to automatically generate feature documentation from
65+
# Cargo.toml, following the example at
66+
# https://docs.rs/document-features/latest/document_features#examples.
67+
document-features = { version = "0.2", optional = true }
6068
# See https://github.com/rust-random/rand/issues/990
6169
# and https://docs.rs/getrandom/0.1.15/getrandom/index.html#support-for-webassembly-and-asmjs
6270
# for why this is needed.
6371
# NB: We depend on 0.1.15, since that's what gets brought in transitively
6472
# by rand and rand_core.
6573
getrandom = { version = "0.1.15", features = ["wasm-bindgen"] }
66-
67-
# We only need web-sys when compiling with the wasm feature.
68-
web-sys = { version = "0.3.4", features = ['console'], optional = true }
69-
74+
itertools = "0.9.0"
75+
lazy_static = "1.4.0"
76+
miette = "4.3.0"
77+
ndarray = { version = "0.15.2", features = ["serde"] }
78+
num-complex = { version = "0.4", features = ["serde"] }
79+
num-traits = "0.2"
80+
num_enum = "0.5.1"
81+
numpy = { version = "0.13.1", optional = true }
7082
# We only need PyO3 when generating Python bindings.
7183
pyo3 = { version = "0.13.2", features = ["extension-module"], optional = true }
72-
numpy = {version = "0.13.1", optional = true}
84+
rand = { version = "0.7.3", features = ["alloc"] }
85+
serde = { version = "1.0", features = ["derive"] }
86+
serde_json = "1.0"
87+
thiserror = "1.0.30"
7388

74-
# We use built to expose compile-time metadata about how this crate
75-
# was built to C and Rust callers.
76-
built = "0.5.0"
89+
# We only need web-sys when compiling with the wasm feature.
90+
web-sys = { version = "0.3.4", features = ['console'], optional = true }
7791

7892
[build-dependencies]
7993
built = "0.5.0"
8094

8195

8296
[dev-dependencies]
97+
approx = { version = "0.5.1", features = ["num-complex"] }
8398
assert-json-diff = "2.0.1"
8499
criterion = { version = "0.3", features = ['html_reports', 'csv_output'] }
100+
ndarray = { version = "0.15.4", features = ["approx"] }
85101

86102
[[bench]]
87-
name = "c_api_benchmark"
88103
harness = false
104+
name = "c_api_benchmark"
89105

90106
[[bench]]
91-
name = "microbenchmark"
92107
harness = false
108+
name = "microbenchmark"

‎src/Simulation/qdk_sim_rs/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
To generate and view the documentation for this crate locally, please
55
run:
66
7-
$ cargo +nightly doc --features python --open
7+
$ cargo doc --features python,document-features --open
88
-->
99

1010
# Quantum Development Kit Preview Simulators
@@ -28,11 +28,6 @@ The [`c_api`] module allows for using the simulation functionality in this crate
2828

2929
Similarly, the [`python`] module allows exposing data structures in this crate to Python programs.
3030

31-
## Cargo Features
32-
33-
- **`python`**: Enables Python bindings for this crate.
34-
- **`wasm`**: Ensures that the crate is compatible with usage from WebAssembly.
35-
3631
## Representing quantum systems
3732

3833
This crate provides several different data structures for representing quantum systems in a variety of different conventions:
@@ -148,3 +143,6 @@ TODO
148143
- Stabilizer simulation not yet exposed via C API.
149144
- Test and microbenchmark coverage still incomplete.
150145
- Too many APIs `panic!` or `unwrap`, and need replaced with `Result` returns instead.
146+
147+
# Crate features
148+
<!-- Note that this section is filled in automatically by document-features. -->

‎src/Simulation/qdk_sim_rs/benches/microbenchmark.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
//! In particular, optimizing these benchmarks may not translate into improved
99
//! performance in user code.
1010
11+
use cauchy::c64;
1112
use criterion::{criterion_group, criterion_main, Criterion};
13+
use ndarray::array;
1214
use qdk_sim::{
1315
common_matrices,
1416
common_matrices::nq_eye,
15-
linalg::{extend_one_to_n, extend_two_to_n, Tensor},
17+
linalg::{extend_one_to_n, extend_two_to_n, Inv, Tensor},
1618
};
1719

1820
fn linalg(c: &mut Criterion) {
@@ -79,6 +81,68 @@ fn linalg(c: &mut Criterion) {
7981
let _result = cnot.tensor(&x);
8082
})
8183
});
84+
group.bench_function("inv 4x4 f64", |b| {
85+
let x = array![
86+
[
87+
0.23935896217435304,
88+
0.34333031120985236,
89+
0.8201953415286973,
90+
0.8074588350909441
91+
],
92+
[
93+
0.11957583380425751,
94+
0.16906445210054732,
95+
0.21728173861409317,
96+
0.7120594445167554
97+
],
98+
[
99+
0.04023516190513021,
100+
0.9635112441739464,
101+
0.9209190516642924,
102+
0.114251355434274
103+
],
104+
[
105+
0.8749507948480983,
106+
0.2661348079904513,
107+
0.17485566324545554,
108+
0.2934138616881069
109+
],
110+
];
111+
b.iter(|| {
112+
let _inv = x.inv();
113+
});
114+
});
115+
group.bench_function("inv 4x4 c64", |b| {
116+
let x = array![
117+
[
118+
c64::new(0.30874277550419704, 0.8167808814398533),
119+
c64::new(0.9303782008146939, 0.8925538040143673),
120+
c64::new(0.11573522743286513, 0.6357551264716991),
121+
c64::new(0.7869240102858357, 0.28376515716360073)
122+
],
123+
[
124+
c64::new(0.9638410081049803, 0.4460520369459663),
125+
c64::new(0.043516097874141346, 0.1652124014187376),
126+
c64::new(0.05938096491956191, 0.7696366269843138),
127+
c64::new(0.9636976605227736, 0.8125701401805293)
128+
],
129+
[
130+
c64::new(0.9548859426476123, 0.7825350828251003),
131+
c64::new(0.4223649577868721, 0.4522018603906839),
132+
c64::new(0.36001119456757835, 0.22138920205104395),
133+
c64::new(0.0044511389785256705, 0.14148562531641973)
134+
],
135+
[
136+
c64::new(0.6066852151129799, 0.5140547256960247),
137+
c64::new(0.23439110687939924, 0.3064074735518828),
138+
c64::new(0.9963759728056912, 0.401859040365666),
139+
c64::new(0.7176238235495955, 0.3948597214947167)
140+
],
141+
];
142+
b.iter(|| {
143+
let _inv = x.inv();
144+
});
145+
});
82146
group.finish();
83147
}
84148

0 commit comments

Comments
 (0)