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

Add some abbreviations for gates and linear algebra #12651

Merged
merged 1 commit into from
Jun 27, 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
5 changes: 1 addition & 4 deletions crates/accelerate/src/convert_2q_block_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ use numpy::ndarray::{aview2, Array2, ArrayView2};
use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2};
use smallvec::SmallVec;

static ONE_QUBIT_IDENTITY: [[Complex64; 2]; 2] = [
[Complex64::new(1., 0.), Complex64::new(0., 0.)],
[Complex64::new(0., 0.), Complex64::new(1., 0.)],
];
use qiskit_circuit::gate_matrix::ONE_QUBIT_IDENTITY;

/// Return the matrix Operator resulting from a block of Instructions.
#[pyfunction]
Expand Down
11 changes: 6 additions & 5 deletions crates/accelerate/src/euler_one_qubit_decomposer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use ndarray::prelude::*;
use numpy::PyReadonlyArray2;
use pyo3::pybacked::PyBackedStr;

use qiskit_circuit::util::c64;
use qiskit_circuit::SliceOrInt;

pub const ANGLE_ZERO_EPSILON: f64 = 1e-12;
Expand Down Expand Up @@ -855,16 +856,16 @@ pub fn params_xyx(unitary: PyReadonlyArray2<Complex64>) -> [f64; 4] {

fn params_xzx_inner(umat: ArrayView2<Complex64>) -> [f64; 4] {
let det = det_one_qubit(umat);
let phase = (Complex64::new(0., -1.) * det.ln()).re / 2.;
let phase = det.ln().im / 2.;
let sqrt_det = det.sqrt();
let mat_zyz = arr2(&[
[
Complex64::new((umat[[0, 0]] / sqrt_det).re, (umat[[1, 0]] / sqrt_det).im),
Complex64::new((umat[[1, 0]] / sqrt_det).re, (umat[[0, 0]] / sqrt_det).im),
c64((umat[[0, 0]] / sqrt_det).re, (umat[[1, 0]] / sqrt_det).im),
c64((umat[[1, 0]] / sqrt_det).re, (umat[[0, 0]] / sqrt_det).im),
],
[
Complex64::new(-(umat[[1, 0]] / sqrt_det).re, (umat[[0, 0]] / sqrt_det).im),
Complex64::new((umat[[0, 0]] / sqrt_det).re, -(umat[[1, 0]] / sqrt_det).im),
c64(-(umat[[1, 0]] / sqrt_det).re, (umat[[0, 0]] / sqrt_det).im),
c64((umat[[0, 0]] / sqrt_det).re, -(umat[[1, 0]] / sqrt_det).im),
],
]);
let [theta, phi, lam, phase_zxz] = params_zxz_inner(mat_zyz.view());
Expand Down
7 changes: 2 additions & 5 deletions crates/accelerate/src/isometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use ndarray::prelude::*;
use numpy::{IntoPyArray, PyReadonlyArray1, PyReadonlyArray2};

use qiskit_circuit::gate_matrix::ONE_QUBIT_IDENTITY;
use qiskit_circuit::util::C_ZERO;

/// Find special unitary matrix that maps [c0,c1] to [r,0] or [0,r] if basis_state=0 or
/// basis_state=1 respectively
Expand Down Expand Up @@ -315,11 +316,7 @@ pub fn merge_ucgate_and_diag(
.enumerate()
.map(|(i, raw_gate)| {
let gate = raw_gate.as_array();
let res = aview2(&[
[diag[2 * i], Complex64::new(0., 0.)],
[Complex64::new(0., 0.), diag[2 * i + 1]],
])
.dot(&gate);
let res = aview2(&[[diag[2 * i], C_ZERO], [C_ZERO, diag[2 * i + 1]]]).dot(&gate);
res.into_pyarray_bound(py).into()
})
.collect()
Expand Down
5 changes: 3 additions & 2 deletions crates/accelerate/src/pauli_exp_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use pyo3::wrap_pyfunction;
use rayon::prelude::*;

use crate::getenv_use_multiple_threads;
use qiskit_circuit::util::c64;

const PARALLEL_THRESHOLD: usize = 19;

Expand Down Expand Up @@ -88,15 +89,15 @@ pub fn expval_pauli_with_x(
let index_0 = ((i << 1) & mask_u) | (i & mask_l);
let index_1 = index_0 ^ x_mask;
let val_0 = (phase
* Complex64::new(
* c64(
data_arr[index_1].re * data_arr[index_0].re
+ data_arr[index_1].im * data_arr[index_0].im,
data_arr[index_1].im * data_arr[index_0].re
- data_arr[index_1].re * data_arr[index_0].im,
))
.re;
let val_1 = (phase
* Complex64::new(
* c64(
data_arr[index_0].re * data_arr[index_1].re
+ data_arr[index_0].im * data_arr[index_1].im,
data_arr[index_0].im * data_arr[index_1].re
Expand Down
3 changes: 2 additions & 1 deletion crates/accelerate/src/sampled_exp_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

use crate::pauli_exp_val::fast_sum;
use qiskit_circuit::util::c64;

const OPER_TABLE_SIZE: usize = (b'Z' as usize) + 1;
const fn generate_oper_table() -> [[f64; 2]; OPER_TABLE_SIZE] {
Expand Down Expand Up @@ -81,7 +82,7 @@ pub fn sampled_expval_complex(
let out: Complex64 = oper_strs
.into_iter()
.enumerate()
.map(|(idx, string)| coeff_arr[idx] * Complex64::new(bitstring_expval(&dist, string), 0.))
.map(|(idx, string)| coeff_arr[idx] * c64(bitstring_expval(&dist, string), 0.))
.sum();
Ok(out.re)
}
Expand Down
29 changes: 15 additions & 14 deletions crates/accelerate/src/sparse_pauli_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use hashbrown::HashMap;
use ndarray::{s, Array1, Array2, ArrayView1, ArrayView2, Axis};
use num_complex::Complex64;
use num_traits::Zero;
use qiskit_circuit::util::{c64, C_ONE, C_ZERO};
use rayon::prelude::*;

use crate::rayon_ext::*;
Expand Down Expand Up @@ -257,9 +258,9 @@ impl<'py> ZXPaulisView<'py> {
let ys = (xs & zs).count_ones();
match (phase as u32 + ys) % 4 {
0 => coeff,
1 => Complex64::new(coeff.im, -coeff.re),
2 => Complex64::new(-coeff.re, -coeff.im),
3 => Complex64::new(-coeff.im, coeff.re),
1 => c64(coeff.im, -coeff.re),
2 => c64(-coeff.re, -coeff.im),
3 => c64(-coeff.im, coeff.re),
_ => unreachable!(),
}
})
Expand Down Expand Up @@ -311,10 +312,10 @@ impl MatrixCompressedPaulis {
.zip(self.z_like.drain(..))
.zip(self.coeffs.drain(..))
{
*hash_table.entry(key).or_insert(Complex64::new(0.0, 0.0)) += coeff;
*hash_table.entry(key).or_insert(C_ZERO) += coeff;
}
for ((x, z), coeff) in hash_table {
if coeff == Complex64::new(0.0, 0.0) {
if coeff.is_zero() {
continue;
}
self.x_like.push(x);
Expand Down Expand Up @@ -347,7 +348,7 @@ pub fn decompose_dense(
let mut coeffs = vec![];
if num_qubits > 0 {
decompose_dense_inner(
Complex64::new(1.0, 0.0),
C_ONE,
num_qubits,
&[],
operator.as_array(),
Expand Down Expand Up @@ -532,7 +533,7 @@ fn to_matrix_dense_inner(paulis: &MatrixCompressedPaulis, parallel: bool) -> Vec
// Doing the initialization here means that when we're in parallel contexts, we do the
// zeroing across the whole threadpool. This also seems to give a speed-up in serial
// contexts, but I don't understand that. ---Jake
row.fill(Complex64::new(0.0, 0.0));
row.fill(C_ZERO);
for ((&x_like, &z_like), &coeff) in paulis
.x_like
.iter()
Expand Down Expand Up @@ -667,7 +668,7 @@ macro_rules! impl_to_matrix_sparse {
((i_row as $uint_ty) ^ (paulis.x_like[a] as $uint_ty))
.cmp(&((i_row as $uint_ty) ^ (paulis.x_like[b] as $uint_ty)))
});
let mut running = Complex64::new(0.0, 0.0);
let mut running = C_ZERO;
let mut prev_index = i_row ^ (paulis.x_like[order[0]] as usize);
for (x_like, z_like, coeff) in order
.iter()
Expand Down Expand Up @@ -748,7 +749,7 @@ macro_rules! impl_to_matrix_sparse {
(i_row as $uint_ty ^ paulis.x_like[a] as $uint_ty)
.cmp(&(i_row as $uint_ty ^ paulis.x_like[b] as $uint_ty))
});
let mut running = Complex64::new(0.0, 0.0);
let mut running = C_ZERO;
let mut prev_index = i_row ^ (paulis.x_like[order[0]] as usize);
for (x_like, z_like, coeff) in order
.iter()
Expand Down Expand Up @@ -844,11 +845,11 @@ mod tests {
// Deliberately using multiples of small powers of two so the floating-point addition
// of them is associative.
coeffs: vec![
Complex64::new(0.25, 0.5),
Complex64::new(0.125, 0.25),
Complex64::new(0.375, 0.125),
Complex64::new(-0.375, 0.0625),
Complex64::new(-0.5, -0.25),
c64(0.25, 0.5),
c64(0.125, 0.25),
c64(0.375, 0.125),
c64(-0.375, 0.0625),
c64(-0.5, -0.25),
],
}
}
Expand Down
Loading
Loading