pyquil icon indicating copy to clipboard operation
pyquil copied to clipboard

Parametrized execution requires modifying executables

Open bramathon opened this issue 3 years ago • 0 comments

Pre-Request Checklist

  • [x] I am running the latest versions of pyQuil and the Forest SDK
  • [x] I checked to make sure that this feature has not already been requested

Issue Description

The current workflow to run a parameterized circuit is as follows:

import numpy as np

from pyquil import Program
from pyquil.gates import RX, RZ, MEASURE

p = Program()
ro = p.declare("ro", "BIT", 1)
theta_ref = p.declare("theta", "REAL")

p += RX(np.pi / 2, 0)
p += RZ(theta_ref, 0)
p += RX(-np.pi / 2, 0)

p += MEASURE(qubit, ro[0])

executable = qc.compile(p)

executable.write_memory(region_name='theta', value=[np.pi/2])

 qc.run(executable)

The executable is modified each time we want to run with a different parameters. This requires the user to keep track of the state of their executable and for cases where users might want to use threading, this can become complex. The cleanest way to avoid issues is to make a copy of the executable for each application of a parameter.

executable_p = executable.copy()
executable_p.write_memory(region_name='theta', value=[np.pi/2])
 qc.run(this_executable)

Proposed Solution

Applying parameters should return a fresh executable in all cases and thus impossible to screw up. Executable should never be modified.

So the new workflow would look like this:

executable_p = executable.write_memory(region_name='theta', value=[np.pi/2])
qc.run(executable_p)

This matches the behaviour of Qiskit. Cirq uses a map provided at run-time, similar to pyquil2's qc.run(memory_map).

Running an executable with parameters not filled in ought to return an error. I believe the current behaviour is to fill in parameters with 0s.

Additional References

If applicable, provide some references that will help us better understand the request.

bramathon avatar Sep 02 '21 19:09 bramathon