Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove optional qir-stdlib dependency for sparse sim #164

Merged
merged 2 commits into from
May 14, 2024
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
1 change: 0 additions & 1 deletion Cargo.lock

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

22 changes: 18 additions & 4 deletions backend/src/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ use qir_stdlib::{
tuples::{__quantum__rt__tuple_create, __quantum__rt__tuple_update_reference_count},
Pauli,
};
use quantum_sparse_sim::exp::Pauli as SparsePauli;
use std::{
mem::size_of,
os::raw::{c_double, c_void},
};

fn map_pauli(pauli: Pauli) -> SparsePauli {
match pauli {
Pauli::I => SparsePauli::I,
Pauli::X => SparsePauli::X,
Pauli::Z => SparsePauli::Z,
Pauli::Y => SparsePauli::Y,
}
}

/// QIR API for applying an exponential of a multi-qubit rotation about the given Pauli axes with the given angle and qubits.
/// # Safety
///
Expand All @@ -33,8 +43,10 @@ pub unsafe extern "C" fn __quantum__qis__exp__body(
let state = &mut *sim_state.borrow_mut();

let paulis_size = __quantum__rt__array_get_size_1d(paulis);
let paulis: Vec<Pauli> = (0..paulis_size)
.map(|index| *__quantum__rt__array_get_element_ptr_1d(paulis, index).cast::<Pauli>())
let paulis: Vec<SparsePauli> = (0..paulis_size)
.map(|index| {
map_pauli(*__quantum__rt__array_get_element_ptr_1d(paulis, index).cast::<Pauli>())
})
.collect();

let qubits_size = __quantum__rt__array_get_size_1d(qubits);
Expand Down Expand Up @@ -97,9 +109,11 @@ pub unsafe extern "C" fn __quantum__qis__exp__ctl(
.collect();

let paulis_size = __quantum__rt__array_get_size_1d(args.paulis);
let paulis: Vec<Pauli> = (0..paulis_size)
let paulis: Vec<SparsePauli> = (0..paulis_size)
.map(|index| {
*__quantum__rt__array_get_element_ptr_1d(args.paulis, index).cast::<Pauli>()
map_pauli(
*__quantum__rt__array_get_element_ptr_1d(args.paulis, index).cast::<Pauli>(),
)
})
.collect();

Expand Down
5 changes: 0 additions & 5 deletions sparsesim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ edition = "2021"
license = "MIT"

[dependencies]
qir-stdlib = { path = "../stdlib", optional = true }
rand = "0.8.5"
rustc-hash = "1.1.0"
num-complex = "0.4"
num-traits = "0.2.19"
num-bigint = { version = "0.4.5", default-features = false }
ndarray = "0.15.4"

[features]
default = ["exp"]
exp = ["qir-stdlib"]
8 changes: 7 additions & 1 deletion sparsesim/src/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
use num_bigint::BigUint;
use num_complex::Complex64;
use num_traits::{One, Zero};
use qir_stdlib::Pauli;
use std::ops::ControlFlow;

use crate::{nearly_zero::NearlyZero, FlushLevel, QuantumSim, SparseState};

pub enum Pauli {
I,
X,
Z,
Y,
}

impl QuantumSim {
/// Exp multi-qubit rotation gate.
pub fn exp(&mut self, paulis: &[Pauli], theta: f64, targets: &[usize]) {
Expand Down
4 changes: 1 addition & 3 deletions sparsesim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
//! This libary implements sparse state simulation, based on the design from
//! <a href="https://arxiv.org/abs/2105.01533">Leveraging state sparsity for more efficient quantum simulations</a>.

mod nearly_zero;

#[cfg(feature = "exp")]
pub mod exp;
mod nearly_zero;

// Additional test infrastructure is available in matrix_testing that allows comparing the transformations
// implemented here with direct matrix application to the state vector.
Expand Down
Loading