quil-lang / quilc

The optimizing Quil compiler.
Apache License 2.0
454 stars 73 forks source link

Regression in support for high-numbered qubit indices since 1.23.0 #738

Open mhodson-rigetti opened 2 years ago

mhodson-rigetti commented 2 years ago

The following code snippet generates a compilation error on quilc 1.25.0 and 1.25.1 (as pulled from DockerHub) and pyquil 3.0.0:

from pyquil import get_qc, Program
from pyquil.gates import H, I, X, MEASURE

qc = get_qc("Aspen-9-qvm")

program = Program("""
DECLARE ro BIT[10]
X 100
I 101
I 102
I 103
X 104
X 105
I 106
X 107
X 108
I 109
MEASURE 100 ro[0]
MEASURE 101 ro[1]
MEASURE 102 ro[2]
MEASURE 103 ro[3]
MEASURE 104 ro[4]
MEASURE 105 ro[5]
MEASURE 106 ro[6]
MEASURE 107 ro[7]
MEASURE 108 ro[8]
MEASURE 109 ro[9]
""")

qc.compiler.quil_to_native_quil(program)

The error is:

---------------------------------------------------------------------------
RPCError                                  Traceback (most recent call last)
/tmp/ipykernel_8036/78573063.py in <module>
----> 1 qc.compiler.quil_to_native_quil(program)

~/venv-qpu-hybrid-benchmark/lib/python3.7/site-packages/pyquil/api/_abstract_compiler.py in quil_to_native_quil(self, program, protoquil)
    121             protoquil=protoquil,
    122         )
--> 123         response = self._compiler_client.compile_to_native_quil(request)
    124 
    125         nq_program = parse_program(response.native_program)

~/venv-qpu-hybrid-benchmark/lib/python3.7/site-packages/pyquil/api/_compiler_client.py in compile_to_native_quil(self, request)
    189                 "quil_to_native_quil",
    190                 rpcq_request,
--> 191                 protoquil=request.protoquil,
    192             )
    193             metadata: Optional[NativeQuilMetadataResponse] = None

~/venv-qpu-hybrid-benchmark/lib/python3.7/site-packages/rpcq/_client.py in call(self, method_name, rpc_timeout, *args, **kwargs)
    203 
    204         if isinstance(reply, RPCError):
--> 205             raise utils.RPCError(reply.error)
    206         else:
    207             return reply.result

RPCError: Unhandled error in host program:
User program incompatible with chip: qubit index 109 used and 38 available.

This compilation works correctly on quilc 1.23.0 and maps to available qubit indices of the Aspen-9 architecture as expected.

I have observed other similar examples failing, though sometimes re-running them works. This case appears to fail reliably. The root cause may include a stateful behavior in the server.

mhodson-rigetti commented 2 years ago

Note: While I'm not Lisp fluent, I did a Git diff between 1.23.0 and 1.25.0. The error emits from initial-rewiring.lisp which was not modified between 1.23.0 and 1.25.0. However, some fixes for initial rewiring pragmas were made, including something that looks like a "slot bound" check in rpc-server.lisp and calls prog-initial-rewiring. The root cause may be lurking in here?

stylewarning commented 2 years ago

@mhodson-rigetti I'm not at a computer at the moment, but does adding this at the top of your Quil program change anything?

PRAGMA INITIAL_REWIRING "PARTIAL"
stylewarning commented 2 years ago

This is the commit but @notmgsk that @mhodson-rigetti is likely referring to: https://github.com/quil-lang/quilc/commit/acf9b16f4a4d7ac14844dcfd85ec28543d9f0d2f

mhodson-rigetti commented 2 years ago

@mhodson-rigetti I'm not at a computer at the moment, but does adding this at the top of your Quil program change anything?

PRAGMA INITIAL_REWIRING "PARTIAL"

@stylewarning - No, adding the pragma doesn't avoid the problem.

notmgsk commented 2 years ago

Thanks for the report, @mhodson-rigetti. I'll take a look today... the obvious/easy solution is to "compress" the logical qubits prior to compilation so that only physical qubits are used. But there might be cases where that breaks / isn't wanted. Also interesting that 1.23.0 doesn't fail so I'll have to diagnose that. Again thanks for the info!

If you come across some more pathological examples, could you attach them here? Cheers