-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathmod.rs
More file actions
412 lines (358 loc) · 14.3 KB
/
mod.rs
File metadata and controls
412 lines (358 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// This code is part of Qiskit.
//
// (C) Copyright IBM 2025
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at https://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.
use approx::{abs_diff_eq, relative_ne};
use faer::Mat;
use faer::MatRef;
use nalgebra::{DMatrix, DMatrixView, Dim, Dyn, MatrixView, Scalar, ViewStorage};
use ndarray::ArrayView2;
use ndarray::ShapeBuilder;
use num_complex::Complex64;
use pyo3::PyErr;
use thiserror::Error;
use crate::QiskitError;
pub mod cos_sin_decomp;
const ATOL_DEFAULT: f64 = 1e-8;
const RTOL_DEFAULT: f64 = 1e-5;
/// Tolerance used for debug checking
pub const VERIFY_TOL: f64 = 1e-7;
/// Errors that might occur in linear algebra computations
#[derive(Error, Debug)]
pub enum LinAlgError {
#[error("Eigen decomposition failed")]
EigenDecompositionFailed,
#[error("SVD decomposition failed")]
SVDDecompositionFailed,
}
impl From<LinAlgError> for PyErr {
fn from(error: LinAlgError) -> Self {
match error {
LinAlgError::EigenDecompositionFailed => QiskitError::new_err(
"Internal eigendecomposition failed. \
This can point to a numerical tolerance issue.",
),
LinAlgError::SVDDecompositionFailed => QiskitError::new_err(
"Internal SVD decomposition failed. \
This can point to a numerical tolerance issue.",
),
}
}
}
#[inline]
pub fn nalgebra_array_view<T: Scalar, R: Dim, C: Dim>(mat: MatrixView<T, R, C>) -> ArrayView2<T> {
let dim = ndarray::Dim(mat.shape());
let strides = ndarray::Dim(mat.strides());
// SAFETY: We know the array is a 2d array from nalgebra and we get the pointer and memory
// layout description from nalgebra so we don't need to check for invalid format as nalgebra
// has already validated this
unsafe { ArrayView2::from_shape_ptr(dim.strides(strides), mat.get_unchecked(0)) }
}
#[inline]
pub fn ndarray_to_faer<T>(array: ArrayView2<'_, T>) -> MatRef<'_, T> {
// This function's code is based on faer-ext's IntoFaer::into_faer implementation for ArrayView2:
// https://codeberg.org/sarah-quinones/faer-ext/src/commit/0f055b39529c94d1a000982df745cb9ce170f994/src/lib.rs#L108-L114
let nrows = array.nrows();
let ncols = array.ncols();
let strides: [isize; 2] = array.strides().try_into().unwrap();
let ptr = array.as_ptr();
// SAFETY: We know the array is a 2d array from ndarray and we get the pointer and memory layout
// description from ndarray and can be assumed to be valid.
unsafe { faer::MatRef::from_raw_parts(ptr, nrows, ncols, strides[0], strides[1]) }
}
#[inline]
pub fn faer_to_ndarray<T>(mat: MatRef<'_, T>) -> ArrayView2<'_, T> {
// This function's code is based on faer-ext's IntoNdarray::into_ndarray implementation at:
// https://codeberg.org/sarah-quinones/faer-ext/src/commit/0f055b39529c94d1a000982df745cb9ce170f994/src/lib.rs#L134-L141
let nrows = mat.nrows();
let ncols = mat.ncols();
let row_stride: usize = mat.row_stride().try_into().unwrap();
let col_stride: usize = mat.col_stride().try_into().unwrap();
let ptr = mat.as_ptr();
// SAFETY: We know the array is a 2d array from nalgebra and we get the pointer and memory layout
// description from faer and can be assumed to be valid since the constraints on
// `ArrayView2::from_shape_ptr()`
// (https://docs.rs/ndarray/latest/ndarray/type.ArrayView.html#method.from_shape_ptr)
// should be be met for a faer Mat.
unsafe { ArrayView2::from_shape_ptr((nrows, ncols).strides((row_stride, col_stride)), ptr) }
}
#[inline]
pub(crate) fn nalgebra_to_faer<R: Dim, C: Dim, RStride: Dim, CStride: Dim, T: nalgebra::Scalar>(
mat: MatrixView<'_, T, R, C, RStride, CStride>,
) -> MatRef<'_, T> {
let dim = ::ndarray::Dim(mat.shape());
let strides = ::ndarray::Dim(mat.strides());
// SAFETY: We know the array is a 2d array from nalgebra and we get the pointer and memory layout
// description from nalgebra and can be assumed to be valid since the constraints on
// `ArrayView2::from_shape_ptr()`
// (https://docs.rs/ndarray/latest/ndarray/type.ArrayView.html#method.from_shape_ptr)
// should be be met for a valid nalgebra matrix.
let array = unsafe { ArrayView2::from_shape_ptr(dim.strides(strides), mat.get_unchecked(0)) };
ndarray_to_faer(array)
}
/// Convert a faer MatRef into a nalgebra MatrixView without copying
///
/// This function has some potential sharp edeges around converting strided
/// views. If you are using a strided `MatRef` you'll typically want to ensure
/// the underlying matrix the view is over is contiguous. Specifically if it's
/// not there is a potential path to undefined behavior when using nalgebra
/// methods like `MatrixView::into_slice()` which doesn't understand
/// striding and will access the memory as if the array was a contiguous R x C
/// matrix. If using striding here it's best to not ever call `into_slice()`. There
/// might also be similar sharp edges with strided matrices.
#[inline]
pub(crate) fn faer_to_nalgebra(
mat: MatRef<'_, Complex64>,
) -> MatrixView<'_, Complex64, Dyn, Dyn, Dyn, Dyn> {
// This function's code is based on faer-ext's IntoNalgebra::into_nalgebra implementation at:
// https://codeberg.org/sarah-quinones/faer-ext/src/commit/0f055b39529c94d1a000982df745cb9ce170f994/src/lib.rs#L77-L96
let nrows = mat.nrows();
let ncols = mat.ncols();
let row_stride = mat.row_stride();
let col_stride = mat.col_stride();
let ptr = mat.as_ptr();
// SAFETY: Pointer came from a faer MatRef as does the description of the memory layout
// so creating a view of the data from the from the pointer is safe unless the faer object
// is already corrupt. nalgebra doesn't support negative striding so that panics and we
// only work with positive strides
unsafe {
MatrixView::<'_, Complex64, Dyn, Dyn, Dyn, Dyn>::from_data(ViewStorage::<
'_,
Complex64,
Dyn,
Dyn,
Dyn,
Dyn,
>::from_raw_parts(
ptr,
(Dyn(nrows), Dyn(ncols)),
(
Dyn(row_stride
.try_into()
.expect("only works for positive strides")),
Dyn(col_stride
.try_into()
.expect("only works for positive strides")),
),
))
}
}
/// Check whether the given matrix is hermitian by comparing it (up to tolerance) with its hermitian adjoint.
pub fn is_hermitian_matrix(mat: DMatrixView<Complex64>) -> bool {
let shape = mat.shape();
let adjoint = mat.adjoint();
for i in 0..shape.0 {
for j in 0..shape.1 {
if relative_ne!(
mat[(i, j)],
adjoint[(i, j)],
epsilon = ATOL_DEFAULT,
max_relative = RTOL_DEFAULT
) {
return false;
}
}
}
true
}
/// Verify SVD decomposition gives the same unitary
fn verify_svd_decomp(
mat: DMatrixView<Complex64>,
v: DMatrixView<Complex64>,
s: DMatrixView<Complex64>,
w: DMatrixView<Complex64>,
) -> bool {
let mat_check = v * s * w;
abs_diff_eq!(mat, mat_check.as_view(), epsilon = VERIFY_TOL)
}
/// Verifies the given matrix U is unitary by comparing U*U to the identity matrix
pub fn verify_unitary(u: &DMatrix<Complex64>) -> bool {
let n = u.shape().0;
let id_mat = DMatrix::identity(n, n);
let uu = u.adjoint() * u;
abs_diff_eq!(uu, id_mat, epsilon = VERIFY_TOL)
}
/// Given a matrix that is "close" to unitary, returns the closest
/// unitary matrix.
/// See https://michaelgoerz.net/notes/finding-the-closest-unitary-for-a-given-matrix/,
pub fn closest_unitary(mat: DMatrixView<Complex64>) -> DMatrix<Complex64> {
let (u, _sigma, v_t) = svd_decomposition(mat);
&u * &v_t
}
/// Calculate the condition number of a matrix w.r.t the L2 norm
/// using SVD
pub fn condition_number(mat: DMatrix<Complex64>) -> Option<f64> {
let svd = mat.svd(false, false);
let singular_values = svd.singular_values;
if singular_values.is_empty() {
return None;
}
let max_sv = singular_values
.iter()
.copied()
.fold(f64::NEG_INFINITY, f64::max);
let min_sv = singular_values
.iter()
.copied()
.fold(f64::INFINITY, f64::min);
if min_sv == 0.0 {
return None; // Singular matrix
}
Some(max_sv / min_sv)
}
/// Returns the SVD decomposition of the given matrix M as three matrices A,S,B such that
/// M=ASB (In the usual notations, M=USV*, and this function returns A=U and B=V*)
pub fn svd_decomposition(
mat: DMatrixView<Complex64>,
) -> (DMatrix<Complex64>, DMatrix<Complex64>, DMatrix<Complex64>) {
let mat_view: DMatrixView<Complex64> = mat.as_view();
let faer_mat: MatRef<Complex64> = nalgebra_to_faer(mat_view);
let faer_svd = faer_mat.svd().unwrap();
let u_faer = faer_svd.U();
let s_faer = faer_svd.S();
let v_faer = faer_svd.V();
let s_na = DMatrix::from_fn(u_faer.ncols(), v_faer.nrows(), |i, j| {
if i == j {
s_faer[i]
} else {
Complex64::new(0.0, 0.0)
}
});
let u_na = faer_to_nalgebra(u_faer);
let v_na = faer_to_nalgebra(v_faer).adjoint();
debug_assert!(verify_svd_decomp(
mat_view,
u_na.as_view(),
s_na.as_view(),
v_na.as_view()
));
(u_na.into(), s_na, v_na)
}
/// Result for the singular value decomposition (SVD) of a matrix `A`.
///
/// The decomposition is given by three matrices `(U, S, V)` such that `A = U * S * V^\dagger`.
pub struct SVDResult {
pub u: Mat<Complex64>,
pub s: Mat<Complex64>,
pub v: Mat<Complex64>,
}
/// Runs singular valued decomposition on `mat`.
pub fn svd_decomposition_faer(mat: MatRef<Complex64>) -> Result<SVDResult, LinAlgError> {
let svd = mat.svd().map_err(|_| LinAlgError::SVDDecompositionFailed)?;
let n = mat.nrows();
let u = svd.U().to_owned();
let v = svd.V().to_owned();
let mut s = Mat::zeros(n, n);
s.diagonal_mut().copy_from(svd.S());
let svd_result = SVDResult { u, s, v };
debug_assert!(verify_svd_decomposition_faer(mat.as_ref(), &svd_result));
Ok(svd_result)
}
/// Computes the eigenvalues and the eigenvectors of a square matrix
pub fn eigendecomposition_faer(
mat: MatRef<Complex64>,
) -> Result<(Vec<Complex64>, Mat<Complex64>), LinAlgError> {
let eigh = mat
.eigen()
.map_err(|_| LinAlgError::EigenDecompositionFailed)?;
let vmat = eigh.U().to_owned();
// unfortunately, we need to call closest_unitary_faer here
let vmat = closest_unitary_faer(vmat.as_ref())?;
let eigvals: Vec<Complex64> = eigh.S().column_vector().iter().copied().collect();
Ok((eigvals, vmat))
}
pub fn closest_unitary_faer(mat: MatRef<Complex64>) -> Result<Mat<Complex64>, LinAlgError> {
let svd = mat.svd().map_err(|_| LinAlgError::SVDDecompositionFailed)?;
Ok(svd.U() * svd.V().adjoint())
}
pub fn from_diagonal_faer(diag: &[Complex64]) -> Mat<Complex64> {
let n = diag.len();
let mut mat = Mat::zeros(n, n);
mat.diagonal_mut()
.column_vector_mut()
.iter_mut()
.zip(diag)
.for_each(|(x, y)| *x = *y);
mat
}
/// Returns a block matrix `[a, b; c, d]`.
/// The matrices `a`, `b`, `c`, `d` are all assumed to be square matrices of the same size
pub fn block_matrix_faer(
a: MatRef<Complex64>,
b: MatRef<Complex64>,
c: MatRef<Complex64>,
d: MatRef<Complex64>,
) -> Mat<Complex64> {
let n = a.nrows();
let mut block_matrix = Mat::<Complex64>::zeros(2 * n, 2 * n);
block_matrix.as_mut().submatrix_mut(0, 0, n, n).copy_from(a);
block_matrix.as_mut().submatrix_mut(0, n, n, n).copy_from(b);
block_matrix.as_mut().submatrix_mut(n, 0, n, n).copy_from(c);
block_matrix.as_mut().submatrix_mut(n, n, n, n).copy_from(d);
block_matrix
}
/// Verify SVD decomposition gives the same unitary
fn verify_svd_decomposition_faer(mat: MatRef<Complex64>, svd: &SVDResult) -> bool {
let mat_check = svd.u.as_ref() * svd.s.as_ref() * svd.v.as_ref().adjoint();
(mat - mat_check).norm_max() < VERIFY_TOL
}
/// Verifies the given matrix U is unitary by comparing U*U to the identity matrix
pub fn verify_unitary_faer(u: MatRef<Complex64>) -> bool {
let n = u.shape().0;
let id_mat = Mat::<Complex64>::identity(n, n);
let uu = u.adjoint() * u;
(uu.as_ref() - id_mat.as_ref()).norm_max() < VERIFY_TOL
}
// check whether a matrix is zero (up to tolerance)
pub fn is_zero_matrix_faer(mat: MatRef<Complex64>, atol: Option<f64>) -> bool {
let atol = atol.unwrap_or(1e-12);
for i in 0..mat.nrows() {
for j in 0..mat.ncols() {
if !abs_diff_eq!(mat[(i, j)], Complex64::ZERO, epsilon = atol) {
return false;
}
}
}
true
}
#[cfg(test)]
mod test {
use super::*;
use nalgebra::DMatrix;
#[test]
fn test_basic_faer_to_nalgebra_conversion() {
let matrix = Mat::from_fn(10, 10, |i, j| {
Complex64::new(i as f64, 0.0) + Complex64::new(j as f64 * 10., 0.)
});
let mat_view = faer_to_nalgebra(matrix.as_ref());
let expected = DMatrix::from_fn(10, 10, |i, j| {
Complex64::new(i as f64, 0.0) + Complex64::new(j as f64 * 10., 0.)
});
assert_eq!(mat_view, expected);
}
#[cfg(not(miri))] // TODO: Remove this after dimforge/nalgebra#1562 is released
#[test]
fn test_transpose_faer_to_nalgebra_conversion() {
let matrix = Mat::from_fn(10, 10, |i, j| {
Complex64::new(i as f64, 0.0) + Complex64::new(j as f64 * 10., 0.)
});
let mat_view = faer_to_nalgebra(matrix.transpose());
let expected = DMatrix::from_fn(10, 10, |i, j| {
Complex64::new(j as f64, 0.0) + Complex64::new(i as f64 * 10., 0.)
});
assert_eq!(mat_view, expected);
}
#[test]
#[should_panic]
fn test_negative_strided_view_faer_to_nalgebra_conversion() {
let matrix = Mat::identity(10, 10);
faer_to_nalgebra(matrix.reverse_rows());
}
}