rigetti / pyquil

A Python library for quantum programming using Quil.
http://docs.rigetti.com
Apache License 2.0
1.4k stars 341 forks source link

Parametric compilation of circuit containing a DefGate? #1530

Open ArfatSalman opened 1 year ago

ArfatSalman commented 1 year ago

Pre-Report Checklist

Issue Description

Insert a short description of the bug here, along with what you expected the behavior to be.

Thanks for helping us improve pyQuil! 🙂

How to Reproduce

When I use a Paramteric value in a custom gate, I get this error. I don't get this issue with a native gate.

Code Snippet

from pyquil import Program, get_qc
from pyquil.gates import *
from pyquil.quil import DefGate
from pyquil.quilatom import Parameter, quil_sin, quil_cos, quil_exp
import numpy

def RYY():
    theta = Parameter("theta")
    cos = quil_cos(theta / 2)
    isin = 1j * quil_sin(theta / 2)
    mat = numpy.array(
        [[cos, 0, 0, isin], [0, cos, -isin, 0], [0, -isin, cos, 0], [isin, 0, 0, cos]],
    )
    return DefGate("RYY", mat, [theta])

circuit = Program()

qr = circuit.declare("ro", "BIT", 2)

theta = circuit.declare("theta", "REAL")

defn = RYY()
RYYGate = defn.get_constructor()

circuit += defn

circuit.inst(RYYGate(theta)(1, 0))

circuit += MEASURE(0, qr[0])
circuit += MEASURE(1, qr[1])

qc = get_qc("3q-qvm")

executable = qc.compile(circuit)

Error Output

Traceback (most recent call last):
  File "/Users/arfat/Desktop/latest-qc/error.py", line 37, in <module>
    executable = qc.compile(circuit)
  File "/Users/arfat/Desktop/latest-qc/latest-qc/lib/python3.10/site-packages/pyquil/api/_quantum_computer.py", line 399, in compile
    nq_program = self.compiler.quil_to_native_quil(program, protoquil=protoquil)
  File "/Users/arfat/Desktop/latest-qc/latest-qc/lib/python3.10/site-packages/pyquil/api/_abstract_compiler.py", line 123, in quil_to_native_quil
    response = self._compiler_client.compile_to_native_quil(request)
  File "/Users/arfat/Desktop/latest-qc/latest-qc/lib/python3.10/site-packages/pyquil/api/_compiler_client.py", line 188, in compile_to_native_quil
    response: rpcq.messages.NativeQuilResponse = rpcq_client.call(
  File "/Users/arfat/Desktop/latest-qc/latest-qc/lib/python3.10/site-packages/rpcq/_client.py", line 205, in call
    raise utils.RPCError(reply.error)
rpcq._utils.RPCError: Unhandled error in host program:
Condition CL-QUIL::COMPILER-DOES-NOT-APPLY was signalled.

Environment Context

Operating System: MacOS Ventura 13.1 (M1 Pro)

Python Version (python -V): Python 3.10.6

Quilc Version (quilc --version): quilc --version

QVM Version (qvm --version): 1.17.1 [cf3f91f]

Python Environment Details (pip freeze or conda list):

Copy and paste the output of `pip freeze` or `conda list` here.
genos commented 1 year ago

For what it's worth, you can avoid this quilc error by passing in a specific value to the gate rather than the parameter theta; e.g. circuit.inst(RYYGate(np.pi / 2)(1, 0)) works just fine.