Initial import

This commit is contained in:
Vincent Bourdon
2026-06-09 16:14:55 +02:00
commit 9af114e391
87 changed files with 20848 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, cc_apply, Gate},
math::PI,
utils::{pretty_print_int, to_table},
};
fn x(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for i in 0..n {
apply(Gate::H, &mut state, i);
apply(Gate::RZ(PI), &mut state, i);
}
if show_results {
to_table(&state);
}
cc_apply(Gate::X, &mut state, 0, 2, 1);
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
x(config.qubits.into(), config.print);
}
+40
View File
@@ -0,0 +1,40 @@
use clap::Parser;
use spinoza::{
circuit::{QuantumCircuit, QuantumRegister},
config::{Config, QSArgs},
core::CONFIG,
math::{pow2f, PI},
utils::{pretty_print_int, to_table},
};
fn benchmark_circuit_value_encoding(n: usize, show_results: bool) {
let v = 2.4;
let now = std::time::Instant::now();
let mut q = QuantumRegister::new(n);
let mut qc = QuantumCircuit::new(&mut [&mut q]);
for i in 0..n {
qc.h(i)
}
for i in 0..n {
qc.p(2.0 * PI / pow2f(i + 1) * v, i)
}
let targets: Vec<usize> = (0..n).rev().collect();
qc.iqft(&targets);
qc.execute();
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(qc.get_statevector()));
}
}
pub fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
benchmark_circuit_value_encoding(config.qubits.into(), config.print);
}
+30
View File
@@ -0,0 +1,30 @@
use clap::Parser;
use spinoza::core::qubit_expectation_value;
use spinoza::{
config::{Config, QSArgs},
core::{xyz_expectation_value, State, CONFIG},
gates::{apply, Gate},
};
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
let n = config.qubits.into();
let mut state = State::new(n);
let target = 0;
apply(Gate::RX(0.54), &mut state, target);
apply(Gate::RY(0.12), &mut state, target);
let targets = (0..n).collect::<Vec<usize>>();
let exp_vals = xyz_expectation_value('z', &state, &targets);
println!("expectation values: {:?}", exp_vals);
let exp_vals: Vec<_> = (0..n).map(|t| qubit_expectation_value(&state, t)).collect();
println!(
"expectation values using `qubit_expectation_value`: {:?}",
exp_vals
);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn h(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for t in 0..n {
apply(Gate::H, &mut state, t);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
h(config.qubits.into(), config.print);
}
+25
View File
@@ -0,0 +1,25 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::CONFIG,
measurement::measure_qubit,
utils::{gen_random_state, pretty_print_int},
};
fn measure_qubits(n: usize) {
let mut state = gen_random_state(n);
let now = std::time::Instant::now();
for t in 0..n {
measure_qubit(&mut state, t, true, None);
}
let elapsed = now.elapsed().as_micros();
println!("measured all qubits in {} us", pretty_print_int(elapsed));
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
measure_qubits(config.qubits.into());
}
+24
View File
@@ -0,0 +1,24 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{mc_apply, Gate},
utils::to_table,
};
fn mcx(_n: usize, show_results: bool) {
let mut state = State::new(4);
mc_apply(Gate::X, &mut state, &[1, 2], None, 0);
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
mcx(config.qubits.into(), config.print);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn p(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for i in 0..n {
apply(Gate::P(1.0), &mut state, i);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
p(config.qubits.into(), config.print);
}
+92
View File
@@ -0,0 +1,92 @@
use spinoza::utils::padded_bin;
fn print_pairs() {
let timer = std::time::Instant::now();
let n = 6;
let p2n = 1 << n;
let t = 0;
let p2t = 1 << t;
let c = 3;
let p2c = 1 << (c + 1);
let p2m = 1 << (c - t - 1);
for i in 0..p2m {
for j in (2 * i + 2 * p2m + 1) * p2t..(2 * i + 2 * p2m + 2) * p2t {
// println!("{} = {}", j, padded_bin(j, n));
for k in (j..j + p2n).step_by(p2c) {
println!("{} = {}", k, padded_bin(k, n));
}
}
}
println!("time elapsed: {} us ... ", timer.elapsed().as_micros());
}
fn print_target_pairs() {
let n = 3;
let target = 1;
let dist = 1 << target;
// 2 for loops
println!("for loops");
for i in 0..1 << (n - 1 - target) {
for j in 2 * i * dist..(2 * i + 1) * dist {
println!(
"{} = {} -> {} = {}",
j,
padded_bin(j, n),
dist + j,
padded_bin(dist + j, n)
)
}
}
// divmod
println!("\ndivmod");
for i in 0..1 << (n - 1) {
// let (p, s) = (i / dist, i % dist);
// i = dist*p + s
let j = i + ((i >> target) << target); //i + (p << target); // i + dist*p; // 2 * dist * p + s;
println!(
"{} = {} -> {} = {}",
j,
padded_bin(j, n),
dist + j, // i + ((1 + (i >> target)) << target),
padded_bin(dist + j, n)
)
}
// bit manipulation
println!("\nbit manipulation");
let neg_dist = !0 << target;
for i in 0..1 << (n - 1) {
let j = i + (i & neg_dist);
println!(
"{} = {} -> {} = {}",
j,
padded_bin(j, n),
dist + j,
padded_bin(dist + j, n)
)
}
for target in 0..n {
for i in 0..1 << (n - 1) {
assert_eq!(
(i >> target) << target,
i & !0 << target,
"different for {}",
i
);
}
}
}
fn main() {
print_target_pairs();
print_pairs();
}
+34
View File
@@ -0,0 +1,34 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn pauli_functional(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
apply(Gate::X, &mut state, 0);
for _ in 1..(1 << 30) {
apply(Gate::H, &mut state, 0);
apply(Gate::X, &mut state, 0);
apply(Gate::Z, &mut state, 0);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
pub fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
pauli_functional(config.qubits.into(), config.print);
}
+55
View File
@@ -0,0 +1,55 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, c_apply, Gate},
utils::{pretty_print_int, to_table},
};
pub fn qcbm_functional(n: usize, show_results: bool) {
let pairs: Vec<_> = (0..n).into_iter().map(|i| (i, (i + 1) % n)).collect();
let now = std::time::Instant::now();
let mut state: State = State::new(n);
for i in 0..n {
apply(Gate::RX(1.0), &mut state, i);
apply(Gate::RZ(1.0), &mut state, i);
}
for i in 0..n {
let (p0, p1) = pairs[i];
c_apply(Gate::X, &mut state, p0, p1);
}
for _ in 0..9 {
for i in 0..n {
apply(Gate::RZ(1.0), &mut state, i);
apply(Gate::RX(1.0), &mut state, i);
apply(Gate::RZ(1.0), &mut state, i);
}
for i in 0..n {
let (p0, p1) = pairs[i];
c_apply(Gate::X, &mut state, p0, p1);
}
}
for i in 0..n {
apply(Gate::RZ(1.0), &mut state, i);
apply(Gate::RX(1.0), &mut state, i);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
pub fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
qcbm_functional(config.qubits.into(), config.print);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn rx(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for t in 0..n {
apply(Gate::RX(1.0), &mut state, t);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
rx(config.qubits.into(), config.print);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn ry(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for i in 0..n {
apply(Gate::RY(1.0), &mut state, i);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
ry(config.qubits.into(), config.print);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn rz(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for t in 0..n {
apply(Gate::RZ(1.0), &mut state, t);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
rz(config.qubits.into(), config.print);
}
+42
View File
@@ -0,0 +1,42 @@
use clap::Parser;
use spinoza::core::reservoir_sampling;
use spinoza::{
config::{Config, QSArgs},
core::{iqft, State, CONFIG},
gates::{apply, Gate},
math::{pow2f, PI},
utils::{pretty_print_int, to_table},
};
fn run(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
let v = 2.4;
for i in 0..n {
apply(Gate::H, &mut state, i);
}
for i in 0..n {
apply(Gate::P(2.0 * PI / (pow2f(i + 1)) * v), &mut state, i);
}
let targets: Vec<usize> = (0..n).rev().collect();
iqft(&mut state, &targets);
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
let reservoir = reservoir_sampling(&state, state.len(), state.len() * 100_000);
let histogram = reservoir.get_outcome_count();
println!("{:?}", histogram);
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
run(config.qubits.into(), config.print);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn u(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for i in 0..n {
apply(Gate::U(1.0, 2.0, 3.0), &mut state, i);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
u(config.qubits.into(), config.print);
}
+16
View File
@@ -0,0 +1,16 @@
use spinoza::{
core::State,
gates::Gate,
unitaries::{apply_unitary, Unitary},
};
fn main() {
let n = 16;
let state = State::new(n);
let u = Unitary::from_single_qubit_gate(&state, Gate::U(1.0, 2.0, 3.0), 0);
let now = std::time::Instant::now();
let _s = apply_unitary(&state, &u);
let elapsed = now.elapsed().as_micros();
println!("{elapsed} us elapsed");
}
+37
View File
@@ -0,0 +1,37 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{iqft, State, CONFIG},
gates::{apply, Gate},
math::{pow2f, PI},
utils::{pretty_print_int, to_table},
};
fn value_encoding(n: usize, show_results: bool) {
let v = 2.4;
let now = std::time::Instant::now();
let mut state = State::new(n);
for i in 0..n {
apply(Gate::H, &mut state, i);
}
for i in 0..n {
apply(Gate::P(2.0 * PI / (pow2f(i + 1)) * v), &mut state, i);
}
let targets: Vec<usize> = (0..n).rev().collect();
iqft(&mut state, &targets);
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
pub fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
value_encoding(config.qubits.into(), config.print);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn x(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for t in 0..n {
apply(Gate::X, &mut state, t);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
x(config.qubits.into(), config.print);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn y(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for i in 0..n {
apply(Gate::Y, &mut state, i);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
y(config.qubits.into(), config.print);
}
+29
View File
@@ -0,0 +1,29 @@
use clap::Parser;
use spinoza::{
config::{Config, QSArgs},
core::{State, CONFIG},
gates::{apply, Gate},
utils::{pretty_print_int, to_table},
};
fn z(n: usize, show_results: bool) {
let now = std::time::Instant::now();
let mut state = State::new(n);
for i in 0..n {
apply(Gate::Z, &mut state, i);
}
let elapsed = now.elapsed().as_micros();
println!("{}", pretty_print_int(elapsed));
if show_results {
println!("{}", to_table(&state));
}
}
fn main() {
let args = QSArgs::parse();
let config = Config::from_cli(args);
CONFIG.set(config).unwrap();
z(config.qubits.into(), config.print);
}