rigetti / pyquil

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

Measurements inserted by run_and_measure() may cause compiler to think we are operating on more qubits than are available. #1050

Open notmgsk opened 5 years ago

notmgsk commented 5 years ago

Try the example:

from pyquil import get_qc, Program
from pyquil.gates import *

p = Program(H(0))

qc = get_qc('Aspen-4-3Q-A-qvm')

result_rm = qc.run_and_measure(p, trials=10)

resulting in the error

rpcq._utils.RPCError: User program used too many qubits: 4 used and 3 available in the largest connected component

The program that pyQuil sends to quilc is

DECLARE ro BIT[2]

H 0
MEASURE 1 ro[0]
MEASURE 2 ro[1]

which references three unique resources: more than are available on the chip (two).

rumschuettel commented 5 years ago

I've encountered the same bug; the program

RY(0.6998761826627558) 0
RZ(-pi/4) 0
RY(0.6998761826627558) 1
RZ(-pi/4) 1
CNOT 0 1
S 0
H 0
S 0

cannot be executed on Aspen-4-2Q-A when using measure_and_run. The issue appears to be in this line in run_and_measure:

        for i, q in enumerate(self.qubits()):
            program.inst(MEASURE(q, ro[i]))
        program.wrap_in_numshots_loop(trials)
        executable = self.compile(program)

The device's qubits list doesn't have the right range; it should be the program's qubits that this is iterated over, not the devices (note the device compilation call is after the measurements are added. So till then we're in "program qubit space".). It should enumerate over program.get_qubits().

~ Johannes

rumschuettel commented 5 years ago

In fact, wouldn't it make much more sense to also re-index the return dict as it comes from the program? The compiler is free to rearrange the order in there anyhow, so there's no extra information for the user gained by indexing by the device's qubits.