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

920 quil spec 2024 1 conformance #921

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

macrologist
Copy link
Contributor

@macrologist macrologist commented Apr 17, 2024

See #920

This PR includes support for a number of requirements introduced into the forthcoming quil spec version 2024.1

Chief among the changes:

  • parsing and addressing support for EXTERN and CALL instructions
  • functions declared extern may appear in expression
  • addressing support for CALLs
  • a new typing pragma for extern functions
  • functions declared extern appearing in expressions need a known extern typing pragma
  • renaming old extern keyword to STUB.
  • non-volatile pragma

@macrologist macrologist marked this pull request as ready for review May 6, 2024 22:32
@macrologist macrologist force-pushed the 920-quil-spec-2024-1-conformance branch from a28c904 to 9deaee6 Compare May 6, 2024 22:34
@macrologist
Copy link
Contributor Author

macrologist commented May 6, 2024

There is now a fair bit of machinery in place surrounding the use of Externs. The following cases are supported:

  1. You want to specify the existence of externs available to the control hardware so that you can CALL them to mutate classical memory.
  2. You want to use externs within quilc numerical expressions that appear in defgate bodies or in gate parameter positions

Case 1 is simple.

In the following example, we assert that a function called randomize is available to some system that processes native Quil code. From what the fragment suggests, randomize probably sets the value of its first argument to some random in the range of its other two arguments:

EXTERN randomize 
DECLARE x REAL
CALL randomize x -3.1415 3.1415
RX(x) 0

Case 2 is more complicated.

In order to use an extern in an expression, the Quil standard insists that the function be known to not mutate any of its arguments, and that it return a value. The inclusion of the PRAGMA EXTERN into quilc satisfies these two requirements.

quilc will evaluate function calls in numeric expressions when those functions belong to a sanctioned list function names. In order to avoid surprises (i.e that extern functions also evaluate their arguments when able), we require that extern functions to appear within numeric expressions must also be registered with quilc. Unregistered functions will signal an error indicating as much. For the moment, this means that users who wish to make use of externs in their expressions must produce a custom build of quilc that registers those externs with the compiler.

Here we register a name "quadroot" for use as an extern.

(defun m-extensions::quadroot (val) (sqrt (sqrt val)))
(cl-quil::register-classical-function  "quadroot" 'my-extensions::quadroot)

We must still declare the extern and provide it a type in our Quil source code in order to benefit from the registered function

EXTERN quadroot
PRAGMA EXTERN quadroot "REAL (val:REAL)"
RX(quadroot(10.2)) 0

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.

add some other documentation about EXTERN, state that stubs are not compliant

src/ast.lisp Outdated Show resolved Hide resolved
src/ast.lisp Show resolved Hide resolved
src/ast.lisp Outdated Show resolved Hide resolved
src/package.lisp Outdated Show resolved Hide resolved
src/parser.lisp Outdated Show resolved Hide resolved
src/pragmas.lisp Outdated Show resolved Hide resolved
src/pragmas.lisp Outdated Show resolved Hide resolved
src/pragmas.lisp Outdated Show resolved Hide resolved
src/pragmas.lisp Show resolved Hide resolved
tests/bad-test-files/bad-empty-extern.quil Outdated Show resolved Hide resolved
@macrologist macrologist force-pushed the 920-quil-spec-2024-1-conformance branch from 1c46e58 to 5b4eea4 Compare June 3, 2024 23:05
s/EXTERN/STUB - renaming EXTERN keyword to STUB

Add zero-qubit frame in a DEFRAME in tests

Remove sample-rate from DEFWAVEFORM

Added NON_VOLATILE pragma

Add EXTERN, numerical expression support, printing

Add: CALL instruction

Addressing CALL instructions

Add PRAMGA EXTERN for declaring extern function signatures

Checking extern type for externs in expressions

Add tests for extern and extern pragma processing

Fix: nits; Catch EXTERN PRAGMA parse error and rethrow warning

Remove: malformed extern pragmas no longer signal errors

Fix: indentation

Add: comment about why we check pragma signatures at parse time

Add: Comment about why we're parsing EXTERN pragmas diffrently

Fix: nits

Updates in light of #88
@macrologist macrologist force-pushed the 920-quil-spec-2024-1-conformance branch from b6aa471 to daf5238 Compare September 9, 2024 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants