quil-lang / qvm

The high-performance and featureful Quil simulator.
Other
413 stars 57 forks source link

DEFCIRCUIT formals and DECLAREd memory of the same name causes issues (was: Error when trying to run compiled QASM program) #236

Closed stylewarning closed 4 years ago

stylewarning commented 4 years ago

Let broken.quil be

OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0],q[1];
measure q[0] -> c[0];
measure q[1] -> c[1];

Compiling this with quilc works fine:

Command: cat ~/Scratch/broken.qasm | quilc -P --safe-include-directory "benchmarking/ibm_qx_mapping/examples/"

DECLARE c BIT[2]

DEFCIRCUIT QASM_u3(%theta, %phi, %lambda) q:
    RZ(%lambda) q
    RY(%theta) q
    RZ(%phi) q

DEFCIRCUIT QASM_u2(%phi, %lambda) q:
    RZ(%lambda) q
    RY(pi/2) q
    RZ(%phi) q

DEFCIRCUIT QASM_u1(%lambda) q:
    RZ(%lambda) q
    RY(0.0) q
    RZ(0.0) q

DEFCIRCUIT QASM_cx c t:
    CNOT c t

DEFCIRCUIT QASM_id a:
    RZ(0.0) a
    RY(0.0) a
    RZ(0.0) a

DEFCIRCUIT QASM_x a:
    QASM_u3(pi, 0.0, pi) a

DEFCIRCUIT QASM_y a:
    QASM_u3(pi, pi/2, pi/2) a

DEFCIRCUIT QASM_z a:
    QASM_u1(pi) a

DEFCIRCUIT QASM_h a:
    QASM_u2(0.0, pi) a

DEFCIRCUIT QASM_s a:
    QASM_u1(pi/2) a

DEFCIRCUIT QASM_sdg a:
    QASM_u1(-pi/2) a

DEFCIRCUIT QASM_t a:
    QASM_u1(pi/4) a

DEFCIRCUIT QASM_tdg a:
    QASM_u1(-pi/4) a

DEFCIRCUIT QASM_rx(%theta) a:
    QASM_u3(%theta, -pi/2, pi/2) a

DEFCIRCUIT QASM_ry(%theta) a:
    QASM_u3(%theta, 0.0, 0.0) a

DEFCIRCUIT QASM_rz(%phi) a:
    QASM_u1(%phi) a

DEFCIRCUIT QASM_cz a b:
    QASM_h b
    QASM_cx a b
    QASM_h b

DEFCIRCUIT QASM_cy a b:
    QASM_sdg b
    QASM_cx a b
    QASM_s b

DEFCIRCUIT QASM_ch a b:
    QASM_h b
    QASM_sdg b
    QASM_cx a b
    QASM_h b
    QASM_t b
    QASM_cx a b
    QASM_t b
    QASM_h b
    QASM_s b
    QASM_x b
    QASM_s a

DEFCIRCUIT QASM_ccx a b c:
    QASM_h c
    QASM_cx b c
    QASM_tdg c
    QASM_cx a c
    QASM_t c
    QASM_cx b c
    QASM_tdg c
    QASM_cx a c
    QASM_t b
    QASM_t c
    QASM_h c
    QASM_cx a b
    QASM_t a
    QASM_tdg b
    QASM_cx a b

DEFCIRCUIT QASM_crz(%lambda) a b:
    QASM_u1((%lambda/(2.0))) b
    QASM_cx a b
    QASM_u1(-((%lambda/(2.0)))) b
    QASM_cx a b

DEFCIRCUIT QASM_cu1(%lambda) a b:
    QASM_u1((%lambda/(2.0))) a
    QASM_cx a b
    QASM_u1(-((%lambda/(2.0)))) b
    QASM_cx a b
    QASM_u1((%lambda/(2.0))) b

DEFCIRCUIT QASM_cu3(%theta, %phi, %lambda) c t:
    QASM_u1(((%lambda-%phi)/(2.0))) t
    QASM_cx c t
    QASM_u3(-((%theta/(2.0))), 0.0, -(((%phi+%lambda)/(2.0)))) t
    QASM_cx c t
    QASM_u3((%theta/(2.0)), %phi, 0.0) t

RZ(pi/2) 0                              # Entering rewiring: #(0 1 2 3 4 5 6 7)
RX(pi/2) 0
RZ(-pi/2) 1
RX(pi/2) 1
CZ 1 0
RZ(-pi/2) 0
RX(-pi/2) 1
RZ(pi/2) 1
MEASURE 1 c[1]
MEASURE 0 c[0]                          # Exiting rewiring: #(0 1 2 3 4 5 6 7)

But running on the QVM errors:

Command: cat ~/Scratch/broken.qasm | quilc -P --safe-include-directory "benchmarking/ibm_qx_mapping/examples/" | qvm

! ! ! Condition raised: The assertion
                        (EVERY #1=#'CL-QUIL:IS-FORMAL CL-QUIL::ARGS) failed
                        with #1# = #<FUNCTION CL-QUIL:IS-FORMAL>, CL-QUIL::ARGS
                        =
                        (#<CL-QUIL:MEMORY-REF c[0]>
                         #S(CL-QUIL:FORMAL :NAME "t")).
<131>1 2020-01-16T19:06:36Z cobbler.local qvm 35093 - - Error encountered, quitting.
stylewarning commented 4 years ago

The issue is that the declared memory is named c, so when the DEFCIRCUIT for QASM_cu3 is being parsed, it thinks the argument lists aren't formal parameters (i.e., c) but rather references to memory (i.e., c[0]).

stylewarning commented 4 years ago

Minimal test case:

File: bork.quil:

DECLARE x BIT

DEFCIRCUIT f x:
    X x

f 0

Run this with:

cat bork.quil | qvm

stylewarning commented 4 years ago

To be clear, this is a CL-QUIL bug, but we will track it here.