Skip to content

Commit 31fbf5e

Browse files
Spin1Halfstylewarning
authored andcommitted
warn on ambiguous circuit or gate definitions by default
1 parent 5ac8f83 commit 31fbf5e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/cl-quil.lisp

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
'(validate-defgate-loops expand-circuits type-check)
1010
"The standard transforms that are applied by PARSE-QUIL after parsing. (See also: *STANDARD-PRE-COMPILATION-TRANSFORMS*)")
1111

12+
(defvar *default-ambiguous-definition-handler*
13+
'warn-on-ambiguous-gate-or-circuit-definition)
14+
1215
(define-condition ambiguous-definition-condition ()
1316
((instruction :initarg :instruction
1417
:reader ambiguous-definition-instruction
@@ -44,6 +47,16 @@
4447
;;; generally determined by a pattern matching system, and so we have to
4548
;;; consider the arity and values of parameters and arguments).
4649

50+
(defun warn-on-ambiguous-gate-or-circuit-definition (condition)
51+
"Handler which prints a warning in the presence of a CONDITION indicating an ambiguous gate or circuit declaration."
52+
(check-type condition ambiguous-definition-condition)
53+
(let ((instr (ambiguous-definition-instruction condition)))
54+
(typecase instr
55+
(gate-definition (alexandria:simple-style-warning "Gate ~A has multiple definitions, this leads to ambiguous behavior." (gate-definition-name instr)))
56+
(circuit-definition (alexandria:simple-style-warning "Circuit ~A has multiple definitions, this leads to ambiguous behavior." (circuit-definition-name instr)))
57+
(t (alexandria:simple-style-warning "Object ~A (of type ~A) represents a duplicate definition." instr (type-of instr))))
58+
(continue)))
59+
4760
(defgeneric definition-signature (instr)
4861
(:documentation "Computes a signature for a Quil definition such that if two definitions are equivalent for the purposes of name resolution, then their signatures are EQUALP.")
4962
(:method ((instr gate-definition))
@@ -109,7 +122,7 @@ This also signals ambiguous definitions, which may be handled as needed."
109122

110123
(defun %parse-quil (string build-parsed-program &key originating-file
111124
transforms
112-
(ambiguous-definition-handler #'continue)
125+
(ambiguous-definition-handler *default-ambiguous-definition-handler*)
113126
(lexer-extensions '())
114127
(parser-extensions '()))
115128
"The actual parsing code. Arguments are as in PARSE-QUIL, except we now have three new ones:
@@ -165,7 +178,7 @@ This also signals ambiguous definitions, which may be handled as needed."
165178

166179
(defun parse (string &key originating-file
167180
(transforms *standard-post-parsing-transforms*)
168-
(ambiguous-definition-handler #'continue))
181+
(ambiguous-definition-handler *default-ambiguous-definition-handler*))
169182
"Parse the input STRING which can be either Quil or OpenQASM code."
170183
(if (%check-for-qasm-header string)
171184
(quil.qasm:parse-qasm string)
@@ -176,7 +189,7 @@ This also signals ambiguous definitions, which may be handled as needed."
176189

177190
(defun parse-quil (string &key originating-file
178191
(transforms *standard-post-parsing-transforms*)
179-
(ambiguous-definition-handler #'continue))
192+
(ambiguous-definition-handler *default-ambiguous-definition-handler*))
180193
"Parse and process the Quil string STRING, which originated from the file ORIGINATING-FILE. Transforms in TRANSFORMS are applied in-order to the processed Quil string.
181194
182195
In the presence of multiple definitions with a common signature, a signal is raised, with the default handler specified by AMBIGUOUS-DEFINITION-HANDLER.

0 commit comments

Comments
 (0)