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

check for diagonal matrices in find-diag-in-e-basis #857

Merged
merged 1 commit into from
Sep 30, 2022
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
68 changes: 38 additions & 30 deletions src/compilers/approx.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -199,36 +199,44 @@ This function should be completely deterministic (i.e., produce the same value f
(check-type m magicl:matrix)
(let* ((u (magicl:@ +edag-basis+ m +e-basis+))
(gammag (magicl:@ u (magicl:transpose u))))
(loop :for attempt :from 1 :to num-attempts :do
(let* ((coeff (diagonalizer-number-generator attempt))
(matrix (magicl:map (lambda (z)
(+ (* coeff (realpart z))
(* (- 1 coeff) (imagpart z))))
gammag))
(evecs (ensure-positive-determinant
(orthonormalize-matrix!
(nth-value 1 (magicl:eig matrix)))))
(evals (magicl:diag
(magicl:@ (magicl:transpose evecs)
gammag
evecs)))
(v (magicl:@ evecs
(from-diag evals)
(magicl:transpose evecs))))
(when (and (double= 1.0d0 (magicl:det evecs))
(magicl:every #'double= gammag v))
(when *check-math*
(assert (magicl:every #'double~
(eye 4 :type 'double-float)
(magicl:@ (magicl:transpose evecs)
evecs))
(evecs)
"The calculated eigenvectors were not found to be orthonormal. ~
EE^T =~%~A"
(magicl:@ (magicl:transpose evecs)
evecs)))
(return-from find-diagonalizer-in-e-basis evecs)))))
(error 'diagonalizer-not-found :matrix m :attempts num-attempts))
(cond
;; XXX: It seems that LAPACK distributions on ARM do not work or
;; are not stable when calculating eigenvectors of a diagonal
;; matrix. We have not closely investigated why, but checking
;; for diagonal matrices as a special case seems to work.
((diagonal-matrix-p gammag)
(magicl:eye (magicl:shape m) :type (magicl:element-type m)))
(t
(loop :for attempt :from 1 :to num-attempts :do
(let* ((coeff (diagonalizer-number-generator attempt))
(matrix (magicl:map (lambda (z)
(+ (* coeff (realpart z))
(* (- 1 coeff) (imagpart z))))
gammag))
(evecs (ensure-positive-determinant
(orthonormalize-matrix!
(nth-value 1 (magicl:eig matrix)))))
(evals (magicl:diag
(magicl:@ (magicl:transpose evecs)
gammag
evecs)))
(v (magicl:@ evecs
(from-diag evals)
(magicl:transpose evecs))))
(when (and (double= 1.0d0 (magicl:det evecs))
(magicl:every #'double= gammag v))
(when *check-math*
(assert (magicl:every #'double~
(eye 4 :type 'double-float)
(magicl:@ (magicl:transpose evecs)
evecs))
(evecs)
"The calculated eigenvectors were not found to be orthonormal. ~
EE^T =~%~A"
(magicl:@ (magicl:transpose evecs)
evecs)))
(return-from find-diagonalizer-in-e-basis evecs))))
(error 'diagonalizer-not-found :matrix m :attempts num-attempts)))))

(defun diagonalizer-in-e-basis (m)
"For M in SU(4), compute an SO(4) column matrix of eigenvectors of E^* M E (E^* M E)^T.
Expand Down
7 changes: 7 additions & 0 deletions src/matrix-operations.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

(in-package #:cl-quil)

(defun diagonal-matrix-p (m)
"Is M diagonal?"
(dotimes (row (magicl:nrows m) t)
(dotimes (col (magicl:ncols m))
(when (and (/= row col) (not (double~ 0 (magicl:tref m row col))))
(return-from diagonal-matrix-p nil)))))

(defun matrix-first-column-equality (x y)
(check-type x magicl:matrix)
(check-type y magicl:matrix)
Expand Down