Skip to content

Commit 57eba3b

Browse files
committed
Define meaningful types.
1 parent 00c4b62 commit 57eba3b

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

dqvm/src/permutation.lisp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@
1717
;;; more than three times faster than the equivalent application of a
1818
;;; PERMUTATION-GENERAL object).
1919

20+
(defconstant +qubit-index-length+ (ceiling (log qvm::+max-nat-tuple-cardinality+ 2))
21+
"Number of bits required to represent a qubit index.")
22+
23+
(defconstant +max-number-of-transpositions+ (* 2 #.qvm::+max-nat-tuple-cardinality+)
24+
"Upper bound on the number of transpositions defining an arbitrary permutation.")
25+
26+
(deftype qubit-index ()
27+
'(unsigned-byte #.+qubit-index-length+))
28+
2029
(deftype transposition ()
21-
'(or null (cons alexandria:non-negative-fixnum
22-
alexandria:non-negative-fixnum)))
30+
'(or null (cons qubit-index qubit-index)))
2331

2432
(defclass permutation ()
2533
()
@@ -39,9 +47,9 @@
3947
:transpositions nil)
4048
(:documentation "Arbitrary permutation acting on sets of qubit indices."))
4149

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

197205
(defmethod apply-qubit-permutation ((permutation permutation-transposition) address)
198206
(declare #.qvm::*optimize-dangerously-fast*
199-
(type (or null permutation) permutation)
200-
;; (type qvm:amplitude-address address)
201-
(type (unsigned-byte 64) address) ; Imposed maximum number of qubits.
207+
(type permutation permutation)
208+
(type qvm:amplitude-address address)
202209
(values qvm:amplitude-address))
203210

204211
(let ((tau (slot-value permutation 'tau)))
205-
(declare (type (unsigned-byte 6) tau))
212+
(declare (type qubit-index tau))
206213

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

217224
(declare #.qvm::*optimize-dangerously-fast*
218-
(type (or null permutation) permutation)
219-
;; (type qvm:amplitude-address address)
220-
(type (unsigned-byte 64) address) ; Imposed maximum number of qubits.
225+
(type permutation permutation)
226+
(type qvm:amplitude-address address)
221227
(values qvm:amplitude-address))
222228

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

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

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

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

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

0 commit comments

Comments
 (0)