Skip to content

Commit

Permalink
Define meaningful types.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbr committed Sep 12, 2019
1 parent 00c4b62 commit 57eba3b
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions dqvm/src/permutation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@
;;; more than three times faster than the equivalent application of a
;;; PERMUTATION-GENERAL object).

(defconstant +qubit-index-length+ (ceiling (log qvm::+max-nat-tuple-cardinality+ 2))
"Number of bits required to represent a qubit index.")

(defconstant +max-number-of-transpositions+ (* 2 #.qvm::+max-nat-tuple-cardinality+)
"Upper bound on the number of transpositions defining an arbitrary permutation.")

(deftype qubit-index ()
'(unsigned-byte #.+qubit-index-length+))

(deftype transposition ()
'(or null (cons alexandria:non-negative-fixnum
alexandria:non-negative-fixnum)))
'(or null (cons qubit-index qubit-index)))

(defclass permutation ()
()
Expand All @@ -39,9 +47,9 @@
:transpositions nil)
(:documentation "Arbitrary permutation acting on sets of qubit indices."))

(defclass permutation-transposition ()
(defclass permutation-transposition (permutation)
((tau
:type (unsigned-byte 6) ; Implies a maximum of 2⁶ = 64 qubits.
:type qubit-index
:initarg :tau
:initform (error-missing-initform :tau)
:documentation "Positive value of τ in π = (0 τ)."))
Expand Down Expand Up @@ -196,13 +204,12 @@ DQVM2> (write (apply-qubit-permutation (make-permutation '((2 . 0))) #b001) :bas

(defmethod apply-qubit-permutation ((permutation permutation-transposition) address)
(declare #.qvm::*optimize-dangerously-fast*
(type (or null permutation) permutation)
;; (type qvm:amplitude-address address)
(type (unsigned-byte 64) address) ; Imposed maximum number of qubits.
(type permutation permutation)
(type qvm:amplitude-address address)
(values qvm:amplitude-address))

(let ((tau (slot-value permutation 'tau)))
(declare (type (unsigned-byte 6) tau))
(declare (type qubit-index tau))

;; Swap bits 0 and TAU in ADDRESS.
(let ((x (logxor (logand address 1) (logand (ash address (- tau)) 1))))
Expand All @@ -215,15 +222,14 @@ DQVM2> (write (apply-qubit-permutation (make-permutation '((2 . 0))) #b001) :bas
;; J. Comput., vol. 24, no. 2, pp. 266–278, Apr. 1995.

(declare #.qvm::*optimize-dangerously-fast*
(type (or null permutation) permutation)
;; (type qvm:amplitude-address address)
(type (unsigned-byte 64) address) ; Imposed maximum number of qubits.
(type permutation permutation)
(type qvm:amplitude-address address)
(values qvm:amplitude-address))

(let* ((transpositions (slot-value permutation 'transpositions))
(number-of-transpositions (slot-value permutation 'number-of-transpositions))
(bit-vector (make-array number-of-transpositions :element-type 'bit)))
(declare (type (integer 0 128) number-of-transpositions)
(declare (type (integer 0 #.+max-number-of-transpositions+) number-of-transpositions)
(dynamic-extent bit-vector))

(loop :for index :from 0
Expand All @@ -235,8 +241,7 @@ DQVM2> (write (apply-qubit-permutation (make-permutation '((2 . 0))) #b001) :bas
:for transposition :of-type transposition :in transpositions :do
(setf address (the qvm:amplitude-address
(dpb (bit bit-vector index)
(byte 1 (the (unsigned-byte 6) (rest transposition))) ; Enable this for speed (assumes a maximum of 64 qubits).
;; (byte 1 (rest transposition))
(byte 1 (the qubit-index (rest transposition)))
address)))
:finally (return address))))

Expand All @@ -256,7 +261,8 @@ DQVM2> (write (apply-qubit-permutation (make-permutation '((2 . 0))) #b001) :bas
(minus-tau (- tau)))
`(lambda (,address)
(declare #.qvm::*optimize-dangerously-fast*
(type (unsigned-byte 64) ,address) ; Imposed maximum number of qubits.
(type permutation permutation)
(type qvm:amplitude-address ,address)
(values qvm:amplitude-address))

;; Swap bits 0 and TAU in ADDRESS.
Expand All @@ -269,8 +275,8 @@ DQVM2> (write (apply-qubit-permutation (make-permutation '((2 . 0))) #b001) :bas
(number-of-transpositions (slot-value permutation 'number-of-transpositions)))
`(lambda (,address)
(declare #.qvm::*optimize-dangerously-fast*
(type (or null permutation) permutation)
(type (unsigned-byte 64) ,address)
(type permutation permutation)
(type qvm:amplitude-address ,address)
(values qvm:amplitude-address))

(let ((bit-vector (make-array ,number-of-transpositions :element-type 'bit)))
Expand All @@ -285,7 +291,7 @@ DQVM2> (write (apply-qubit-permutation (make-permutation '((2 . 0))) #b001) :bas
:for transposition :of-type transposition :in transpositions
:collect `(setf ,address (the qvm:amplitude-address
(dpb (bit bit-vector ,index)
(byte 1 (the (unsigned-byte 6) ,(rest transposition)))
(byte 1 (the qubit-index ,(rest transposition)))
,address))))
,address))))

Expand Down

0 comments on commit 57eba3b

Please sign in to comment.