From 8a3d353f20f5f7a09f23b3edef120d5f1c7e0de7 Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Fri, 3 May 2024 13:25:39 -0700 Subject: [PATCH 1/2] Remove optional qir_stdlib dependency for sparse sim --- Cargo.lock | 1 - backend/src/exp.rs | 19 +++++++++++++++---- sparsesim/Cargo.toml | 5 ----- sparsesim/src/exp.rs | 8 +++++++- sparsesim/src/lib.rs | 2 -- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 414cb122..0c090e79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -740,7 +740,6 @@ dependencies = [ "num-bigint", "num-complex", "num-traits", - "qir-stdlib", "rand", "rustc-hash", ] diff --git a/backend/src/exp.rs b/backend/src/exp.rs index 7d6ae65f..1e04a3da 100644 --- a/backend/src/exp.rs +++ b/backend/src/exp.rs @@ -13,11 +13,22 @@ 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 /// @@ -33,8 +44,8 @@ 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 = (0..paulis_size) - .map(|index| *__quantum__rt__array_get_element_ptr_1d(paulis, index).cast::()) + let paulis: Vec = (0..paulis_size) + .map(|index| map_pauli(*__quantum__rt__array_get_element_ptr_1d(paulis, index).cast::())) .collect(); let qubits_size = __quantum__rt__array_get_size_1d(qubits); @@ -97,9 +108,9 @@ pub unsafe extern "C" fn __quantum__qis__exp__ctl( .collect(); let paulis_size = __quantum__rt__array_get_size_1d(args.paulis); - let paulis: Vec = (0..paulis_size) + let paulis: Vec = (0..paulis_size) .map(|index| { - *__quantum__rt__array_get_element_ptr_1d(args.paulis, index).cast::() + map_pauli(*__quantum__rt__array_get_element_ptr_1d(args.paulis, index).cast::()) }) .collect(); diff --git a/sparsesim/Cargo.toml b/sparsesim/Cargo.toml index 1b007c08..d6378fb2 100644 --- a/sparsesim/Cargo.toml +++ b/sparsesim/Cargo.toml @@ -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"] diff --git a/sparsesim/src/exp.rs b/sparsesim/src/exp.rs index e0d9ca5e..cfd63074 100644 --- a/sparsesim/src/exp.rs +++ b/sparsesim/src/exp.rs @@ -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]) { diff --git a/sparsesim/src/lib.rs b/sparsesim/src/lib.rs index e8e19c2f..89e7e974 100644 --- a/sparsesim/src/lib.rs +++ b/sparsesim/src/lib.rs @@ -8,8 +8,6 @@ //! Leveraging state sparsity for more efficient quantum simulations. mod nearly_zero; - -#[cfg(feature = "exp")] pub mod exp; // Additional test infrastructure is available in matrix_testing that allows comparing the transformations From 7517a7a145c64af64e7b21e521639df886778d00 Mon Sep 17 00:00:00 2001 From: "Stefan J. Wernli" Date: Fri, 3 May 2024 13:33:36 -0700 Subject: [PATCH 2/2] Fix formatting --- backend/src/exp.rs | 9 ++++++--- sparsesim/src/lib.rs | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/backend/src/exp.rs b/backend/src/exp.rs index 1e04a3da..77ac02de 100644 --- a/backend/src/exp.rs +++ b/backend/src/exp.rs @@ -28,7 +28,6 @@ fn map_pauli(pauli: Pauli) -> SparsePauli { } } - /// QIR API for applying an exponential of a multi-qubit rotation about the given Pauli axes with the given angle and qubits. /// # Safety /// @@ -45,7 +44,9 @@ pub unsafe extern "C" fn __quantum__qis__exp__body( let paulis_size = __quantum__rt__array_get_size_1d(paulis); let paulis: Vec = (0..paulis_size) - .map(|index| map_pauli(*__quantum__rt__array_get_element_ptr_1d(paulis, index).cast::())) + .map(|index| { + map_pauli(*__quantum__rt__array_get_element_ptr_1d(paulis, index).cast::()) + }) .collect(); let qubits_size = __quantum__rt__array_get_size_1d(qubits); @@ -110,7 +111,9 @@ pub unsafe extern "C" fn __quantum__qis__exp__ctl( let paulis_size = __quantum__rt__array_get_size_1d(args.paulis); let paulis: Vec = (0..paulis_size) .map(|index| { - map_pauli(*__quantum__rt__array_get_element_ptr_1d(args.paulis, index).cast::()) + map_pauli( + *__quantum__rt__array_get_element_ptr_1d(args.paulis, index).cast::(), + ) }) .collect(); diff --git a/sparsesim/src/lib.rs b/sparsesim/src/lib.rs index 89e7e974..378f90db 100644 --- a/sparsesim/src/lib.rs +++ b/sparsesim/src/lib.rs @@ -7,8 +7,8 @@ //! This libary implements sparse state simulation, based on the design from //! Leveraging state sparsity for more efficient quantum simulations. -mod nearly_zero; 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.