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

Accelerate APPLY-QUBIT-PERMUTATION. #179

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

jmbr
Copy link
Contributor

@jmbr jmbr commented Sep 11, 2019

Part of #177.

@jmbr jmbr added the enhancement New feature or request label Sep 11, 2019
@jmbr jmbr requested a review from a team as a code owner September 11, 2019 18:31
@jmbr jmbr force-pushed the enhancement/accelerate-permutations branch 3 times, most recently from 0197001 to 6b4158e Compare September 11, 2019 19:19
@jmbr jmbr force-pushed the enhancement/accelerate-permutations branch from 6b4158e to 6afe4c8 Compare September 11, 2019 21:13
@jmbr jmbr added the WIP Work-in-progress. label Sep 12, 2019
@jmbr jmbr force-pushed the enhancement/accelerate-permutations branch from 0842977 to 00c4b62 Compare September 12, 2019 14:11
Copy link
Contributor

@appleby appleby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small stuff

dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
dqvm/src/permutation.lisp Show resolved Hide resolved
dqvm/src/permutation.lisp Show resolved Hide resolved
dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
@jmbr jmbr force-pushed the enhancement/accelerate-permutations branch from 57eba3b to f2619b8 Compare September 12, 2019 16:50
Copy link
Member

@stylewarning stylewarning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more

dqvm/src/permutation.lisp Show resolved Hide resolved
dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
(declare (type qubit-index tau))

;; Swap bits 0 and TAU in ADDRESS.
(let ((x (logxor (logand address 1) (logand (ash address (- tau)) 1))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about (rotatef (ldb (byte 1 ...) address) (ldb (byte 1 ...) address))?

(setf ldb) is a valid function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly how it was before but the above produces tighter assembly code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, can you make a comment with #+#:slightly-slower (rotatef ...) or so just to demo something equivalent and a lot more readable?

dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
(values qvm:amplitude-address))

;; Swap bits 0 and TAU in ADDRESS.
(let ((x (logxor (logand ,address 1) (logand (ash ,address ,minus-tau) 1))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see rotatef comment above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. Perhaps factor it out into an inlined function

dqvm/src/permutation.lisp Outdated Show resolved Hide resolved
@jmbr jmbr removed the WIP Work-in-progress. label Sep 16, 2019
Copy link
Contributor

@appleby appleby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the lookup table is a neat hack

dqvm/src/permutation.lisp Show resolved Hide resolved
;;; 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use integer-length appropriately

(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+)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for #.

"Upper bound on the number of transpositions defining an arbitrary permutation.")

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quasiquoting looks a little cleaner


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can a null transposition show up?

(transpositions (permutation-transpositions permutation)))
(cond
((and (null domain) (null codomain)) nil)
((and (= 1 (length domain))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(null (cdr domain))

(the qvm:amplitude-address (first codomain)))))
(make-instance 'permutation-transposition :tau max-index :size (1+ max-index)))
((and (= 2 (length domain))
(null (set-difference domain codomain))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want difference or intersection?

(loop :for a :of-type qubit-index :in domain
:for b :of-type qubit-index :across codomain
:unless (= a b) :do
(pushnew (cons a b) transpositions :test #'equal)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write a transposition= function and use it here, even if it's as simple as

(defun transposition= (a b)
  (and (= (car a) (car b)) (= (cdr a) (cdr b))))

(declare (type qubit-index tau))

;; Swap bits 0 and TAU in ADDRESS.
(let ((x (logxor (logand address 1) (logand (ash address (- tau)) 1))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, can you make a comment with #+#:slightly-slower (rotatef ...) or so just to demo something equivalent and a lot more readable?

(values qvm:amplitude-address))

;; Swap bits 0 and TAU in ADDRESS.
(let ((x (logxor (logand ,address 1) (logand (ash ,address ,minus-tau) 1))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. Perhaps factor it out into an inlined function

(byte ,num-bits 0)
,address)))))

(defun compile-qubit-permutation (permutation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants