Skip to content

Commit

Permalink
Add function exports for RPC API
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-s committed Jul 20, 2023
1 parent 1309e19 commit 2a1395a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lib/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
SBCL_HOME?=$(HOME)/sbcl
SBCL?=$(SBCL_HOME)/run-sbcl.sh

.PHONY: all clean

all: libquilc.dylib

libquilc.core libquilc.c libquilc.h libquilc.py: src/libquilc.lisp
sbcl --dynamic-space-size 4096 --load "src/build-image.lisp"
$(SBCL) --dynamic-space-size 4096 --load "src/build-image.lisp"

libquilc.dylib: libquilc.core libquilc.c
gcc -dynamiclib -o $@ libquilc.c -lsbcl
gcc -dynamiclib -o $@ libquilc.c -lsbcl -L$(SBCL_HOME)/src/runtime

example: example.c libquilc.dylib
gcc src/example.c -o example -lsbcl -lquilc -L.
gcc src/example.c -o example -lsbcl -lquilc -L. -L$(SBCL_HOME)/src/runtime

clean:
rm -f libquilc.c libquilc.h libquilc.core libquilc.py libquilc.dylib example
35 changes: 34 additions & 1 deletion lib/src/libquilc.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@
(defun compile-protoquil (parsed-program chip-specification)
(compiler-hook parsed-program chip-specification :protoquil t))

(defun generate-rb-sequence (depth qubits gateset seed interleaver)
(let* ((request (make-instance 'rpcq::|RandomizedBenchmarkingRequest|
:|depth| depth :|qubits| qubits
:|gateset| (uiop:split-string gateset :separator ",")
:|seed| seed :|interleaver| interleaver))
(response (quilc::generate-rb-sequence-handler request)))
(with-output-to-string (stream)
(yason:encode (rpcq::|RandomizedBenchmarkingResponse-sequence| response)
stream))))

(defun conjugate-pauli-by-clifford (pauli-indices pauli-symbols clifford)
(let* ((indices (mapcar #'parse-integer
(uiop:split-string pauli-indices
:separator ",")))
(pauli (make-instance 'rpcq::|PauliTerm|
:|indices| indices
:|symbols| (uiop:split-string pauli-symbols :separator ",")))
(request (make-instance 'rpcq::|ConjugateByCliffordRequest|
:|pauli| pauli :|indices| indices
:|clifford| clifford))
(response (quilc::conjugate-pauli-by-clifford-handler request)))
(rpcq::to-json-string response)))

(defun %rewrite-arithmetic (quil)
(let* ((request (make-instance 'rpcq::|RewriteArithmeticRequest|
:|quil| quil))
(response (quilc::rewrite-arithmetic-handler request)))
(rpcq::to-json-string response)))

(sbcl-librarian:define-api quilc (:error-map error-map
:function-prefix "quilc_")
(:literal "/* types */")
Expand All @@ -26,5 +55,9 @@
(("compile_protoquil" compile-protoquil) quil-program ((program quil-program) (chip-spec chip-specification)))
(("build_nq_linear_chip" cl-quil::build-nq-linear-chip) chip-specification ((n :int)))
(("chip_spec_from_isa_descriptor" quilc::lookup-isa-descriptor-for-name) chip-specification ((descriptor :string)))
(("print_chip_spec" cl-quil::debug-print-chip-spec) :void ((chip-spec chip-specification)))))
(("print_chip_spec" cl-quil::debug-print-chip-spec) :void ((chip-spec chip-specification)))
(("generate_rb_sequence" generate-rb-sequence) :string ((depth :int) (qubits :int) (gateset :string) (seed :int) (interleaver :string)))
(("conjugate_pauli_by_clifford" conjugate-pauli-by-clifford) :string ((pauli-indices :string) (pauli-symbols :string) (clifford :string)))
(("rewrite_arithmetic" %rewrite-arithmetic) :string ((quil :string)))
(("get_version_info" quilc::get-version-info-handler) :string ())))

6 changes: 3 additions & 3 deletions lib/tests/python/compile_quil.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def die(msg):
die('unable to compile program')

libquilc.quilc_print_program(processed_program);
libquilc.quilc_release_handle(program)
libquilc.quilc_release_handle(processed_program)
libquilc.quilc_release_handle(chip_spec)
libquilc.lisp_release_handle(program)
libquilc.lisp_release_handle(processed_program)
libquilc.lisp_release_handle(chip_spec)
55 changes: 55 additions & 0 deletions lib/tests/python/rpc_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from ctypes import *
import json
import sys

import libquilc


def die(msg):
print(msg)
exit(1)


if __name__ == "__main__":
gateset = "PHASE(pi/2) 0, H 0"
sequence = c_char_p()

if (
libquilc.quilc_generate_rb_sequence(
2, 1, gateset.encode("utf-8"), 52, None, byref(sequence)
)
!= 0
):
die("unable to generate RB sequence")

print(json.loads(sequence.value.decode("utf-8")))

indices = "0"
symbols = "X"
clifford = "H 0"
pauli = c_char_p()

if (
libquilc.quilc_conjugate_pauli_by_clifford(
indices.encode("utf-8"),
symbols.encode("utf-8"),
clifford.encode("utf-8"),
byref(pauli),
)
!= 0
):
die("unable to conjugate pauli")

result = json.loads(pauli.value.decode("utf-8"))
result.pop("_type")
print(result)

quil = "RX((2+1/2)*pi/7) 0"
raw_result = c_char_p()

if libquilc.quilc_rewrite_arithmetic(quil.encode("utf-8"), byref(raw_result)) != 0:
die("unable to rewrite arithmetic")

result = json.loads(raw_result.value.decode("utf-8"))
result.pop("_type")
print(result)

0 comments on commit 2a1395a

Please sign in to comment.