Closed steve-jeffrey closed 2 years ago
Repro with plain Quil:
(defun test-perm-thing ()
(let* ((p (quil:parse-quil
"
DEFGATE PERM AS PERMUTATION:
5, 1, 2, 6, 7, 0, 4, 3
DECLARE ro BIT[3]
X 0
PERM 0 1 2
MEASURE 0 ro[0]
MEASURE 1 ro[1]
MEASURE 2 ro[2]
"))
(p* (cl-quil::compiler-hook p (cl-quil::build-nq-linear-chip 3)))
(q (make-qvm 3))
(q* (make-qvm 3)))
(load-program q p :supersede-memory-subsystem t)
(load-program q* p* :supersede-memory-subsystem t)
(format t "Without Compilation:~%")
(run q)
(dotimes (i 3)
(format t " ro[~D] = ~D~%" i (memory-ref q "ro" i)))
(format t "~%With Compilation:~%")
(run q*)
(dotimes (i 3)
(format t " ro[~D] = ~D~%" i (memory-ref q* "ro" i)))))
Running this gives:
QUIL> (qvm::test-perm-thing)
Without Compilation:
ro[0] = 1
ro[1] = 1
ro[2] = 0
With Compilation:
ro[0] = 1
ro[1] = 1
ro[2] = 1
Repro without the QVM as a QUILC failure:
(deftest test-perm-compilation ()
(let* ((chip (quil::build-nq-linear-chip 3 :architecture ':cnot))
(orig-prog (quil::parse-quil "
DEFGATE PERM AS PERMUTATION:
5, 1, 2, 6, 7, 0, 4, 3
X 0
PERM 0 1 2
"))
(orig-matrix (quil:parsed-program-to-logical-matrix orig-prog))
(proc-prog (quil::compiler-hook orig-prog chip))
(proc-matrix (quil:parsed-program-to-logical-matrix proc-prog))
(2q-code (program-2q-instructions proc-prog)))
(is (quil::matrix-equals-dwim orig-matrix proc-matrix))
(is (every (link-nativep chip) 2q-code))))
Found the bug. The compiler was compiling the inverse permutations. Fixed in https://github.com/quil-lang/quilc/pull/806.
Fixed in master.
The code shown below indicates there is a problem compiling selected permutation gates (
DefPermutationGate
). The code uses four methods:quilc
numpy
,to evaluate a circuit consisting of one
X
gate and one permutation gate. The results indicate:SWAP
, all four methods yield the same result.SWAP
s, or an arbitrary permutation, the result from method 2 (QVM with compilation) appears to differ from the other three methods.Output:
Note: the manual result
"0 0 0 0 0 0 1 0"
corresponds to state|110⟩
, which I think is the correct result for the[5, 1, 2, 6, 7, 0, 4, 3]
permutation.The code was tested using
quilc 1.26.0
,qvm 1.17.2
(as pulled from DockerHub) andpyquil 3.1.0
.The attachments contain the compiled instructions (i.e. the output from
qc.compile(program)
) for two cases:SWAP
s (SWAP(1,2)
followed bySWAP(0,2)
) compiled_program_arbitrary_permutation.txt compiled_program_two_SWAPs.txt