Closed josh146 closed 5 years ago
Hi, thanks for reporting this.
I am not super familiar with the PyQVM
API, but it looks like there are several things going on.
PyQVM
doesn't support parametric defgates. There is code in the PyQVM
class to detect parametric defgates and report a sane error, but calling execute
in this way appears to circumvent it. So this is probably a bug.
PyQVM
implements the "Quantum Abstract Machine" (QAM
) interface, and I suspect you're meant to either interact with it via the QAM
interface, or else indirectly via a Quantum Computer
. Neither of those classes implement an execute
method, so perhaps that method should "private" to the PyQVM class. This is just a guess though.
If instead of calling execute
directly, you follow a procedure similar to the one outlined in the QuantumComputer
docs, then you'll get an explicit NotImplementedError: PyQVM does not support parameterized DEFGATEs
. For example, if you replace the final two lines of your example code with the following:
from pyquil import get_qc
qc = get_qc('2q-pyqvm')
ro = p.declare('ro', 'BIT', 1)
p += MEASURE(1, ro[0])
compiled_program = qc.compile(p)
results = qc.run(compiled_program)
PyQVM
to not throw an error about the parametric DEFGATE
if you're passing it a compiled program that no longer contains any references to said parametric gate other than the DEFGATE
form itself. But for all I know, that is expected behavior.If instead of calling execute directly, you follow a procedure similar to the one outlined in the QuantumComputer docs, then you'll get an explicit NotImplementedError: PyQVM does not support parameterized DEFGATEs.
Ah, that's good to know!
PyQVM doesn't support parametric defgates.
I should clarify, that the reason I noticed this was that non-parametrized DEFGATE
s used to work in the PyQVM --- they work in PyQuil 2.10, for example. We rely on this feature over in https://github.com/rigetti/pennylane-forest to compute multi-qubit arbitrary expectation values.
For example, using
theta = 0.543
U = np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, np.cos(theta / 2), -1j * np.sin(theta / 2)],
[0, 0, -1j * np.sin(theta / 2), np.cos(theta / 2)]
])
gate_definition = DefGate('U_test', U)
U_test = gate_definition.get_constructor()
I get the same error as above in the latest PyQuil PyQVM, but it works fine in 2.10.
I see. Yes, looking at the git history, the behavior of execute
w.r.t. defgates did likely change recently. As a (partial) workaround, calling the PyQVM
via QAM
interface methods load
and run
should still work in the presence of non-parametric defgates, or indeed via the QuantumComputer
interfaces. Note that these reset the wavefunction and memory registers each time they are called though.
Full example calling PyQVM
via QAM
interface:
import numpy as np
from pyquil import Program
from pyquil.quilbase import DefGate
from pyquil.numpy_simulator import NumpyWavefunctionSimulator
from pyquil.pyqvm import PyQVM
theta = 0.543
U = np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, np.cos(theta / 2), -1j * np.sin(theta / 2)],
[0, 0, -1j * np.sin(theta / 2), np.cos(theta / 2)]
])
gate_definition = DefGate('U_test', U)
U_test = gate_definition.get_constructor()
p = Program()
p += gate_definition
p += U_test(0, 1)
ro = p.declare('ro', 'BIT', 2)
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])
qam = PyQVM(n_qubits=2, quantum_simulator_type=NumpyWavefunctionSimulator)
qam.load(p)
qam.run()
qam.read_memory(region_name="ro")
Or via the QuantumComputer
interface (building on the last example):
from pyquil import get_qc
qc = get_qc('2q-pyqvm')
p = Program()
p += gate_definition
p += U_test(0, 1)
qc.run_and_measure(p, trials=1)
Issue Description
DefGate
seems to be broken inNumpyWavefunctionSimulator
.How to Reproduce
Code Snippet
Use any custom gate definition. Here I just copied from http://docs.rigetti.com/en/stable/basics.html#defining-parametric-gates.
Error Output
Environment Context
Operating System: Ubuntu 18.04.1
Python Version (
python -V
): 3.6.8Quilc Version (
quilc --version
): latest rigetti/qvm docker imageQVM Version (
qvm --version
): latest rigetti/quilc docker imagePython Environment Details (
pip freeze
orconda list
):