From 77dd0d5bdab3435d38d1c4731853be4ac6011d65 Mon Sep 17 00:00:00 2001 From: "O'Keefe, Colin B" Date: Wed, 18 Oct 2023 10:50:21 -0700 Subject: [PATCH 1/2] bugfix: copy-instance was not robust enough This partially addresses one source of "nondeterminism" in quilc compilation (see #676) that was due to a violation of referential transparency. Specifically, non destructive compilation was not non-destructive enough: compiling the same parsed-program instance twice did not successfuly copy measurement instances deeply, resulting in different outputs from compress-qubits, with downstream consequences. --- cl-quil-tests.asd | 2 +- src/frontend-utilities.lisp | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cl-quil-tests.asd b/cl-quil-tests.asd index 314b3c5f9..46cf38b90 100644 --- a/cl-quil-tests.asd +++ b/cl-quil-tests.asd @@ -40,7 +40,7 @@ (:file "cfg-tests") (:file "defcircuit-tests") (:file "compilation-tests") - (:file "clifford-tests") + ;(:file "clifford-tests") (:file "translator-tests") (:file "rewrite-tests") (:file "state-prep-tests") diff --git a/src/frontend-utilities.lisp b/src/frontend-utilities.lisp index 9810b0f09..a4439198f 100644 --- a/src/frontend-utilities.lisp +++ b/src/frontend-utilities.lisp @@ -9,13 +9,22 @@ (:documentation "Create a shallow copy of the object INSTANCE. WARNING: The default will work for instances of \"idiomatic\" classes that aren't doing too many crazy things.") - (:method ((instance t)) + (:method ((instance t)) instance) + (:method ((instance structure-object)) (let* ((class (class-of instance)) (copy (allocate-instance class))) (dolist (slot (mapcar #'closer-mop:slot-definition-name (closer-mop:class-slots class))) (when (slot-boundp instance slot) (setf (slot-value copy slot) - (slot-value instance slot)))) + (copy-instance (slot-value instance slot))))) + copy)) + (:method ((instance standard-object)) + (let* ((class (class-of instance)) + (copy (allocate-instance class))) + (dolist (slot (mapcar #'closer-mop:slot-definition-name (closer-mop:class-slots class))) + (when (slot-boundp instance slot) + (setf (slot-value copy slot) + (copy-instance (slot-value instance slot))))) copy))) (defmacro dohash (((key val) hash &optional ret) &body body) From 21565ccd9b990db164f778abff99cd453fa2724a Mon Sep 17 00:00:00 2001 From: "O'Keefe, Colin B" Date: Wed, 18 Oct 2023 15:31:13 -0700 Subject: [PATCH 2/2] Small bugfix in cliford/perm --- cl-quil-tests.asd | 2 +- src/clifford/perm.lisp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cl-quil-tests.asd b/cl-quil-tests.asd index 46cf38b90..314b3c5f9 100644 --- a/cl-quil-tests.asd +++ b/cl-quil-tests.asd @@ -40,7 +40,7 @@ (:file "cfg-tests") (:file "defcircuit-tests") (:file "compilation-tests") - ;(:file "clifford-tests") + (:file "clifford-tests") (:file "translator-tests") (:file "rewrite-tests") (:file "state-prep-tests") diff --git a/src/clifford/perm.lisp b/src/clifford/perm.lisp index da2dbb78f..3c45c05d2 100644 --- a/src/clifford/perm.lisp +++ b/src/clifford/perm.lisp @@ -35,7 +35,7 @@ "Convert a Clifford group element C into a permutation representation." (let* ((num-qubits (num-qubits c)) (num-points (clifford-perm-degree num-qubits)) - (rep (perm::iota-vector (1+ num-points)))) + (rep (perm::iota-vector (1+ num-points) :element-type 'perm::perm-element))) (dotimes (i num-points (perm::%make-perm rep)) ;; The 2*(2 + i) and x/2 - 2 are to convert in/out from integers ;; which keep track of +-i/I and which don't.