Skip to content

Commit

Permalink
Test the permutation synthesis machinery better.
Browse files Browse the repository at this point in the history
  • Loading branch information
karlosz committed Oct 11, 2021
1 parent 4c57d76 commit 01f87e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/compilers/permutation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
;; controlled Toffoli gate, so we didn't do anything and
;; should give up.
(when (and code (null (rest code)))
(give-up-compilation :acts-trivially))
(give-up-compilation :because :acts-trivially))
code))
(t
(give-up-compilation :invalid-domain))))))
(give-up-compilation :because :invalid-domain))))))
38 changes: 31 additions & 7 deletions tests/permutation-tests.lisp
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
;;;; linear-reversible-circuit.lisp
;;;; permutation-tests.lisp
;;;;
;;;; Author: Charles Zhang

(in-package #:cl-quil-tests)

(a:define-constant +prime+ #(0 2 3 5 7 1 4 6) :test #'equalp)

;; Test that internal assertions are all satisfied.
(deftest permutation-internal-assertion ()
(quil::synthesize-permutation #(0 1))
(quil::synthesize-permutation #(1 0))
(quil::synthesize-permutation #(3 0 1 2))
(quil::synthesize-permutation +prime+))
(defun matrix-from-permutation (permutation)
(let* ((size (length permutation))
(matrix (magicl:zeros (list size size) :type '(complex double-float))))
(loop :for i :from 0
:for j :across permutation
:do (setf (magicl:tref matrix j i) 1))
matrix))

(defun permutation-synthesis-as-parsed-program (permutation)
(make-instance 'quil::parsed-program
:executable-code (coerce (quil::synthesize-permutation permutation) 'vector)))

;;; Test that the synthesized permutation when simulated performs the
;;; action of the permutation.
(deftest test-permutation-gates-logical-matrix-equivalent ()
(flet ((test (permutation)
(assert (quil::operator=
(quil:parsed-program-to-logical-matrix
(permutation-synthesis-as-parsed-program permutation)
:compress-qubits nil)
(matrix-from-permutation permutation)))))
(test #(0 1))
(test #(1 0))
(test #(3 0 1 2))
(test #(2 0 3 1))
(test #(1 0 3 2))
(test #(0 2 3 1))
(test #(2 1 3 0))
(test #(3 1 2 0))
(test +prime+)))

0 comments on commit 01f87e9

Please sign in to comment.